java基于对象流实现银行系统的具体代码,供大家参考,具体内容如下
系统特点:
- 数据持久化到文件中,系统启动后,加载文件中数据到集合中,相当于做了一个缓存。
- 文件读写使用的是对象流(objectinputstream和objectoutputstream),用户是看不懂那些文件的,一定程度上保证了密码和余额的安全性。
- 采用了mvc分层设计思想,虽然没有c控制层和v视图层,但是有完备的m模型层,数据处理service,数据读写dao,数据存储pojo(每一层只负责处理自己的事情,层次内部的改动不会影响其他层次)。
- 虽然有些细节问题由于时间关系没有处理好,比如没有处理输入类型不匹配的异常,但是绝大多数功能都合理得实现了。
全部代码
pojo:
public class user implements serializable { private static final long serialversionuid = -7019656536540026883l; private string username;//用户名 private string password;//密码 private double balance;//余额 public user(string username, string password, double balance) { this.username = username; this.password = password; this.balance = balance; } public user() { } public string getusername() { return username; } public void setusername(string username) { this.username = username; } 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 tostring(){ stringbuilder builder = new stringbuilder("{"); builder.append(this.username).append(",").append(this.password).append(",").append(this.balance).append("}"); return builder.tostring(); }
dao层:
public class atmdao { private string userfilepath = "src\\usermsg.txt";//相对路径 private hashmap<string, user> usermsg; { usermsg = this.loadfile();//读取文件信息 } public hashmap<string, user> getusermsg() { return usermsg; } //查询用户 public user selectuser(string username){ return usermsg.get(username); } //更新用户信息 public void updateuser(user user){ this.usermsg.replace(user.getusername(),user); this.updatefile(usermsg); } //增加用户 public void adduser(user user){ this.usermsg.put(user.getusername(),user); this.updatefile(usermsg); } //删除用户 public void deleteuser(user user){ this.usermsg.remove(user.getusername()); this.updatefile(usermsg); } //只在初始化时时调用一次 /* public void initfile(){ user user1 = new user("张三","123",800.0); user user2 = new user("李四","666",70.0); user user3 = new user("王五","888",555.0); usermsg.put(user1.getusername(),user1); usermsg.put(user2.getusername(),user2); usermsg.put(user3.getusername(),user3); this.updatefile(usermsg); }*/ private hashmap<string, user> loadfile(){ file file = new file(userfilepath); fileinputstream fis = null; objectinputstream ois = null; hashmap<string, user> box = null; try { fis = new fileinputstream(file); ois = new objectinputstream(fis); try { box = (hashmap<string, user>)ois.readobject(); } catch (classnotfoundexception e) { e.printstacktrace(); } } catch (ioexception e) { e.printstacktrace(); }finally { try { if(fis!=null) fis.close(); } catch (ioexception e) { e.printstacktrace(); } try { if(ois!=null) ois.close(); } catch (ioexception e) { e.printstacktrace(); } } return box; } private void updatefile(hashmap<string,user> usermsg){ file file = new file(userfilepath); fileoutputstream fos = null; objectoutputstream oos = null; try { fos = new fileoutputstream(file); oos = new objectoutputstream(fos); oos.writeobject(usermsg); oos.flush(); } catch (ioexception e) { e.printstacktrace(); }finally { try { if(fos!=null) fos.close(); } catch (ioexception e) { e.printstacktrace(); } try { if(oos!=null) oos.close(); } catch (ioexception e) { e.printstacktrace(); } } } }
service层:
public class atmservice { atmdao dao = new atmdao(); public boolean login(string username,string password){ user user = dao.selectuser(username); if (user!=null && user.getpassword().equals(password)) return true; else return false; } //查询余额 public double querybalance(string name){ user user = dao.selectuser(name); return user.getbalance(); } //存款 public void deposit(string name,double account){ user user = dao.selectuser(name); user.setbalance(user.getbalance()+account); dao.updateuser(user); } //取款 public void withdrawal(string name,double account){ user user = dao.selectuser(name); if (user.getbalance()>=account) { user.setbalance(user.getbalance() - account); dao.updateuser(user); }else { system.err.println("对不起,"+user.getusername()+",您的余额不足"); } } //转账 public void transfer(string oldname,string newname,double account){ user olduser = dao.selectuser(oldname); user newuser = dao.selectuser(newname); if (newuser!=null){ if (olduser.getbalance()>=account){ olduser.setbalance(olduser.getbalance()-account); newuser.setbalance(newuser.getbalance()+account); dao.updateuser(olduser); dao.updateuser(newuser); }else { system.err.println("对不起,"+olduser.getusername()+",您的余额不足"); } }else { system.err.println("对不起,您输入的账号不存在"); } } //销户 public void closeaccount(string name){ user u = dao.selectuser(name); dao.deleteuser(u); } //注册 public void register(string name,string password){ user user = dao.selectuser(name); if (user!=null){ system.err.println("对不起,您输入的账号已存在"); }else { user = new user(name,password,0.0); dao.adduser(user); } } }
测试:
public class test { public static void main(string[] args) { atmservice service = new atmservice(); scanner input = new scanner(system.in); system.out.println("欢迎您使用银行自助服务系统\n请输入您的账号:"); string username = input.nextline(); system.out.println("请输入您的密码:"); string password = input.nextline(); if (service.login(username,password)){ system.out.println("登录成功!"); while(true) { system.out.println("请选择您的操作:\n0.注册\n1.查询余额\n2.存款\n3.取款\n4.转账\n5.销户\n6.退出"); string option = input.nextline(); switch (option) { case "0": system.out.println("请输入您的用户名:"); string newname = input.nextline(); system.out.println("请输入您的密码:"); string newpass = input.nextline(); service.register(newname, newpass); system.out.println("恭喜您,注册成功!,您的用户名为" + newname); break; case "1": double balance = service.querybalance(username); system.out.println("您的余额为" + balance); break; case "2": system.out.println("请输入存款金额:"); double amount = input.nextdouble(); input.nextline(); service.deposit(username, amount); system.out.println("恭喜您,存款成功"); break; case "3": system.out.println("请输入取款金额:"); double d = input.nextdouble(); input.nextline(); service.withdrawal(username, d); system.out.println("恭喜您,取款成功"); break; case "4": system.out.println("请输入对方账户:"); string anothername = input.nextline(); system.out.println("请输入转账金额:"); double t = input.nextdouble(); input.nextline(); service.transfer(username, anothername, t); system.out.println("恭喜您,转账成功"); break; case "5": service.closeaccount(username); system.out.println("销户成功"); break; case "6": system.exit(0); } } }else { system.err.println("用户名或密码错误"); } //dao.initfile(); /*hashmap<string, user> usermsg = dao.getusermsg(); iterator<string> iterator = usermsg.keyset().iterator(); while (iterator.hasnext()){ string username = iterator.next(); user user = usermsg.get(username); system.out.println(user); }*/ } }
部分测试截图
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。