银行系统版本二

Java课程设计银行系统——版本二

1. 题目说明

扩展 Account1 类为 Account2 类: (Account1类为版本1内的)

■ Account2 类继承 Account1 类。

■ 为 Account2 类新增一个名为 password 的 String 类型的私有数据域存储账号密码。

password 只能为字母或数字,长度不能小于 6 且不能大于 10。

■ 为 Account2 类新增一个名为 name 的 String 类型的私有数据域存储客户名字。

■ 为 Account2 类新增一个名为 transactions 的 ArrayList 类型的新数据域,其为客户存储交易记录。这要求新建一个名为 Transaction 的类,类的定义请参照教材第 10 版 P381。每笔交易都是 Transaction 类的一个实例。

■ 新增一个带初始余额的构造方法,其 id 随机产生,但不能与当前系统的 id 重复。若初始余额的参数为负数,则抛出一个自定义异常并在当前构造方法中进行处理。

■ 重写方法 withDraw,要求支取的金额为 100 的整数倍,并且当日支取金额不能超过5000,支取金额不允许透支。每进行一次操作应向 transactions 数组线性表添加一笔交易。

■ 重写方法 deposit,要求每进行一次操作应向 transactions 数组线性表添加一笔交易。

■ 新增一个方法 changePassword,只有旧密码正确,新密码符合要求,且两次输入相同的情况下才可以成功修改密码

设计测试类 ATMMachine2,其主菜单如下:

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 文件中相应账户中。

■ 所有用户操作应有友好、简介的提示语。

2. 分析过程

1.创建一个名为Account2的类,该类继承Account1类,实现了可序列化接口,继承了Account1的相关方法和属性,由于不满足Account2类的需求,所以根据题目要求,创建了一个名为name的String类型的私有数据域;一个名为password的String类型的私有数据域;创建一个类型为Transaction的列表,Transaction类下面进行阐述;设置了一个isCorrectPassword()方法,用于判断用户输入的密码是否是6到10位的,而且只能为字母和数字,如果是,则返回true,否则返回false;

类Account2提供了password和name的访问器和修改器,子类新增了带特定id和balance的构造方法,其中id为随机数,我使用了Math.Random()产生,balance不能负数,否则会抛出一个自定义的异常;子类Account2重写了父类的withDraw()和deposit()方法,当取款金额不是100的整数或取款金额大于余额的时候,会进行提示,用户没进行一次存款和取款的操作,都需要向transactions添加一笔交易;类中包含一个changePassword()方法,我这里使用了多层判断,当用户需要修改密码的是时候,用户输入原密码,如果原密码正确,则输入新的密码,密码合法,则再次输入,输入的密码和第一次输入的密码相同,则修改密码成功,否则修改失败。

2.创建一个Transaction类,创建一个名为dateCreated的Date类型的私有数据域,一个名为type的char类型的私有数据域,一个名为amount、balance的double类型的私有数据域,一个名为description的String类型的私有数据域;在里面包含所有私有数据域的访问器和修改器,还在里面创建了一个无参构造方法,一个带特定类型、金额、余额、描述的有参构造方法,如果是取款,则balance是balance减去amount,如果是存款,则balance是balance加上amount。

3.测试类ATMMachine2:

用户登录,显示菜单,用户可输入0、1、2、3、4、5、6分别执行创建账户、查询余额、取款、存款、查询记录、修改密码、退出等操作。

(1)当用户输入0的时候会随机产生一个id,执行openAccount()方法,openAccount()方法里设置了一个名为same的变量,初始值为0,for循环用于判断随机生成的id是否已经存在,如果存在,则same等于1,输出用户id已被使用,否则same等于0,用户输入密码、余额、名字,然后创建Account2对象,并将创建的对象添加到accounts列表中。

当用户输入不为0时,不会更改id。

(2)当用户输入1的时候,获取accounts列表中相应的Account2对象,并通过getBalance()方法返回用户的余额。

(3)当用户输入为2的时候,提示用户输入取款金额,获取accounts列表中相应的Account2对象,并通过withDraw()方法取出金额。

(4)当用户输入为3的时候,提示用户输入存款金额,获取accounts列表中相应的Account2对象,并通过deposit()方法存入金额。

(5)当用户输入为4的时候,通过获取accounts列表中相应的Account2对象,并调用其中的transactions列表,再调用其中的方法访问用户进行的一些列操作,然后输出。

(6)当用户输入为5时,获取accounts列表中相应的Account2对象,并通过changePassword()修改密码。

(7)当用户输入为6时,将isLogin设置为false。

否则要求用户输入一个正确的命令。

每次创建一个对象的时候,使用writeObject将对象列表写入到一个名为accounts.dat的文件中,同时将对该账户的一系列操作都存放发文件中。

相关函数:

displayMenu():展示菜单

