当前位置: 首页 > 知识库问答 >
问题:

Java银行删除帐户

孙佐
2023-03-14

你好,我正在Java办理银行申请。

我的问题是我不知道如何删除创建的帐户。

当我创建一个新的帐户,它得到一个新的号码,但我不知道如何删除一个帐户,如果有像3个帐户,我想删除帐号2,当我想创建一个新的帐户,它将得到ID号码2。(有1号和3号)。

当我想创建另一个帐户时,我想将其ID更改为4。

提前道谢。:)

import java.util.Scanner;
public class BankApp {
public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    Bank myBank = new Bank();

    int user_choice = 2;

    do {
        //display menu to user
        //ask user for his choice and validate it (make sure it is between 1 and 6)
        System.out.println();
        System.out.println("1) Open a new bank account");
        System.out.println("2) Deposit to a bank account");
        System.out.println("3) Withdraw to bank account");
        System.out.println("4) Print short account information");
        System.out.println("5) Print the detailed account information including last transactions");
        System.out.println("6) Quit");
        System.out.println();
        System.out.print("Enter choice [1-6]: ");
        user_choice = s.nextInt();
        switch (user_choice) {
            case 1: System.out.println("Enter a customer name");
                    String cn = s.next();
                    System.out.println("Enter a opening balance");
                    double d = s.nextDouble();
                    System.out.println("Account was created and it has the following number: " + myBank.openNewAccount(cn, d));
                    break;
            case 2: System.out.println("Enter a account number");
                    int an = s.nextInt();
                    System.out.println("Enter a deposit amount");
                    double da = s.nextDouble();
                    myBank.depositTo(an, da);
                    break;
            case 3: System.out.println("Enter a account number");
                    int acn = s.nextInt();
                    System.out.println("Enter a withdraw amount");
                    double wa = s.nextDouble();
                    myBank.withdrawFrom(acn, wa);
                    break;
            case 4: System.out.println("Enter a account number");
                    int anum = s.nextInt();
                    myBank.printAccountInfo(anum);
                    break;
           case 5:  System.out.println("Enter a account number");
                    anum = s.nextInt();
                    myBank.printTransactionInfo(anum);
                    break;
          default: System.out.println("Invalid option. Please try again.");

        }
}
while (user_choice != '6');
}

static class Bank {
private BankAccount[] accounts;     // all the bank accounts at this bank
private int numOfAccounts;      // the number of bank accounts at this bank

    //Constructor: A new Bank object initially doesn’t contain any accounts.
public Bank() {
    accounts = new BankAccount[100];
    numOfAccounts = 0;
    }

// Creates a new bank account using the customer name and the opening balance given as parameters
// and returns the account number of this new account. It also adds this account into the account list
// of the Bank calling object.
public int openNewAccount(String customerName, double openingBalance) {

    BankAccount b = new BankAccount(customerName, openingBalance);
    accounts[numOfAccounts] = b;
    numOfAccounts++;
    return b.getAccountNum();
}

// Withdraws the given amount from the account whose account number is given. If the account is
// not available at the bank, it should print a message.
public void withdrawFrom(int accountNum, double amount) {
    for (int i =0; i<numOfAccounts; i++) {
        if (accountNum == accounts[i].getAccountNum()  ) {
            accounts[i].withdraw(amount);
            System.out.println("Amount withdrawn successfully");
            return;
        }
    }
    System.out.println("Account number not found.");
    }

// Deposits the given amount to the account whose account number is given. If the account is not
// available at the bank, it should print a message.
public void depositTo(int accountNum, double amount) {
    for (int i =0; i<numOfAccounts; i++) {
        if (accountNum == accounts[i].getAccountNum()  ) {
            accounts[i].deposit(amount);
            System.out.println("Amount deposited successfully");
            return;
        }
    }
    System.out.println("Account number not found.");
}

// Prints the account number, the customer name and the balance of the bank account whose
// account number is given. If the account is not available at the bank, it should print a message.
public void printAccountInfo(int accountNum) {
    for (int i =0; i<numOfAccounts; i++) {
                if (accountNum == accounts[i].getAccountNum()  ) {
                    System.out.println(accounts[i].getAccountInfo());
                    return;
                }
            }
    System.out.println("Account number not found.");
}

public void printTransactionInfo(int accountNum) {
    for (int i =0; i<numOfAccounts; i++) {
                if (accountNum == accounts[i].getAccountNum()  ) {
                    System.out.println(accounts[i].getAccountInfo());
                    System.out.println("Last transaction: " + accounts[i].getTransactionInfo(accounts[i].getNumberOfTransactions()-1));
                    return;
                }
            }
    System.out.println("Account number not found.");
}


// Prints the account number, the customer number and the balance of the bank account whose
// account number is given, together with last n transactions on that account. If the account is not
// available at the bank, it should print a message.
public void printAccountInfo(int accountNum, int n) {
    for (int i =0; i<numOfAccounts; i++) {
                        if (accountNum == accounts[i].getAccountNum()  ) {
                            System.out.println(accounts[i].getAccountInfo());
                            System.out.println(accounts[i].getTransactionInfo(n));
                            return;
                        }
                    }
    System.out.println("Account number not found.");
    }

}
    static class BankAccount{

       private int accountNum;
       private String customerName;
       private double balance;
       private double[] transactions;
       private String[] transactionsSummary;
       private int numOfTransactions;
       private  static int noOfAccounts=0;

       public String getAccountInfo(){
           return "Account number: " + accountNum + "\nCustomer Name: " + customerName + "\nBalance:" + balance +"\n";
       }

       public String getTransactionInfo(int n)
       {
            String transaction = transactionsSummary[n];
            if (transaction == null) {
                return "No transaction exists with that number.";
            }
            else {
                return transaction;
            }
        }

       public BankAccount(String abc, double xyz){
         customerName = abc;
         balance = xyz;
         noOfAccounts ++;
         accountNum = noOfAccounts;
         transactions = new double[100];
         transactionsSummary = new String[100];
         transactions[0] = balance;
         transactionsSummary[0] = "A balance of : $" + Double.toString(balance) + " was deposited.";
         numOfTransactions = 1;
       }

    public int getAccountNum(){
        return accountNum;
    }

    public int getNumberOfTransactions() {
        return numOfTransactions;
    }

    public void deposit(double amount){

        if (amount<=0) {
            System.out.println("Amount to be deposited should be positive");
        } else {
            balance = balance + amount;
            transactions[numOfTransactions] = amount;
            transactionsSummary[numOfTransactions] = "$" + Double.toString(amount) + " was deposited.";
            numOfTransactions++;
        }
    }
    public void withdraw(double amount)
    {
        if (amount<=0){
             System.out.println("Amount to be withdrawn should be positive");
         }
        else
        {
            if (balance < amount) {
                System.out.println("Insufficient balance");
            } else {
                balance = balance - amount;
                transactions[numOfTransactions] = amount;
                transactionsSummary[numOfTransactions] = "$" + Double.toString(amount) + " was withdrawn.";
                numOfTransactions++;
            }
        }
    }

    }//end of class
}

