TypeScript/타입스크립트 TypeScript

[타입스크립트] 커피머신 클래스만들기 실습

Rainbow🌈Coder 2022. 4. 19. 18:21
728x90
  //커피머신 만들기
  type CoffeeCup = {
    orderShot: number;
  };

  class coffeeMachine {
    private static ONE_SHOT_CAPSULE: number = 3;
    private static ONE_CUP_WATER: number = 10;

    static makeMachine(beans: number, water: number): coffeeMachine {
      return new coffeeMachine(beans, water);
    }

    constructor(private capsule: number = 3, private water: number = 10) {}

    makeCoffee(orderShot: number): CoffeeCup {
      if (this.capsule < orderShot * coffeeMachine.ONE_SHOT_CAPSULE) {
        throw new Error(`커피캡슐 부족!!! 현재 보유 캡슐 : ${this.capsule}`);
      }
      if (this.water < coffeeMachine.ONE_CUP_WATER) {
        throw new Error(`물 부족!!! 남은 물 ${this.water}`);
      }
      console.log("캡슐쓰기 전 " + this.capsule);
      this.useCapsule(orderShot);
      console.log("물쓰기 전 " + this.water);
      this.useWater();
      return {
        orderShot: orderShot,
      };
    }

    useCapsule(shot: number) {
      console.log("캡슐쓰러왔습니다.");
      this.capsule -= shot * coffeeMachine.ONE_SHOT_CAPSULE;
      console.log("캡슐 쓰고 난 후" + this.capsule);
    }

    useWater() {
      console.log("물쓰러왔습니다.");
      this.water -= coffeeMachine.ONE_CUP_WATER;
      console.log("물쓰고 난 후 " + this.water);
    }

    addCapsule(capsule: number) {
      this.capsule += capsule;
    }

    fulfillWater(water: number) {
      this.water += water;
    }

    get showWaterState() {
      return this.water;
    }

    showCapsuleState(): number {
      return this.capsule;
    }
    
  }

  console.log("/* 첫번째 주문 */");
  let normalMachine = new coffeeMachine(10, 10);
  console.log(normalMachine.showWaterState); //10
  console.log(normalMachine.showCapsuleState()); //10
  let order1 = normalMachine.makeCoffee(2);
  console.log(order1); //{ orderShot: 2 }
  console.log("물 상태" + normalMachine.showWaterState); //0
  console.log("캡슐 상태" + normalMachine.showCapsuleState()); //4

  console.log("/* 두번째 주문 */");
  normalMachine.fulfillWater(100);
  let order2 = normalMachine.makeCoffee(1);
  console.log(order2); //{ orderShot: 1 }
  console.log("물 상태" + normalMachine.showWaterState); //90
  console.log("캡슐 상태" + normalMachine.showCapsuleState()); //1

  console.log("/* 세번째 주문 */");
  let order3 = normalMachine.makeCoffee(3);
  console.log(order3); //Error: 커피캡슐 부족!!! 현재 보유 캡슐 : 1

 

 

출력결과는 똑같은데 커피캡슐과 물 상태를 확인하는 애매한 함수를 getter로 손봄

  //커피머신 만들기
  type CoffeeCup = {
    orderShot: number;
  };

  class coffeeMachine {
    private static ONE_SHOT_CAPSULE: number = 3;
    private static ONE_CUP_WATER: number = 10;

    static makeMachine(beans: number, water: number): coffeeMachine {
      return new coffeeMachine(beans, water);
    }

    constructor(private capsule: number = 3, private water: number = 10) {}

    makeCoffee(orderShot: number): CoffeeCup {
      if (this.capsule < orderShot * coffeeMachine.ONE_SHOT_CAPSULE) {
        throw new Error(`커피캡슐 부족!!! 현재 보유 캡슐 : ${this.capsule}`);
      }
      if (this.water < coffeeMachine.ONE_CUP_WATER) {
        throw new Error(`물 부족!!! 남은 물 ${this.water}`);
      }
      console.log("캡슐쓰기 전 " + this.capsule);
      this.useCapsule(orderShot);
      console.log("물쓰기 전 " + this.water);
      this.useWater();
      return {
        orderShot: orderShot,
      };
    }

    useCapsule(shot: number) {
      console.log("캡슐쓰러왔습니다.");
      this.capsule -= shot * coffeeMachine.ONE_SHOT_CAPSULE;
      console.log("캡슐 쓰고 난 후" + this.capsule);
    }

    useWater() {
      console.log("물쓰러왔습니다.");
      this.water -= coffeeMachine.ONE_CUP_WATER;
      console.log("물쓰고 난 후 " + this.water);
    }

    addCapsule(capsule: number) {
      this.capsule += capsule;
    }

    fulfillWater(water: number) {
      this.water += water;
    }

    get Water(): number {
      return this.water;
    }

    get Capsule(): number {
      return this.capsule;
    }
  }

  console.log("/* 첫번째 주문 */");
  let normalMachine = new coffeeMachine(10, 10);
  console.log(normalMachine.Water); //10
  console.log(normalMachine.Capsule); //10
  let order1 = normalMachine.makeCoffee(2);
  console.log(order1); //{ orderShot: 2 }
  console.log("물 상태" + normalMachine.Water); //0
  console.log("캡슐 상태" + normalMachine.Capsule); //4

  console.log("/* 두번째 주문 */");
  normalMachine.fulfillWater(100);
  let order2 = normalMachine.makeCoffee(1);
  console.log(order2); //{ orderShot: 1 }
  console.log("물 상태" + normalMachine.Water); //90
  console.log("캡슐 상태" + normalMachine.Capsule); //1

  console.log("/* 세번째 주문 */");
  let order3 = normalMachine.makeCoffee(3);
  console.log(order3); //Error: 커피캡슐 부족!!! 현재 보유 캡슐 : 1
728x90