• Java


    //创建的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.io.*;
    import java.util.HashMap;
    import java.util.Iterator;
    
    //这个类是用来文件与集合 数据交换的
    public class UserSystem {
        //文件路径
        private String pathname =null;
        //数据导入到集合中操作
        public UserSystem(String pathname){
            this.pathname = pathname;
        }
    
        //创建一个集合 临时存储文件内容信息
        public HashMap<String,User> loadFile (){
            HashMap<String,User> userBox = new HashMap<String, User>();
            File file = null;
            FileReader fr = null;
            BufferedReader br= null;
            try {
                //创建一个输入流,读取文件内容到集合中
                  file = new File(pathname);
                 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();
                }
            }
            return userBox;
        }
    
        //将操作完的集合存入文件中
        public void commit(HashMap<String,User> userBox){
            File file = null;
            FileWriter fw = null;
            BufferedWriter bw= null;
            try {
                file = new File(pathname);
                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();
                }
            }
        }
    }
    
    
    import java.util.HashMap;
    
    public class AtmDao {
        //DAO作为一个层次---持久层   操作数据  读写数据
        private UserSystem userSystem = new UserSystem("src\bank_exercise\UserData.txt");
        private HashMap<String,User> userBox = userSystem.loadFile();
        //更新文件内容---把集合内容存入文件中
        //提供一个创建User对象的方法,更好的体现面向对象思想
        public User objectModel(String username){
            return userBox.get(username);
        }
    
        public void update(User user){
            userBox.put(user.getUsername(),user);
            userSystem.commit(userBox);
        }
    }
    
    
    //这个类用来存放业务逻辑
    public class AtmService {
    
        private AtmDao atmDao = new AtmDao();
        //登录
        public String login(String username,String password){
            User user = atmDao.objectModel(username);//获取用户名与密码
            //如果为空,则证明 用户不存在
            if(user!=null && user.getApassword().equals(password)){
                return "登录成功";
            }
            return "登录失败";
        }
    
        //查看余额
        public Float lookOver(String username){
            User user = atmDao.objectModel(username);
            return user.getAbalance();
        }
        //存入 deposit
        public void deposit(String username,Float money){
            User user = atmDao.objectModel(username);
            user.setAbalance(user.getAbalance()+money);
            atmDao.update(user);
            System.out.println("存入成功");
        }
        //取出 takeOut
        public void takeOut(String username,Float money){
            User user = atmDao.objectModel(username);
            if(user.getAbalance()>money){
                user.setAbalance(user.getAbalance()-money);
                atmDao.update(user);
                System.out.println("已取款");
            }else{
                System.out.println("余额不足!");
            }
        }
        //转账
        public void transfer(String username,String username2,Float money) {
            User user = atmDao.objectModel(username);
            User newUser = atmDao.objectModel(username2);
            if (user.getAbalance() > money) {
                if (newUser != null) {
                    user.setAbalance(user.getAbalance() - money);
                    atmDao.update(user);
                    newUser.setAbalance(user.getAbalance() + money);
                    atmDao.update(newUser);
                    System.out.println("已向"+username2+"转账"+money+"元");
                } else {
                    System.out.println("您输入的账户不存在");
                }
            } else {
                System.out.println("余额不足");
            }
        }
    }
    
    import java.util.Scanner;
    //可以参考以前的计算器代码,使其循环
    public class Test {
        public static void main(String[] args){
            AtmService atmService = new AtmService();
            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 = atmService.login(username,password);
            if(value.equals("登录成功")){
                System.out.println("登录成功");
                System.out.println("请输入要执行的操作:
    1.存钱
    2.取钱
    3.转账
    4.查看余额
    5.退出");
                String xuanxiang = input.nextLine();
                switch (xuanxiang){
                    case "1":
                        System.out.println("请输入存款金额:");
                        Float money = input.nextFloat();
                        atmService.deposit(username,money);
    
                        break;
                    case "2":
                        System.out.println("请输入取款金额:");
                        Float money1 = input.nextFloat();
                        atmService.takeOut(username,money1);
                        break;
                    case"3":
                        System.out.println("请输入您需要转款的账户:");
                        String username2 = input.nextLine();
                        System.out.println("请输入转账金额:");
                        Float money2 = input.nextFloat();
                        atmService.transfer(username,username2,money2);
                        break;
                    case "4":
                        System.out.println("您的余额为:"+atmService.lookOver(username));
                        break;
                        default:
                            System.out.println("正在退出,请收好您的物件.");
                            break;
                }
            }else {
                System.out.println("登录失败");
            }
    
        }
    }
  • 相关阅读:
    C语言I博客作业05
    C语言I博客作业04
    C语言II博客作业04
    C语言II博客作业03
    C语言II—作业02
    C语言II博客作业01
    学期总结
    C语言I博客作业08
    C语言I博客作业07
    C语言I博客作业06
  • 原文地址:https://www.cnblogs.com/yyanghang/p/11287538.html
Copyright © 2020-2023  润新知