美文网首页
11,银行卡

11,银行卡

作者: 富尔察纯生 | 来源:发表于2017-12-29 15:07 被阅读0次

    任务目的

    银行卡
    综合练习题

    public class Mian {
        public static void main(String[] args) {
        // write your code here
    //        UserCard userCard = new UserCard("001","123","zhangsan", 10);
    //        UserCard userCard2 = new UserCard("002","123","lisi", 10);
    
        ATM atm = new ATM();
        atm.print();
    
        UserCard userCard = new UserCard("001","123","zhangsan", 10);
        UserCard userCard2 = new UserCard("002","123","lisi", 10);
        atm.addUserCard(userCard);
        atm.addUserCard(userCard2);
    
        //用户输入卡号
        atm.inputCard();
    
    }
    
    
    
    public class UserCard {
         private String cardNo;//卡号
        private String password;//密码
        private String name;//户名
        private double money;//账户余额
    
    
        public String toString()
        {
            return "卡号是:"+cardNo+"户名是:"+name + "余额是:"+money;
        }
    
        //存款
        public double deposit(double money)
        {
            this.money += money;
            return this.money;
        }
    
        //取款
        public double withdraw(double money)
        {
            //判断余额
            if(money > this.money)
            {
                return -1;
            }
            this.money -= money;
            return this.money;
        }
    
        //构造方法
        public UserCard(String cardNo, String password,String name, double money)
        {
            this.money = money;
            this.cardNo = cardNo;
            this.name = name;
            this.password = password;
        }
    
        public String getCardNo() {
            return cardNo;
        }
    
        public void setCardNo(String cardNo) {
            this.cardNo = cardNo;
        }
    
        public String getPassword() {
            return password;
        }
        //修改密码
        public void setPassword(String password) {
            this.password = password;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public double getMoney() {
            return money;
        }
    
        public void setMoney(double money) {
            this.money = money;
        }
    
    }
    
    
    public class ATM {
        //当前插入的卡
        private UserCard currentUserCard = null;
    
        private int index = 0;//数组的索引,下一个应该放到数组中的位置
        //ATM机可以识别的卡的集合
        private UserCard[] userCards = new UserCard[100];
    
        //向ATM中添加可以识别的卡片信息(让ATM机认这张卡)
        public void addUserCard(UserCard userCard)
        {
            if(index < userCards.length)
            {
                userCards[index] = userCard;
                index++;
            }
    
        }
    
        //输出atm首页界面
        public void print()
        {
            Scanner scanner = new Scanner(System.in);
            while (true)
            {
                System.out.println("----------------------");
                System.out.println("欢迎使用睿道java25出品的模拟ATM程序.");
                System.out.println("----------------------");
    
    
                System.out.println(" 1.>取款.");
                System.out.println(" 2.>存款.");
                System.out.println("3.>查询信息.");
                System.out.println("4.>退出系统.");
                int command = scanner.nextInt();
                switch (command)
                {
                    case 1:
                    {
                        System.out.println("请输入取款金额");
                        double value = scanner.nextDouble();
                        double result = currentUserCard.withdraw(value);
                        if(result == -1)
                        {
                            System.out.println("余额不足");
                        }
                        else
                        {
                            System.out.println("取款成功");
                            System.out.println("卡内余额为" + result);
                        }
                        break;
                    }
                    case 2:
                    {
                        System.out.println("请输入存款金额");
                        double value = scanner.nextDouble();
                        double result = currentUserCard.deposit(value);
    
                        System.out.println("存款成功");
                        System.out.println("卡内余额为" + result);
    
                        break;
                    }
                    case 3:
                    {
                        System.out.println(currentUserCard);
                        break;
                    }
                    case 4:
                    {
                        System.out.println("程序退出");
                        return;
                    }
                    default:
                    {
                        break;
                    }
    
                }
    
            }
    
    
        }
    
    
        //插卡
        public void inputCard()
        {
            System.out.println("请输入您的卡号:");
            Scanner scanner = new Scanner(System.in);
            String strNo = scanner.next();
            System.out.println("请输入您的密码:");
            String password = scanner.next();
    
            currentUserCard = checkCard(strNo,password);//检查卡
            if(currentUserCard != null)
            {
                print();
            }
            else
            {
                System.out.println("账号或密码错误");
            }
        }
    
        //检查卡是否合法,如果用户名密码对,返回那张有效的卡
        public UserCard checkCard(String strNo, String password)
        {
            //遍历有效的卡的数组,检查是否有和传入用户名,密码一致的卡存在
            for(int i = 0; i < index; i++)
            {
                if(strNo.equals(userCards[i].getCardNo()) && password.equals(userCards[i].getPassword()))
                {
                    return userCards[i];
                }
            }
    
            return null;
        }
    
    
    }
    
    
    
    

    相关文章

      网友评论

          本文标题:11,银行卡

          本文链接:https://www.haomeiwen.com/subject/dakigxtx.html