项目介绍
完成基本的银行业务功能
存款 取款 转账 查询余额 修改密码 修改预留手机号 注销账号 退出
任务分解:
定义三个基本类:
BankTest():银行业务主程序
Bank(): 银行类,包含主要业务逻辑
User():客户类,存储客户相关个人信息
任务逻辑图:
实现代码
银行主类(BankTest)
程序开头在这个类中
/*BankTest*/ public class BankTest { public static void main(String[] args) { Bank bank = new Bank();
bank.login(); System.out.println("欢迎再次使用"); } }
银行类(Bank)
login()函数完成了3个功能:
1.调用初始化函数。//存入初始用户信息
2.接收用户输入的卡号密码等信息
3.调用check()函数核对用户信息是否正确
import java.util.Scanner; public class Bank { Scanner sc = new Scanner(System.in); User[] users = new User[5]; //String no = ;//字符串的默认值? public void initial() {//存入初始用户信息 User user = new User();//生成用户对象 user.setCardNo("6222021702033334444"); user.setPwd("123456"); user.setIdNo("4444444444444"); user.setPhone("13900000000"); user.setName("tom"); user.setBalance(4000.0); users[0] = user; User user1 = new User("6222021702033334444","123456","5555555555555","13911111111","tony",5000.0);//有参构造方式生成对象 users[1] = user1; } public void login() { initial();//调用初始化函数 System.out.print("请输入卡号:"); String no = sc.next(); System.out.print("请输入密码:"); String pwd = sc.next(); User mine = check(no , pwd);//将接收的卡号和密码传给check()函数确认有无客户信息,有客户信息则返回客户对象,无客户信息则返回null if (mine != null) { System.out.println("已经到这里了"); this.showMenu(mine);//客户信息存在,调出银行业务菜单 return; } else { System.out.println("输入错误,用户不存在"); } } //check函数,校验是否存在该用户 public User check(String no ,String pwd) { for (int i = 0;i < users.length; i++) { if (users[i] != null) {//数组的非空判断 if (no.equals(users[i].getCarNo()) && pwd.equals(users[i].getPwd())) { System.out.println("用户存在"); return users[i]; } } } return null; } }
客户类(User)
/*用户类*/ public class User { private String cardNo; private String pwd; private String idNo; private String phone; private String name; private double balance; public User() {} public User (String cardNo , String pwd , String idNo , String phone , String name , double balance) { this.cardNo = cardNo; this.pwd = pwd; this.idNo = idNo; this.phone = phone; this.name = name; this.balance = balance; } public void setCardNo(String cardNo) { this.cardNo = cardNo; } public String getCarNo() { return this.cardNo; } public void setPwd(String pwd) { this.pwd = pwd; } public String getPwd() { return this.pwd; } public void setIdNo(String idNo) { this.idNo = idNo; } public String getIdNo() { return this.idNo; } public void setPhone(String phone) { this.phone = phone; } public String getPhone() { return this.phone; } public void setName(String name) { this.name = name; } public String getName() { return this.name; } public void setBalance(double balance) { this.balance = balance; } public double getBalance() { return this.balance; } }
银行类(Bank)
业务功能相关代码
public void showMenu(User mine) { do { System.out.println(); System.out.println(); System.out.println(); System.out.println("欢迎使用银行ATM自助服务"); System.out.println("--------------------"); System.out.println("1.开户 2.存款 3.取款 4.转账 5.查询余额 6.修改密码 7.修改手机号 0.退出"); System.out.println("--------------------"); int choice = 0; choice = sc.nextInt(); switch (choice) { case 1: System.out.println("执行开户功能"); break; case 3: System.out.println("执行取款功能"); this.withdrawal(mine); break; case 0: System.out.println("执行退出功能"); return; } } while (true);
银行类(Bank)
具体取款操作相关代码
public void withdrawal(User mine) { System.out.print("请输入取款金额:"); double moneyOut = sc.nextDouble(); if (mine.getBalance() >= moneyOut) { System.out.println("请取款金额为:" + moneyOut + "。"); mine.setBalance(mine.getBalance()-moneyOut); System.out.println("剩余存款为:" + mine.getBalance() + "。"); } }