Hey guys. I have two questions about my code(which has no errors but isnt giving the output i want)...Is it possible to have an if statement in my 3rd constructor? Also why is my output giving the filepath instead of outputting calcutations? Thanks for having a look

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)///////////////// Is this an issue here?  /////////
        {
           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+"cannot be totalled as its invalid";
        }
        else if(isDisCounted)
        {
            Total = Quantity*Price;
        }
        else
        {
            Total =Quantity *Price - Quantity* Price *(Discount/100);
        }
    }
        public String ToString()
        {
            if(isValidOrder && isDisCounted)
            {
                return(Message= OrderNum+ProductName+Price+Quantity+Total);
            }
            else if(isValidOrder && isDisCounted != isDisCounted)
            {
                return(Message = OrderNum+ProductName+Price+Quantity+Total+Discount );
            }
            return(Message);
            }
        }

        public class ordercreator {

    public static void main(String[] args) {

        tescase0();
    }
        public static void testcase0()///// this is the template to use, Is it possible to do this? ////////
        {
            order o1 = new order("stapler",12.7, 6);
            System.out.println("testcase 0:"+"\n"+ o1);
        }              


    testcase 0:   ////////////////this is the output i get///////////////////////
    stellarstationary.order@8dc8569

Recommended Answers

All 4 Replies

The answer to your first two questions is "no problem, that's perfectly OK"
The output you get is what you asked for on lines 82/83. When you print the order object o1 Java has no information on how to print that, so you get a default printout which shows the the object's class (stellarstationary.order) and its hash (8dc8569). If you want to print someting more useful you define a toString method in your order class. This overrides the default one you inherit from Object, and allows you to specify how to print an order, eg

@Override
public String toString() {
   return "This is an order, its number is " + OrderNum;
}

ps Java naming conventions are that classes should begin with an upper case letter, variables should begin with a lower-case letter. It's easier for others to understand your code if you stick to the conventions.

Thanks james, i wasnt sure if the if statement would work inside the constructor but now i know thats ok ill put all effort into the print out

As James says: you can't get information, if you print object as output.instead take/return something from that constructor and print them.
You can also add print statement inside Constructor.

thanks

Cool ive got the print out working but now i will have to get my calculate method to work, hopefully its the last hurdle

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.