2013-09-16 106 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