一、项目包含知识点
二、项目需求
三、系统模块设计
3. 1、模块介绍
3·2、银行管理系统技术分析如图
3.3银行管理系统界面展示
四、银行管理系统源码
4.1Account类
4.2ATMSystem类
本项目需要完成以下功能:
银行管理系统总体设计如图
//创建客户类 public class Account { //声明变量 private String bankNumber;//卡号 private String clientName;//客户名称 private String passWord;//密码 private double balance;//余额 private double onceMoney;//当次取现限额 public String getBankNumber() { return bankNumber; } public void setBankNumber(String bankNumber) { this.bankNumber = bankNumber; } public String getClientName() { return clientName; } public void setClientName(String clientName) { this.clientName= clientName; } public String getPassWord() { return passWord; } public void setPassWord(String passWord) { this.passWord = passWord; } public double getBalance() { return balance; } public void setBalance(double balance) { this.balance = balance; } public double getOnceMoney() { return onceMoney; } public void setOnceMoney(double onceMoney) { this.onceMoney = onceMoney; } //创建无参构造方法 public Account() { } //创建有参构造方法 public Account(String bankNumber, String clientName, String passWord, double onceMoney) { this.bankNumber= bankNumber; this.clientName=clientName; this.passWord = passWord; this.onceMoney=onceMoney; } }
import java.util.ArrayList; import java.util.Random; import java.util.Scanner; public class ATMSystem { public static void main(String[] args) { //创建集合存储账户对象 ArrayList<Account> accounts = new ArrayList<>(); //调用首页方法 showMain(accounts); } //定义首页方法 public static void showMain(ArrayList<Account> accounts){ System.out.println("===========欢迎进入中国银行首页================="); Scanner scanner = new Scanner(System.in); while (true) { System.out.println("请您输入您对应的菜单选项:"); System.out.println("1、登录"); System.out.println("2、开户"); System.out.print("请输入对应的选项:"); int input=scanner.nextInt(); switch (input){ case 1: //调用登录方法 loginSystem(accounts,scanner); break; case 2: //调用开户方法 registerSystem(accounts,scanner); break; default: System.out.println("对不起,没有该选项!"); } } } //定义登录方法 private static void loginSystem(ArrayList<Account> accounts,Scanner scanner) { //必须系统中存在用户才可以登录 if(accounts.size()==0){ //没有任何用户 System.out.println("当前系统中没有客户,请先注册"); return; } //让用户输入卡号,根据卡号查询账户对象 while (true) { System.out.println("请输入卡号:"); String inputNumber=scanner.next(); //根据卡号查询账户对象 Account accout=getAccountBankNumber(inputNumber,accounts); //判断账户对象是否存在,存在说明卡号正常 if(accout!= null){ while (true) { //让用户继续输入密码 System.out.println("请输入密码:"); String pwd=scanner.next(); //判断密码是否正确 if(accout.getPassWord().equals(pwd)){ //密码正确,登录成功,展示系统登录后的操作界面 System.out.println("恭喜您,"+accout.getClientName()+"先生/女士成功进入系统,您的卡号是:"+accout.getBankNumber()); //调用菜单方法 showMenu(scanner,accout,accounts); return; }else { System.out.println("您的密码输入有误!"); } } }else { System.out.println("不存在该客户!"); } } } private static void showMenu(Scanner scanner,Account account,ArrayList<Account> accounts) { while (true) { System.out.println("===========用户操作界面==============="); System.out.println("1、查询"); System.out.println("2、存款"); System.out.println("3、取款"); System.out.println("4、转账"); System.out.println("5、修改"); System.out.println("6、退出"); System.out.println("7、销户"); System.out.println("请您输入对应的选项:"); int inputMenu=scanner.nextInt(); switch (inputMenu){ case 1: //调用查询方法 showMessage(account); break; case 2: //调用存款方法 Deposit(account,scanner); break; case 3: //调用取款方法 drawMoney(account,scanner); break; case 4: //调用转账方法 Transfer(accounts,account,scanner); break; case 5: //调用修改密码方法 Modify(account,scanner); return; case 6: //退出 System.out.println("欢迎下次光临!"); return; case 7: //注销账户 accounts.remove(account); System.out.println("销户成功!"); return; default: System.out.println("您的选项输入有误!"); } } } //定义修改密码方法 private static void Modify(Account account,Scanner scanner) { System.out.println("=======修改密码============"); while (true) { System.out.println("请输入原密码:"); String inputRightpwd=scanner.next(); //判断密码是否正确 if(account.getPassWord().equals(inputRightpwd)){ while (true) { //可以输入新密码 System.out.println("请您输入新密码:"); String newPwd=scanner.next(); System.out.println("请确认密码:"); String identityPwd=scanner.next(); if(newPwd.equals(identityPwd)){ //修改账户对象的密码为新密码 account.setPassWord(newPwd); return; }else { System.out.println("两次输入的密码不一致!"); } } }else { System.out.println("密码输入不正确!"); } } } //定义转账方法 private static void Transfer(ArrayList<Account> accounts, Account act, Scanner scanner) { //判断系统中是否有2个客户及以上 if(accounts.size()<2){ System.out.println("系统中没有其它客户,您不可以转账!"); return; } //判断自己银行卡中是否有钱 if(act.getBalance()==0){ System.out.println("您的账户没有余额!"); return; } while (true) { System.out.println("请您输入对方账户的卡号:"); String bankNumber=scanner.next(); Account account =getAccountBankNumber(bankNumber,accounts); //判断客户对象是否存在 if(account !=null){ //判断这个账户是否是账户自己 if(account.getBankNumber().equals(act.getBankNumber())){ //给自己转账 System.out.println("您不可以为自己转账!"); }else { //确认对方的姓氏 String name ="*"+ account.getClientName().substring(1); System.out.println("请您确认【"+name+"】的姓氏:"); String preName = scanner.next(); //判断 if(account.getClientName().startsWith(preName)){ //开始转账 System.out.println("请您输入转账的金额:"); double inputMoney=scanner.nextDouble(); //判断金额是否超过了自己的余额 if(inputMoney>act.getBalance()){ System.out.println("对不起,你要转账的金额太多,您最多可以转账"+act.getBalance()); }else { //可以转账 act.setBalance(act.getBalance()-inputMoney); account.setBalance(account.getBalance()+inputMoney); System.out.println("恭喜转账成功,已经为"+account.getClientName()+"转账"+inputMoney); showMessage(act); return; } }else { System.out.println("您输入的信息有误!"); } } }else { System.out.println("您输入的转账卡号有问题!"); } } } //定义取款方法 private static void drawMoney(Account account, Scanner scanner) { System.out.println("========取款操作=========="); //判断它的账户是否足够100元 if(account.getBalance()>=100){ while (true) { System.out.println("请输入取款的金额:"); double inputMoney= scanner.nextDouble(); //判断金额是否超过当次限额 if(inputMoney>account.getOnceMoney()){ System.out.println("您当次取款金额超过每次限制,每次最多可以取:"+account.getOnceMoney()); }else { //判断当前余额是否足够取钱 if(account.getBalance()>=inputMoney){ //可以取钱 account.setBalance(account.getBalance()-inputMoney); System.out.println("恭喜您,取钱"+inputMoney+"成功了,当前账户剩余:"+account.getBalance()); return; }else { System.out.println("余额不足!"); } } } }else { System.out.println("您的账户没有超过100元,不允许取款!"); } } //定义存款方法 private static void Deposit(Account account,Scanner scanner) { System.out.println("=============存款操作==============="); System.out.println("请输入存款的金额:"); double money = scanner.nextDouble(); //直接把金额修改到账户对象的money属性中去 account.setBalance(account.getBalance()+money);//需要把原来的钱get出来再加上新存入的money System.out.println("存款成功!"); showMessage(account); } //查询客户信息 private static void showMessage(Account account) { System.out.println("==================您的账户信息===================="); System.out.println("姓名" + " \t" + "卡号" + " \t" + "余额" + " \t" + "当次限额"); System.out.println(account.getClientName() + " \t" + account.getBankNumber() + " \t" + account.getBalance() + " \t " + account.getOnceMoney() + " \t"); } //定义开户方法 private static void registerSystem(ArrayList<Account> accounts,Scanner scanner) { System.out.println("请输入开户名:"); String openName = scanner.next(); String openPassword= ""; while (true) { System.out.println("请输入开户密码:"); openPassword= scanner.next(); System.out.println("请输入确认密码:"); String identityPassword = scanner.next(); //判断两次输入的密码是否一致 if(identityPassword.equals(openPassword)){ break; }else { System.out.println("两次密码必须一致!"); } } System.out.println("请您输入当次限额:"); double onceMoney = scanner.nextDouble(); //调用生成账户卡号的方法 String bankNumber=automationBankNumber(accounts); //创建一个客户对象封装账户的信息 Account account= new Account(bankNumber,openName,openPassword,onceMoney); //把客户对象添加到集合中去 accounts.add(account); System.out.println("恭喜您!您开户成功,您的卡号是:"+account.getBankNumber()+",请您妥善保管!"); } //定义生成银行卡账号方法 public static String automationBankNumber(ArrayList<Account> accounts){ //生成8位随机的数字代表卡号 while (true) { String bankNumber= ""; Random r = new Random(); for (int i = 0; i < 8; i++) { bankNumber += r.nextInt(10); } //判断是否重复了 Account acc =getAccountBankNumber(bankNumber,accounts); if(acc == null){ //说明当前卡号没有重复 return bankNumber; } } } public static Account getAccountBankNumber(String number,ArrayList<Account> accounts){ //根据卡号查询账户对象 for (int i = 0; i < accounts.size(); i++) { Account a= accounts.get(i); if(a.getBankNumber().equals(number)){ return a; } } //查无此账户,说明卡号没有重复了; return null; } }