共有1个答案

严安志
2023-03-14

重新使用账号原则上是错误的,但如果你坚持那样做:

您可能希望将帐户标记为活动帐户或非活动帐户。创建帐户时,将其标记为活动帐户。删除帐户时,将其标记为非活动帐户。

当程序为一个“新”帐户决定一个帐号时,首先得到所有非活动帐户的有序列表,并选择第一个。如果没有任何非活动帐户,则使用增量numOfAccounts变量

 类似资料:
  • 问题内容: 每次开设银行帐户,帐户ID都应加1,但是每次我尝试拉出ID时,我只会得到帐户ID为0,这是任何建议,因为我完全按照从书中学习的方式进行学习而且仍然没有更新。 帐户构造函数: 银行账户主程序: 问题答案: 您可以采用一种杂乱的方式构造BankAccount对象,其中是否分配ID取决于您使用的构造函数。如果您重写构造函数以便将它们链接在一起,而主要构造函数负责所有核心职责,而次要构造函数将

  • 首先,我将引用另外两个对这个问题有部分答案的问题。 删除VM后从Azure中删除磁盘 如何删除Microsoft Azure存储中的租用blob 使用Azure Explorer,我可以删除blob和容器,但当我尝试删除存储帐户时,我出现了以下错误: 删除存储帐户“bitnamiWestusflwdmy”失败。无法删除存储帐户“bitnamiWestusflwdmy”:“存储帐户bitnamiWe

  • 我想在取款后把账户上的余额换一下,但它只是停留在10点。我不知道如何在SavingsAccount中正确应用一个会改变它的方法。我试了但没有成功。

  • 我修改了一小段代码,但无法运行。我总是发现一个例外: 保存失败:保存包时出错:重复条目:docProps/core.xml 错误抛出在这行: 我认为出现了这个例外,因为我不允许在同一个工作簿上读写,但是如果没有现有的Excel文件,我如何初始化新工作簿? 代码:(来自这里(感谢发起者):如何使用ApachePOI删除行) 和 [编辑2016-06-21]: 我加了 给我的POM,现在它产生了 线程

  • 问题内容: 我想删除DefaultTable的所有行。我发现有两种常见的方法可以在Internet上删除它们,但是在我的情况下它们都不起作用,因为这些方法在DefaultTableModel中不存在。我想知道为什么:S。我使用DefaultTableModel的代码是 DefaultTableModel Table =(DefaultTableModel)Table.getModel(); 删除的

  • 我正在用java创建一个没有数据库的库系统程序。(直接使用文件)。 我在删除jtable中的一行时遇到了一个奇怪的问题(也就是从文件中删除)。 有时当我在表格中选择一行并单击删除按钮时,不止一行被删除了! 大多数时候它也能正常工作!! 我的代码: 我的模型类: 用户信息类别: 我的文本文件: