-4
  • 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; 
}
ChokeOnThis
  • 29
  • 1
  • 9
  • 6
    I'm not sure I'm getting any question – Prasad Kharkar Sep 16 '13 at 12:21
  • do you want to know, how you can call the constructor to create new objects? – JohnnyAW Sep 16 '13 at 12:27
  • Hey, you really really should read up on the Java tutorials on constructors. It is so so important to get this understood now, as it is so fundemental. You have all you need in your code, you just need to get to grips with the basics. If someone posts a code answer you may not get it. Take it from me, I made that mistake. – RossC Sep 16 '13 at 13:41
  • Yeah I looked through tutorials and they didn't really give much information on what exactly I needed, or some of the tutorials were just uhm, a bit too complicated to understand and I don't really often ask for help or anything but was just stuck on the constructors but the answer below is very helpful and very descriptive and definitely easier to understand and learn from then what I've been looking at and trying to understand when they use other terms and not exactly what I needed per se – ChokeOnThis Sep 16 '13 at 15:10

1 Answers1

0

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.

Levenal
  • 3,796
  • 3
  • 24
  • 29
  • Thank you so much for your reply. I'll have a look through it in the morning but by the looks of it, it's a lot more than what I would have expected so I'm really appreciated by your response and the work load you've provided. By quickly looking through it, it looks like there's a lot more then what I would've even figured out for myself and it has so much information that is very useful so again thank you so much. :D I didn't actually post all of my project as there was a lot, but I've covered pretty much most of it, just that part was quite difficult and again thank you :) – ChokeOnThis Sep 16 '13 at 15:00
  • If you actually had any spare chance just to show you what I have done with the whole project, I've put your code in a changed a couple of things to match it up with my code but I've got a few mistakes that I can't actually figure out. You definitely don't have to, but the help you provided me could possibly help me finish off my project – ChokeOnThis Sep 18 '13 at 10:50
  • You want me to have a look at another section of your project? – Levenal Sep 18 '13 at 12:06
  • If you wouldn't mind and you have the time, that'd be nice, yes. – ChokeOnThis Sep 19 '13 at 01:53