import java.io.*; import java.util.HashMap; import java.util.Iterator; public class UserSystem { //数据导入到集合中操作 private HashMap<String,User> userBox = new HashMap<String,User>(); { File file = null; FileReader fr = null; BufferedReader br= null; try { file = new File("src\bank_exercise\UserData.txt"); fr = new FileReader(file); br = new BufferedReader(fr); String value = br.readLine(); //获取用户文件的信息 while (value!=null){ String [] valueUser = value.split("-"); User user = new User(valueUser[0],valueUser[1],Float.parseFloat(valueUser[2])); userBox.put(user.getUsername(),user); value = br.readLine(); } } catch (IOException e) { e.printStackTrace(); }finally { try { if(fr!=null){ fr.close(); } } catch (IOException e) { e.printStackTrace(); } try { if(br!=null){ br.close(); } } catch (IOException e) { e.printStackTrace(); } } } //集合存入数据文件中 public void commit(HashMap<String,User> userBox){ File file = null; FileWriter fw = null; BufferedWriter bw= null; try { file = new File("src\bank_exercise\UserData.txt"); fw = new FileWriter(file); bw = new BufferedWriter(fw); Iterator <String> iterator = userBox.keySet().iterator(); while (iterator.hasNext()){ String name = iterator.next();//获取名字 User user = userBox.get(name); //使用StringBuilder拼接,存入文件 StringBuilder stringBuilder = new StringBuilder(user.getUsername()); stringBuilder.append("-"); stringBuilder.append(user.getApassword()); stringBuilder.append("-"); stringBuilder.append(user.getAbalance()); bw.write(stringBuilder.toString());//将其转为string类型 bw.newLine();//换行 bw.flush(); } } catch (IOException e) { e.printStackTrace(); }finally { try { if(fw!=null){ fw.close(); } } catch (IOException e) { e.printStackTrace(); } try { if(bw!=null){ bw.close(); } } catch (IOException e) { e.printStackTrace(); } } } //登录 public String login(String username,String password){ User user = userBox.get(username);//获取用户名与密码 //如果为空,则证明 用户不存在 if(user!=null && user.getApassword().equals(password)){ return "登录成功"; } return "登录失败"; } //查看余额 public Float lookOver(String username){ User user = userBox.get(username); return user.getAbalance(); } //存入 deposit public void deposit(String username,Float money){ User user = userBox.get(username); user.setAbalance(user.getAbalance()+money); userBox.put(username,user); this.commit(userBox); } //取出 takeOut public void takeOut(String username,Float money){ User user = userBox.get(username); if(user.getAbalance()>money){ user.setAbalance(user.getAbalance()-money); userBox.put(username,user); this.commit(userBox); }else{ System.out.println("余额不足!"); } } //转账 public void transfer(String username,String username2,Float money){ User user = userBox.get(username); User newUser = userBox.get(username2); if(user.getAbalance()>money){ if(newUser!=null){ user.setAbalance(user.getAbalance()-money); userBox.put(username,user); newUser.setAbalance(user.getAbalance()+money); userBox.put(username2,newUser); this.commit(userBox); }else { System.out.println("您输入的账户不存在"); } }else{ System.out.println("余额不足"); } } } //创建的User类型 public class User { private String ausername; //用户名 private String apassword;//密码 private Float abalance;//余额 public User(){} public User(String ausername,String apassword,Float abalance){ this.abalance = abalance; this.apassword = apassword; this.ausername = ausername; } public String getUsername() { return ausername; } public void setUsername(String aname) { this.ausername = aname; } public String getApassword() { return apassword; } public void setApassword(String apassword) { this.apassword = apassword; } public Float getAbalance() { return abalance; } public void setAbalance(Float abalance) { this.abalance = abalance; } } import java.util.Scanner; public class Test { public static void main(String[] args){ UserSystem user = new UserSystem(); Scanner input = new Scanner(System.in); System.out.println("欢迎进入银行系统!"); System.out.println("请输入账号:"); String username = input.nextLine(); System.out.println("请输入密码:"); String password = input.nextLine(); String value = user.login(username,password); if(value.equals("登录成功")){ System.out.println("请输入要执行的操作: 1.存钱 2.取钱 3.转账 4.退出 "); String xuanxiang = input.nextLine(); UserSystem userSystem = new UserSystem(); switch (xuanxiang){ case "1": System.out.println("请输入存款金额:"); Float money = input.nextFloat(); userSystem.deposit(username,money); break; case "2": System.out.println("请输入取款金额:"); Float money1 = input.nextFloat(); userSystem.takeOut(username,money1); break; } }else { System.out.println("登录失败"); } } }