Java基于对象流实现银行系统
短信预约 -IT技能 免费直播动态提醒
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 = "class="lazy" data-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);
}
//只在初始化时时调用一次
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();
}
}
部分测试截图
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程网。
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341