Java:银行atm案例

注:

该案例为离线案例

该银行atm包括登录,注册功能

atm机里暂时只存储了三个用户的数据

而且有许多地方没有完善,这个案例只完成了大概三分之一

具体代码如下:

/** 银行案例的测试类(程序的起始)**/

public class TestBank {
    public static void main(String[]args){

        Bank bank = new Bank();//创建了一家银行网点
        //显示菜单
        bank.welcomeMenu();

    }
}
public class User {

    private String cardNo;//卡号
    private String identity;//身份证号
    private String username;//用户名
    private String password;//密码
    private String phone;//手机号码
    private double balance;//余额

    public User(){

    }

    public User(String cardNo , String identity , String username ,
      String password , String phone , double balance){
        this.cardNo = cardNo;
        this.identity = identity;
        this.username = username;
        this.password = password;
        this.phone = phone;
        this.balance = balance;
    }

    public void setCardNo(String cardNo){
        this.cardNo = cardNo;
    }

    public String getCardNo(){
        return this.cardNo;
    }
    public void setIdentity(String identity){
        this.identity = identity;
    }

    public String getIdentity(){
        return this.identity;
    }
    public void setUsername(String username){
        this.username = username;
    }

    public String getUsername(){
        return this.username;
    }
    public void setPassword(String password){
        this.password = password;
    }

    public String getPassword(){
        return this.password;
    }
    public void setPhone(String phone){
        this.phone = phone;
    }

    public String getPhone(){
        return this.phone;
    }
    public void setBalance(double balance ){
        this.balance= balance;
    }

    public double getBalance(){
        return this.balance;
    }
}
import java.util.Random;
import java.util.Scanner;

public class Bank {//中国建设银行

    Scanner input = new Scanner(System.in);//实例变量(实例方法共享)

    //保存已注册的用户数据
    User[] users = new User[5];

    int size = 3;//有效元素的个数


    //初始方法(如果存在一些程序起始是必须要执行的代码,则写入初始化方法)
    public Bank() {
        //第一行逻辑 初始化实例变量


        User user = new User();

        users[0] = new User("622202020004613255", "551822644215423657", "张三", "123456", "13785324722", 2000.0);


        users[1] = new User("622202020005461325", "552311544720264519", "李四", "123456", "13213241452", 3000.0);


        users[2] = new User("622202020005851597", "552344200548987219", "王五", "123321", "13725474254", 5000.0);


    }

    //欢迎页
    public void welcomeMenu() {
        System.out.println("---------------------------------------------------");
        System.out.println("1.登录 2.注册");
        System.out.println("---------------------------------------------------");
        System.out.print("请输入操作编码:");
        int choice = input.nextInt();
        switch (choice) {
            case 1:
                this.login();
                break;
            case 2:
                this.register();
                this.login();
                break;
            default:
                System.out.println("输入有误,请重新输入");
        }
    }

    //登录、验证
    public void login() {

        System.out.print("请输入卡号:");
        String no = input.next();
        System.out.print("请输入密码:");
        String pwd = input.next();

        User u = check(no, pwd);

        if (u != null) {
            this.showMenu(u);
        } else {
            System.out.println("用户名或密码错误");
        }
    }


    //注册
    public void register() {
        System.out.print("请输入姓名:");
        String uname = input.next();

        System.out.print("请输入身份证号:");
        String id = input.next();

        System.out.print("请输入电话号:");
        String phone = input.next();

        System.out.print("请输入密码:");
        String pwd = input.next();

        System.out.print("请输入预存金额:");
        double money = input.nextDouble();

        //根据用户输入的信息+随机卡号完成对象的封装

        String myCardNo = this.getRandomCardNo();

        User user = new User();
        user.setCardNo( myCardNo );
        user.setUsername(uname);
        user.setIdentity(id);
        user.setPhone(phone);
        user.setPassword(pwd);
        user.setBalance(money);

        //将该对象存储到users数组中
        //if(){}//扩容判断
        users[size] = user;
        size++;

        System.out.println("注册成功,您的卡号为:" + myCardNo);

    }

    //获取随机卡号
    public String getRandomCardNo() {
        String prefix = "62220202000";

        Random random = new Random();

        int r = random.nextInt(9000000);

        return prefix + (r + 1000000);
    }

    //验证
    public User check(String no, String pwd) {//单一职能原则
        for (int i = 0; i < size; i++) {
            //验证卡号和密码

            //if (users[i] != null) {//非空判断
            if (no.equals(users[i].getCardNo()) && pwd.equals(users[i].getPassword())) {
                //查找到该用户,用户存在
                return users[i];//存在
            }
        }

        return null;//不存在
    }


