2013-09-16 104 views
-4
  • 第二個構造函數是接收兩個參數,productName和 數量。 productName參數將被分配給類的實例變量productName 。數量參數將傳遞給 testQuantity方法。在此之後,調用getPrice方法應使 傳遞給productName參數。僅當訂單有效時才需要調用計算方法初學者做Java項目

  • 第三個構造函數是接收三個參數productName,數量爲 和折扣。
    productName參數將被分配給類的 productName實例變量。 testQuantity,getPrice和testDiscount方法都需要傳遞所需的參數。 僅當訂單有效時才需要調用計算方法。

這個問題得到了回答,並以此代碼結束。感謝您的幫助

public Order() { 
     isValidOrder = false; 
     message = "**ERROR** Order number cannot be totalled as no details have been supplied."; 
     orderNum++; 
    } 

    public Order(String productName, int quantity){ 
     this.productName = productName; 
     this.quantity = quantity; 
     getPrice(this.productName); 


     if(isValidOrder != false){ 
      calculate(); 
     } 
     orderNum++; 

    } 

public Order(String productName, int quantity, int discount){ 
    this.productName = productName; 
    testQuantity(quantity); 
    getPrice(productName); 

     if(isValidOrder != false){ 
      calculate(); 
     } 
       orderNum++; 
} 

private String getOrderDetails(){ 
    message = message; 
    if(isValidOrder == true && isDiscounted == false){ 

     message = "Order Number: " + quantity + "\n" + "Product Name; " + productName + "\n" + "Product Price: $" + price + "\n" + "Order Quantity: " + quantity + "\n" + "Total Price: $" + total; 

    } else if(isValidOrder == true && isDiscounted == true){ 

     message = "Order Number: " + quantity + "\n" + "Product Name; " + productName + "\n" + "Product Price: $" + price + "\n" + "Order Quantity: " + quantity + "\n" + "Total Price: $" + total; 
    } else { 
     return message; 
    } 
    return message; 
} 
+6

我不確定我收到任何問題 –

+0

你想知道嗎,你如何調用構造函數來創建新對象? – JohnnyAW

+0

嘿,你真的應該閱讀關於構造函數的Java教程。現在明白這一點非常重要,因爲它非常有趣。你的代碼中包含所有你需要的東西,你只需要掌握基本知識。如果有人發佈代碼答案,您可能無法得到它。拿走它,我犯了這個錯誤。 – RossC

回答

0

我也仍然非常學習者,因此認識到,這是不容易提問的時候,你甚至不完全知道你是問什麼。

基於您的具有挑戰性的描述,我試圖編寫一個可以構建的示例程序。請閱讀我的評論,因爲我試圖描述性很強,以幫助你理解/學習。

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'方法創建兩個命令來測試我們的應用程序。

有一個修補程序和玩它。我不確定這是否是您希望從您的問題中得到的結果,但希望您能找到有用的方法。

+0

感謝你非常感謝你的回覆。我會在早上看看它,但從外觀上看,這遠遠超出我的預期,所以我非常感謝您的迴應和您提供的工作量。通過快速瀏覽它,看起來有很多我甚至想到了我自己的東西,它有很多非常有用的信息,所以再次非常感謝你。 :DI實際上並沒有發佈我的所有項目,因爲有很多,但我已經涵蓋了大部分內容,只是這一部分非常困難,再次感謝你:) – ChokeOnThis

+0

如果你真的有任何多餘的機會只是爲了向您展示我對整個項目所做的工作,我已經將您的代碼修改成了幾個與代碼匹配的代碼,但是我遇到了一些我實際弄不清楚的錯誤。你絕對不需要,但你提供給我的幫助可能可以幫助我完成我的項目 – ChokeOnThis

+0

你想讓我看看你的項目的另一部分? – Levenal