赞助

Bank.java

package com.qf.day12.t2.bank;

import java.util.Scanner;

/**
* 银行类
*/
public class Bank {
Scanner input = new Scanner(System.in);// Bank的属性

User[] users = new User[5];//存储所有银行用户的信息 { 0x11223344 , 0x22222222 , 0x33333333 , 0x44444444 , null }

int size = 4;//有效元素个数 { 0x11223344 , null , 0x33333333 , 0x44444444 , null }

/**
* 完成用户信息的初始化
*/
public void initial() {
User user = new User();// 0x11223344
user.setCardNo("6222020200001234567");
user.setPassword("123456");
user.setUsername("tom123");
user.setIdentityCard("110101199009096789");
user.setPhone("13909094455");
user.setBalance(3000.0);

users[0] = user;

User user2 = new User("6222020200001237654", "123456", "marry", "110101199009106666", "13988887777", 5000.0);// 0x22222222
users[1] = user2;// 0x22222222

users[2] = new User("6222020200005555666", "123456", "annie", "110101199009108888", "13988889999", 4000.0);// 0x33333333

users[3] = new User("6222020200005555443", "123456", "eric", "110101199009104433", "13988881234", 7000.0);// 0x44444444
}

/**
* 欢迎菜单(包含开户、登录功能的调用)
*/
public void welcomeMenu() {

System.out.println("-------------欢迎使用ATM自动银行系统-------------");
System.out.println("1.开户 2.登录");
System.out.println("请输入操作编号:");
int choice = input.nextInt();

switch(choice) {
case 1:
this.showAll();
this.register();//调用开户
this.showAll();
break;
case 2:
this.login();//调用登录
break;
}
}

/**
* 接收用户名和密码,并调用check方法验证
*/
public void login() {

// 用户输入卡号、密码
System.out.println("请输入卡号:");
String no = input.next();

System.out.println("请输入密码:");
String pwd = input.next();

User myAccount = this.check(no,pwd);//如果验证成功

if(myAccount != null) {
this.showMenu(myAccount);//调用业务功能菜单
}else {
System.out.println("卡号或密码不正确");
}
}

/**
* 验证用户名和密码是否正确
*/
public User check(String no , String pwd) {
// 验证卡密是否与开户时的信息一致
for (int i = 0; i < users.length; i++) {
if (users[i] != null) { // 非空判断
if (no.equals(users[i].getCardNo()) && pwd.equals(users[i].getPassword())) {
return users[i];//查找到与自己卡号密码匹配的“完整账户”(身份证号、手机号、余额)
}
}
}

return null;
}

/**
* 开户功能
*/
public void register() {//insert

//做数组长度的判断,是否需要扩容
if(users.length == size) {
//this.expand();
}

System.out.println("请输入密码:");
String pwd = input.next();

System.out.println("请输入姓名:");
String name = input.next();

System.out.println("请输入身份证号:");
String id = input.next();

System.out.println("请输入电话:");
String phone = input.next();

//固定前缀(622202020000)+ 自动生成的随机数(7位)
String prefix = "622202020000";

//随机后缀:Math.random(); 获取一个大于0小于1的整数(0421543.756894768954)
int suffix = (int)(Math.random() * 1000000) + 1000000;

String no = prefix + suffix;//生成的卡号


//新的账户信息
User user = new User();
user.setCardNo(no);
user.setPassword(pwd);
user.setUsername(name);
user.setPhone(phone);
user.setIdentityCard(id);

//将新账户信息,存入用户数组
users[size] = user;

size++;
}

/**
* 取款功能
*/
public void withdrawal(User myAccount) {

System.out.println("请输入取款金额:");
double money = input.nextDouble();

if(money > 0 && money < myAccount.getBalance() ) {//myAccount.getBalance();
//正常取款

double currentBalance = myAccount.getBalance() - money;

myAccount.setBalance(currentBalance);

System.out.println("取款成功,当前余额为:" + myAccount.getBalance());

}else {
System.out.println("余额不足!");
}

}

/**
* 转账功能
*/
public void transfer(User myAccount) {
//1.判断对方的卡号和姓名 User toUser = this.checkToAccount("62220000xxxxxxxx" , "marry");
//2.输入转账的金额money
//3.判断自己的余额
//4.自己账户的余额-money
//5.对方账户的余额+money
}

/**
* 展示银行业务功能的菜单
*/
public void showMenu(User myAccount) {
int choice;
while (true) {
System.out.println("2.存款 3.取款 4.转账 5.查询余额 6.修改密码 7.修改预留手机号码 8.注销账号 0.退出");
System.out.println("-------------------------------------");
System.out.println("请输入操作编号:");
choice = input.nextInt();

switch (choice) {
case 2:
System.out.println("执行存款");
break;
case 3:
this.withdrawal(myAccount);//调用取款功能
break;
case 4:
this.transfer(myAccount);//调用转账功能
break;
case 5:
System.out.println("执行查询余额");
break;
case 6:
System.out.println("执行修改密码");
break;
case 7:
System.out.println("执行修改预留手机号码");
break;
case 8:
System.out.println("执行注销账号");
break;
case 0:
System.out.println("执行退出");
return;// 退出整个方法
default:
System.out.println("输入有误,请重新输入!");
}
}
}


public void showAll() {
for(int i = 0 ; i < size ; i++) {
System.out.println( users[i].getCardNo() +"\t"+ users[i].getUsername() +"\t"+ users[i].getBalance() );
}
}
}

User.java

package com.qf.day12.t2.bank;

/**
* 用户类
* 存储用户的相关信息
*/
public class User {

private String cardNo;//卡号
private String password;//密码
private String username;//姓名
private String identityCard;//身份证号
private String phone;//电话
private double balance;//余额

public User() {}

public User(String cardNo, String password, String username, String identityCard, String phone, double balance) {
this.cardNo = cardNo;
this.password = password;
this.username = username;
this.identityCard = identityCard;
this.phone = phone;
this.balance = balance;
}

public String getCardNo() {
return cardNo;
}

public void setCardNo(String cardNo) {
this.cardNo = cardNo;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getIdentityCard() {
return identityCard;
}

public void setIdentityCard(String identityCard) {
this.identityCard = identityCard;
}

public String getPhone() {
return phone;
}

public void setPhone(String phone) {
this.phone = phone;
}

public double getBalance() {
return balance;
}

public void setBalance(double balance) {
this.balance = balance;
}
}

TestBank.java

package com.qf.day12.t2.bank;

/**
* 用以测试的类
*/
public class TestBank {

public static void main(String[] args) {

//0.完成过开户的操作(卡号、密码、姓名、身份证号、电话号、余额.....)

//1.输入卡号、输入密码

//2.卡密的校验,成功展示菜单;失败重新输入

//3.显示showMenu的菜单,完成业务编号的选择

//4.完成具体的业务操作(如:取款,转账....)


// 程序的起始
Bank bank = new Bank();// 好比选址之后开了一家银行网点

bank.initial();//初始化信息

bank.welcomeMenu();//欢迎页面



//没事优势和的是啊的发 啊士大夫看见案例看



// User user = null;//对象类型的变量中,没有指向一个具体存在的地址,而存储了一个null值
// System.out.println(user.getBalance());//NullPointerException

// outer:for(int i = 0 ; i < 5 ; i++) {
// System.out.println("当前值:" + i);
// switch(i) {
// case 3:
// System.out.println("满足条件,退出");
// break outer;
// }
// }
}

}

这三个文件都在同一个包下运行

posted on 2022-06-04 23:30  Tsunami黄嵩粟  阅读(91)  评论(0)    收藏  举报