본문 바로가기

JavaCode(review)

Burger Price/ OOP ex/

반응형
Burger 만들기

Basic Burger
package com.company;

public class BasicBurger {              //basic burger + 4 additions

    private String name;                    //field
    private String rollType;
    private String meat;
    private double price;

    private String addition1Name;
    private double addition1Price;

    private String addition2Name;
    private double addition2Price;

    private String addition3Name;
    private double addition3Price;

    private String addition4Name;
    private double addition4Price;


    //constructor
    public BasicBurger(String name, String rollType, String meat, double price) {
        this.name = name;
        this.rollType = rollType;
        this.meat = meat;
        this.price = price;
    }
    //additions - assigned/access
    public void setAddition1Name(String name, double price) {
        this.addition1Name = name;
        this.addition1Price = price;
    }
    public void setAddition2Name(String name, double price) {
        this.addition2Name = name;
        this.addition2Price = price;
    }
    public void setAddition3Name(String name, double price) {
        this.addition3Name = name;
        this.addition3Price = price;
    }
    public void setAddition4Name(String name, double price) {
        this.addition4Name = name;
        this.addition4Price = price;
    }

    //method
    public double calculate(){
        double hamburgerPrice = this.price;
        System.out.println
                (this.name+"burger on a "+this.rollType+"rollType with"+meat+" Price ="+this.price);
        if(this.addition1Name!=null){
            hamburgerPrice += this.addition1Price;
            System.out.println("Added "+this.addition1Name+"("+this.addition1Price+") price");
        }
        if(this.addition2Name!=null){
            hamburgerPrice += this.addition2Price;
            System.out.println("Added"+this.addition2Name+"("+this.addition2Price+") price");

        }
        if(this.addition3Name!=null){
            hamburgerPrice += this.addition3Price;
            System.out.println("Added"+this.addition3Name+"("+this.addition3Price+") price");

        }
        if(this.addition4Name!=null){
            hamburgerPrice += this.addition4Price;
            System.out.println("Added"+this.addition4Name+"("+this.addition4Price+") price");

        }
        return hamburgerPrice;
    }

}
Healthy burger  /  add option 2
package com.company;

public class HealthyBurgur extends BasicBurger{         //basic burger option4개와 추가2개 option

    private String healthyAdditionName;
    private double healthyAdditionPrice;
    private String healthyAddition2Name;
    private double healthyAddition2Price;

                                                            //constructor
    public HealthyBurgur(String meat, double price) {
        super("Healthy", "Brown rye", meat, price);
    }

    // 추가 2개옵션
    public void setHealthyAddition1(String name, double price){
        this.healthyAdditionName = name;
        this.healthyAdditionPrice = price;
    }
    public void setHealthAddition2(String name, double price){
        this.healthyAddition2Name = name;
        this.healthyAddition2Price = price;
    }

    @Override
    public double calculate() {
        double hamburgerPrice = super.calculate();
          if(this.healthyAdditionName !=null){
            hamburgerPrice += this.healthyAdditionPrice;
            System.out.println("Added "+this.healthyAdditionName+"("+this.healthyAdditionPrice+") price");
        }
        if(this.healthyAddition2Name!=null){
            hamburgerPrice += this.healthyAddition2Price;
            System.out.println("Added"+this.healthyAddition2Name+"("+this.healthyAddition2Price+") price");

        }return hamburgerPrice;
    }
}

Deluxe burger / Chips and Drink automatically (No option-No additions.) 
package com.company;

public class DeluxeHamburger extends BasicBurger{      //2 option추가이나 옵션(additional)하지말고 자동추가로
    //Chips and Drink

    public DeluxeHamburger() {
        super("Deluxe","GrainBread","Bacon % cheese",14);
        super.setAddition1Name("Chips",3);
        super.setAddition2Name("Drink",1);      //constructor자체에 추가.
    }

                  //basic 햄버거의 addition 입력자체를 못쓰게 해야함.->입력시 오류메세지 나오도록설정.


    @Override
    public void setAddition1Name(String name, double price) {
        System.out.println("cannot add additional items to a deluxe burger");
    }

    @Override
    public void setAddition2Name(String name, double price) {
        System.out.println("cannot add additional items to a deluxe burger");
    }

    @Override
    public void setAddition3Name(String name, double price) {
        System.out.println("cannot add additional items to a deluxe burger");
    }

    @Override
    public void setAddition4Name(String name, double price) {
        System.out.println("cannot add additional items to a deluxe burger");
    }
}


Print out Code
package com.company;

public class Main {

    public static void main(String[] args) {
        // write your code here
        BasicBurger basicBurger = new BasicBurger("basic","normal","chicken",6.5);
        //basicBurger.calculate();
        basicBurger.setAddition1Name("lettuce",2.0);
        basicBurger.setAddition2Name("onion",3);
        System.out.println("Total price = "+ basicBurger.calculate());

        HealthyBurgur healthyBurgur = new HealthyBurgur("Bacon",5.0);
        healthyBurgur.setAddition3Name("tomatoes",2.0); //basic burger option
        healthyBurgur.setHealthAddition2("bean",4.0);   //healty burger option
        System.out.println("Total healthy burger Price = "+ healthyBurgur.calculate());

        DeluxeHamburger deluxeHamburger = new DeluxeHamburger();
        deluxeHamburger.setAddition1Name("onion",2); // shoudn't choose option.
        System.out.println("Total deluxe Price = "+deluxeHamburger.calculate());

    }
}


Output
D:\IT\JDK\jdk11.0.6_10\bin\java.exe "-javaagent:D:\IT\IDEA\IntelliJ IDEA Community Edition 2020.1\lib\idea_rt.jar=50578:D:\IT\IDEA\IntelliJ IDEA Community Edition 2020.1\bin" -Dfile.encoding=UTF-8 -classpath D:\IT\NewProject\Main\out\production\Main com.company.Main
basicburger on a normalrollType withchicken Price =6.5
Added lettuce(2.0) price
Addedonion(3.0) price
Total price = 11.5
Healthyburger on a Brown ryerollType withBacon Price =5.0
Addedtomatoes(2.0) price
Addedbean(4.0) price
Total healthy burger Price = 11.0
cannot add additional items to a deluxe burger
Deluxeburger on a GrainBreadrollType withBacon % cheese Price =14.0
Added Chips(3.0) price
AddedDrink(1.0) price
Total deluxe Price = 18.0

Process finished with exit code 0