我也仍然非常學習者,因此認識到,這是不容易提問的時候,你甚至不完全知道你是問什麼。
基於您的具有挑戰性的描述,我試圖編寫一個可以構建的示例程序。請閱讀我的評論,因爲我試圖描述性很強,以幫助你理解/學習。
public class Order {
//Constructor 2
public Order(String productName, int quantity) {
this.productName = productName;//Get the name
this.quantity = testQuantity(quantity); //Get the total quantity
this.orderPrice =getPrice(productName, quantity); //Get the price of the order
this.discountPrice = 0; //Should be set to a value, even though it won't be used in this constructor
orderNum++; //Another order in, up the count :)
displayOrder(productName, quantity, this.orderPrice, this.discountPrice); //Show order details
}
//Constructor 3
public Order(String productName, int quantity, int discount) {
this.productName = productName; //Get the name
this.quantity = testQuantity(quantity); //Get the total quantity
this.orderPrice = getPrice(productName, quantity); //Get the price of the order
this.discountPrice = testDiscount(discount, this.orderPrice); //Get the price if there is a discount
orderNum++; //Another order in, up the count :)
displayOrder(productName, quantity, this.orderPrice, this.discountPrice); //Show order details
}
我已經在類的底部添加了一些額外的領域,使我的例子更容易顯示出來,因爲我是爲你想怎麼你的一些方法,以工作非常不確定。我已經在您的構造函數中放置並初始化了這些元素,並對其進行了評論。
private void displayOrder(String productName, int quantity, int orderPrice, int discountPrice){
if(discountPrice == 0){ //If the discount is 0 then there is no discount so the discount price is the same as the order price
discountPrice = orderPrice;
}
// \n is called an escape character and when in a string creates a new line.
System.out.println("You have ordered: " + quantity + " " + productName + ("(s)") //Number and name of item ordered
+ "\nTotal cost: £" + orderPrice + "\nPrice with Discount (If applicable)= £" + discountPrice //Order price and discount price displayed
+ "\nOrder number: " + orderNum +"\n"); //Order Number
}
上述方法顯示由構造函數創建的順序的所有細節,並顯示它的細節以查看。
private int testQuantity(int q){
//What you want to do in this method,
//For example
//System.out.println("You have ordered " + q + "Items");
return q;
}
我不確定你想用這種方法做什麼,所以都留下了空白。如果您需要存儲可用項目的總數來檢查數量,最好的辦法是數據庫,這需要一整套不同的學習。
private int testDiscount(int discount, int orderPrice){ //Will return a discounted price and store it in 'discountPrice' field
int reducedPrice = (orderPrice - discount);
return reducedPrice;
}
如果用戶除總訂單價格外還有折扣,則返回折扣價格。兩者都是由您與構造
private int getPrice(String productname, int quantity){
int price = 0;
switch(productName){ //Switch so you can find what item ordered and allocate the correct price
//Add cases for different items?
case "Sweater":
price = 10;
break;
default:
price = 0;
break;
}
int totalPrice = price * quantity; //Work out price by multiplying quantity of item by price determined by switch
return totalPrice;
}
上述開關也可能會被檢查項目的價格最好的方式來創建Order對象舉行。每個案例都有一個項目的名稱和價格。您使用該價格乘以數量創建訂單價格。
private String productName; //Name of product
private int quantity; //Quantity ordered
private int orderPrice; // The total order of the price
private int discountPrice; //Somewhere to store the price of an order with a discount
private static int orderNum; //This is static so that you it can be referenced even if no orders have been made
public static void main(String[] args){ //Test the ordering
Order order1 = new Order("Sweater", 2);
Order order2 = new Order("Sweater", 2, 5);
}
從'main'方法創建兩個命令來測試我們的應用程序。
有一個修補程序和玩它。我不確定這是否是您希望從您的問題中得到的結果,但希望您能找到有用的方法。
我不確定我收到任何問題 –
你想知道嗎,你如何調用構造函數來創建新對象? – JohnnyAW
嘿,你真的應該閱讀關於構造函數的Java教程。現在明白這一點非常重要,因爲它非常有趣。你的代碼中包含所有你需要的東西,你只需要掌握基本知識。如果有人發佈代碼答案,您可能無法得到它。拿走它,我犯了這個錯誤。 – RossC