ATM(java源码)
Account
package ATM;
public class Account {
private int accountNumber;//银行账号
private int pin;//个人识别码 也就是银行密码
private double availableBalance;//可用余额
private double totalBalance;//用户总余额
public Account(int theAccountNumber, int thePin, double theAvailableBalance, double theTotalBalance) {
accountNumber = theAccountNumber;
pin = thePin;
availableBalance = theAvailableBalance;
totalBalance = theTotalBalance;
}
public boolean validatePIN(int userPIN){
if(userPIN==pin){
return true;
}else {
return false;
}
}
public double getAvailableBalance() {
return availableBalance;
}
public int getAccountNumber() {
return accountNumber;
}
public double getTotalBalance() {
return totalBalance;
}
public void credit(double amount){
totalBalance+=amount;
}
public void debit(double amount){//你持有的钱
availableBalance-=amount;
totalBalance-=amount;
}
}
Screen
package ATM;
public class Screen {
//不带换行的打印信息
public void displayMessage(String message){
System.out.print(message);
}
//带换行的打印信息
public void displayMessageLine(String message){
System.out.println(message);
}
//打印美元金额
public void displayDollarAmount(double amount){
System.out.printf("$%,.2f",amount);
}
//打印RMB金额
public void displayRMBAmount(double amount){
System.out.printf("$%,.2f",amount);
}
}
BankDatabase
package ATM;
public class BankDatabase {
private Account[] accounts;
public Account getAccounts(int accountNumber) {
for (Account currentAccount:accounts){
if(currentAccount.getAccountNumber()==accountNumber){
return currentAccount;
}
}
return null;
}
public boolean authenticateUser(int userAccountNumber,int userPIN){
Account userAccount=getAccounts(userAccountNumber);
if(userAccount!=null){
return userAccount.validatePIN(userPIN);
}else {
return false;
}
}
public BankDatabase() {
accounts=new Account[2];
accounts[0]=new Account(12345,54321,1000.0,1200.0);
accounts[1]=new Account(98765,56789,200.0,200.0);
}
public double getAvailableBalance(int userAccountNumber){
return getAccounts(userAccountNumber).getTotalBalance();
}
public double getTotalBalance(int userAccountNumber){
return getAccounts(userAccountNumber).getTotalBalance();
}
public void credit(int userAccountNumber,double amount){
getAccounts(userAccountNumber).credit(amount);
}
public void debit(int userAccountNumber,double amount){
getAccounts(userAccountNumber).debit(amount);
}
}
DepositSlot
package ATM;
public class DepositSlot {//存款口
//钱 是否 被存款槽 接受
public boolean isEnvelopeReceived(){
return true;//返回存款槽 已经 接受 钱(薪水)
}
}
Keypad
package ATM;
import java.util.Scanner;//用Scanner 获取用户输入数据
public class Keypad {//自动取款机的键盘
private Scanner input;//数据读取
public Keypad() {//无参构造函数
input=new Scanner(System.in);
}
public int getInput(){//返回用户输入的整形数字
return input.nextInt();
}
}
Transaction
package ATM;
public abstract class Transaction {//自动取款机交易
private int accountNumber;
private Screen screen;
private BankDatabase bankDatabase;
public Transaction(int userAccountNumber, Screen atmScreen, BankDatabase atmBankDatabase) {
accountNumber = userAccountNumber;
screen = atmScreen;
bankDatabase = atmBankDatabase;
}
public int getAccountNumber() {
return accountNumber;
}
public Screen getScreen() {
return screen;
}
public BankDatabase getBankDatabase() {
return bankDatabase;
}
abstract public void execute();
}
CashDispenser
package ATM;
public class CashDispenser {//自动提款机
private final static int INITIAL_COUNT=500;//初始化账单数目
private int count;
public CashDispenser() {
count=INITIAL_COUNT;
}
public void dispenseCash(int amount){
int billsRequired=amount;
count-=billsRequired;
}
public boolean isSufficientCashAvailable(int amount){
int billsRequired=amount/20;
if(count>=billsRequired){
return true;
}else {
return false;
}
}
}
Deposit
package ATM;
public class Deposit extends Transaction {//存款交易
private double amount;
private Keypad keypad;
private DepositSlot depositSlot;
private final static int CANCELED=0;
public Deposit(int userAccountNumber, Screen atmScreen, BankDatabase atmBankDatabase, Keypad atmKeypad, DepositSlot atmDepositSlot) {
super(userAccountNumber, atmScreen, atmBankDatabase);
keypad = atmKeypad;
depositSlot = atmDepositSlot;
}
@Override
public void execute() {//存款交易处理
BankDatabase bankDatabase=getBankDatabase();
Screen screen=getScreen();
amount=promptForDepositAmount();
if (amount!=CANCELED){
screen.displayMessage("\nplease insert a deposit envelope containing\t");
screen.displayDollarAmount(amount);
screen.displayMessageLine(".");
boolean envelopeReceived=depositSlot.isEnvelopeReceived();
if (envelopeReceived){
screen.displayMessageLine("\n your envelope has been "+
"received.\nNOTE:the money just deposited will not "+
"be available until we verify the amount of any"+
"enclosed cash and your checks clear");
bankDatabase.credit(getAccountNumber(),amount);
}else {
screen.displayMessageLine("\nyou did not insert an "+
"envelop,so the ATM has canceled your transaction.");
}
}else {
screen.displayMessageLine("\nCanceling transaction");
}
}
private double promptForDepositAmount(){
Screen screen=getScreen();
screen.displayMessage("\nplease enter a deposit amount in "+"CENTS(or 0 to cancel):\t");
int input=keypad.getInput();
if (input==CANCELED){
return CANCELED;
}else {
return (double)input/100;
}
}
}
Withdrawal
package ATM;
public class Withdrawal extends Transaction{//取钱交易or 提款
private int amount;
private Keypad keypad;
private CashDispenser cashDispenser;
private final static int CANCELED=6;
public Withdrawal(int userAccountNumber, Screen atmScreen, BankDatabase atmBankDatabase, Keypad atmKeypad, CashDispenser atmCashDispenser) {
super(userAccountNumber, atmScreen, atmBankDatabase);
keypad = atmKeypad;
cashDispenser = atmCashDispenser;
}
@Override
public void execute() {//取钱交易处理
boolean cashDispensed=false;
double availableBalance;
BankDatabase bankDatabase=getBankDatabase();
Screen screen=getScreen();
do{
amount=displayMenuOfAmounts();
if(amount!=CANCELED){
availableBalance=bankDatabase.getAvailableBalance(getAccountNumber());
if(amount<=availableBalance){
if(cashDispenser.isSufficientCashAvailable(amount)){
bankDatabase.debit(getAccountNumber(),amount);
cashDispenser.dispenseCash(amount);
cashDispensed=true;
screen.displayMessageLine("\n your cash has been"+"dispensed.pls take your cash now");
}else {
screen.displayMessageLine("\nInsufficient cash available in the ATM"+"\n\npls choose a smaller amount.");
}
}else {
screen.displayMessageLine("\nInsufficient cash available in the ATM"+"\n\npls choose a smaller amount.");
}
}else {
screen.displayMessageLine("\n Canceling transaction...");
return;
}
}while (!cashDispensed);
}
private int displayMenuOfAmounts(){
int userChoice=0;
Screen screen=getScreen();
int[]amount={0,20,40,60,100,200};
while (userChoice==0){
screen.displayMessageLine("\nWithdrawal Menu:");
screen.displayMessageLine("1-$20");
screen.displayMessageLine("2-$40");
screen.displayMessageLine("3-$60");
screen.displayMessageLine("4-$100");
screen.displayMessageLine("5-$200");
screen.displayMessageLine("6-Cancel transaction");
screen.displayMessage("\n Choose a withdrawal amount: ");
int input=keypad.getInput();
switch (input){
case 1:
case 2:
case 3:
case 4:
case 5:
userChoice=amount[input];
break;
case CANCELED:
userChoice=CANCELED;
break;
default:
screen.displayMessage("\n Invalid selection.Try again.");
}
}
return userChoice;
}
}
BanlanceInquiry
package ATM;
public class BanlanceInquiry extends Transaction{//余额查询
public BanlanceInquiry(int userAccountNumber, Screen atmScreen, BankDatabase atmBankDatabase) {
super(userAccountNumber, atmScreen, atmBankDatabase);
}
@Override
public void execute() {
BankDatabase bankDatabase=getBankDatabase();
Screen screen=getScreen();
double availableBalance=bankDatabase.getAvailableBalance(getAccountNumber());
double totalBalance=bankDatabase.getTotalBalance(getAccountNumber());
screen.displayMessageLine("\n余额信息");
//screen.displayMessageLine("\nBalance Information");
screen.displayMessageLine("\n可用余额");
// screen.displayMessage("--Available balance");
screen.displayDollarAmount(availableBalance);//美元金额
// screen.displayRMBAmount(availableBalance);//RMB金额
screen.displayMessageLine("\n总余额");
screen.displayMessage("\n--total balance ");
screen.displayDollarAmount(totalBalance);//美元金额
//screen.displayRMBAmount(totalBalance);//RMB金额
screen.displayMessageLine("\t");
}
}
ATM
package ATM;
public class ATM {
private boolean userAuthenticated;//用户是否被授权
private int currentAccountNumber;//当前用户账号
private Screen screen;//ATM's 屏幕
private Keypad keypad;//ATM's 键区
private CashDispenser cashDispenser;//ATM's 自动取款机
private DepositSlot depositSlot;//ATM's 存款槽
private BankDatabase bankDatabase;//账户信息数据库
//常数对应主界面的选项
private static final int BALANCE_INQUIRY=1;//余额查询
private static final int WITHDRAWAL=2;//取钱交易
private static final int DEPOSIT=3;//存钱交易
private static final int EXIT=4;//退出交易
public ATM() {
userAuthenticated=false;
currentAccountNumber=0;
screen=new Screen();
keypad=new Keypad();
cashDispenser=new CashDispenser();
depositSlot=new DepositSlot();//存款口
bankDatabase=new BankDatabase();
}
//ATM 开始工作
public void run(){
while (true){
while (!userAuthenticated){
screen.displayMessageLine("-----------------------------------------------------");
//screen.displayMessageLine("\nWelcome");
screen.displayMessageLine("\n热烈欢饮您");
authenticateUser();
}
performTransactions();
userAuthenticated=false;
currentAccountNumber=0;
screen.displayMessageLine("谢谢您,再见!!!");
//screen.displayMessageLine("\nThank you! GoodBye");
}
}
private void authenticateUser(){
screen.displayMessageLine("请输入您的卡号:");
//screen.displayMessage("\nPlease enter your account number:\t");
int accountNumber=keypad.getInput();
screen.displayMessageLine("请输入您的密码:");
//screen.displayMessage("\nEnter your PIN: ");
int pin=keypad.getInput();
userAuthenticated=bankDatabase.authenticateUser(accountNumber,pin);
if(userAuthenticated){
currentAccountNumber=accountNumber;
}else {
screen.displayMessageLine("无效的账户或者密码,请重试");
//screen.displayMessageLine("Invalid account number or PIN.pls try again.");
}
}
private void performTransactions(){
Transaction currentTransaction=null;
boolean userExited=false;
while (!userExited){
int mainMenuSelection=displayMainMenu();
switch (mainMenuSelection){
case BALANCE_INQUIRY:
case WITHDRAWAL:
case DEPOSIT:
currentTransaction=createTransaction(mainMenuSelection);
currentTransaction.execute();
break;
case EXIT:
screen.displayMessageLine("\n系统已经退出......");
//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\n");
screen.displayMessage("Enter a choice:\t");*/
screen.displayMessageLine("\n主界面:");
screen.displayMessageLine("1-总余额显示");
screen.displayMessageLine("2-取钱");
screen.displayMessageLine("3-存钱");
screen.displayMessageLine("4-退出\n");
screen.displayMessage("用户操作选项输入(1~4):\t");
return keypad.getInput();
}
private Transaction createTransaction(int type){
Transaction temp=null;
switch (type){
case BALANCE_INQUIRY://余额查询
temp=new BanlanceInquiry(currentAccountNumber,screen,bankDatabase);
break;
case WITHDRAWAL://取钱交易
temp=new Withdrawal(currentAccountNumber,screen,bankDatabase,keypad,cashDispenser);
break;
case DEPOSIT://存钱交易
temp=new Deposit(currentAccountNumber,screen,bankDatabase,keypad,depositSlot);
break;
}
return temp;
}
}
Driver
package ATM;
public class Driver {
//创建并运行 自动取款机
public static void main(String[] args) {
ATM atm=new ATM();
atm.run();
}
}
posted on 2018-08-29 08:09 Indian_Mysore 阅读(593) 评论(0) 编辑 收藏 举报