初学者做Java项目

时间:2013-09-16 12:19:26

标签: java superclass

  • 第二个构造函数是接收两个参数,productName和 数量。将productName参数分配给productName 类的实例变量。数量参数将传递给 testQuantity方法。在此之后,应该调用getPrice方法 传递productName参数。只有在订单有效时才需要调用计算方法

  • 第三个构造函数是接收三个参数,productName,quantity 和折扣 将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; 
}

1 个答案:

答案 0 :(得分: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;
}

如果用户除总订单价格外还有折扣,则返回折扣价。两者都由您使用构造函数

创建的Order对象持有
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;   
}

上述开关可能是检查物品价格的最佳方式。每个案例都包含一个项目的名称和价格。您使用该价格乘以数量来创建订单价格。

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'方法创建两个订单以测试我们的应用程序。

修补一下并玩弄它。我不确定这是否是你想要的问题,但希望你发现这对你有帮助。