Can I improve my code? There are 3 types of hamburgers.
The basic hamburger will have these properties:
- prices
- type of bread
- type of meat
- maximum 4 supplements
Then, create a Bio burger that will be gray bread, and which can receive in addition 2 elements supplement to the basic either 6 supplements max.
After, create a Deluxe Hamburger : that will be provided with fries and a drink, but it will not be possible to add supplements.
Class Hamburger
public class Hamburger {
// Attributes
double price;
private String typeBread;
private String typeMeat;
int counter;
public Hamburger(double price, String typeBread, String typeMeat)
{
this.price = price;
this.typeBread = typeBread;
this.typeMeat = typeMeat;
this.counter = 0;
System.out.println("Hamburger " + this.getTypeBread() + " - " + this.typeMeat + " - " + this.getPrice() + " euros");
}
// add supplement 4 maximum
public void addSupplement(String type, double amount)
{
counter++;
if(counter <= 4)
{
System.out.println("Supplement " + counter + " : " + type + " - " + amount + " euros");
this.price = this.price + amount;
}
else
{
System.out.println("Error supplement maximum is 4 ");
}
}
public void total()
{
System.out.println("Total => " + this.getPrice() + " euros");
}
// les getters
public double getPrice()
{
return price;
}
public String getTypeBread()
{
return typeBread;
}
public String getTypeMeat()
{
return typeMeat;
}
public int getCounter()
{
return counter;
}
}
Class Bio
public class Bio extends Hamburger {
public Bio(double price, String typeMeat) {
super(price, "gray bread", typeMeat);
}
public void addSupplement(String type, double amount) {
counter++;
if (counter<=6){
System.out.println("Supplement " + counter + " : " + type + " - " + amount + " euros ");
this.price = this.price+ amount;
}
else{
System.out.println("Error supplement 6 maximum ");
}
}
}
Class Deluxe
public class Deluxe extends Hamburger {
private String accompaniment;
private String drink;
// Builders
public Deluxe () {
super (7.99, "white bread", "veal");
this.accompaniment = "fried";
this.drink = "drink";
System.out.println ("Included in the DELUXE formula:" + this.getDrink () + "-" + this.getAccompaniment ());
}
public String getAccompaniment () {
return accompaniment;
}
public String getDrink () {
return drink;
}
public void addSupplement (String type, double price) {
System.out.println ("No supplement allowed for this formula");
}
public void total () {
super.total ();
}
}
Class Main
public class Main {
public static void main(String[] args) {
System.out.println("BASIC");
Hamburger myHamburger = new Hamburger(5.99, "white bread", "beef");
myHamburger.addSupplement("Tomatos", 0.99);
myHamburger.addSupplement("Bacons", 0.59);
myHamburger.addSupplement("Onions", 0.49);
myHamburger.addSupplement("Ketchup", 0.29);
System.out.println("__________");
myHamburger.total();
System.out.println("-----------------------");
System.out.println("BIO");
Bio myBio = new Bio (6.99, "Veal");
myBio.addSupplement ("Onions", 0.49);
myBio.addSupplement("Tomatoes", 0.99);
myBio.addSupplement ("Bacon", 0.59);
myBio.addSupplement ("Cucumbers", 0.49);
myBio.addSupplement("Ketchup", 0.29);
myBio.addSupplement ("Mayo", 0.29);
myBio.addSupplement ("Cocktail", 0.29);
System.out.println ( "__________");
myBio.total ();
System.out.println("-----------------------");
System.out.println("DELUXE");
Deluxe myDeluxe = new Deluxe();
myDeluxe.addSupplement("Tomatoes", 0.99);
System.out.println("__________");
myDeluxe.total();
}
}