黑马程序员技术交流社区
标题:
【成都校区】java实现银行管理系统
[打印本页]
作者:
小刀葛小伦
时间:
2019-12-26 17:47
标题:
【成都校区】java实现银行管理系统
Bank类
package First;
import java.util.TreeSet;
//银行类
public class Bank {
private String Bankname = "坑对对银行"; //银行名称
TreeSet<User> holder = null; //银行存储的信息
private static Bank INSTANCE = new Bank();
private Bank() {} //私有化银行类
public static Bank getInstance() {
// TODO Auto-generated method stub
return INSTANCE;
}
//存款功能
public void saveMoney(User user,double money) {
//初始金额与存入金额相加
double sum = user.getSum()+money;
user.setSum(sum);
System.out.print(money+"存款成功!!!"+",当前余额是:"+user.getSum());
System.out.print("\n");
}
//取款功能
public void drawMoney(User user,double money,String pw) {
if(user.getPw().equals(pw)) { //先判断取款密码
if(user.getSum()>=money) { //判断余额
user.setSum(user.getSum()-money);
System.out.print("取款成功");
System.out.print("\n");
System.out.print("当前余额为:"+"\t"+user.getSum());
System.out.print("\n");
}else {
System.out.print("抱歉余额不足!!! 当前余额:"+user.getSum());
System.out.print("\n");
}
}
}
//查看余额以及信息
public void looksum(User user) {
System.out.print("姓名:"+user.getName()+",账号:"+user.getAccount()+",余额:"+user.getSum());
System.out.print("\n");
}
//欢迎语句
public void welcom(User user) {
if(user.getSexual().equals("男")) {
System.out.print(user.getName()+" 先生!!"+" "+(this.Bankname)+" 欢迎您!!!");
System.out.print("\n");
}else if(user.getSexual().equals("女")){
System.out.print(user.getName()+" 女士!!"+" "+(this.Bankname)+" 欢迎您!!!");
System.out.print("\n");
}
}
//再见语句
public void GoodBye(User user) {
if(user.getSexual().equals("男")) {
System.out.print(user.getName()+" 先生!!"+" "+(this.Bankname)+" 祝您生活愉快!!!");
System.out.print("\n");
}else if(user.getSexual().equals("女")){
System.out.print(user.getName()+" 女士!!"+" "+(this.Bankname)+" 祝您生活愉快!!!");
System.out.print("\n");
}
}
}
Data:数据存储类
package First;
import java.util.TreeSet;
//模拟一个数据库
//银行的数据库
public class Data {
TreeSet<User> holder = new TreeSet<User>(new MyComparator()); //用于存储用户信息的容器---自定义比较规则
File file = File.getFile(); //获取一个文件类
private static Data data = new Data();
//单例模式--因为存储数据的类唯一.所以不能随意被实例化
private Data() {} //私有化构造方法
public static Data getData() { //提供返回该对象的静态方法
return data;
}
//添加用户
public void addUser(User user) {
holder.add(user);
System.out.print("注册成功!!");
System.out.print("\n");
}
//删除用户
public void removeUser(User user) {
if(holder.contains(user)) { //如果容器中包含所user对象,则移除对象
holder.remove(user);
}else { //如果不包含则打印语句
System.out.print("抱歉请检查输入的信息是否正确");
System.out.print("\n");
}
}
//获取容器
public TreeSet<User> getHolder() {
return holder;
}
//写入容器
public void setHolder(TreeSet<User> holder) {
this.holder = holder;
}
}
FIie:文件IO类
package First;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Iterator;
import java.util.TreeSet;
/*
* 文件类--负责向内存中初始化信息以及程序结束后向内存中存入信息
* */
public class File {
TreeSet<User> holder = Data.getData().getHolder(); //从Data数据库中获取容器
//文件类只供使用不能被任意实际化
public static File FILE; //私有化一个File类
private File() {};
public static File getFile() { //对外界提供获取方法
return FILE;
}
//保存到文件里
public static void SaveToFile(TreeSet<User> list) { //将获取到的容器存入到文件中----序列化
ObjectOutputStream oo = null;
try {
oo = new ObjectOutputStream(new FileOutputStream(".\\src\\Test\\text1.txt")); //获取文件存储地址
oo.writeObject(list); //将容器写入
} catch (Exception ex) {
ex.printStackTrace();
}finally {
try {
oo.close(); //将流关闭
}catch(IOException e) {
e.printStackTrace();
}
}
}
//从文件里读取
public static TreeSet<User> ReadFromFile() {
ObjectInputStream ois = null;
TreeSet<User> list = null;
try {
ois = new ObjectInputStream(new FileInputStream(".\\src\\Test\\text1.txt"));//反序列化从文件中获取容器加载到内存
list = Data.getData().getHolder(); //获取容器
list = (TreeSet<User>) ois.readObject(); //从流中获取对象
Iterator it = list.iterator(); //迭代器
System.out.print("当前用户信息:");
System.out.print("\n");
System.out.print("\n");
while(it.hasNext()) { //迭代初始信息
User user = (User) it.next();
System.out.print("\t"+"姓名:"+user.getName()+"\t"+"账号:"+user.getAccount()+"\t"+"密码:"+user.getPassword()+"\t"+"余额:"+user.getSum()+"\t");
System.out.print("\n");
System.out.print("\n");
}
} catch (Exception ex) {
ex.printStackTrace();
}finally {
try {
ois.close(); //将流关闭
}catch(IOException e){
e.printStackTrace();
}
}
return list;
}
}
MyComparator:自定义比较器类
package First;
import java.io.Serializable;
import java.util.Comparator;
//自定义比较器
//想要存入集合必须实现序列化和反序列化
public class MyComparator implements Comparator,Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public int compare(Object arg0, Object arg1) {
User user1 = (User)arg0; //重写比较方法 --- 强转成User类型
User user2 = (User)arg1;
return user1.getAccount().compareTo(user2.getAccount());//比较两个银行账户
}
}
Operate:系统核心
package First;
import java.util.Iterator;
import java.util.Scanner;
import java.util.TreeSet;
//后台操作系统
//后台管理员
public class Operate {
Scanner sc = new Scanner(System.in); //键盘录入
TreeSet<User> holder = Data.getData().getHolder(); //从Data数据库中获取容器
Bank bank = Bank.getInstance(); //获取一个银行类
File file = File.getFile(); //获取一个文件类
//登录菜单
public void registerMenu(){
System.out.print("****************************");
System.out.print("\n");
System.out.print("\n");
System.out.print(" 欢迎来到坑多多银行登录系统!!!\n");
System.out.print("\n");
System.out.print("****************************");
System.out.print("\n");
System.out.print("\n");
holder = File.ReadFromFile(); //从文件中获取容器
Data.getData().setHolder(holder); //把从文件中获取容器存储到数据库中
int str=1; //从键盘获取数字
while(str!=0) {
System.out.print("****************************");
System.out.print("\n");
System.out.print(" 1: 登录 ");
System.out.print("\n");
System.out.print(" 2: 注册 ");
System.out.print("\n");
System.out.print(" 3: 忘记密码");
System.out.print("\n");
System.out.print(" 4: 注销用户");
System.out.print("\n");
System.out.print(" 0: 退出");
System.out.print("\n");
System.out.print("****************************");
System.out.print("\n");
System.out.print("请输入:");
str = sc.nextInt();
switch(str){ //根据用户输入的值进行选择
case 1:
System.out.print("请输入您的账号:");
String account = sc.next(); //从键盘中获取录入用户名
System.out.print("请输入您的密码:");
String pw = sc.next(); //从键盘中录入密码
User user = logon(holder,account,pw);
if(user!=null) { //判断是否登录成功
System.out.print("登录成功!!!!欢迎使用");
System.out.print("\n");
System.out.print("正在跳转到银行操作界面.........");
System.out.print("\n");
this.BankMenu(user); //如果登录成功则将user对象传入到银行操作界面并跳转
}else {
System.out.print("登录失败!!!请从新输入"); //如果登录失败打印此语句
System.out.print("\n");
}
break;
case 2:
this.logon(); //调用注册方法
break;
case 3:
this.modify(); //调用修改方法
break;
case 4:
holder = logout(holder); //调用删除操作
break;
case 0:
File.SaveToFile(holder); //退出系统式将信息保存到文件中
System.out.print("再见!!!!");
System.out.print("\n");
default:
System.out.print("输入错误!!!!");
System.out.print("\n");
}
}
}
//登录方法
public User logon(TreeSet<User> holder,String account,String pw) {
Iterator<User> it = holder.iterator(); //获取迭代器
while(it.hasNext()) {
User user = it.next();
if(user.getAccount().equals(account)&&user.getPassword().equals(pw)){
System.out.print("登录成功!!");
System.out.print("\n");
return user;
}
}
System.out.print("账户或密码错误 登录失败!!");
System.out.print("\n");
return null;
}
//注册方法体
public void logon(){
System.out.print("欢迎来到注册界面!!!!");
System.out.print("\n");
String name; //用户名
String account; //银行账号
String password; //密码
String pw; //取款密码
double sum; //账户金额
String sexual; //性别
User user = new User(); //先创建一个空参的user对象
boolean flag = true; //语句的开关
while(flag) { //将注册的信息依次输入
switch(1) {
case 1:
System.out.print("请输入姓名:"); //依次从键盘中获取对应的姓名----正则表达式
name = sc.next();
user.setName(name); //将名字写入对象中
case 2:
System.out.print("请输入账号(9位数字):");
account = sc.next(); //从键盘中获取账号
String reges ="[0-9]{9}"; //正则表达式规则对账号进行限制
if(!account.matches(reges)) { //如果不符合要求则报错
System.out.print("账号不符合规范!!");
System.out.print("\n");
System.out.print("请重新注册");
System.out.print("\n");
break;
}
user.setAccount(account); //将账号写入对象中
case 3:
System.out.print("请输入密码(6位):");
password = sc.next();
reges ="[0-9]{6}"; //正则表达式规则
if(!password.matches(reges)){ //如果密码不符合正则规则则报错
System.out.print("密码不符合规范!!");
System.out.print("\n");
System.out.print("请重新注册");
System.out.print("\n");
break;
}
user.setPassword(password); //将密码写入对象中
case 4:
System.out.print("请输入取款密码(6位):");
pw = sc.next();
reges ="[0-9]{6}"; //正则表达式规则
if(!pw.matches(reges)) {
System.out.print("取款密码不符合规范!!");
System.out.print("\n");
System.out.print("请重新注册");
System.out.print("\n");
break;
}
user.setPw(pw); //将取款密码写入到对象中
case 5:
System.out.print("请输入金额:");
sum = sc.nextDouble();
user.setSum(sum); //将金额写入到对象中
case 6:
System.out.print("请输入性别:");
sexual = sc.next();
user.setSexual(sexual); //将性别写入到对象中
case 7:
if(!holder.contains(user)){
Data.getData().addUser(user); //一切准确无误后将user存入数据库Data中
}
else {
System.out.print("用户存在!!!");
System.out.print("\n");
//结束循环
}
flag = false;
break;
}
}
}
//忘记密码操作
public void modify() {
boolean flag = true;
Iterator<User> it = holder.iterator(); //获取迭代器
System.out.print("请输入账户:");
String account = sc.next(); //从键盘中获取账户
while(it.hasNext()) {
User user = it.next();
if(user.getAccount().equals(account)){ //如果容器中有这个对象则对它进行修改
System.out.print("请输入新账户的密码:");
String pw = sc.next(); //从监盘上录入新的密码
String reges ="[0-9]{6}"; //正则表达式规则
if(!pw.matches(reges)) { //如果密码不符合正则规则则报错
System.out.print("密码不符合规范!!");
System.out.print("\n");
}else { //正则表达式判断是否符合条件
holder.remove(user); //先从原容器中删除user
user.setPassword(pw); //修改user的属性
holder.add(user); //然后再向容器中添加
System.out.print("修改成功!!!");
System.out.print("\n");
flag = false;
break;
}
}
}
if(flag) {
System.out.print("没有找到该用户");
System.out.print("\n");
}
}
//注销方法
public TreeSet<User> logout(TreeSet<User> list) {
String username; //用户名
String password; //密码
System.out.print("请输入用户账号:");
username = sc.next(); //从键盘录入用户名
System.out.print("请输入密码:");
password = sc.next(); //从键盘录入密码
Iterator it = list.iterator(); //获取迭代器
while(it.hasNext()) {
User user = (User)it.next(); //获取对象
if(user.getAccount().equals(username)&&user.getPassword().equals(password)){
list.remove(user); //如果录入的信息匹配则从容器中删除user对象
System.out.print("注销成功!!");
System.out.print("\n");
return list;
}
}
System.out.print("账号或者密码错误"); //不匹配打印提示语句
System.out.print("\n");
return list;
}
//银行操作界面
public void BankMenu(User user) {
bank.welcom(user); //银行调用欢迎语句
System.out.print("****************************");
System.out.print("\n");
System.out.print("\n");
System.out.print(" 欢迎来到坑多多银行擦操作系统!!!\n");
System.out.print("\n");
int str = 1;
while(str!=0) {
System.out.print("****************************");
System.out.print("\n");
System.out.print(" 1: 取钱 ");
System.out.print("\n");
System.out.print(" 2: 存钱 ");
System.out.print("\n");
System.out.print(" 3: 查看余额");
System.out.print("\n");
System.out.print(" 0: 退出登录");
System.out.print("\n");
System.out.print("****************************");
System.out.print("\n");
System.out.print("\n");
System.out.print("请输入您的选项:"); //从键盘中读取字符串
str = sc.nextInt();
switch(str){ //根据用户输入的值进行选择
case 1: //取钱选项
System.out.print("请输入取款密码:");
String pw = sc.next(); //从键盘获取输入的密码
if(user.getPw().equals(pw)) { //将输入的取款密码与用户的密码进行比对:如果相同则继续执行
System.out.print("请输入取款金额:");
double money = sc.nextDouble(); //从键盘获取输入的金额
if(money>=0) { //判断金额是否有误
bank.drawMoney(user, money, pw); //调用取款方法
}else {
System.out.print("输入金额错误!!!");
System.out.print("\n");
}
}else {
System.out.print("输入密码有误!!!");
System.out.print("\n");
}
break;
case 2:
System.out.print("请输入取款金额:");
System.out.print("\n");
double money1 = sc.nextDouble(); //从键盘获取输入的金额
if(money1>=0) { //判断金额是否有误
bank.saveMoney(user, money1);; //调用存款方法
}else {
System.out.print("输入金额错误!!!");
System.out.print("\n");
}
break;
case 3:
bank.looksum(user); //调用银行---查询账户操作
break;
case 0:
bank.GoodBye(user); //调用结束语句
break;
default:
System.out.print("输入错误");
System.out.print("\n");
}
}
}
}
User类:
package First;
import java.io.Serializable;
/*
* 这是一个User类
* */
public class User implements Serializable{
/**
* 实现接口,用于序列化和反序列化
*/
private static final long serialVersionUID = 1L;
private String name; //用户名
private String account; //银行账号
private String password; //密码
private String pw; //取款密码
private double sum; //账户金额
private String sexual; //性别
//获取用户名
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 getSum() {
return sum;
}
//修改余额
public void setSum(double sum) {
this.sum = sum;
}
//写入性别
public String getSexual() {
return sexual;
}
//修改性别
public void setSexual(String sexual) {
this.sexual = sexual;
}
//获取取款密码
public String getPw() {
return pw;
}
//写入取款密码
public void setPw(String pw) {
this.pw = pw;
}
//hashCode比较方法
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((account == null) ? 0 : account.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((password == null) ? 0 : password.hashCode());
result = prime * result + ((pw == null) ? 0 : pw.hashCode());
result = prime * result + ((sexual == null) ? 0 : sexual.hashCode());
long temp;
temp = Double.doubleToLongBits(sum);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}
//equals比较方法
/*
* 知识点:
* public boolean equals(Object obj)名称不可改变
* */
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
User other = (User) obj;
if (account == null) {
if (other.account != null)
return false;
} else if (!account.equals(other.account))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (password == null) {
if (other.password != null)
return false;
} else if (!password.equals(other.password))
return false;
if (pw == null) {
if (other.pw != null)
return false;
} else if (!pw.equals(other.pw))
return false;
if (sexual == null) {
if (other.sexual != null)
return false;
} else if (!sexual.equals(other.sexual))
return false;
if (Double.doubleToLongBits(sum) != Double.doubleToLongBits(other.sum))
return false;
return true;
}
//初始化用户
User(String name,String account,String password,String pw,double sum,String sexual){
this.name = name;
this.account = account;
this.password = password;
this.sum = sum;
this.sexual = sexual;
this.pw = pw;
}
//空参构造函数
User(){
}
}
Main()类
package First;
public class Test {
//测试类----唯一的主函数
public static void main(String[] args) {
Operate operate = new Operate();
operate.registerMenu(); //调用登录方法
}
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2