Ruhroh
Ruhroh

Reputation: 3

Accepting inputs, doing calculations and showing the answer

So i'm trying to write a basic program in java. it accepts inputs, does calculations on the inputs and displays the result. It has 3 constructors, the first accepts no inputs and creates an error message, the second and third accept inputs and perform calculations on them.

So the program should accept (from a creator class):

Order o1 = new Order ("Number plate", 12.7, 6)

The program does accept the above code, but doesn't perform any calculations, it only displays the 'new order.'

Not sure what im doing wrong here, ive added comments to try and explain what im doing.

public class Order {
private static int orderNum = 0;
public Order(){
    orderNum++;                 //setting the static variable
}

private String productName;
public double price;
private double discount;
public double quantity;
public double total;
private String message;
private boolean isDiscounted = false;
private boolean isValidOrder = true;

//setting the instance variables and booleans


public boolean isDiscounted(){          //setting the valid
discount boolean value
    if (discount > 100 || discount ==0) {
        return false;
    }
    else {
        return true;
    }
}

public boolean isValidOrder(){      //setting the valid order boolean value
    if (total ==0){
        return false;
    }
    else {
        return true;
    }
}

public Order(String m) {
    orderNum++;
    this.message = m;
    isValidOrder = false;
    isDiscounted = false;
}
//first constructor accepting no parameters, with boolean values set

public Order(String pN, double p, int q) {
    orderNum++;
    this.productName = pN;
    this.price = p;
    this.quantity = q;
    isValidOrder = true;
    isDiscounted = false;
}
//second constructor accepting 3 parameters, with boolean values set

public Order(String pN, double p, double d, int q) {
    orderNum++;
    this.productName = pN;
    this.price = p;
    this.discount = d;
    this.quantity = q;
    isValidOrder = true;
    isDiscounted = true;
}
//third constructor accepting 4 parameters, with boolean values set

    public void setTotal(double t){
        total = t;
        isDiscounted = true;
    }

//setting the total



public double getTotal(){
    if (isValidOrder == true){
        return ((price * quantity)-(price * quantity * (discount/100)));
    }
    else if (isDiscounted == false){
        return (price * quantity);
    }
    return total;
}
//getting the total and calculations



@Override
public String toString(){
    if (isDiscounted == true){
        message = ("Order number: " + Order.orderNum + "\nProduct
 Name: " + productName
                + "\nProduct Price: " + price + "\nOrder Quantity:
 " + quantity
                + "\nDiscount: " + discount + "\nTotal Price: " + total);
        }

    //first part of toString method, with discount
    else if (isDiscounted == false){
        message = ("Order number: " + Order.orderNum + "\nProduct
 Name: " + productName
                + "\nProduct Price: " + price + "\nOrder Quantity:
" + quantity
                + "\nTotal Price: " + total);
    }
    //second part of toString method, with no discount
    else if (isValidOrder == false){
        message = "Error";
    }
    //third part of toString method with error message
    return message;
 }

}

Upvotes: 0

Views: 53

Answers (1)

Shashi Dk
Shashi Dk

Reputation: 174

I have modified your code check it once .

public class Order {
private static int orderNum = 0;
public Order(){
orderNum++;                 //setting the static variable
}
private String productName;
public double price;
private double discount;
public double quantity;
public double total;
private String message;
private boolean isDiscounted = false;
private boolean isValidOrder = true;

//setting the instance variables and booleans


public boolean isDiscounted() {          //setting the discount boolean 
    if (discount > 100 || discount == 0) {
        return false;
    } else {
        return true;
    }
}

public boolean isValidOrder() {      //setting the valid order boolean value
    if (total == 0) {
        return false;
    } else {
        return true;
    }
}

public Order(String m) {
    orderNum++;
    this.message = m;
    isValidOrder = false;
    isDiscounted = false;
}
//first constructor accepting no parameters, with boolean values set

public Order(String pN, double p, int q) {
    orderNum++;
    this.productName = pN;
    this.price = p;
    this.quantity = q;
    isValidOrder = true;
    isDiscounted = false;
}
 //second constructor accepting 3 parameters, with boolean values set

public Order(String pN, double p, double d, int q) {
    orderNum++;
    this.productName = pN;
    this.price = p;
    this.discount = d;
    this.quantity = q;
    isValidOrder = true;
    isDiscounted = true;
}
 //third constructor accepting 4 parameters, with boolean values set

public void setTotal(double t) {
    total = t;
    isDiscounted = true;
}

//setting the total


public double getTotal() {
    if (isValidOrder == true) {
        return ((price * quantity) - (price * quantity * (discount / 100)));
    } else if (isDiscounted == false) {
        return (price * quantity);
    }
    return total;
}
 //getting the total and calculations


@Override
public String toString() {
    if (isDiscounted == true) {
        message = ("Order number: " + Order.orderNum + " \n Product Name: "
                + productName + "\nProduct Price: " + price + "\nOrder Quantity: " + quantity +
                "\nDiscount: " + discount + "\nTotal Price: " + total);
    }

    //first part of toString method, with discount
    else if (isDiscounted == false) {
        message = ("Order number: " + Order.orderNum + "\nProduct Name: " + productName
                + "\nProduct Price: " + price + "\nOrder Quantity:" + quantity
                + "\nTotal Price: " + total);
    }
    //second part of toString method, with no discount
    else if (isValidOrder == false) {
        message = "Error";
    }
    //third part of toString method with error message
    return message;
}

}

Upvotes: 1

Related Questions