[Java基础]面向对象_原创银行ATM系统

最近学完了黑马程序员Java的入门基础课之后,该课的最终结果是做一个ATM系统,虽然界面相似,但为了巩固一下学习成果,故还是自己编了一段代码实现这个系统,当然因为还有更重要的内容去学习,确实还有需要完善的部分,也就当是为自己的学习成果做一个总结。

1.  Account:

package com.WenJun;

import java.util.ArrayList;
import java.util.Scanner;

public class Account {
    private String accountID;
    private String accountName;
    private int accountPassword;
    private double accountBalance;
    private int count = 0;
    private double cashLimit;

    public Account() {

    }

    public Account(String accountID,int accountPassword,String accountName, double accountBalance, double cashLimit) {
        this.accountID = accountID;
        this.accountName = accountName;
        this.accountPassword = accountPassword;
        this.accountBalance = accountBalance;
        this.cashLimit = cashLimit;
        count++;
    }

    public double getCashLimit() {
        return cashLimit;
    }

    public void setCashLimit(double cashLimit) {
        this.cashLimit = cashLimit;
    }

    public double getAccountBalance() {
        return accountBalance;
    }

    public void setAccountBalance(double accountBalance) {
        this.accountBalance = accountBalance;
    }

    public String getAccountID() {
        return accountID;
    }

    public void setAccountID(String accountID) {
        this.accountID = accountID;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public String getAccountName() {
        return accountName;
    }

    public void setAccountName(String accountName) {
        this.accountName = accountName;
    }

    public int getAccountPassword() {
        return accountPassword;
    }

    public void setAccountPassword(int accountPassword) {
        this.accountPassword = accountPassword;
    }

    // 任务执行功能
    public void optionInInterface(int theSecondNum){
        Scanner sc = new Scanner(System.in);
        switch (theSecondNum){
            //1.查询
            case 1:
                System.out.println("户主:" + getAccountID());
                System.out.println("卡号:" + getAccountName());
                System.out.println("余额:" + getAccountBalance());
                System.out.println("当次取现额度:" + getCashLimit());
                break;
            //2.存款
            case 2:
                System.out.println("请输入您的存款金额:");
                double deposit = sc.nextDouble();
                this.accountBalance += deposit;
                System.out.println("存款成功");;
                break;
            //3.取款
            case 3:
                System.out.println("请输入您所需要的取款金额:");
                double withdraw = sc.nextDouble();
                if (withdraw <= this.cashLimit) {
                    if (this.accountBalance >= withdraw){
                        this.accountBalance -= withdraw;
                        System.out.println("取款成功!");
                    }else{
                        System.out.println("您的存款金额不足");
                    }
                } else {
                    System.out.println("您的取现额度不足");
                }
                break;
            //5.修改密码
            case 5:
                while (true) {
                    System.out.println("请输入您当前密码");
                    int inputPassword = sc.nextInt();
                    if (inputPassword == this.accountPassword){
                        System.out.println("请输入您想要修改的密码");
                        int theChangedNum = sc.nextInt();
                        System.out.println("请确认修改后的密码");
                        int theChangeConfirmNum = sc.nextInt();
                        if (theChangedNum == theChangeConfirmNum){
                            setAccountPassword(theChangedNum);
                            System.out.println("修改成功!");
                            break;
                        }else{
                            System.out.println("两次密码不一致,请重新修改。");
                        }
                    }else{
                        System.out.println("密码错误,请重新输入");
                    }
                }
                break;
        }
    }

    // 转账功能
    public void transferMoney(ArrayList accounts, String inputTransferAccountID, String inputTransferAccountFirstName){
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < accounts.size(); i++) {
            Account transferAccount = accounts.get(i);
            double maxTransferMoney;
            if (transferAccount.getAccountID().equals(inputTransferAccountID)) {
                if (transferAccount.getAccountName().substring(0,1).equals(inputTransferAccountFirstName)){
                    if (this.getAccountBalance() > this.getCashLimit()){
                        maxTransferMoney = this.getCashLimit();
                    }else {
                        maxTransferMoney = this.getAccountBalance();
                    }
                    System.out.println("请输入转入金额" + "(" + "您最多转入" + maxTransferMoney + ")");
                    double inputTransferMoney = sc.nextInt();
                    if (this.getAccountBalance() >= inputTransferMoney) {
                        if (this.getCashLimit() >= inputTransferMoney){
                            double transferAccountBalance = transferAccount.getAccountBalance();
                            double finalTransferAccountBalance = inputTransferMoney + transferAccountBalance;
                            transferAccount.setAccountBalance(finalTransferAccountBalance);
                            double finalAccountBalance = this.getAccountBalance() - inputTransferMoney;
                            this.setAccountBalance(finalAccountBalance);
                            System.out.println("转账成功!!");
                            break;
                        }else{
                            System.out.println("您的取款限额不足");
                        }
                    }else {
                        System.out.println("转账失败,您的余额不足。");
                    }
                }else{
                    System.out.println("确认失败,请重新输入");;
                }
            }
        }
    }
}
  • 方法介绍:

  1. optionInInterface(int theSecondNum):实现查询、存款、取款、修改密码功能。
  2. transferMoney(ArrayList accounts, String inputTransferAccountID, String inputTransferAccountFirstName):实现转账功能

