250x250
Rainbow🌈Coder
My dev Note📒
Rainbow🌈Coder
전체 방문자
오늘
어제
  • 분류 전체보기 (411)
    • 공지사항 (0)
    • Debugger (10)
      • Visual Studio Debugger (1)
      • Chrome DevTools (3)
      • Visual Studio Code Debugger (4)
      • eclipse (1)
      • intelliJ (1)
    • OOP (2)
      • OOP (2)
    • TypeScript (54)
      • 타입스크립트 TypeScript (54)
    • Javascript (87)
      • Javascript (45)
      • Node.js (19)
      • React (5)
      • FE 개발환경설정 (3)
      • React와 Node 같이 때려잡기 (6)
      • next.js (2)
      • pixi.js (7)
    • 마크업 (23)
      • Html & Css (23)
    • C# (80)
      • C# (12)
      • 이것이 C#이다 (68)
    • C++ (30)
      • c++ (27)
      • win api (3)
    • Unity (18)
      • Unity(기초) (8)
      • Unity(C#중급) (5)
      • 유니티 포톤(네트워크) (4)
      • unity c# MyCode (1)
    • Java & Spring (29)
      • Java (11)
      • 스프링 (8)
      • Java Algorithm (9)
      • Javs Data Structures (1)
    • 자료구조와 알고리즘 (15)
      • 자료구조 (5)
      • 알고리즘 (10)
    • 형상관리 (15)
      • Git (11)
      • 소스트리 (3)
    • 그래픽스 (7)
      • WebGl (7)
    • AWS (3)
      • aws (3)
    • 리눅스 (5)
      • 리눅스 (5)
    • 책 리뷰 (13)
      • 클린코드(책리뷰) (3)
      • 유지보수가능한코딩의기술C#편(책리뷰) (1)
      • 리팩토링(자바스크립트판) (9)
    • Server (2)
      • 게임 서버(네트워크, 멀티쓰레드,OS) (2)
    • 설계, 아키텍쳐 (4)
    • 파이썬 (5)
    • 디자인패턴 (2)
    • mocha (2)
    • Jest (1)
    • Spine (1)
    • 인공지능 (1)
      • 혼자공부하는머신러닝+딥러닝 (1)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • MySQL
  • ㅣㄷ
  • 위임
  • 컴포지션

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
Rainbow🌈Coder

My dev Note📒

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

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

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

'TypeScript > 타입스크립트 TypeScript' 카테고리의 다른 글

[타입스크립트] 호출 시그니처  (0) 2022.04.20
[타입스크립트] 제너레이터, 반복자(iterator) 추론과 명시  (0) 2022.04.20
[타입스크립트] 제너레이터 지원, 명시  (0) 2022.04.19
[타입스크립트] this 관련 오류  (0) 2022.04.19
[타입스크립트] class의 getter와 setter  (0) 2022.04.18
    'TypeScript/타입스크립트 TypeScript' 카테고리의 다른 글
    • [타입스크립트] 호출 시그니처
    • [타입스크립트] 제너레이터, 반복자(iterator) 추론과 명시
    • [타입스크립트] 제너레이터 지원, 명시
    • [타입스크립트] this 관련 오류
    Rainbow🌈Coder
    Rainbow🌈Coder
    몰라도 결국은 아는 개발자, 그런 사람이 되기 위한 매일의 한걸음

    티스토리툴바