Reputation: 53
So I have a project where I need to create this:
public void calculate (){
if (isDiscounted == true){
total = quantity * price - quantity * price * (discount/100);
}
else {
total = quantity * price;
}
}
But the total does not appear correctly in here, when I set it as say ("Compass", 20 , 30), the discount does not apply, only the normal total:
public 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" +"Discount : " + discount + "%" + "\n" + "Total Price: $" + total;
}
else {
return message;
}
return message;
}
I'm a very beginner programmer so i'm not sure if you understand what i'm saying. Basically I can't link it up :/
Here's the rest of the code:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package order;
import java.util.Arrays;
/**
*
* @author Alexander
*/
public class Order {
private static String[] products = {
"Compass", "Eraser", "Pen", "Pencil", "Pencil Case", "Pencil Sharpener", "Ruler", "Scissors"
};
private static double[] prices = {
4.5, 0.5, 0.3, 0.6, 10, 0.3, 1.2, 2.5
};
public static int orderNum = 0; //
private String productName;
private double price = 0;
private int discount = 0;
private final int minDiscount = 0;
private final int maxDiscount = 50;
private int quantity = 0;
private final int minQuantity = 0;
private final int maxQuantity = 1000;
private double total;
private String message;
private boolean isDiscounted = false;
private boolean isValidOrder = true;
public void calculate() {
if (isDiscounted == true) {
total = quantity * price - quantity * price * (discount / 100);
} else {
total = quantity * price;
}
}
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.quantity = quantity;
this.productName = productName;
testQuantity(quantity);
getPrice(productName);
if (isValidOrder == true) {
calculate();
}
orderNum++;
}
public Order(String productName, int quantity, int discount) {
this.productName = productName;
this.quantity = quantity;
this.discount = discount;
testQuantity(quantity);
testDiscount(discount);
getPrice(productName);
if (isValidOrder != false) {
calculate();
}
orderNum++;
}
private void getPrice(String pce) {
Arrays.sort(products);
int searchProductArray = Arrays.binarySearch(products, pce);
if (searchProductArray >= 0) {
price = prices[searchProductArray];
productName = products[searchProductArray];
isValidOrder = true;
} else {
price = 0.0;
isValidOrder = false;
message = "**ERROR**: Invalid product name";
}
}
public void testQuantity(int quantity) {
boolean isValidOrder = true;
if (quantity <= minQuantity) {
message = "**ERROR**: Invalid quantity. Quantity cannot be 0 or less";
isValidOrder = false;
} else if (quantity > maxQuantity) {
message = "**ERROR**: Invalid quantity. Quantity cannot be greater than 1000";
isValidOrder = false;
} else {
this.quantity = quantity;
this.isValidOrder = true;
}
}
public void testDiscount(int discount) {
boolean isDiscounted = false;
if (discount <= minDiscount) {
message = "**ERROR**: The discount rate cannot be lower than or equal to 0";
} else if (discount > maxDiscount) {
message = "**ERROR**: The discount rate cannot be greater than 50";
} else {
this.discount = discount;
this.isDiscounted = true;
}
}
public 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" + "Discount : " + discount + "%" + "\n" + "Total Price: $" + total;
} else {
return message;
}
return message;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Order O1 = new Order("Compass", 20, 30);
System.out.println(O1.total);
OrderLaunch frame = new OrderLaunch();
frame.setVisible(true);
}
}
Upvotes: 0
Views: 85
Reputation: 20608
Integer division!
This line
total = quantity * price - quantity * price * (discount / 100);
divides discount
by 100. Both are integers, so this evaluates to 0 if the discount is lower than 100.
You simply have to make it a double operation:
total = quantity * price - quantity * price * (discount / 100.0);
Upvotes: 1