Programming Language/C#

[C#] TQC+ 문제 CS_610:

공구일 2025. 6. 15. 16:56
728x90

🔍C#: CS_610.

Part 클래스를 상속받는 세 개의 클래스를 구현한 뒤 has-a 관계로 Notebook 클래스를 구현하시오. 

 

1. 문제 정답

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Threading;
namespace CSD06
{
    class CSD06
    {
        static void Main(string[] args)
        {

            try
            {
                string[] data = Console.ReadLine().Split(' ');

                for (int i = 0; i < data.Length; i++)
                    if (Convert.ToDouble(data[i].ToString()) < 0) { throw new Exception(); }

                double cost = 0;
                double total = 0;
                Notebook n;
                if(data.Length == 2){ //기본사양
                    n = new Notebook();
                } else {
                    n = new Notebook(Convert.ToInt32(data[2]),Convert.ToDouble(data[3]),Convert.ToInt32(data[4]));
                }
                int num = Convert.ToInt32(data[0]);
                cost = n.getCost()*num;
                total = n.getPrice(Convert.ToDouble(data[1]))*num;
                Console.Write("NB:{0},Cost:{1},Total:{2}", data[0].ToString(), cost, total);
            }
            catch
            {
                Console.Write("error");
            }
            Console.ReadKey();
        }


 
    }
    abstract class Part
    {
        public int cost = 0;
        public int getCost()
        {
            return cost;
        }
    }
    class LCD : Part {
        private int size = 0;
        public LCD(int size){
            this.size = size;
            switch(size){
                case 10:
                    cost = 1500;
                    break;
                case 15:
                    cost = 2500;
                    break;
                case 17:
                    cost = 3500;
                    break;
            }
        }
    }

    class CPU : Part {
        private double speed = 0;
        public CPU(double speed){
            this.speed = speed;
            switch(speed){
                case 1.66:
                    cost = 7000;
                    break;
                case 2.2:
                    cost = 8500;
                    break;
                case 2.4:
                    cost = 13000;
                    break;
            }
        }
    }

    class HD : Part {
        private int vol = 0;
        public HD(int vol){
            this.vol = vol;
            switch(vol){
                case 256:
                    cost = 1800;
                    break;
                case 512:
                    cost = 2500;
                    break;
            }
        }
    }

    public class Notebook{
        LCD lcd;
        CPU cpu;
        HD hd;
        private double profit = 0;
        public int getCost(){
            return lcd.getCost()+cpu.getCost()+hd.getCost();
        }
        public double getPrice(double profit){
            return this.getCost()*profit;
        }

        public Notebook(int size = 10, double speed = 1.66,int vol = 256){
            lcd = new LCD(size);
            cpu = new CPU(speed);
            hd = new HD(vol);
        }
    }
}

 

 

2. 정리

- Part 함수내 cost가 private이라는 조건 하에 getCost()로 cost 값에 접근했지만 현재 코드에서는 cost로 접근해도 무관합니다. 기본 값의 경우 자바에서는 오버로딩으로 처리하지만 C/C++과 유사하게 기본값을 파라미터에 대입시킬 수 있습니다. 이 때 중요한 점은 매개변수를 중간부터 주기로 했으면 끝 파라미터의 기본값까지는 나타내줘야합니다. 예를 들어 size가 기본값이 없고 speed와 vol만 기본값이 있는 것은 되지만 size가 기본값이 있을 때는 모두 둘다 오른쪽에 있으므로 speed와 vol은 기본값을 가져야합니다.( 위치를 바꿔주면 상관없습니다. )

728x90