091

[C#] TQC+ 문제 CS_606: 본문

Programming Language/C#

[C#] TQC+ 문제 CS_606:

공구일 2025. 6. 15. 15:25
728x90

🔍C#: CS_606.

계좌 클래스에 요구되는 메서드를 추가하여 다양한 기능을 구현할 수 있게 만드시오.

 

1. 문제 정답

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace CSD06
{
    class CSD06
    {
        static void Main(string[] args)
        {
             try
            {
                int rate = Convert.ToInt32(Console.ReadLine());
                if (rate < 0 || rate > 5){ throw new Exception();}
                using(StreamReader reader = new StreamReader("read.txt")){
                    Account account = new Account("Jack",rate);
                    string line;
                    while((line  = reader.ReadLine()) != null){
                        string[] part = line.Split(',');
                        if(part[2] == "d") account.deposit(Convert.ToDouble(part[1]));
                        else account.withdraw(Convert.ToDouble(part[1]));
                    }
                    Console.Write(account.getFutureValue());
                }
            }
            catch
            {
                Console.Write("error");
            }
            Console.ReadKey();
        }

    }

    class Account
    {
        String name="";
        int rate=0;
        double balance=0;

        public Account(String name, int rate)
        {
            this.name = name;
            this.rate = rate;
        }
        
        public void setRate(int rate){
            this.rate = rate;
        }
        public void deposit(double input){
            balance += input;
        }
        public void withdraw(double output){
            balance -= output;
        }
        public double getBalance(){
            return balance;
        }
        public double getFutureValue(){
            return balance*(100+rate)/100;
        }
    }
}

 

2. 정리

- 캡슐화를 위해 직접 변수에 접근하는 것이 아닌 메서드를 통해 접근할 수 있게 만들어줍니다.

728x90

'Programming Language > C#' 카테고리의 다른 글

[C#] TQC+ 문제 CS_608:  (1) 2025.06.15
[C#] TQC+ 문제 CS_607:  (0) 2025.06.15
[C#] TQC+ 문제 CS_605:  (0) 2025.06.15
[C#] TQC+ 문제 CS_604:  (0) 2025.06.15
[C#] TQC+ 문제 CS_603:  (0) 2025.05.27