银行在java中删除帐号

时间:2016-04-18 13:44:03

标签: java account bank

您好我是用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
}

3 个答案:

答案 0 :(得分:1)

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

您可能希望将帐户标记为有效或无效。创建帐户时,请将其标记为活动。删除帐户时,请将其标记为非活动状态。

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

答案 1 :(得分:0)

也许你应该考虑重新思考这个的逻辑。 但是,解决方案可能是将银行帐户标记为删除,添加字段,例如:

class BankAccount{
...
private boolean isMarkedForDeletion = false;
...
}

并向其添加getters and setters

现在你会:

1)当删除帐户时,请将该字段切换为true。

public int closeAccount(int id) {
boolean isAccountFound = false;
int i = 0;
while(i < numOfAccounts && isAccountFound == false)
{
  // if() // Add here the condition to find the account to delete.
  // { accounts[i].isMarkedForDeletion = true; } 
}

现在,不再对此帐户执行任何存款或其他操作。

2)当创建新帐户时,循环浏览每个现有帐户并验证帐户是否标记为删除 - 可能使用 while 循环。 如果要找到一个帐户&#34;标记为删除= true&#34;那么你会重新声明当前迭代的元素是一个与旧ID相同的新帐户。

public int openNewAccount(String customerName, double openingBalance) {
BankAccount b = new BankAccount(customerName, openingBalance);

boolean isAccountFound = false;
int i = 0;
while(isAccountFound == false && i < accounts.length)
{
  // if() add conditions for this account to replace the old one.
  // hint: use the isMarkedForDeletion field!
  // Finally add the new value assignment ( accounts[i] = ... )
  // return the account number. 
}
// This will happen in the case there were no matches.
accounts[numOfAccounts] = b;
numOfAccounts++;
return b.getAccountNum();
}

答案 2 :(得分:0)

我会使用一个事实,即可能有最大数量的帐户(100),因此数组大小固定。

public int openNewAccount(String customerName, double openingBalance) {
    for (int i = 0; i < 100; i++) {
        if (accounts[i] == null) { //Empty slot
            BankAccount b = new BankAccount(customerName, openingBalance);
            b.accountNum = i + 1;
            break;
        }
    }
    return b.getAccountNum();
}

并且删除意味着将该插槽设置为空

public void deleteAccount(int accountNo) {
    accounts[accountNo - 1] = null;
}

这两种方法都可能使用某种错误处理方法,具体取决于所需的反馈。您还需要从BankAccount构造函数中删除不必要的内容。