processMenu():执行1.创建账户、2.取款、3.存款、4.查询记录、5.修改密码、6.退出相关的操作

getBalance():查询余额

withDraw():取款

deposit():存款

changePassword():修改密码

getCorrectIdIndex():由于是随机产生id的,而存储的时候是顺序存储的,该函数是获取正确的id的下标的。

OpenAccount():创建一个账户,将账户的信息存放在列表中。

相关类中有一些set和get方法,用于修改和获取一些数据域信息。

3. 系统测试

输入数据创建账户:输入密码、余额、名字,取款200,存款400,查询记录,更改密码
预期输出账户余额为1200,记录信息,密码修改成功
实际输出[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-cqxR7fPG-1640184136590)(file:///C:\Users\25016\AppData\Local\Temp\ksohtml\wpsF489.tmp.jpg)]在这里插入图片描述在这里插入图片描述

4.代码展示

Account2类

package version2;

import version1.Account1;
import version1.InvalidBalanceException;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Scanner;

/**
 * @Auther: paradise
 * @Date: 2021/6/24 - 06 - 24 - 15:48
 */
public class Account2 extends Account1 implements Serializable {
    private String password;
    private String name;
    ArrayList<Transaction> transactions = new ArrayList<>();

    /**Constructing a parameter-less construction method*/
    public Account2(){
    }

    /**Constructing a construction method with special id and balance*/
    public Account2(int id,double balance) throws InvalidBalanceException {
        super(id, balance);
    }

    /**Return the password*/
    public String getPassword() {
        return password;
    }

    /**Judge whether the password is qualified**/
    public boolean isCorrectPassword(String string) {
        if (6 <= string.length() && 10 >= string.length()) {
            for (int i = 0; i < string.length(); i++) {
                if (!Character.isLetterOrDigit(string.charAt(i))) {
                    return false;
                }
            }
            return true;
        }
        else {
            return false;
        }
    }

    /***Set the Password*/
    public void setPassword(String password) {
        if(isCorrectPassword(password)){
            this.password = password;
        }
        else{
            System.out.println("输入的密码为6至10位且只能为字母或数字");
        }

    }

    /***Return thr name*/
    public String getName() {
        return name;
    }

    /**Set the Name**/
    public void setName(String name) {
        this.name = name;
    }

    @Override
    public void withDraw(double withdrawalAmount) throws InvalidBalanceException {
        if(withdrawalAmount % 100 == 0 && 0 < withdrawalAmount && withdrawalAmount <= 5000){
            transactions.add(new Transaction('W', withdrawalAmount, getBalance(), name));
            setBalance(getBalance() - withdrawalAmount);
        }
        else{
            System.out.println("支取金额需要为100的整数倍,并且支出金额要大于0,不能超过5000");
        }
    }

    @Override
    public void deposit(double depositAmount) throws InvalidBalanceException {
        transactions.add(new Transaction('D', depositAmount, getBalance(), name));
        setBalance(getBalance() + depositAmount);
    }

    public void changePassword(){
        Scanner input = new Scanner(System.in);
        System.out.println("请输入原密码:");
        String string = input.next();
        if(password.equals(string)){
            System.out.println("请输入新的密码:");
            String newPassword1 = input.next();

            if(!isCorrectPassword(newPassword1)){
                System.out.println("输入的密码不合法");
            }
            else{
                System.out.println("请再一次输入您的密码:");
                String newPassword2 = input.next();
                if(newPassword1.equals(newPassword2)){
                    setPassword(newPassword1);
                }
                else{
                    System.out.println("两次输入的密码不一致");
                }
            }
        }
        else{
            System.out.print("密码输入错误!!!");
        }
    }
}

Transaction类

package version2;

import java.util.Date;

/**
 * @Auther: paradise
 * @Date: 2021/6/24 - 06 - 24 - 15:49
 */
public class Transaction{
    private Date dateCreated;
    private char type;
    private double amount;
    private double balance;
    private String description;

    /**Create a null parameter construction method*/
    public Transaction(){
    }

    /**Create a construction method with special type, amount, balance, description*/
    public Transaction(char type, double amount, double balance, String description) {
        if(type == 'W'){
            this.type = type;
            this.amount = amount;
            this.balance = balance - amount;
            this.description = description;
        }
        if(type == 'D'){
            this.type = type;
            this.amount = amount;
            this.balance = balance + amount;
            this.description = description;
        }

    }

    /**Set the date created*/
    public void setDateCreated(Date dateCreated) {
        this.dateCreated = dateCreated;
    }

    /**Return the type*/
    public char getType() {
        return type;
    }

    /**Set the type*/
    public void setType(char type) {
        this.type = type;
    }

    /**Return the amount*/
    public double getAmount() {
        return amount;
    }

    /**Set the amount*/
    public void setAmount(double amount) {
        this.amount = amount;
    }

    /**Get the balance*/
    public double getBalance() {
        return balance;
    }

    /**Set the balance*/
    public void setBalance(double balance) {
        this.balance = balance;
    }

    /**Return the description*/
    public String getDescription() {
        return description;
    }


    /**Set thr description*/
    public void setDescription(String description) {
        this.description = description;
    }

    /**Return the date*/
    public Date getDateCreated() {
        dateCreated = new Date();
        return dateCreated;
    }
}

ATMMachine2类

package version2;

import version1.InvalidBalanceException;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * @Auther: paradise
 * @Date: 2021/6/24 - 06 - 24 - 15:49
 */
public class ATMMachine2{
    private static Scanner scanner;
    private static boolean isLogin = true;
    private static ArrayList<Account2> accounts = new ArrayList<Account2>(100);
    static int id = (int)(Math.random() * 100);

    /**Display the menu*/
    public static void displayMenu(){
        System.out.print("Main menu" + "\n0: create a account" + "1: check balance\n" +
                "2: withdraw\n" + "3: deposit\n" + "4: details of the transaction\n" + "5: change password\n" + "6: exit\n");
        System.out.print("Enter a choice: ");
    }

    public static int getCorrectIdIndex(int id){
        int index = -1;
        for(int i = 0; i < accounts.size(); i++){
            if(accounts.get(i).getId() == id){
                index = i;
            }
        }
        return index;
    }

    /**Display the process menu*/
    public static void processMenu(int id, int command) throws InvalidBalanceException,IndexOutOfBoundsException {
        try{
        switch (command) {
            case 0:
                openAccount(id);
            case 1:
                System.out.print("The balance is " + accounts.get(getCorrectIdIndex(id)).getBalance() + "\n" );
                break;
            case 2:
                System.out.print("Enter an amount to withdraw: " );
                double withdrawMoney = scanner.nextDouble();
                accounts.get(getCorrectIdIndex(id)).withDraw(withdrawMoney);
                break;
            case 3:
                System.out.print("Enter an amount to deposit:" );
                double depositMoney = scanner.nextDouble();
                accounts.get(getCorrectIdIndex(id)).deposit(depositMoney);
                break;
            case 4:
                System.out.print("     The name of the account \t The type \t  The dispatch amount\t The balance\n");
                for(int i = 0; i < accounts.get(getCorrectIdIndex(id)).transactions.size(); i++){
                    System.out.println(
                            (i+1) + "                 " + accounts.get(getCorrectIdIndex(id)).getName() + "               "
                                    + accounts.get(getCorrectIdIndex(id)).transactions.get(i).getType() + "                "+
                                    accounts.get(getCorrectIdIndex(id)).transactions.get(i).getAmount() + "           " +
                                    accounts.get(getCorrectIdIndex(id)).transactions.get(i).getBalance());
                }
                break;
            case 5:
                accounts.get(getCorrectIdIndex(id)).changePassword();
            case 6:
                isLogin = false;
                break;
            default:
                System.out.println("please enter a right command:");
                break;
        }}
        catch (IndexOutOfBoundsException e){
            System.out.println("需要先创建一个用户");
        }
    }

    public static void openAccount(int id) throws InvalidBalanceException {
        int same = 0;
        for (int i = 0; i < accounts.size(); i++) {
            if(id == accounts.get(i).getId()){
                same = 1;
                break;
            }
        }
        if(same == 1){
            System.out.print("This id has been used");
        }
        else{
            System.out.println("Please enter your password:");
            String password = scanner.next();
            System.out.println("Please enter your balance:");
            Double balance = scanner.nextDouble();
            System.out.println("Please enter your name:");
            String name = scanner.next();

            Account2 account2 = new Account2(id, balance);
            account2.setPassword(password);
            account2.setAnnualInterestRate(0.015);
            account2.setName(name);

            accounts.add(account2);
        }
    }
    //这是一个main方法,是程序的入口
    public static void main(String[] args) throws InvalidBalanceException, IOException, ClassNotFoundException{
        while (true) {
            isLogin = true;
            while (isLogin) {
                displayMenu();
                scanner = new Scanner(System.in);
                int command = scanner.nextInt();
                if(command == 0){
                    id = (int)(Math.random() * 100);
                    processMenu(id, command);

                    try(ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream("accounts.dat"))) {
                        output.writeObject(accounts);
                        for (int i = 0; i < accounts.size(); i++) {
                            List<Transaction> transactionsList = accounts.get(i).transactions;
                            output.writeObject(transactionsList);
                        }
                    }
                }
                else{
                    processMenu(id, command);
                }
            }
        }
    }
}


版本三将通过JavaFX构建图形化界面,尽情期待哦~

  • 7
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 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 4: exit 版本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 分 请参照银行的ATM 机界面,在Account2 类的基础上开发一个GUI 界面的ATM 系统。 要求界面应模拟小键盘,并且账户信息读、写于文件accounts.dat。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值