본문 바로가기

TIL

Day41(DeliveryTycoon 완료 후 회고)

반복문을 이용한 버튼 생성

Before

private void buttonSets() {
        JButton hamburgerButton = new JButton("햄버거");
        hamburgerButton.addActionListener(event -> {
            buttonClickList.add(foodList.get(0));
            compare();
        });
        hamburgerButton.setBounds(240, 0, 80, 20);
        buttonDisplayPanel.add(hamburgerButton);

        JButton pizzaButton = new JButton("피자");
        pizzaButton.addActionListener(event -> {
            buttonClickList.add(foodList.get(1));
            compare();
        });
        pizzaButton.setBounds(240, 30, 80, 20);
        buttonDisplayPanel.add(pizzaButton);
    }
    
    


After
	for (int i = 0; i < DeliveryGame.FOODS.length; i += 1) {
      String name = DeliveryGame.FOODS[i];
      int price = DeliveryGame.FOOD_PRICES[i];
    
    Food food = new Food(name, price);
      JButton button = new JButton(food.name());
      button.setAlignmentX(Component.CENTER_ALIGNMENT);
      button.setBackground(new Color(255, 255, 255, 255));

      button.addActionListener(event -> {
        boolean isSuccess = deliveryGame.cookFood(food);

        processGame(isSuccess);
      });

      dialogPanel.add(button);
  }

 

food name에 대해서 각 각 버튼을 만드는 것이 아닌 for문을 이용해 Food클래스로부터 이름을 받아오는 생각을 하지 못했다. 후자의 방법대로 한다면 코드의 변경 사항에 대해서도 관리하기 편하고 보기에도 편하다. 즉 이전의 코딩에 대해서 리팩토링 과정을 거쳤어야된다고 생각한다.

중복되지 않게 음식이름 나열과 핵심로직과 UI 분리

 temporaryStorage = new ArrayList<>(foodList);

        orderListPriceSum = 0;

        for (int i = 0; i < orderNumber ; i += 1) {

            int index = (orderNumber - i) - 1;

            orderList.add(temporaryStorage.get(index));

            orderListPriceSum += temporaryStorage.get(index).getPrice();

            orderDisplayPanel.add(new JLabel((i + 1) + ". " + temporaryStorage.get(index).getName()));

            temporaryStorage.remove(index);
        }

 

음식 이름을 중복되지 않게 뽑는 코드인데 아직 랜덤에 대해 익숙하지않아서인지 코드를 작성하는데 있어서 어려움을 겪었다. 또한 음식을 뽑는 메서드에 레이아웃에 관련된 코드를 작성하면 안됐었는데 작성했다. 핵심로직과 UI의 분리가 되지 않았다.

 


딜리버리타이쿤을 하면서 생각한 점

2번째 하는 것이기에 핵심로직과 UI를 분리하고 관심사의 분리가 될 수 있게 리팩토링을 전 보다 더 나은 방향으로 나아갈 줄 알았다.

그러나 코드를 작성하고 완성해본 결과 첫 번째 한것과 변수명, 리팩토링 등 거의 모든 것이 비슷했다. 나름 열심히 노력했지만 결과로 봤을 때는 똑같았다. 또한 트레이너님의 코드를 보고 내 코드와 비교했을 때 나의 코드에는 중복이 정말 많고 리팩토링이 하나도 안되있구나를 많이 깨달았다. 
계속해서 UI와 핵심로직을 분리하는데 신경을 써야할꺼 같고 분리를 하지 못하겠다고 느끼면 트레이너분들이나 매니저님께 질문하여 완벽한 분리를 할 수 있는 실력으로 나아가야겠다.

'TIL' 카테고리의 다른 글

Day43(의존성)  (0) 2023.03.19
Day42(습관)  (0) 2023.03.19
Day40(특정 commit부르기)  (0) 2023.03.16
try catch 쓰는 이유  (0) 2023.03.14
Day39(정수제곱근판별,Math.pow,Math.sqrt)  (0) 2023.03.13