Cake Store in Java Homework Sample

Create a base class of Cake and derived classes of OrderCake and ReadyMadeCake. The OrderCake has a weight which is used to determine the price. The ReadyMadeCake has a quantity which has a quantity which is used to determine the price. Create an array of 20 cakes, display the total price for all the cakes, and the total price and quantity of the ReadyMadeCakes. Display the information for the cake that was sold for the highest price. For more Java programming assignments contact us for a quote.

Solution:

import java.util.Scanner;

class Cake{
String name;
double rate;
public Cake() {
name=””;
rate=0;
}
public Cake(String n,double r) {
name=n;
rate=r;
}
public String getName() {
return name;
}
public double getRate() {
return rate;
}
public void setName(String n) {
name=n;
}
public void setRate(double r) {
rate=r;
}
public double price() {
return rate;
}
public String toString() {
return “Name : “+name+”\nRate : “+rate;
}
}
class OrderCake extends Cake{
double weight;
public OrderCake() {
name=””;
rate=0;
weight=0;
}
public OrderCake(String n,double r,double w) {
name=n;
rate=r;
weight=w;
}
public void setWeight(double w) {
weight=w;
}
public double getWeight() {
return weight;
}
public double price() {
return weight*rate;
}
public String toString() {
return “Name : “+name+”\nPrice : “+weight*rate;
}
}
class ReadyMadeCake extends Cake{
int quantity;
public ReadyMadeCake() {
name=””;
rate=0;
quantity=0;
}
public ReadyMadeCake(String n,double r,int q) {
name=n;
rate=r;
quantity=q;
}
public void setQuantity(int q) {
quantity=q;
}
public double getQuantity() {
return quantity;
}
public double price() {
return quantity*rate;
}
public String toString() {
return “Name : “+name+”\nPrice : “+quantity*rate;
}
}
public class delicious {

public static void main(String[] args) {
Cake[] arr=new Cake[20];
int i=0;
Scanner sc=new Scanner(System.in);
String opt;
String n;
double r;
double w;
int q;
double totalPrice=0;
double readyMadePrice=0;
double maxPrice=-1;
int maxIndex=0;
while(i<20) {
System.out.println(“Enter type of cake from below menu(1 or 2) for cake number “+(i+1));
System.out.println(“1.OrderCake”);
System.out.println(“2.ReadyMadeCake”);
opt=sc.next();
if(opt.equals(“1”)) {
System.out.println(“Enter name of cake”);
sc.nextLine();
n=sc.nextLine();
System.out.println(“Enter rate of cake”);
r=sc.nextDouble();
System.out.println(“Enter weight of cake”);
w=sc.nextDouble();
arr[i]=new OrderCake(n,r,w);
totalPrice=totalPrice+arr[i].price();
if(maxPrice<arr[i].price()) {
maxPrice=arr[i].price();
maxIndex=i;
}
i++;

}
else if(opt.equals(“2”)) {
System.out.println(“Enter name of cake”);
sc.nextLine();
n=sc.nextLine();
System.out.println(“Enter rate of cake”);
r=sc.nextDouble();
System.out.println(“Enter quantity of cake”);
q=sc.nextInt();
arr[i]=new ReadyMadeCake(n,r,q);
totalPrice=totalPrice+arr[i].price();
readyMadePrice=readyMadePrice+arr[i].price();
if(maxPrice<arr[i].price()) {
maxPrice=arr[i].price();
maxIndex=i;
}
i++;
}
else {
System.out.println(“Invalid input!!”);
}
}
System.out.println(“Total price of cakes = $”+totalPrice);
System.out.println(“Total price of readymade cakes = $”+readyMadePrice);
System.out.println(“Cake sold for highest price is below:”);
System.out.println(arr[maxIndex].toString());
}

}