Search code examples
javasuperclass

Beginner doing a Java Project


  • The second constructor is to receive two parameters, productName and quantity. The productName parameter is to be assigned to the productName instance variable of the class. The quantity parameter is to be passed to the testQuantity method. Following this a call to the getPrice method should be made passing the productName parameter. Calculate method needs to be called only if order is valid

  • The third constructor is to receive three parameters, productName, quantity and discount.
    The productName parameter is to be assigned to the productName instance variable of the class. The testQuantity, getPrice and testDiscount methods all need to be called passing in the required parameters. Calculate method needs to be called only if order is valid.

Question for this got answered and ended up with this code. Thanks for the help

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

Solution

  • I am also still very much a learner, and so appreciate that it's not easy asking questions when you aren't even totally sure what you're asking about.

    Based on your challenging description I have tried to write a example program that you can build upon. Please read my comments as i have tried to be very descriptive to help you understand/learn.

     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
    }
    

    I have added some extra fields at the bottom of the class to make my example easier to show, and because I was very unsure as to how you want some of your methods to work. I have placed and initialized these in your constructors, and commented as to why.

    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
    }
    

    The above method displays all the details of the order created by either constructor and displays it's details to see.

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

    I am unsure what you want to do with this method and so have left it blank. If you need to store a total number of available items to check the quantity against the best way would be a database, which takes a whole different set of learning.

    private int testDiscount(int discount, int orderPrice){  //Will return a discounted price and store it in 'discountPrice' field
    int reducedPrice = (orderPrice - discount);
    return reducedPrice;
    }
    

    Returns a discounted price if the user has a discount in addition to the total order price. Both are held by the Order object which you create with the constructor

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

    The above switch could possibly be the best way to check the price for items. Each case holds the name of an item and a price for it. You use that price multiplied by the quantity to create the order price.

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

    Create two orders from a 'main' method to test our application.

    Have a tinker and play with it. I am unsure if this was what you wanted from your question but hopefully you find this helpful either way.