用java实现一个简单简洁的银行管理系统
java实现银行管理系统
银行类
package 银行管理系统;
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;
}
}
接下来创建一个用户类
package 银行管理系统;
public class User {
private String name;//用户
private String account;//用户账号
private String password;//用户密码
private double balance; //用户余额
//用户有参构造
public User(String name, String account, String password, double balance) {
super();
this.name = name;
this.account = account;
this.password = password;
this.balance = balance;
}
public User() {
// TODO Auto-generated constructor stub
}
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;
}
}
银行管理系统主界面
package 银行管理系统;
import java.util.ArrayList;
import java.util.Scanner;
public class BankTest {
public static void main(String[] args) {
Bank bk = new Bank ("中国银行");
User u = new User();
ArrayList<User> array = new ArrayList<User>();
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("请输入您的选择:");
Scanner sc = new Scanner(System.in);
String choiceString = sc.nextLine();
switch(choiceString) {
case"1":
FindAllUser(array);//查看用户信息
break;
case"2":
addUser(array);//注册用户
break;
case"3":
dengluUser(array);//用户登录
break;
case"4":
addMoney(array);//存钱
break;
case"5":
updateUser(array);//修改个人信息
break;
case"6":
default:
System.out.println("谢谢你的使用");
System.exit(0);
break; //退出
}
}
}
private static void dengluUser(ArrayList<User> array) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
System.out.println("请输入账号");
String name=sc.nextLine();
System.out.println("请输入密码");
String password=sc.nextLine();
for(int i=0;i<array.size();i++) {
User u=array.get(i);
if(u.getName().equals(name)&&u.getPassword().equals(password)) {
System.out.println("登陆成功");
addUser(array);
}else {
System.out.println("您的账号/密码输入有误");
break;
}
}
}
private static void updateUser(ArrayList<User> array) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
System.out.println("请输入你要修改的用户账号");
String account = sc.nextLine();
int index = -1;
for(int x=0;x<array.size();x++) {
User u = array.get(x);
if(u.getAccount().equals(account)) {
index = x;
break;
}
}
if (index == -1) {
System.out.println("不好意西,你要修改的用户账号信息不存在,请回去重新你的选择");
}else {
System.out.println("请输入新账号:");
String account1 = sc.next();
System.out.println("请输入新密码:");
String password = sc.next();
System.out.println("请输入新名字:");
String name = sc.next();
User user = array.get(index);
//方法2
// user.setAccount(account1);
// user.setPassword(password);
// user.setName(name);
User u = new User();
u.setAccount(account1);
u.setPassword(password);
u.setName(name);
u.setBalance(user.getBalance());
array.set(index, u);
System.out.println("修改账户成功");
}
}
private static void addMoney(ArrayList<User> array) {
// TODO Auto-generated method stub
double sum =0;
System.out.println("欢迎进入存款页面,请输入存款的金额");
while(true){
Scanner sc = new Scanner(System.in);
System.out.println("请输入你要存入的金额:");
double money=sc.nextInt();
Scanner s1 = new Scanner(System.in);
System.out.println("请输入你要存入的账号:");
String account=s1.nextLine();
//存款总数
for(int i=0;i<array.size();i++) {
User s=array.get(i);
if(s.getAccount().equals(account)) {
double balance =money+s.getBalance();
s.setBalance(balance);
System.out.println("存款成功");
System.out.println("你存入了"+money+"元"+"现在共有存款"+balance);
}else {
System.out.println("存款失败");
}
}
System.out.println("继续请按1,退出请按0");
int a=sc.nextInt();
if(a==0){
break;
}else{
continue;
}
}
}
public static void addUser(ArrayList<User> array) {
//创建键盘录入对象
Scanner sc = new Scanner(System.in);
String account;
while(true) {
System.out.println("请输入账号:");
account=sc.next();
boolean flag = false;
for(int x=0;x<array.size();x++) {
User u = array.get(x);
if(u.getAccount().equals(account)) {
flag = true;
break;
}
}
if(flag) {
System.out.println("你输入的账号已经被占用,请重新输入");
}else {
break;
}
}
System.out.println("请输入密码");
String password = sc.next();
System.out.println("请输入姓名");
String name = sc.next();
User u =new User();
u.setAccount(account);
u.setPassword(password);
u.setName(name);
array.add(u);
System.out.println("恭喜你注册成功");
}
private static void FindAllUser(ArrayList<User> array) {
// TODO Auto-generated method stub
if(array.size() == 0) {
System.out.println("不好意思,目前没有用户的信息,请重新操作");
return;
}
System.out.println("账号\t\t密码\t姓名\t余额");
for(int x = 0; x<array.size();x++) {
User u = array.get(x);
System.out.println(u.getAccount()+"\t\t"+u.getPassword()+"\t"+u.getName()+"\t"+u.getBalance());
}
}
}