1. 程式人生 > >java編寫的ATM自動存款機專案

java編寫的ATM自動存款機專案

自動取款機專案(ATMTest是測試類)

ATMTest類

/**
 * @author colin
 *
 */
public class ATMTest {

	public static void main(String[] args) {

		AtmView atmView = new AtmView();

		atmView.view();

	}

}

Account類、

/**
 * @author Colin
 *
 */
public class Account {

	private String accountId;// 賬戶帳號

	private String accountPwd;// 賬戶密碼

	private double accountBalance;// 賬戶餘額

	/**
	 * @param accountId
	 * @param accountPwd
	 * @param accountBalance
	 */
	public Account(String accountId, String accountPwd, double accountBalance) {
		super();
		this.accountId = accountId;
		this.accountPwd = accountPwd;
		this.accountBalance = accountBalance;
	}

	/**
	 * 
	 */
	public Account() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @return the accountId
	 */
	public String getAccountId() {
		return accountId;
	}

	/**
	 * @param accountId
	 *            the accountId to set
	 */
	public void setAccountId(String accountId) {
		this.accountId = accountId;
	}

	/**
	 * @return the accountPwd
	 */
	public String getAccountPwd() {
		return accountPwd;
	}

	/**
	 * @param accountPwd
	 *            the accountPwd to set
	 */
	public void setAccountPwd(String accountPwd) {
		this.accountPwd = accountPwd;
	}

	/**
	 * @return the accountBalance
	 */
	public double getAccountBalance() {
		return accountBalance;
	}

	/**
	 * @param accountBalance
	 *            the accountBalance to set
	 */
	public void setAccountBalance(double accountBalance) {
		this.accountBalance = accountBalance;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "Account [accountId=" + accountId + ", accountPwd=" + accountPwd + ", accountBalance=" + accountBalance
				+ "]";
	}

}

類:Bank類、

import java.util.Arrays;

public class Bank {

	private Account[] accounts;// 賬戶資訊
	private static Bank bank = new Bank();

	private Bank() {
		initAccount();
	}

	/**
	 * 
	 */
	public static Bank getInstance() {
		return bank;
	}

	private void initAccount() {
		accounts = new Account[3];
		accounts[0] = new Account("00111", "123456", 3000);
		accounts[1] = new Account("00112", "123456", 5000);
		accounts[2] = new Account("00113", "123456", 13000);

	}

	public Bank(Account[] accounts) {

		this.accounts = accounts;
	}

	public Account[] getAccounts() {
		return accounts;
	}

	public void setAccounts(Account[] accounts) {
		this.accounts = accounts;
	}

	@Override
	public String toString() {
		return "Bank [accounts=" + Arrays.toString(accounts) + "]";
	}

}

BankService類、

/**
 * 1、提供取款功能的getMoney方法 2、提供存款功能的saveMoney方法 3、顯示當前賬戶餘額功能的showMoney方法
 * 4、提供轉賬功能的transToMoney方法
 */
public class BankService {
	private Bank bank;

	public BankService() {
		bank = Bank.getInstance();
	}

	/**
	 * @return the bank
	 */
	public Bank getBank() {
		return bank;
	}

	/**
	 * @param bank
	 *            the bank to set
	 */
	public void setBank(Bank bank) {
		this.bank = bank;
	}

	public void getMoney(Account accountCheck, double money) {
		boolean isOk = checkMoney(accountCheck, money);
		if (isOk) {
			accountCheck.setAccountBalance(accountCheck.getAccountBalance() - money);
			System.out.println("取出 " + money + " 成功");
		} else {
			System.out.println("餘額不足");
		}

	}

	private boolean checkMoney(Account accountCheck, double money) {
		if (money > accountCheck.getAccountBalance()) {
			return false;
		}
		return true;
	}

	public void saveMoney(Account accountCheck, double money) {
		accountCheck.setAccountBalance(accountCheck.getAccountBalance() + money);
		System.out.println("存入 " + money + " 成功");
	}

	public void showMoney(Account accountCheck) {
		System.out.println(accountCheck.getAccountBalance());
	}

	public void transToXX(Account accountCheck, String toIn, double toInMoney) {
		Account isAccount = checkAccount(toIn);
		if (isAccount != null) {
			boolean isOk = checkMoney(accountCheck, toInMoney);
			if (isOk) {
				isAccount.setAccountBalance(isAccount.getAccountBalance() + toInMoney);
				accountCheck.setAccountBalance(accountCheck.getAccountBalance() - toInMoney);
				System.out.println("轉入 賬號" + toIn + " " + toInMoney + "元 成功");
			} else {
				System.out.println("您的餘額不足,無法轉賬!");
			}
		} else {
			System.out.println("您要轉入的卡號不存在!");
		}

	}

	/**
	 * @param toIn
	 * @return
	 */
	private Account checkAccount(String toIn) {
		for (int i = 0; i < bank.getAccounts().length; i++) {
			Account account = bank.getAccounts()[i];
			if (account.getAccountId().equals(toIn)) {
				return account;
			}
		}
		return null;
	}

	public Account checkLogin(String accountIn, String psd) {
		if (accountIn == null) {
			return null;
		}
		for (int i = 0; i < bank.getAccounts().length; i++) {
			Account account = bank.getAccounts()[i];
			if (accountIn.equals(account.getAccountId()) && psd.equals(account.getAccountPwd())) {
				return account;
			}
		}
		System.out.println("賬號密碼錯誤!");
		return null;
	}

}

AtmView類、

import java.util.Scanner;

/**
 * @author Colin
 *
 */
public class AtmView {

	private Scanner sc = new Scanner(System.in);

	public void view() {
		BankService bs = new BankService();
		for (int count = 0; count < 3; count++) {
			System.out.println("歡迎進入中國銀行ATM機操作介面");
			Account accountCheck = null;
			for (int i = 0; i < 10; i++) {
				System.out.println("--------------------");
				System.out.println("請輸入賬號和密碼");
				String accountIn = sc.next();
				String psd = sc.next();

				accountCheck = bs.checkLogin(accountIn, psd);
				if (accountCheck != null) {
					break;
				}
			}

			while (accountCheck != null) {
				System.out.println("1.\t取款");
				System.out.println("2.\t存款");
				System.out.println("3.\t查詢");
				System.out.println("4.\t轉賬");
				System.out.println("5.\t退出");
				int key = sc.nextInt();
				double money = 0;
				boolean isExit = false;
				switch (key) {
				case 1:
					System.out.println("請輸入取款金額:");
					money = sc.nextDouble();
					bs.getMoney(accountCheck, money);
					break;
				case 2:
					System.out.println("請輸入存款金額:");
					money = sc.nextDouble();
					bs.saveMoney(accountCheck, money);
					break;
				case 3:
					bs.showMoney(accountCheck);
					break;
				case 4:
					System.out.println("請輸入要轉入的賬號");
					String toIn = sc.next();
					System.out.println("請輸入金額");
					double toInMoney = sc.nextDouble();
					bs.transToXX(accountCheck, toIn, toInMoney);
					break;
				default:
					isExit = true;
					break;
				}
				if (isExit) {
					System.out.println("您已退出登入,歡迎再次使用中國銀行ATM機");
					break;
				}
			}
		}

	}

}