基于c#的atm按照类的设计图编写代码,有基类、继承类,体现多态的思想,至少包含的类类ATM、Account(账户)、Screen(屏幕)、Keypad(键盘)、CashDispenser(取钱口)、DepositSlot(存钱槽)、BankDatabase(银行数据库) 、 Deposit (存款)、Withdrawal(取款) 、BalanceInquiry(查询)、交易(Transaction)
时间: 2023-07-10 20:32:18 浏览: 150
好的,以下是基于c#的atm按照类的设计图编写代码,包含基类、继承类,体现多态的思想,至少包含的类类ATM、Account(账户)、Screen(屏幕)、Keypad(键盘)、CashDispenser(取钱口)、DepositSlot(存钱槽)、BankDatabase(银行数据库) 、 Deposit (存款)、Withdrawal(取款) 、BalanceInquiry(查询)、交易(Transaction)的示例代码:
```csharp
// 基类Transaction
public abstract class Transaction
{
protected BankDatabase bankDatabase;
public Transaction(BankDatabase theBankDatabase)
{
bankDatabase = theBankDatabase;
}
public abstract void Execute();
}
// 存款类 Deposit
public class Deposit : Transaction
{
private int accountNumber;
private decimal amount;
private Keypad keypad;
private DepositSlot depositSlot;
private const int CANCELED = 0;
public Deposit(int userAccountNumber, Keypad atmKeypad, DepositSlot atmDepositSlot, BankDatabase theBankDatabase)
: base(theBankDatabase)
{
accountNumber = userAccountNumber;
keypad = atmKeypad;
depositSlot = atmDepositSlot;
}
public override void Execute()
{
amount = PromptForDepositAmount();
if (amount != CANCELED)
{
bankDatabase.Credit(accountNumber, amount);
}
}
private decimal PromptForDepositAmount()
{
Screen.DisplayMessage("\nPlease enter a deposit amount in CENTS (or 0 to cancel): ");
int input = keypad.GetInput();
if (input == CANCELED)
{
return CANCELED;
}
else
{
return input / 100.0M;
}
}
}
// 取款类 Withdrawal
public class Withdrawal : Transaction
{
private int accountNumber;
private decimal amount;
private Keypad keypad;
private CashDispenser cashDispenser;
private const int CANCELED = 6;
public Withdrawal(int userAccountNumber, Keypad atmKeypad, CashDispenser atmCashDispenser, BankDatabase theBankDatabase)
: base(theBankDatabase)
{
accountNumber = userAccountNumber;
keypad = atmKeypad;
cashDispenser = atmCashDispenser;
}
public override void Execute()
{
bool cashDispensed = false;
decimal availableBalance;
do
{
amount = PromptForWithdrawalAmount();
availableBalance = bankDatabase.GetAvailableBalance(accountNumber);
if (amount <= availableBalance)
{
if (cashDispenser.IsSufficientCashAvailable(amount))
{
bankDatabase.Debit(accountNumber, amount);
cashDispenser.DispenseCash(amount);
cashDispensed = true;
Screen.DisplayMessageLine("\nYour cash has been dispensed. Please take your cash now.");
}
else
{
Screen.DisplayMessageLine("\nInsufficient cash available in the ATM." +
"\n\nPlease choose a smaller amount.");
}
}
else
{
Screen.DisplayMessageLine("\nInsufficient funds in your account." +
"\n\nPlease choose a smaller amount.");
}
} while (!cashDispensed);
}
private decimal PromptForWithdrawalAmount()
{
Screen.DisplayMessage("\nPlease enter a withdrawal amount in CENTS (or 0 to cancel): ");
int input = keypad.GetInput();
if (input == CANCELED)
{
return CANCELED;
}
else
{
return input / 100.0M;
}
}
}
// 查询类 BalanceInquiry
public class BalanceInquiry : Transaction
{
private int accountNumber;
private Screen screen;
public BalanceInquiry(int userAccountNumber, Screen atmScreen, BankDatabase theBankDatabase)
: base(theBankDatabase)
{
accountNumber = userAccountNumber;
screen = atmScreen;
}
public override void Execute()
{
decimal availableBalance = bankDatabase.GetAvailableBalance(accountNumber);
decimal totalBalance = bankDatabase.GetTotalBalance(accountNumber);
screen.DisplayMessageLine("\nBalance Information:");
screen.DisplayMessage(" - Available balance: ");
screen.DisplayDollarAmount(availableBalance);
screen.DisplayMessage("\n - Total balance: ");
screen.DisplayDollarAmount(totalBalance);
screen.DisplayMessageLine("");
}
}
// 账户类 Account
public class Account
{
private int accountNumber;
private int pin;
private decimal availableBalance;
private decimal totalBalance;
public Account(int theAccountNumber, int thePIN, decimal theAvailableBalance, decimal theTotalBalance)
{
accountNumber = theAccountNumber;
pin = thePIN;
availableBalance = theAvailableBalance;
totalBalance = theTotalBalance;
}
public bool ValidatePIN(int userPIN)
{
return (userPIN == pin);
}
public decimal GetAvailableBalance()
{
return availableBalance;
}
public decimal GetTotalBalance()
{
return totalBalance;
}
public void Credit(decimal amount)
{
totalBalance += amount;
}
public void Debit(decimal amount)
{
availableBalance -= amount;
totalBalance -= amount;
}
public int GetAccountNumber()
{
return accountNumber;
}
}
// 银行数据库类 BankDatabase
public class BankDatabase
{
private Account[] accounts;
public BankDatabase()
{
accounts = new Account[2];
accounts[0] = new Account(12345, 54321, 1000.0M, 1200.0M);
accounts[1] = new Account(98765, 56789, 200.0M, 200.0M);
}
public bool AuthenticateUser(int userAccountNumber, int userPIN)
{
Account userAccount = GetAccount(userAccountNumber);
if (userAccount != null)
{
return userAccount.ValidatePIN(userPIN);
}
else
{
return false;
}
}
public decimal GetAvailableBalance(int userAccountNumber)
{
Account userAccount = GetAccount(userAccountNumber);
return userAccount.GetAvailableBalance();
}
public decimal GetTotalBalance(int userAccountNumber)
{
Account userAccount = GetAccount(userAccountNumber);
return userAccount.GetTotalBalance();
}
public void Credit(int userAccountNumber, decimal amount)
{
Account userAccount = GetAccount(userAccountNumber);
userAccount.Credit(amount);
}
public void Debit(int userAccountNumber, decimal amount)
{
Account userAccount = GetAccount(userAccountNumber);
userAccount.Debit(amount);
}
private Account GetAccount(int accountNumber)
{
for (int i = 0; i < accounts.Length; i++)
{
if (accounts[i].GetAccountNumber() == accountNumber)
{
return accounts[i];
}
}
return null;
}
}
// ATM类
public class ATM
{
private bool userAuthenticated;
private int currentAccountNumber;
private Screen screen;
private Keypad keypad;
private CashDispenser cashDispenser;
private DepositSlot depositSlot;
private BankDatabase bankDatabase;
private const int BALANCE_INQUIRY = 1;
private const int WITHDRAWAL = 2;
private const int DEPOSIT = 3;
private const int EXIT = 4;
public ATM()
{
userAuthenticated = false;
currentAccountNumber = 0;
screen = new Screen();
keypad = new Keypad();
cashDispenser = new CashDispenser();
depositSlot = new DepositSlot();
bankDatabase = new BankDatabase();
}
public void Run()
{
while (true)
{
while (!userAuthenticated)
{
screen.DisplayMessageLine("\nWelcome!");
AuthenticateUser();
}
PerformTransactions();
userAuthenticated = false;
currentAccountNumber = 0;
screen.DisplayMessageLine("\nThank you! Goodbye!");
}
}
private void AuthenticateUser()
{
screen.DisplayMessage("\nPlease enter your account number: ");
int accountNumber = keypad.GetInput();
screen.DisplayMessage("\nEnter your PIN: ");
int pin = keypad.GetInput();
userAuthenticated = bankDatabase.AuthenticateUser(accountNumber, pin);
if (userAuthenticated)
{
currentAccountNumber = accountNumber;
}
else
{
screen.DisplayMessageLine("\nInvalid account number or PIN. Please try again.");
}
}
private void PerformTransactions()
{
bool userExited = false;
while (!userExited)
{
int userChoice = DisplayMainMenu();
switch (userChoice)
{
case BALANCE_INQUIRY:
BalanceInquiry balanceInquiry = new BalanceInquiry(currentAccountNumber, screen, bankDatabase);
balanceInquiry.Execute();
break;
case WITHDRAWAL:
Withdrawal withdrawal = new Withdrawal(currentAccountNumber, keypad, cashDispenser, bankDatabase);
withdrawal.Execute();
break;
case DEPOSIT:
Deposit deposit = new Deposit(currentAccountNumber, keypad, depositSlot, bankDatabase);
deposit.Execute();
break;
case EXIT:
screen.DisplayMessageLine("\nExiting the system...");
userExited = true;
break;
default:
screen.DisplayMessageLine("\nYou did not enter a valid selection. Try again.");
break;
}
}
}
private int DisplayMainMenu()
{
screen.DisplayMessageLine("\nMain Menu:");
screen.DisplayMessageLine("1 - View my balance");
screen.DisplayMessageLine("2 - Withdraw cash");
screen.DisplayMessageLine("3 - Deposit funds");
screen.DisplayMessageLine("4 - Exit");
screen.DisplayMessage("\nEnter a choice: ");
return keypad.GetInput();
}
}
// 屏幕类 Screen
public class Screen
{
public void DisplayMessage(string message)
{
Console.Write(message);
}
public void DisplayMessageLine(string message)
{
Console.WriteLine(message);
}
public void DisplayDollarAmount(decimal amount)
{
Console.Write("${0:F2}", amount);
}
}
// 键盘类 Keypad
public class Keypad
{
public int GetInput()
{
return Convert.ToInt32(Console.ReadLine());
}
}
// 取钱口类 CashDispenser
public class CashDispenser
{
private const int INITIAL_COUNT = 500;
private int count;
public CashDispenser()
{
count = INITIAL_COUNT;
}
public void DispenseCash(decimal amount)
{
int billsRequired = ((int)amount) / 20;
count -= billsRequired;
}
public bool IsSufficientCashAvailable(decimal amount)
{
int billsRequired = ((int)amount) / 20;
if (count >= billsRequired)
{
return true;
}
else
{
return false;
}
}
}
// 存钱槽类 DepositSlot
public class DepositSlot
{
public bool IsEnvelopeReceived()
{
return true;
}
}
```
以上是基于c#的atm按照类的设计图编写代码,体现多态的思想,至少包含的类类ATM、Account(账户)、Screen(屏幕)、Keypad(键盘)、CashDispenser(取钱口)、DepositSlot(存钱槽)、BankDatabase(银行数据库) 、 Deposit (存款)、Withdrawal(取款) 、BalanceInquiry(查询)、交易(Transaction)的示例代码。
阅读全文