ATM自动取款机

第一个类

import java.util.Scanner;

/**
 * Created by ttc on 17-12-31.
 */
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;
    }

}

第二个类

package com.company;

/**
 * Created by ttc on 17-12-31.
 */
public class Main {
    public static void main(String[] args) {
//        // write your code here
//        String strName = "陈赫、陈坤、邓超、杜淳、冯绍峰、韩庚、胡歌、何炅、黄渤、黄晓明、贾乃亮、李晨、李易峰、鹿晗、井柏然、刘烨、陆毅、孙红雷、佟大为、薛之谦、王宝强、汪峰、王俊凯、王凯、王源、魏晨、文章、吴亦凡、小沈阳、徐峥、杨洋、张翰、张杰、张艺兴、郑恺";
//        String[] arrays = strName.split("、");
//
//        ATM atm = new ATM();
//
//        for(int i = 0; i < arrays.length; i++)
//        {
//            UserCard userCard = new UserCard("00" + (i+1),"123",arrays[i], (Math.random()*100000)+1000000);
//            atm.addUserCard(userCard);
//        }
//
//        //用户输入卡号
//        atm.inputCard();
        String strName = "陈赫、陈坤、邓超、杜淳、冯绍峰、韩庚、胡歌、何炅、黄渤、黄晓明、贾乃亮、李晨、李易峰、鹿晗、井柏然、刘烨、陆毅、孙红雷、佟大为、薛之谦、王宝强、汪峰、王俊凯、王凯、王源、魏晨、文章、吴亦凡、小沈阳、徐峥、杨洋、张翰、张杰、张艺兴、郑恺";
        String[] arrays = strName.split("、");
        ATM atm = new ATM();
        for (int i = 0; i < arrays.length; i++) {
            UserCard userCard = new UserCard("00" + (i + 1), "123", arrays[i], (Math.random() * 100000) + 100000);
            atm.addUserCard(userCard);
        }
        atm.inputCard();
    }
}

第三个类

package com.company;

/**
 * Created by ttc on 17-12-31.
 */
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;
    }
}

你可能感兴趣的:(ATM自动取款机)