게임 알고리즘

2일차 과일 관리(Easy)

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

namespace Study00
{
    internal class Program
    {
        static void Main(string[] args)
        {
            new App();
        }
    }
}
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();
        }
    }
}
using System;

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}");
        }
    }
}
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})이 생성됬습니다.");
        }
    }
}