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;
				}
			}
		}

	}

}

猜你喜欢

转载自blog.csdn.net/shui_jin_shan/article/details/84337747