In: Computer Science
ITAP2005 Java Programming Fundamentals
Healthy Eat Bakery
Healthy Eat bakery is a family owned business. It is renowned for the healthy bread baked since generations in the family. In HealthyEat bakery, there are three types of bread sold: Rye Bread, Multi seeds bread, Brown Bread. It is not only known for the healthy bread but also for the economical prices of the bread.
The prices of the bread
-> Rye Bread (RB) – $1.3 ,
-> Multiseeds bread (MB) - $1.2 and
-> Brown bread (BB) - $1.
Each bread has predefined price, amount sold and name (the abbreviations).
The person holds 2 data fields:
➔ the type of the bread bought (the abbreviations)
➔ the money they have
You are required to develop program as below by applying OOP principles and constructs:
1) That calculates the total earning of each type of bread;
2) That calculates your total earnings;
3) That shows a person, who has a definite amount of money, how much of a particular type of bread may buy.
4) Draw the class diagram showing the relationship among the classes
The required output as follows.
Input: 4,"RB" -> Output: Rye bread: 5.2
Input: 10, "MB" -> Output: Multiseeds Bread: 12.0
Input: 4, "BB" -> Output: Brown Bread: 4.0 Total earnings: 21.2
Input: (for a person) 22.0,"RB" -> Output: You may buy 16 loaves of this type of bread
Hello Here is the java code implementated for Bread, Person and Bakery class(Main test). User can enter number of inputs and find individual and total earnings. Hope you like it.
Source Code:
Person.java:
public class Person extends Bread {
private double amount;
private String bread_type;
public Person(double amount, String bread_type) {
this.amount = amount;
this.bread_type = bread_type;
}
public int calculateLoaves(double amount, String breadType) {
int loaves = 0;
if(breadType.equalsIgnoreCase("RB"))
loaves = (int) (amount / RB);
else if(breadType.equalsIgnoreCase("MB"))
loaves = (int) (amount / MB);
else
loaves = (int) (amount/BB) ;
return loaves;
}
}
Bread.java:
public class Bread {
private double price = 0.0;
private int amount_sold;
private String name;
final double RB = 1.3;
final double MB = 1.2;
final float BB = 1;
public Bread()
{
public Bread(int amount_sold, String name) {
this.amount_sold = amount_sold;
this.name = name;
}
public String gettBreadType() {
if(name.equalsIgnoreCase("RB"))
return "Rye Bread";
else if(name.equalsIgnoreCase("MB"))
return "Multiseeds Bread";
else
return "Brown Bread";
}
public double calculateTotalEarnings() {
if(name.equalsIgnoreCase("RB"))
price = RB * amount_sold;
else if(name.equalsIgnoreCase("MB"))
price = MB * amount_sold;
else
price = BB *amount_sold;
return price;
}
@Override
public String toString() {
return gettBreadType() + " : " + calculateTotalEarnings();
}
}
****************************************************************************
Bakery.java:
import java.util.ArrayList;
import java.util.Scanner;
public class Bakery {
public static void runBakery() {
Scanner sc = new Scanner(System.in);
String choice ="y" ;
ArrayList<Bread> breads = new ArrayList<Bread>();
while (choice.equalsIgnoreCase("y")) {
System.out.println("Enter the amount sold: ");
int amountSold = sc.nextInt();
System.out.println("Enter the bread type: ");
String breadType = sc.next();
Bread newBread = new Bread(amountSold,breadType);
breads.add(newBread);
System.out.println("Do want to add another bread(Y/N)?: ");
choice= sc.next();
}
double totalEarnings = 0.0;
for(Bread bread : breads) {
System.out.println(bread);
totalEarnings += bread.calculateTotalEarnings();
}
System.out.println("Total Earnings: "+ totalEarnings);
System.out.println("\nEnter the amount sold: ");
double amountPerson = sc.nextInt();
System.out.println("Enter the bread type bought: ");
String boughtBreadType = sc.next();
Person p = new Person(amountPerson, boughtBreadType);
int loaves = p.calculateLoaves(amountPerson, boughtBreadType);
System.out.println("You may buy " + loaves + " loaves of this type of bread");
sc.close();
}
public static void main(String[] args) {
runBakery();
}
}
Class Diagram :