一个关于ATM的测试

实体类
1,。Student

public class Userinfo {
    private String name; //姓名
    private String account; //卡号
    private String pwd; //密码
    private double balance; //余额

    public Userinfo() {

    }

    public Userinfo(String account, String pwd) {
        this.account = account;
        this.pwd = pwd;
    }

    public Userinfo(double balance) {
        this.balance = balance;
    }

    public Userinfo(String name, String account, String pwd, double balance) {
        this.name = name;
        this.account = account;
        this.pwd = pwd;
        this.balance = balance;
    }

    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 getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }

    @Override
    public String toString() {
        return "Userinfo{" +
                "name='" + name + '\'' +
                ", account='" + account + '\'' +
                ", pwd='" + pwd + '\'' +
                ", balance=" + balance +
                '}';
    }
}

2.转账信息(Transfer)

public class Transfer {
    private String payerName;//转账人
    private String payeeName;//收账人
    private double amount;

    public Transfer() {
    }


    public Transfer(String payerName, String payeeName, double amount) {
        this.payerName = payerName;
        this.payeeName = payeeName;
        this.amount = amount;
    }

    public String getPayerName() {
        return payerName;
    }

    public void setPayerName(String payerName) {
        this.payerName = payerName;
    }

    public String getPayeeName() {
        return payeeName;
    }

    public void setPayeeName(String payeeName) {
        this.payeeName = payeeName;
    }

    public double getAmount() {
        return amount;
    }

    public void setAmount(double amount) {
        this.amount = amount;
    }
}

接口

public interface UserinfoDao {
    public Userinfo login(Userinfo userinfo, List<Userinfo> list);//登录

    public void showMon(Userinfo userinfo, List<Userinfo> list);//查询

    public void takeMon(Userinfo userinfo, List<Userinfo> list);//取款

    public void saveMon(Userinfo userinfo, List<Userinfo> list);//存钱

    public void trunMon(Userinfo userinfo, List<Userinfo> list,List<Transfer> transfer);//转账

    public void writeMon(Userinfo userinfo, List<Userinfo> list);//打印卡号信息

    public void showTransfer(List<Transfer> transfer);//查询转账信息
}

接口实现类

public class UserinfoDaoImpl implements UserinfoDao {

    @Override
    public Userinfo login(Userinfo userinfo, List<Userinfo> list) {
        Userinfo user = new Userinfo();
        for (int i = 0; i < list.size(); i++) {
            Userinfo u = list.get(i);
            if (u.getAccount().equals(userinfo.getAccount()) && u.getPwd().equals(userinfo.getPwd())) {
                user = u;
                break;
            } else {
                user = null;
            }
        }
        return user;
    }

    @Override
    public void showMon(Userinfo userinfo, List<Userinfo> list) {//查看余额
        for (Userinfo u : list) {
            if (u.getName().equals(userinfo.getName())) {
                System.out.println("余额:" + u.getBalance());
            }
        }
    }

