게임 알고리즘

2일차 과일 관리(Normal)

park-gom 2024. 8. 13. 14:18

namespace Study00
{
    internal class Program
    {
        static void Main(string[] args)
        {
            new App();
        }
    }
}
using System;

namespace Study00
{
    public class App
    {

        //생성자
        public App() 
        {
            Store store = new Store();

            store.AddFruit(new Fruit(Fruit.FruitType.apple, 1500));
            store.AddFruit(new Fruit(Fruit.FruitType.apple, 1500));
            store.AddFruit(new Fruit(Fruit.FruitType.orange, 3000));
            store.AddFruit(new Fruit(Fruit.FruitType.banana, 5000));
            store.AddFruit(new Fruit(Fruit.FruitType.banana, 5000));
            store.AddFruit(new Fruit(Fruit.FruitType.banana, 5000));

            store.SumPrice();

            int cnt = store.FindFruit(Fruit.FruitType.banana);
            Console.WriteLine($"바나나의 갯수 : {cnt}");

            store.ComparePrice(2000);
        }
    }
}
using System;
using static Study00.Fruit;

namespace Study00
{
    public class Store
    {
        Fruit[] cage;
        private int idx;
        

        //생성자
        public Store() 
        {
            Console.WriteLine("가게가 생성되었습니다.");
            cage = new Fruit[10];
        }

        //과일을 추가하는 메서드
        public void AddFruit(Fruit fruit)
        {
            if (idx < 10)
            {
                cage[idx++] = fruit;
                Console.WriteLine("과일을 저장했습니다.");
            }
            else
            {
                Console.WriteLine("공간이 부족합니다.");
            }
        }

        //모든 과일의 가격을 합산하는 메서드
        public void SumPrice()
        {
            int total = 0;
            for (int i = 0; i < cage.Length; i++)
            {
                Fruit fruit = cage[i];
                if (fruit != null)
                {
                    total += fruit.price;
                }
            }
            Console.WriteLine($"금액 합산 : {total}");
        }

        //타입으로 과일의 갯수를 반환하는 메서드
        public int FindFruit(Fruit.FruitType fruitType)
        {
            int cnt = 0;
            for (int i = 0; i < idx; i++)
            {
                Fruit fruit = cage[i];
                if (fruit.fruitType == fruitType)
                {
                    cnt++;
                }
            }
            return cnt;
        }

        //매개변수로 받은 가격보다 비싼 과일을 찾는 메서드
        public void ComparePrice(int price)
        {
            int[] arr = new int[3];
            for (int i = 0; i < idx; i++)
            {
                Fruit fruit = cage[i];
                if (fruit.price > price)
                {
                    if(fruit.fruitType == FruitType.apple)
                    {
                        arr[0]++;
                    }
                    else if(fruit.fruitType == FruitType.orange)
                    {
                        arr[1]++;
                    }
                    else
                    {
                        arr[2]++;
                    }
                }
            }
            Console.WriteLine($"{price}원보다 비싼 과일 목록");
            if (arr[0] != 0)
            {
                Console.WriteLine($"apple x{arr[0]}");
            }
            if (arr[1] != 0)
            {
                Console.WriteLine($"orange x{arr[1]}");
            }
            if (arr[2] != 0)
            {
                Console.WriteLine($"banana x{arr[2]}");
            }
        }
    }
}
using System;

namespace Study00
{
    public class Fruit
    {
        public enum FruitType
        {
            apple,
            orange,
            banana
        }

        public FruitType fruitType;
        public int price;

        //생성자
        public Fruit(FruitType fruitType, int price)
        {
            this.fruitType = fruitType;
            this.price = price;
            Console.WriteLine($"과일({fruitType})이 생성됬습니다.");
        }
    }
}

'게임 알고리즘' 카테고리의 다른 글

2일차 과제 (Siege Tank)  (0) 2024.08.13
2일차 과일 관리(Hard)  (0) 2024.08.13
2일차 과일 관리(Easy)  (0) 2024.08.13
1일차 과제2 (player vs monster)  (0) 2024.08.12
1일차 과제1 (List로 인벤토리 만들기)  (0) 2024.08.12