Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone, I wanted to ask if there was a problem with my calculate method because
it wont do anything. I also wanted to ask if its possible for an override tostring method to have more then one return statement because it seems its only processing the first one. Any ideas would help at this stage, thanks a heap

public class order {

    public static int OrderNum=0;
    public double Price = 0.0;
    public double Discount = 0.0;
    public int Quantity = 0;
    public double Total = 0.0;
    public String ProductName="";
    public String Message = "ERROR, you havent supplied any information";
    boolean isDisCounted= false;
    boolean isValidOrder = true;

    public order()
    {
        isValidOrder = false;
        Message= "ERROR: you havent supplied any information";
        OrderNum++;
    }
    public order(String s, double d, int i)
    {
        ProductName=s;
        Price = d;
        Quantity = i;
        OrderNum++;
    }
    public order(String s, double d, int i, double d2)
    {
        ProductName=s;
        Price = d;
        Quantity = i;
        Discount = d2;

       if(Discount >=1 && Discount <=100)
        {
           isDisCounted = true;
           Discount = d2;
           OrderNum++;
        }
        else
        {
            isValidOrder=false;
            Message = "Error: discount-" +Discount+"isnt valid";
            OrderNum++;
        }
        }

    public void calculate()
    {
        if (isValidOrder = false)
        {
            Message="ERROR:Order number"+OrderNum+" is invalid";
        }
        else if(isDisCounted)
        {
            Total = Quantity*Price;
        }
        else
        {
            Total =Quantity *Price - Quantity* Price *(Discount/100);
        }
    }
  
  @Override
  public String ToString()
        {
            if(isValidOrder && isDisCounted)
            {
                return  "order number"+"\n"+OrderNum+"\n"+"\n"+"price"+Price+"\n"+"quantity"+Quantity+"\n"+"total"+Total;
            }
            else if(isValidOrder && isDisCounted != isDisCounted)
            {
                return "order number"+"\n"+OrderNum+"price"+Price+"\n"+"quantity"+Quantity+"\n"+"discount"+Discount+"\n"+"total"+Total;
            }
            return(Message);
            }
        }


this is the secound class, incase this is where the problem lies

VB
public class ordercreator {

    
    public static void main(String[] args) {
        testcase0();
        tescase1();
    }

        public static void testcase0()
        {
            order o1 = new order("stapler",12.7, 6);
            System.out.println("testcase 0:"+"\n"+ o1);
        }

    private static void tescase1() {
         order o2 = new order("glue",6.4, 4, 30);
            System.out.println("testcase 1:"+"\n"+ o2);
    }
    }
Posted

1 solution

Reading the code there are some flaws in your logic, lets start at the top and go through it.

It is an accepted convention in Java that all class names start with a capital letter and all other identifiers with a lower case letter.

So, with our class name Order, let's look at the variables. A static variable is shared by all instances of the class, so whenever you reference OrderNum it is the same value as used by every other class. The normal approach for this is to have a static last number and an instance variable with the current value for the object; I'll demonstrate this in a while.

It is also considered best practice to have no public member variables, keep them private and expose accessor methods.

So our class now [without any methods] becomes:

Java
public class Order {
 
    private static int lastOrderNum = 0;
    private int orderNumber;
    private double price;
    private double discount;
    private int quantity;
    private double total;
    private String productName;
    private String message;
    private boolean isDisCounted;
    private boolean isValidOrder;
}

Note nothing is initialised bar the static last order number.

Now a good approach, that I would recommend to you, is to have a single constructor that does the work and all the other constructors call into it:
Java
public Order()
{
    Order("",0.0,0);
}
public Order(String s, double d, int i)
{
    Order("",0.0,0,0.0);
}
public Order(String productName, double price, int quantity, double discount)
{
    this.orderNumber = ++Order.lastOrderNumber;
    this.productName = productName;
    this.price = price;
    this.quantity = quantity;
    this.discount = discount;
    this.calculate();
}

Now we have a simple constructor that anyone can understand, better for maintainablility.

Now for the calculate method. Check it's parameter and if they're all okay work out the total:
Java
public void calculate()
{
    if (this.productName == null || "".equals(this.productName))
    {
        this.message = "ERROR: No product name supplied";
    }
    else if (this.price <= 0.0)
    {
        this.message = "ERROR: Price must be positive.";
    }
    else if (this.quantity <= 0.0)
    {
        this.message = "ERROR: Quantity must be positive.";
    }
    else if (this.discount <= 0.0 || this.discount >= 100.0)
    {
        this.message = "ERROR: Discount must be between 0 and 100.";
    }
    else
    {
        this.message = null;

        this.total = this.quantity * this.price;
        if (this.discount > 0.0) {
            this.total = this.total * (100.0 - this.discount) / 100.0;
        }
    }
}


The final method, toString should return the CURRENT state of the object, without changing anything. Once a return statement is reached, the method exits:
Java
public String toString()
{
    if (this.message != null) {
        return this.message ;
    }
    if (this.discount = 0.0)
    {
        return  "order number"+"\n" + this.orderNum+"\n"+"\n"+
                "price"+this.price+"\n"+
                "quantity"+this.quantity+"\n"+
                "total"+this.total;
    }
    return  "order number"+"\n" + this.orderNum+"\n"+"\n"+
            "price"+this.price+"\n"+
            "quantity"+this.quantity+"\n"+
            "discount"+this.discount+"\n"+
            "total"+this.total;
}

Simples.
 
Share this answer
 
Comments
Richard MacCutchan 31-Oct-12 5:44am    
Excellent tutorial, although I should dock you some points for double posting.
Nagy Vilmos 31-Oct-12 6:02am    
I didn't see that duplicate, stupid hamsters.
Removed now.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900