    @Override
    public void takeMon(Userinfo userinfo, List<Userinfo> list) {
        Userinfo user = new Userinfo();//记录操作人
        int index = 0;
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getAccount().equals(userinfo.getAccount())) {
                user = list.get(i);
                index = i;
                break;
            }
        }
        Scanner sc = new Scanner(System.in);
        System.out.println("1.100 2.200 3.500 4.1000 5.2000 6.其他金额");
        String mon = sc.next();
        switch (mon) {
            case "1":
                if (user.getBalance() > 100) {
                    double n = user.getBalance() - 100.0;
                    list.get(index).setBalance(n);
                    System.out.println("取款成功,你的余额为" + list.get(index).getBalance());
                } else {
                    System.out.println("对不起你的余额不足");
                }
                break;
            case "2":
                if (user.getBalance() > 200) {
                    double n = user.getBalance() - 200.0;
                    list.get(index).setBalance(n);
                    System.out.println("取款成功,你的余额为" + list.get(index).getBalance());
                } else {
                    System.out.println("对不起你的余额不足");
                }
                break;
            case "3":
                if (user.getBalance() > 500) {
                    double n = user.getBalance() - 500.0;
                    list.get(index).setBalance(n);
                    System.out.println("取款成功,你的余额为" + list.get(index).getBalance());
                } else {
                    System.out.println("对不起你的余额不足");
                }
                break;
            case "4":
                if (user.getBalance() > 1000) {
                    double n = user.getBalance() - 1000.0;
                    list.get(index).setBalance(n);
                    System.out.println("取款成功,你的余额为" + list.get(index).getBalance());
                } else {
                    System.out.println("对不起你的余额不足");
                }
                break;
            case "5":
                if (user.getBalance() > 2000) {
                    double n = user.getBalance() - 2000.0;
                    list.get(index).setBalance(n);
                    System.out.println("取款成功,你的余额为" + list.get(index).getBalance());
                } else {
                    System.out.println("对不起你的余额不足");
                }
                break;
            case "6":
                System.out.println("请输入你要取得金额");
                double d = sc.nextDouble();
                if (user.getBalance() > d) {
                    double n = user.getBalance() - d;
                    list.get(index).setBalance(n);
                    System.out.println("取款成功,你的余额为" + list.get(index).getBalance());
                } else {
                    System.out.println("对不起你的余额不足");
                }
                break;


        }
    }

    @Override
    public void saveMon(Userinfo userinfo, List<Userinfo> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("输入你要存的钱");
        double d = sc.nextDouble();
        Userinfo user = new Userinfo();//记录操作人
        int index = 0;
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getAccount().equals(userinfo.getAccount())) {
                user = list.get(i);
                index = i;
                break;
            }
        }
        if (d > 0) {
            double n = user.getBalance() + d;
            list.get(index).setBalance(n);
            System.out.println("存钱成功,你的余额为" + list.get(index).getBalance());
        } else {
            System.out.println("输入有误请重新输入");
            saveMon(userinfo, list);
        }
    }

    /**
     * 转账
     *
     * @param userinfo
     * @param list
     */
    @Override
    public void trunMon(Userinfo userinfo, List<Userinfo> list,List<Transfer> transfer) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入对方卡号");
        String card = sc.next();
        System.out.println("请输入转账金额");
        double mon = sc.nextDouble();
        Userinfo payer = new Userinfo();//打款人
        int index1 = 0;//记录打款人的下标
        Userinfo payee = new Userinfo();//收款人
        int index2 = 0;//记录收款人的下标
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getAccount().equals(userinfo.getAccount())) {
                payer = list.get(i);
                index1 = i;

            }
            if (list.get(i).getAccount().equals(card)) {
                payee = list.get(i);
                index2 = i;

            }
        }
        if (list.get(index1).getBalance() > mon) {
            double a = payer.getBalance() - mon;
            double b = payee.getBalance() + mon;
            list.get(index1).setBalance(a);
            list.get(index2).setBalance(b);
            transfer.add(new Transfer(payer.getName(),payee.getName(),mon));
            System.out.println("打款人的余额:" + list.get(index1).getBalance());
            System.out.println("收款人的余额:" + list.get(index2).getBalance());
        } else {
            System.out.println("你的余额不足,转账失败");
        }


    }

    @Override
    public void writeMon(Userinfo userinfo, List<Userinfo> list) {
        Userinfo user=new Userinfo();
        int index=0;
        for (int i = 0; i <list.size() ; i++) {
            if(list.get(i).getAccount().equals(userinfo.getAccount())){
                user=list.get(i);
                break;
            }
        }
        System.out.println(user+"-----------------");
        String path="file.txt";//文件的目录
        File file=new File(path);
        if(file.exists()){//判断是否有该文件  有删除  没有创建
            file.delete();
        }else {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        String str="姓名:"+user.getName()+",卡号:"+user.getAccount()+",密码:"+user.getPwd()+",余额:"+user.getBalance();
        FileWriter fw=null;
        try {
            fw=new FileWriter(file);
            fw.write(str);
            fw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }



    }

    @Override
    public void showTransfer(List<Transfer> transfer) {
        if(transfer==null){
            System.out.println("你还没有转账信息");
        }else {
            for (Transfer t:transfer){
                System.out.println("转账人:"+t.getPayerName()+",  收款人:"+t.getPayeeName()+",  转账金额:"+t.getAmount());
            }
        }
    }


}

测试类

public class UserTest {
    static List<Userinfo> list=new ArrayList<>();
    static List<Transfer> transfer=new ArrayList<>();//转账信息集合

    public static void setList(){
        list.add(new Userinfo("皇甫","123","123",10000));
        list.add(new Userinfo("继展","456","456",1000));
    }

    static UserinfoDao u=new UserinfoDaoImpl();
    static Scanner sc=new Scanner(System.in);
    static boolean kai=true;

    public static void main(String[] args) {
        setList();
        System.out.println("欢迎来到甫银行");
        while (kai){
            login();
        }
    }
    /*
    登录
     */
    public static void login(){
        int num=0;//记录大于三次就不能登录
        if(num<3){
            System.out.println("请输入账号");
            String ac=sc.next();
            System.out.println("请输入密码");
            String pwd=sc.next();
            Userinfo loginUser=new Userinfo(ac,pwd);
            Userinfo login = u.login(loginUser,list);
            if(login!=null){
                System.out.println("登录成功,欢迎"+login.getName());
                Mainpape(login);


            }else{
                login();
            }
        }else {
            System.out.println("登录失败三次,禁止登录");
            num++;
        }
    }

    private static void Mainpape(Userinfo loginuser) {//登录人
        System.out.println("1.余额查询 2.取款 3.存款 4.转账 5.打印 6.退卡 7.查询转账信息");
        String n=sc.next();
        switch (n){
            case "1":
                u.showMon(loginuser,list);
                Mainpape(loginuser);//继续调回主页面
                break;
            case "2":
                u.takeMon(loginuser,list);
                Mainpape(loginuser);
                break;

            case "3":
                u.saveMon(loginuser,list);
                Mainpape(loginuser);
                break;
            case "4":
                u.trunMon(loginuser,list,transfer);
                Mainpape(loginuser);
                break;
            case "5":
                u.writeMon(loginuser,list);
                Mainpape(loginuser);
                break;
            case "6":
                System.out.println("退卡成功");
                kai=false;
                break;
            case "7":
                u.showTransfer(transfer);
                Mainpape(loginuser);
                break;
            default:

                break;
        }
    }


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值