我仍然是一个学习者,非常感谢当你甚至不完全确定自己在问什么时,提出问题并不容易。
根据您具有挑战性的描述,我尝试编写一个您可以构建的示例程序。请阅读我的评论,因为我试图非常描述性以帮助您理解/学习。
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' 方法创建两个订单来测试我们的应用程序。
有一个修补匠并玩它。我不确定这是否是您从问题中想要的,但希望您发现这对您有所帮助。