2. 主程序

package com.WenJun;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
import java.util.concurrent.atomic.AtomicBoolean;

@SuppressWarnings("InfiniteLoopStatement")
public class ATMSystem {
    public static void main(String[] args) {
        ArrayList accounts = new ArrayList();
        // 1.显示页面
        while (true) {
            System.out.println("=====欢迎来到ATM系统=====");
            System.out.println("1.登录账户");
            System.out.println("2.注册账户");
            System.out.println("请选择您所需要的服务");
            // 选择相应服务
            Scanner sc = new Scanner(System.in);
            int firstSelectNumber = sc.nextInt(); // sc: 填写1或2
            // 2.进入下一页面
            switchOfLogin:switch (firstSelectNumber){
                // 进入登录界面
                case 1:
                    System.out.println("=====欢迎您进入到登录界面=====");
                    System.out.println("请输入账户卡号");
                    String inputAccountID = sc.next();
                    System.out.println("请输入账户密码");
                    int inputAccountPassword = sc.nextInt();
                    AtomicBoolean loginCheckFlag = new AtomicBoolean(checkTheID(accounts, inputAccountID));
                    if (accounts.size() != 0) {
                        for (Account account:
                             accounts) {
                            loginCheckFlag.set(checkTheID(accounts, inputAccountID));
                            if (loginCheckFlag.get() == false) {
                                System.out.println("系统中没有您的账户ID,请检查后重新输入或注册");
                                break;
                            } else if (account.getAccountID().equals(inputAccountID)){
                                if (account.getAccountPassword() == inputAccountPassword) {
                                    while (true) {
                                        optionInterface(account.getAccountName());
                                        int secondSelectNum = sc.nextInt();
                                        if (secondSelectNum == 1 || secondSelectNum == 2 || secondSelectNum == 3 || secondSelectNum == 5){
                                            account.optionInInterface(secondSelectNum);
                                        }else if (secondSelectNum == 4 || secondSelectNum == 6 || secondSelectNum == 7){
                                            switchOfOption:switch (secondSelectNum) {
                                                // 4.转账
                                                case 4:
                                                    System.out.println("请输入您转入的账户ID");
                                                    String inputTransferAccountID = sc.next();
                                                    String theSecondName = account.getAccountName().substring(1);
                                                    System.out.println("请您确认"+ "["+"*"+theSecondName + "]" +"的姓氏");
                                                    String inputTransferAccountFirstName = sc.next();
                                                    account.transferMoney(accounts, inputTransferAccountID, inputTransferAccountFirstName);
                                                    break;
                                                // 6.退出
                                                case 6:
                                                    break switchOfLogin;
                                                // 7.注销账户
                                                case 7:
                                                    accounts.remove(account);
                                                    System.out.println("注销成功!");
                                                    break switchOfLogin;
                                            }
                                        }
                                    }
                                }else{
                                    System.out.println("密码错误,请重新输入");
                                    break;
                                }
                            }
                        }
                    }else{
                        System.out.println("系统中无账户,请先注册账户");
                    }
                    break;
                // 进入注册界面
                case 2:
                    System.out.println("=====欢迎您进入到开户界面=====");
                    System.out.println("请您输入账户名称:");
                    String accountName = sc.next();
                    while (true) {
                        System.out.println("请您输入账户密码");
                        int accountPassword = sc.nextInt();
                        System.out.println("请再次输入密码");
                        int confirmPassword = sc.nextInt();
                        if (accountPassword == confirmPassword){
                            String accountId = new String();
                            for (int i = 0; i < 9; i++) {
                                Random r = new Random();
                                int numInAccount = r.nextInt(10);
                                accountId += numInAccount;
                            }
                            double accountBalance = 0;
                            System.out.println("请输入您当次取现额度");
                            double cashLimit = sc.nextDouble();
                            Account theAccount = new Account(accountId,
                                    accountPassword,
                                    accountName,
                                    accountBalance,
                                    cashLimit
                                    );
                            accounts.add(theAccount);
                            System.out.println("恭喜您开户成功!");
                            for (Account account:
                                 accounts) {
                                if (account.getAccountID() == theAccount.getAccountID()){
                                    System.out.println("账户名称:" + account.getAccountName());
                                    System.out.println("账户ID:" + account.getAccountID());
                                    System.out.println("账户余额:" + account.getAccountBalance());
                                    System.out.println("当前取现额度:" + account.getCashLimit());
                                }
                            }
                            break;
                        }else{
                            System.out.println("两次输入的密码不相同,请重新输入");
                        }
                    }
                    System.out.println("账户数量为:" + accounts.size());
                    break;
                // 3.隐藏管理系统
                case 3:
                    System.out.println("账户数量为" + accounts.size());
                    if (accounts.size() != 0) {
                        for (Account account:
                             accounts) {
                            System.out.println("账户名称\t账户ID\t账户密码");
                            System.out.println(account.getAccountName() + "\t" + account.getAccountID() + "\t" + account.getAccountPassword());
                        }
                    } else {
                        System.out.println("暂无账户");
                    }
                    break;
            }
        }
    }

