본문 바로가기
프로그래밍 _공부자료./C#

C# ATM 기능 만들기

by 대구부자 2024. 6. 28.
반응형
public class BankAccount
{
    public string AccountNumber { get; set; }
    public string OwnerName { get; set; }
    public string Password { get; set; }
    public decimal Balance { get; set; }
}

public class ATM
{
    private List<BankAccount> accounts;

    public ATM()
    {
        accounts = new List<BankAccount>();
    }

    public void CreateAccount(string accountNumber, string ownerName, string password)
    {
        BankAccount newAccount = new BankAccount
        {
            AccountNumber = accountNumber,
            OwnerName = ownerName,
            Password = password,
            Balance = 0
        };
        accounts.Add(newAccount);
        Console.WriteLine($"새 계좌가 생성되었습니다: {accountNumber}");
    }

    public void Deposit(string accountNumber, string password, decimal amount)
    {
        BankAccount account = FindAccount(accountNumber, password);
        if (account != null)
        {
            account.Balance += amount;
            Console.WriteLine($"입금 완료. 현재 잔액: {account.Balance}원");
        }
        else
        {
            Console.WriteLine("계좌 정보가 일치하지 않습니다.");
        }
    }

    public void Withdraw(string accountNumber, string password, decimal amount)
    {
        BankAccount account = FindAccount(accountNumber, password);
        if (account != null)
        {
            if (account.Balance >= amount)
            {
                account.Balance -= amount;
                Console.WriteLine($"출금 완료. 현재 잔액: {account.Balance}원");
            }
            else
            {
                Console.WriteLine("잔액이 부족합니다.");
            }
        }
        else
        {
            Console.WriteLine("계좌 정보가 일치하지 않습니다.");
        }
    }

    public void CheckBalance(string accountNumber, string password)
    {
        BankAccount account = FindAccount(accountNumber, password);
        if (account != null)
        {
            Console.WriteLine($"현재 잔액: {account.Balance}원");
        }
        else
        {
            Console.WriteLine("계좌 정보가 일치하지 않습니다.");
        }
    }

    public void Transfer(string senderAccountNumber, string senderPassword, string receiverAccountNumber, decimal amount)
    {
        BankAccount senderAccount = FindAccount(senderAccountNumber, senderPassword);
        BankAccount receiverAccount = FindAccount(receiverAccountNumber, "");
        if (senderAccount != null && receiverAccount != null)
        {
            if (senderAccount.Balance >= amount)
            {
                senderAccount.Balance -= amount;
                receiverAccount.Balance += amount;
                Console.WriteLine($"{amount}원이 이체되었습니다.");
            }
            else
            {
                Console.WriteLine("잔액이 부족합니다.");
            }
        }
        else
        {
            Console.WriteLine("계좌 정보가 일치하지 않습니다.");
        }
    }

    private BankAccount FindAccount(string accountNumber, string password)
    {
        return accounts.FirstOrDefault(a => a.AccountNumber == accountNumber && a.Password == password);
    }
}
반응형

'프로그래밍 _공부자료. > C#' 카테고리의 다른 글

C# .xml 파일 실행파일 경로 가져오는법  (0) 2024.07.31
C# wpf 오류 알림 모듈화  (0) 2024.07.23
C# 로그 클래스  (0) 2024.07.23

댓글