블로그 이미지
Kanais
Researcher & Developer 퍼즐을 완성하려면 퍼즐 조각들을 하나 둘씩 맞춰나가야 한다. 인생의 퍼즐 조각들을 하나 둘씩 맞춰나가다 보면 인생이란 퍼즐도 완성되는 날이 오려나...?

calendar

1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31

Notice

05-02 02:59

Recent Post

Recent Comment

Recent Trackback

Archive

2015. 4. 16. 11:46 Programming/C/C++

원문 : http://kin.naver.com/qna/detail.nhn?d1id=1&dirId=1040102&docId=129635396&page=1#answer1
네이버 지식인 11.04.29 답변 





일단 결과를 보시면 제대로 나온 걸 확인할 수 있습니다. 

소스를 보자면..

일단 GetPrice메소드와 GetGram 메소드를 정의부분을 수정해 주셔야 하고,

출력 메소드에서도 수정이 필요합니다.

 class Program
    {
        abstract class Product
        {
            protected string product;
            public string GetProduct()
            {
                return product;
            }
            public abstract string GetGram();
            public abstract int GetPrice();
        }
        class Meat : Product
        {
            int gram, price;
            public Meat(string product, int gram, int price)
            {
                this.gram = gram; this.price = price; this.product = product;
            }
            public override string GetGram()
            {
// 일단은 간단하게 string 지역 변수에 값을 넣어주고 뒤에 " " 이걸로 붙여주는 식으로 썻지만  출력을  string.format("{0}g",gram.ToString()); 이런식으로 써주시면됩니다.
                string re = gram.ToString();
                return re+"g";
            }
            public override int GetPrice()
            {
// price 는 매개변수로 받은 gram과 price를 곱해주셔야 제대로된 가격이 출력됩니다.
               return (gram/100) * price;
            }
        }
        class Chicken : Product
        {
            string productsize = string.Empty;
            public Chicken(string size)
            {
// 입력받은 매개변수는 닭의 사이즈를 받으므로.. product 이름에는 "삶계닭" 이라고 직접 넣어주고요.. 지역 변수를 하나 선언한 후 매개변수로 입력받은 값을 넣어 주시면 됩니다.
               this.product = "삶계닭";
                productsize = size;
            }
            public override string GetGram()
            {
                return productsize;
            }
            public override int GetPrice()
            {
// 그리고 입력 받은 사이즈에 따라 price가 다르므로 price 설정을 해줘야합니다.
               if (string.Equals(productsize, "대"))
                {
                    return 10000;
                }
                else if (string.Equals(productsize, "중"))
                {
                    return 7000;
                }
                else
                {
                    return 3000;
                }
            }
        }
        class ShoppingCart
        {
            Product[] showProductList;
            int index;
            public ShoppingCart()
            {
                showProductList = new Product[100];
                index = 0;
            }
            public void AddProduct(Product showProduct)
            {
                showProductList[index++] = showProduct;
            }
            public void ShowProductList()
            {
                for (int i = 0; i < index; i++)
                {
                    Console.WriteLine("{0}<{1}> \t {2}원",
                    showProductList[i].GetProduct(), showProductList[i].GetGram(),
                    showProductList[i].GetPrice());
// GetType() 메소드 대신에 두번째는 GetGram()으로 세번째는 GetPrice()메소드를 써주면 됩니다.
                }
            }
        }
        class ShoppingMall
        {
            static void Main(string[] args)
            {
                ShoppingCart shoppingCart = new ShoppingCart();
                /* new Meat(부위, 그램수, 그램당 가격);*/
                shoppingCart.AddProduct(new Meat("삼겹살", 500, 1500));
                shoppingCart.AddProduct(new Meat("목살", 200, 1000));
                /* 삼계닭 대:10000원, 중: 7000원, 소: 3000원);*/
                shoppingCart.AddProduct(new Chicken("대"));
                shoppingCart.AddProduct(new Chicken("소"));
                shoppingCart.ShowProductList();
            }
        }
    }

Product 클래스가 추상 클래스 이므로 Product를 상속받는 클래스에서는 정의부분만 다르게 재정의 해주면 되기때문에, 

ShowProductList() 메소드에서 가격을 계산해서 출력하지 않아도 됩니다.


posted by Kanais