    // 账户检查功能(检查是否存在该账户)
    public static boolean checkTheID(ArrayList accounts, String theAccountID){
        AtomicBoolean checkFlag = new AtomicBoolean(false);
        for (int i = 0; i < accounts.size(); i++) {
            Account account = accounts.get(i);
            if (theAccountID.equals(account.getAccountID())){
                checkFlag.set(true);
                return checkFlag.get();
            }
        }
        return checkFlag.get();
    }

    // 任务界面功能
    public static void optionInterface(String accountName){
        System.out.println("欢迎您," + accountName + "先生/女士进入系统,您可以开始办理你的业务了");
        System.out.println("=====欢迎您进入到操作界面=====");
        System.out.println("1.查询\t\t2.存款");
        System.out.println("3.取款\t\t4.转账");
        System.out.println("5.修改密码\t6.退出");
        System.out.println("7.注销账户");
        System.out.println("请您输入操作命令");
    }
}
  • 方法介绍

  1. checkTheID:账户检查功能(检查是否存在该账户)
  2. optionInterface:任务界面显示

3. 运行结果:

1)欢迎界面

  • [Java基础]面向对象_原创银行ATM系统_第1张图片

2)注册功能

  • [Java基础]面向对象_原创银行ATM系统_第2张图片

  • [Java基础]面向对象_原创银行ATM系统_第3张图片  

3)登录功能

  • 系统中无任一账户时:

[Java基础]面向对象_原创银行ATM系统_第4张图片

  • 系统中有账户但不匹配时:

[Java基础]面向对象_原创银行ATM系统_第5张图片

  •  系统中有匹配账户时:

[Java基础]面向对象_原创银行ATM系统_第6张图片

4)后台隐藏查询功能:

  • [Java基础]面向对象_原创银行ATM系统_第7张图片

 5)查询功能:

[Java基础]面向对象_原创银行ATM系统_第8张图片

6)存款功能: 

  • [Java基础]面向对象_原创银行ATM系统_第9张图片
  • [Java基础]面向对象_原创银行ATM系统_第10张图片

7)取款功能 :

  • 取款大于取现额度时:

  • [Java基础]面向对象_原创银行ATM系统_第11张图片
  •  取款成功时:

  • [Java基础]面向对象_原创银行ATM系统_第12张图片
  •  [Java基础]面向对象_原创银行ATM系统_第13张图片
  • 取款大于存款但小于取现额度时:

  •  [Java基础]面向对象_原创银行ATM系统_第14张图片

 8)转账

  • [Java基础]面向对象_原创银行ATM系统_第15张图片
  •  未确认成功时:

  • [Java基础]面向对象_原创银行ATM系统_第16张图片
  •  确认成功却存款不足时:

  • [Java基础]面向对象_原创银行ATM系统_第17张图片
  •  转账成功:

  • [Java基础]面向对象_原创银行ATM系统_第18张图片

 9)修改密码

[Java基础]面向对象_原创银行ATM系统_第19张图片

 [Java基础]面向对象_原创银行ATM系统_第20张图片

[Java基础]面向对象_原创银行ATM系统_第21张图片 

 10)退出

[Java基础]面向对象_原创银行ATM系统_第22张图片

 11)注销账户

[Java基础]面向对象_原创银行ATM系统_第23张图片

[Java基础]面向对象_原创银行ATM系统_第24张图片 

 参考:
Java入门基础视频教程,java零基础自学首选黑马程序员Java入门教程(含Java项目和Java真题)_哔哩哔哩_bilibiliicon-default.png?t=M5H6https://www.bilibili.com/video/BV1Cv411372m?p=86&spm_id_from=333.880.my_history.page.click&vd_source=7cb0fea3852037f79841a2a000065406

你可能感兴趣的:(java,java-ee)