    //显示菜单
    public void showMenu(User u) {
        int choice;
        do {
            System.out.println("---------------------------------------------------");
            System.out.println("1.修改预留手机号 2.存款 3.取款 4.转账 5.查询余额 6.修改密码 7.注销账户 0.退出账户");
            System.out.println("---------------------------------------------------");
            System.out.print("请输入操作编码:");
            choice = input.nextInt();
            switch (choice) {
                case 1:
                    System.out.println("修改预留手机号码");
                    break;
                case 2:
                    System.out.println("执行存款");
                    break;
                case 3:
                    this.withdrawal(u);
                    break;
                case 4:
                    System.out.println("执行转账");
                    break;
                case 5:
                    System.out.println("执行查询余额功能");
                    break;
                case 6:
                    System.out.println("执行修改密码");
                    break;
                case 7:
                    this.cancel(u);
                    break;
                case 0:
                    return;
                default:
                    System.out.println("输入有误,请重新输入!");
            }
        } while (true);
    }

    //注销
    public void cancel(User user) {

        int index = size + 1;

        //查找该用户所在的数组下标
        for (int i = 0; i < size; i++) {

            if (users[i] == user) {
                index = i;
                break;
            }
        }
        //移动该元素之后的每个元素
        for (int i = index; i < size - 1; i++) {//index = 0; size = 2
            users[i] = users[i + 1];
        }
        size--;
    }


    //取款
    public void withdrawal(User mine) {
        System.out.print("请输入取款金额:");
        double money = input.nextDouble();

        if (money < users[0].getBalance()) {
            //可以取款
            mine.setBalance(mine.getBalance() - money);
            System.out.println("取款成功,当前余额为" + mine.getBalance());
        } else {
            System.out.println("余额不足");
        }
    }
 }

结果如下:

在这里插入图片描述
参考博客:千锋java培训视频

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
1. 账户类(满分50分) 版本1:满分10 分 设计Account1 类,包含: ■ 一个名为 id 的int 类型的私有数据域(默认值为 0),长度为 6 位。 ■ 一个名为 balance的double 类型的私有数据域(默认值为 0)。 ■ 一个名为 annualInterestRate 的double 类型的私有数据域存储当前利率(默认值为 0)。 假设所有的账户都有相同的利率。 ■ 一个名为 dateCreated 的Date 类型的私有数据域存储账户的开户日期。 ■ 一个能创建默认账户的无参构造方法。 ■ 一个能创建带特定 id 和初始余额的构造方法,初始余额不能为负数。 ■ id 、balance和annualInterestRate 的访问器和修改器。 ■ dateCreated 的访问器。 ■ 一个名为 getMonthlyInterestRate 的方法返回月利率。 ■ 一个名为 withDraw 的方法从账户提取特定金额。 ■ 一个名为 deposit 的方法向账户存人特定金额。 ■ double 类型的数据域保留 2 位小数。 ■ 成员方法和数据域应进行基本的合理性检查。 设计测试类 ATMMachine1: ■ 创建一个有 100 个账户的数组,其 id 为0,1,2,...99, 并初始化收支为 1000 美元。 ■ 主菜单如下(可参考教材中文版 P296或英文版 P367): Main menu 1: check balance 2: withdraw 3: deposit 版本2:满分20 分 扩展Account1 类为Account2 类: ■ Account2 类继承 Account1 类。 ■ 为Account2 类新增一个名为 password 的String 类型的私有数据域存储账号密码。 password 只能为字母或数字,长度不能小于 6 且不能大于 10。密码显示时为*******。 ■ 为Account2 类新增一个名为 name的String 类型的私有数据域存储客户名字。 ■ 为Account2 类新增一个名为 transactions 的ArrayList 类型的新数据域,其为客户存 储交易记录。这要求新建一个名为 Transaction 的类,类的定义请参照教材中文版 P327或英 文版P404。每笔交易都是 Transaction 类的一个实例。 ■ 新增一个带初始余额的构造方法,其 id 随机产生,但不能与当前系统的 id 重复。 若初始余额的参数为负数,则抛出一个自定义异常并在当前构造方法中进行处理。 ■ 重写方法 withDraw ,要求支取的金额为 100 的整数倍,并且当日支取金额不能超过 5000,支取金额不允许透支。每进行一次操作应向 transactions 数组线性表添加一笔交易。 ■ 重写方法 deposit ,要求每进行一次操作应向 transactions 数组线性表添加一笔交易。 ■ 新增一个方法 changePassword ,只有旧密码正确,新密码符合要求,且两次输入相 同的情况下才可以成功修改密码 设计测试类 ATMMachine2,其主菜单如下(可参考教材中文版 P296或英文版 P367): Main menu 0:create a account 1: check balance 2: withdraw 3: deposit 4:details of the transaction 5: change password 6:exit ■ 若用户选择新建一个账号,则应提示用户输入账号 password 、balance 和 annualInterestRate ,其中 id 随机产生。新产生的账户应序列化到名为 accounts.dat 的文件中。 所有账户只能通过这种方式产生。 ■ 所有用户操作结果应同步到 accounts.dat 文件中相应账户中。 ■ 所有用户操作应有友好、简介的提示语。 版本3:满分20 分 请参照银行的 AT M机界面,在 Account2 类的基础上开发一个 GUI 界面的AT M系统。 要求界面应模拟小键盘,并且账户信息读、写于文件 accounts.dat 。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值