【问题标题】:Beginner doing a Java Project [closed]初学者做一个Java项目[关闭]
【发布时间】:2013-09-20 14:15:50
【问题描述】:
  • 第二个构造函数是接收两个参数productName和 数量。 productName 参数将分配给 productName 类的实例变量。数量参数将被传递给 测试数量方法。在此之后,应该调用 getPrice 方法 传递 productName 参数。只有在订单有效时才需要调用calculate方法

  • 第三个构造函数是接收三个参数,productName,quantity 和折扣。
    productName 参数将分配给 productName 类的实例变量。 testQuantity、getPrice 和 testDiscount 方法都需要调用并传入所需的参数。 只有在订单有效时才需要调用calculate方法。

这个问题得到了回答,并以这段代码结束。感谢您的帮助

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; 
}

【问题讨论】:

  • 我不确定我是否有任何问题
  • 你想知道,如何调用构造函数来创建新对象吗?
  • 嘿,您真的应该阅读有关构造函数的 Java 教程。现在理解这一点非常重要,因为它是如此基础。您在代码中拥有所需的一切,您只需要掌握基础知识。如果有人发布代码答案,您可能不会得到它。把它从我这里拿走,我犯了那个错误。
  • 是的,我浏览了教程,但他们并没有真正提供太多关于我到底需要什么的信息,或者有些教程只是呃,有点太复杂而无法理解,我并不经常寻求帮助或其他任何东西,但只是停留在构造函数上,但下面的答案非常有帮助且非常具有描述性,并且绝对更容易理解和学习,从那时起我一直在寻找并试图理解他们何时使用其他术语而不是完全我需要什么

标签: java superclass


【解决方案1】:

我仍然是一个学习者,非常感谢当你甚至不完全确定自己在问什么时提出问题并不容易。

根据您具有挑战性的描述,我已尝试编写一个示例程序,您可以在此基础上进行构建。请阅读我的 cmets,因为我试图非常详细地帮助您理解/学习。

 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);
}

从一个“主”方法创建两个订单来测试我们的应用程序。

有一个修补匠并与它一起玩。我不确定这是否是您希望从您的问题中得到的结果,但希望您觉得这对您有所帮助。

【讨论】:

  • 非常感谢您的回复。早上我会看一下,但从外观上看,它比我预期的要多得多,所以我非常感谢您的回复和您提供的工作量。通过快速浏览它,看起来比我自己想出来的要多得多,而且它有很多非常有用的信息,所以再次非常感谢你。 :D 我实际上并没有发布我所有的项目,因为有很多,但我已经涵盖了大部分,只是那部分非常困难,再次感谢你:)
  • 如果您真的有空余的机会向您展示我对整个项目所做的工作,我已经将您的代码更改了几处以使其与我的代码相匹配,但我有一些我实际上无法弄清楚的错误。你当然不必这样做,但你提供给我的帮助可能会帮助我完成我的项目
  • 你想让我看看你项目的另一部分吗?
  • 如果您不介意并且有时间,那就太好了,是的。
猜你喜欢
  • 1970-01-01
  • 2017-07-31
  • 1970-01-01
  • 2011-09-20
  • 2011-03-17
  • 2020-07-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多