我也仍然非常学习者,因此认识到,这是不容易提问的时候,你甚至不完全知道你是问什么。
基于您的具有挑战性的描述,我试图编写一个可以构建的示例程序。请阅读我的评论,因为我试图描述性很强,以帮助你理解/学习。
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