import java.util.Scanner;
public class Bakery {
private static String flavors;
private static String weight;
private static int quantity;
public Bakery(){
this("Chocolate Moist", "1(KG)", 1);
}
public Bakery(String flavors, String weight, int quantity) {
this.flavors = flavors;
this.weight = weight;
this.quantity = quantity;
}
public void setFlavors(String flavors){
this.flavors = flavors;
}
public String getFlavors(){
return flavors;
}
public void setWeight(String weight){
this.weight = weight;
}
public String getWeight(){
return weight;
}
public void setQuantity(int quantity){
this.quantity = quantity;
}
public int getQuantity(){
return quantity;
}
public double getPrice(){
double price = 0;
if(weight == "1KG")
price = 25.5;
if(weight == "2KG")
price = 50;
if(weight == "3KG")
price = 70;
return price;
}
public String toString(){
return "\t" + getFlavors() + "\t\t" + getWeight() + "\t\t\t" + getPrice() + "\t\t\t\t" + getQuantity() + "\t\t\t\t" + getPrice() * getQuantity();
}
}
class TestBakery{
static double grandTotal = 0;
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
System.out.print("Enter how many types of cake you would like to order: ");
int numberOrder = scan.nextInt();
Bakery[] bakeryOrder = new Bakery[numberOrder];
System.out.println();
System.out.println("Flavour");
System.out.println("\t1. Chocolate Moist");
System.out.println("\t2. StrawBerry");
System.out.println("\t3. BlueBerry");
System.out.println("\t4. Cheesy Cake");
System.out.println("\t5. American Chocolate");
System.out.println();
System.out.println("Price List");
System.out.println("\t(1)KG = RM25.50");
System.out.println("\t(2)KG = RM50.00");
System.out.println("\t(3)KG = RM70.00");
int i = 1;
for(int x = 0; x < bakeryOrder.length; x++, i++){
String flavour = "";
String w = "";
System.out.println("Bakery item " + i);
System.out.println("-------------");
System.out.print("Enter your choice of cake flavour (1 - 5): ");
int choiceFlavour = scan.nextInt();
if(choiceFlavour == 1)
flavour = "Chocolate Moist";
if(choiceFlavour == 2)
flavour = "StrawBerryt";
if(choiceFlavour == 3)
flavour = "BlueBerry";
if(choiceFlavour == 4)
flavour = "Cheesy Cake";
if(choiceFlavour == 5)
flavour = "American Chocolate";
System.out.print("Enter the Weight Of Cake (1 - 1KG, 2 - 2KG and 3 - 3KG): ");
int choiceWeight = scan.nextInt();
if(choiceWeight == 1)
w = "1KG";
if(choiceWeight == 2)
w = "2KG";
if(choiceWeight == 3)
w = "3KG";
System.out.print("Enter quantity ordered: ");
int q = scan.nextInt();
System.out.println();
bakeryOrder[x] = new Bakery(flavour, w, q);
}
int j = 1;
System.out.println("Order Details:");
System.out.println("----------------");
System.out.println("No Cake Flavour Weight Unit Price(RM) Quantity Total Price(RM)");
System.out.println("-- -------------- ------ -------------- -------- ---------------");
for(int x = 0; x < bakeryOrder.length; x++, j++){
System.out.println(j + bakeryOrder[x].toString());
grandTotal += (bakeryOrder[x].getPrice() * bakeryOrder[x].getQuantity());
}
System.out.println("---------------------------------------------------------------------------");
System.out.println("\t\t\t\t\t\t\t\t\t\t\t\tGrand Total:\t\t" + grandTotal);
}
}
take a look and test with it,
if i input 2 into numberOrder, the output should have two information out, but the information is the same as second input.