银行管理系统(升级规范版)
admin类
package com.shanxiguofang.bank;
public class Admin {
//管理员账号
private String account;
//管理员密码
private String password;
//有参构造方法
public Admin(String account, String password) {
super();
this.account = account;
this.password = password;
}
//无参构造方法
public Admin() {
super();
}
//属性(账号,密码)get/set方法
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
bank类
package com.shanxiguofang.bank;
public class Bank {
private String name;//银行名
private String cardNum;//银行卡号
public Bank(String name) {
this.name = name;
}
public Bank() {
super();
}
public Bank(String name, String cardNum) {
super();
this.name = name;
this.cardNum = cardNum;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCardNum() {
return cardNum;
}
public void setCardNum(String cardNum) {
this.cardNum = cardNum;
}
}
BankCustomerMethod类
package com.shanxiguofang.bank;
public class BankCustomerMethod {
BankUtils bkUtils = new BankUtils();
/**
*
* 本方法整合用户的 功能实现 1.存钱 2.取钱 3.修改密码 4.查看个人信息 5.返回上一级
*
*/
public void bankCustomerMethod(Customer [] customer) {
boolean isExit = true;
while(isExit) {
System.out.println("请选择你需要进行的操作:1.存钱 2.取钱 3.修改密码 4.查看个人信息 5.返回上一级");
int customerSelect = bkUtils.getReadInt();
switch (customerSelect) {
case 1:
//1.存钱
System.out.println("存钱");
break;
case 2:
//2.取钱
System.out.println("取钱");
break;
case 3:
//3.修改密码
System.out.println("修改密码");
break;
case 4:
//查看个人信息
System.out.println("查看个人信息");
break;
case 5:
System.out.println("返回上一级");
isExit = false;
break;
default:
System.out.println("系统操作错误!!!");
break;
}
}
//return customer;
}
public Customer sysCustomerDengLu(Customer [] customers) {
for(int x = 1 ; x <= 3 ; x++) {
//获取输入的管理员账号
System.out.println("请输入用户账号:");
String account = bkUtils.getReadString();
//获取输入的管理员密码
System.out.println("请输入用户密码:");
@SuppressWarnings("unused")
String psd = bkUtils.getReadString();
/*
* 循环遍历所有管理员账号,密码;
* 如果输入的管理员账号存在,则用verificatAccount存储账号
* 如果输入的管理员账号密码,则用verificatPsd存储密码
* 并把该对象返回 ,使用admin存储
*/
String verificatAccount = null;
String verificatPsd = null;
Customer customer = null ;
for(int y = 0 ; y < customers.length; y++) {
if(customers[y].getAccount().equals(account)) {
verificatAccount = customers[y].getAccount();
verificatPsd = customers[y].getPassword();
customer = customers[y];
break;
}
}
/*
* 如果verificatAccount和verificatPsd为null
* 都为null:则说明输入的管理员账号,密码不存在,显示该管理员不存在提示
* verificatAccount为null:则说明输入的管理员账号不存在,显示该管理员账号不存在提示
* verificatPsd为null:则说明输入的管理员密码不存在,显示该管理员密码错误提示
*/
if(verificatAccount == null) {
System.out.println("该用户账号不存在!!!你还有"+(3-x)+"次机会!");
}else if(!verificatPsd.equals(psd)) {
System.out.println("用户密码错误!!!你还有"+(3-x)+"次机会!");
}else if(verificatAccount != null && verificatPsd != null) {
System.out.println("登陆成功");
return customer;
}
//登陆失败只限3次,3次之后冻结
if((3-x) == 0) {
System.out.println("你已被冻结,请联系老师解决。");
}
}
return null;
}
}
BankSysMethod类
package com.shanxiguofang.bank;
import java.util.Scanner;
public class BankSysMethod {
//关于Customer的操作方法都存储在Customer类,通过创建Customer对象来调用
Customer cust = new Customer();
/**
* 此方法用于对管理员功能的总的整合
*
*/
public void sysRootMethod(Customer [] customers) {
boolean isExit = true;
while(isExit) {
System.out.println();
System.out.println("请选择你需要进行的操作:1.查看用户信息 2.修改用户信息 3.增加用户 4.删除用户 5.返回上一级");
int sysMenu = bkUtils.getReadInt();
switch (sysMenu) {
case 1:
System.out.println("查看用户信息");
Lookcustomer(customers);
break;
case 2:
//修改用户信息
System.out.println("修改用户信息");
Setcustomer(customers);
break;
case 3:
//增加用户
Addcustomer(customers);
break;
//Customer[] newCustomers = cust.addCustomer(customer);
//这里的查询用户传进的是Customer[] customer ,所以需要把把扩容后添加进新用户的数组赋值给customer;
//customer = newCustomers;
case 4:
//删除用户 [1,2,3,4,5] [1,2,null,4,5] -- [,1,2,4,null,5] -- [1,2,3,4,null] .length - 2
System.out.println("删除用户");
Removecustomer(customers);
//Customer[] deleteCustomer = cust.deleteCustomer(customer);
//这里的查询用户传进的是Customer[] customer ,所以需要把减容后的新用户的数组赋值给customer;
//customer = deleteCustomer;
break;
case 5:
isExit = false;
break;
default:
System.out.println("系统操作错误!!!");
break;
}
}
//return customer;
}
BankUtils bkUtils = new BankUtils() ;
//存储登陆的管理员
Admin admin = null ;
/**
* 此方法用于管理员登陆,整合了账号,密码验证方法
*
*/
public Admin sysRootDengLu(Admin [] admins) {
for(int x = 1 ; x <= 3 ; x++) {
//获取输入的管理员账号
System.out.println("请输入管理员账号:");
String account = bkUtils.getReadString();//admin01
//获取输入的管理员密码
System.out.println("请输入管理员密码:");
@SuppressWarnings("unused")
String psd = bkUtils.getReadString();
/*
* 循环遍历所有管理员账号,密码;
* 如果输入的管理员账号存在,则用verificatAccount存储账号
* 如果输入的管理员账号密码,则用verificatPsd存储密码
* 并把该对象返回 ,使用成员变量admin存储
*/
String verificatAccount = null;
String verificatPsd = null;
for(int y = 0 ; y < admins.length; y++) {
if(admins[y].getAccount().equals(account)) {
verificatAccount = admins[y].getAccount();
verificatPsd = admins[y].getPassword();
admin = admins[y];
break;
}
}
/*
* 如果verificatAccount和verificatPsd为null
* 都为null:则说明输入的管理员账号,密码不存在,显示该管理员不存在提示
* verificatAccount为null:则说明输入的管理员账号不存在,显示该管理员账号不存在提示
* verificatPsd与输入的密码比较:则说明输入的管理员密码不存在,显示该管理员密码错误提示
*/
if(verificatAccount == null) {
System.out.println("该管理员账号不存在!!!你还有"+(3-x)+"次机会!");
}else if(!psd.equals(verificatPsd)) {
System.out.println("管理员密码错误!!!你还有"+(3-x)+"次机会!");
}else if(verificatAccount != null && verificatPsd != null) {
System.out.println("登陆成功");
return admin;
}
//登陆失败只限3次,3次之后冻结
if((3-x) == 0) {
System.out.println("你已被冻结,请联系老师解决。");
}
}
return null;
}
public void Lookcustomer(Customer[] customers) {
if(customers==null) {
System.out.println("本银行暂时没人");
}
for(int i=1;i<=customers.length;i++) {
System.out.println("序号"+i+" "+customers[i-1].toString());
}
}
Customer[] customers1;
public Customer[] Addcustomer(Customer[] customers) {
System.out.println("----------添加开始----------");
Scanner sa=new Scanner(System.in);
System.out.println("请输入用户姓名");
String name=sa.nextLine();
Scanner sb=new Scanner(System.in);
System.out.println("请输入用户账号");
String account=sb.nextLine();
Scanner sc=new Scanner(System.in);
System.out.println("请输入用户密码");
String password=sc.nextLine();
Scanner sd=new Scanner(System.in);
System.out.println("请输入用户余额");
double balance=sd.nextDouble();
Scanner se=new Scanner(System.in);
System.out.println("请输入用户手机号");
String phone=se.nextLine();
boolean abc=false;
for(int i=0;i<customers.length;i++) {
Customer customer=customers[i];
if(customer.getAccount().equals(account)) {
System.out.println("添加失败,用户已存在");
}else {
Customer a=new Customer(name,account,password,balance,phone);
customers1=bkUtils.addCapacity(customers);
customers1[customers.length]=a;
System.out.println("添加成功");
break;
}
}
return customers1;
}
public void Removecustomer(Customer[] customers) {
Scanner sc=new Scanner(System.in);
System.out.println("请输入您要删除的客户序号");
int num=sc.nextInt();
if(customers[num].getBalance()!=0) {
System.out.println("--------您的余款不为0,无法删除---------");
}else if(customers[num].getBalance()==0) {
customers[num].setName(null);
customers[num].setAccount(null);
customers[num].setPassword(null);
customers[num].setBalance(0);
customers[num].setPhone(null);
}
System.out.println("--------删除成功---------");
}
public void Setcustomer(Customer[] customers) {
Scanner sc=new Scanner(System.in);
System.out.println("请输入您要修改的用户账号");
String account1=sc.nextLine();
System.out.println("-------------------------------------");
System.out.println("如无需要修改的信息,回车即可");
System.out.println("-------------------------------------");
boolean abc=false;
for(int i=0;i<customers.length;i++) {
Customer customer=customers[i];
if(customer.getAccount().equals(account1)) {
Scanner a=new Scanner(System.in);
System.out.println("请输入新的用户名");
String Newname=a.nextLine();
Scanner b=new Scanner(System.in);
System.out.println("请输入新的密码");
String Newpassword=b.nextLine();
Scanner c=new Scanner(System.in);
System.out.println("请输入用户余额");
int Newbalance=c.nextInt();
Scanner d=new Scanner(System.in);
System.out.println("请输入新的手机号");
String Newphone=d.nextLine();
customers[i].setName(Newname);
customers[i].setPassword(Newpassword);
customers[i].setBalance(Newbalance);
customers[i].setPhone(Newphone);
abc=true;
break;
}
}
if(abc) {
System.out.println("修改成功");
}else {
System.out.println("查无此用户");
}
}
}
BankTest类
package com.shanxiguofang.bank;
public class BankTest {
public static void main(String[] args) {
Bank bk = new Bank("中国招商银行");
BankUtils bkUtils = new BankUtils();
DataBase db = new DataBase();
Admin [] admins = db.getAdmin();
Customer [] customers = db.getCostomer();
Admin admin = new Admin();
Customer customer = new Customer();
BankSysMethod bkSysMethod = new BankSysMethod();
BankCustomerMethod bkCustomerMethod = new BankCustomerMethod();
//判断是否退出
Boolean isExit = true;
do {
//登陆界面
System.out.println("Welcome to the use "+bk.getName()+"系统,请根据你的需求选择你需要的操作");
System.out.println("***************请选择:1.管理员登陆 ,2.用户登陆,3.退出***************");
//获取输入的 int 类型的值
int selectMenu = bkUtils.getReadInt();
switch (selectMenu) {
case 1:
//1、管理员登陆验证,并把登陆的管理员返回
admin = bkSysMethod.sysRootDengLu(admins);
if(admin == null) {
return;
}
System.out.println(admin);
bkSysMethod.sysRootMethod(customers);
break;
case 2:
customer = bkCustomerMethod.sysCustomerDengLu(customers);
if(customer == null) {
return;
}
bkCustomerMethod.bankCustomerMethod(customers);
System.out.println("用户登陆成功");
break;
case 3:
isExit = false;
System.out.println("退出成功");
break;
default:
System.out.println("你的输入有误,请重新输入!!!");
break;
}
} while (isExit);
}
}
BankUtils类
package com.shanxiguofang.bank;
import java.util.Scanner;
public class BankUtils {
/**
*
* 返回一个手动输入的字符串值
* @return String
*/
public String getReadString() {
Scanner sc = new Scanner(System.in);
String string = sc.nextLine();
return string;
}
/**
*
* 返回一个手动输入的字符串值,如果输入的值为null,则给定默认值
* @return String
*/
public String getReadString(String str,String shuxing) {
Scanner sc = new Scanner(System.in);
String string = sc.nextLine();
if(string.length() == 0 || string == null ) {
string = str;
System.out.print("你此次未输入更改数据,该成员"+shuxing+":"+str+",属性值未变动");
System.out.println();
}
return string;
}
/**
*
* 返回一个手动输入的int值
* @return int
*/
public int getReadInt() {
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
return num;
}
/**
*
* 返回一个手动输入的int值,如果输入的值为null,则给定默认值
* @return int
*/
public int getReadNum(int nums) {
Scanner sc = new Scanner(System.in);
String numStr = sc.nextLine();
if(numStr == null || numStr.length() == 0 ) {
return nums;
}
return nums;
}
/**
*
* 返回一个手动输入的double值
* @return double
*/
public double getReadDouble() {
Scanner sc = new Scanner(System.in);
String numStr = sc.nextLine();
double parseDouble = (double)Integer.parseInt(numStr);
return parseDouble;
}
/**
*
* 返回一个手动输入的double值,如果输入的值为null,则给定默认值
* @return double
*/
public double getReadDouble(double doubl , String shuxing) {
Scanner sc = new Scanner(System.in);
String numStr = sc.nextLine();
double parseInt;
if(numStr == null || numStr.length() == 0 ) {
System.out.print("你此次未输入更改数据,该成员"+shuxing+":"+doubl+",属性值未变动");
System.out.println();
return doubl;
}else {
parseInt = (double)Integer.parseInt(numStr);
}
return parseInt;
}
/**
* 获得一个输入的数据,并判断该数据的数据类型
* 如果输入的是数字,则说明输入符合规范,返回false ,停止循环输入数字;
* 如果输入的是其他字符,则不符合输入规范,返回一个true ,继续循环输入,直到输入的值为数字;
*/
public boolean isInt(String input) {
// if (Integer.parseInt(input) == -1) {
// System.out.println("已退出存钱!");
// return false;
// }
for (char c : input.toCharArray()) {
if (Character.getType(c) == Character.OTHER_LETTER) {
System.out.println("输入错误!!!你输入的是 中文 ,请重新输入此次存钱数");
return true;
} else if (Character.isDigit(c)) {
return false;
} else if (Character.isLetter(c)) {
System.out.println("输入错误!!!你输入的是 英文字母 ,请重新输入此次存钱数");
return true;
} else {
System.out.println("输入错误!!!你的输入不符合规范,请重新输入此次存钱数");
return true;
}
}
return false;
}
public boolean verificationIsSame(Customer[] customers, String account) {
boolean isFind = false ;
for(int x = 0 ; x < customers.length ; x++) {
if(customers[x].getAccount().equals(account)) {
System.out.println("该账号已存在,请重新输入!!!");
isFind = true;
return isFind;
}
}
return isFind;
}
public Customer[] addCapacity(Customer[] customers) {
Customer [] newCustomer = new Customer[customers.length + 1] ;
//判断传进来的数组长度是否大于等于10
if(customers.length >= 10) {
//如果数组长度大于等于10,把object数组的值赋值给newObj
for(int x = 0 ; x < customers.length ; x++) {
newCustomer[x] = customers[x];
}
}
return newCustomer;
}
}
customer类
package com.shanxiguofang.bank;
public class Customer {
//客户属性
private String name;//用户
private String account;//用户账号
private String password;//用户密码
private double balance; //用户余额
private String phone; //用户手机号
//toString方法
public String toString() {
return "Customer [name=" + name + ", account=" + account + ", password=" + password + ", balance="
+ balance + ", phone=" + phone + "]";
}
//客户无参构造方法
public Customer() {
super();
}
//客户有参构造方法
public Customer(String name, String account, String password, double balance, String phone) {
super();
this.name = name;
this.account = account;
this.password = password;
this.balance = balance;
this.phone = phone;
}
//属性get/set方法
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
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 String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
Customer [] newCustomers ;
BankUtils bkUtils = new BankUtils();
public Customer[] addCustomer(Customer [] customers) {
/*
* 需要添加的对象数据
* private String name;//用户
* private String password;//用户密码
* private double balance; //用户余额
* private String phone; //用户手机号
*
*/
System.out.println("---------------添加开始---------------");
System.out.println("请输入添加的客户用户名:");
String name = bkUtils.getReadString();
//判断输入的用户账号是否相同;
String account = null ;
boolean isFlag = true;
while(isFlag) {
System.out.println("请输入添加的客户账号:");
account = bkUtils.getReadString();
boolean verificationIsSame = bkUtils.verificationIsSame(customers, account);
isFlag = verificationIsSame;
}
System.out.println("请输入添加的客户密码:");
String password = bkUtils.getReadString();
System.out.println("请输入添加的客户余额:");
double blance = bkUtils.getReadDouble();
System.out.println("请输入添加的客户手机号:");
String phone = bkUtils.getReadString();
Customer newCustomer = new Customer(name , account , password , blance , phone);
/**
* 往对象里面添加新数据,要判断数据的长度,如果数组长度大于等于10,则每次添加数据需要对数组进行动态扩容。
*
*/
//数组扩容
newCustomers = bkUtils.addCapacity(customers);
newCustomers[customers.length] = newCustomer;
System.out.println("---------------添加完成---------------");
return newCustomers;
}
}
DataBase类
package com.shanxiguofang.bank;
public class DataBase {
//定义一个数组存储管理员信息
private Admin [] admins = new Admin[3];
//定义一个数组存储用户信息
private Customer [] customers = new Customer[10];
//定义获取管理Admin信息的get/set方法
public Admin[] getAdmins() {
return admins;
}
public void setAdmins(Admin[] admins) {
this.admins = admins;
}
//定义获取用户Customer信息的get/set方法
public Customer[] getCustomers() {
return customers;
}
public void setCustomers(Customer[] customers) {
this.customers = customers;
}
//模拟存储的管理员信息
public Admin[] getAdmin() {
for(int x = 1 ; x <= admins.length ; x++) {
admins[x - 1]= new Admin("admin0"+x,"abc"+x);
}
return admins;
}
//模拟存储的用户信息
public Customer[] getCostomer() {
for(int x = 1 ; x <= customers.length ; x++) {
customers[x - 1]= new Customer("customerName0"+x,"user0"+x,"abcde"+x,10000,"11122233"+x);
}
return customers;
}
}