如何在arraylist中添加和减去数字(在我的特定条件下)?

时间:2022-06-13 08:50:19

In my bank application, users are able to create multiple accounts. Now I am adding an extra feature so that it can perform transaction within different accounts and also within the same account. At this moment, in summury, I have no idea how to make this happen.

在我的银行应用程序中,用户可以创建多个帐户。现在我添加了一个额外的功能,以便它可以在不同的帐户内以及同一帐户内执行交易。在这个时刻,总之,我不知道如何实现这一目标。

Specially: considering the block of code I have given below, once the withdraw_accNum is found in the array list, withdraw_amount must be subtracted from the current balance of withdraw_accNum. And after that that value must be added to the current balance of deposit_accNum. And of course the array should be updated according to the transaction I made so that it can get diplayed with the recent status. case 2: should also follow this basic rules.

特别是:考虑到我在下面给出的代码块,一旦在数组列表中找到withdraw_accNum,就必须从withdraw_accNum的当前余额中减去withdraw_amount。之后,该值必须添加到deposit_accNum的当前余额中。当然,应该根据我所做的事务更新数组,以便它可以显示最近的状态。案例2:还应遵循这一基本规则。

This is my complete code for this application:

这是我对此应用程序的完整代码:

BankApp_Assignment:'

    package bankapp_assignment;

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

public class BankApp_Assignment {

    static int numOfAcc = 0;

    public static void main(String[] args) {
        System.out.println("WELCOME TO OUR BANK!\n\n");

        List<BankAccount> bankAccounts = new ArrayList<BankAccount>();
        Scanner sc = new Scanner(System.in);

        int option1;
        int option2;
        double withdraw_accNum;
        double deposit_accNum;

        double withdraw_amount;
        double dep_amount;

        while (true) {
            System.out.println("Choose your option:\n"
                    + "1. Create new account\n"//done
                    + "2. Deposit/withdraw\n"//working with this...
                    + "3. View One account\n"//not used yet
                    + "4. Deleting an account\n"//not used yet
                    + "5. View all the accounts\n"//done
                    + "6. Quit\n");//done
            System.out.println("*************\n"
                    + "************");

            option1 = sc.nextInt();
            sc.nextLine();
            //switch-case starts
            switch (option1) {
                case 1:
                    //create account
                    BankAccount bankAcc = new BankAccount();
                    System.out.println("Enter Full Name:");
                    bankAcc.setName(sc.nextLine());
                    System.out.println("Choose an Account Number:");
                    bankAcc.setAccNum(sc.nextInt());
                    System.out.println("Choose the initial amount:");
                    bankAcc.setInitiateAmount(sc.nextDouble());
                    //adding those into the arrayList
                    bankAccounts.add(bankAcc);
                    System.out.println("-------------\n"
                            + "-------------");
                    break;
                case 2:
                    //First displaying the current accouns info
                    System.out.println("Name \tAccount No \tInitial Amount");
                    for (BankAccount bankAccount : bankAccounts) {

                        System.out.println(bankAccount);

                    }

                    System.out.println("\t\t.........\n"
                            + "\t\t.........");

                    System.out.println("To transfer money within the bank accounts enter 1\n"
                            + "To deposit/withdraw money in the same account enter 2");
                    option2 = sc.nextInt();
                    sc.nextLine();

                    //inner switch-case starts
                    switch (option2) {
                        case 1:

                            System.out.println("Enter the account number you want to withdraw from:");

                            withdraw_accNum = sc.nextDouble();

                            System.out.println("Enter the amount you want to withdraw:");

                            withdraw_amount = sc.nextDouble();

                            System.out.println("Enter the account number you want to deposit to:");

                            deposit_accNum = sc.nextDouble();//the deposit amount is alwyas the same as withdraw_amount

                            break;
                        case 2://deposit/withdraw money in the same accounts
                            System.out.println("Enter the account number you want to deposit or withdraw from:");
                            //read the accNum
                            System.out.println("Enter the amount (To withdraw enter a negative value)");
                            //read the amount
                            break;
                    }

                    //inner switch-case ends
                    System.out.println("\n\n");
                    break;

                case 3:
                //View One account
                case 4:
                //Deleting an account

                case 5:
                    //View all the accounts/printing them out
                    System.out.println("Name\tAccount No\tInitial Amount");
                    for (BankAccount bankAccount : bankAccounts) {

                        System.out.println(bankAccount);

                    }

                    System.out.println("\n\n");
                    break;

                case 6:
                    //Quit
                    return;
            }

            //switch-case ends
        }

    }

}

BankAccount:

package bankapp_assignment;

public class BankAccount {

    private String name;
    private int accNum;
    private double initiateAmount;

    //constructor
    public BankAccount() {

        this.name = null;
        this.accNum = 0;
        this.initiateAmount = 0;

    }

    public void setName(String name) {
        this.name = name;

    }

    public void setAccNum(int accNum) {

        this.accNum = accNum;
    }

    public void setInitiateAmount(double initiateAmount) {
        this.initiateAmount = initiateAmount;
    }

    @Override
    public String toString() {
        return name + "\t\t" + accNum + "\t\t" + initiateAmount;
    }

}

2 个解决方案

#1


0  

Instead of keeping an ArrayList of BankAccount, keep a HashMap with key as account number and value as BankAccount object.

不要保留BankAccount的ArrayList,而是使用密钥作为帐号和值保存HashMap作为BankAccount对象。

Map<int, BankAccount> bankAccounts = new HashMap<int, BankAccount>();

Map bankAccounts = new HashMap (); ,bankaccount> ,bankaccount>

when you add an account, simply do-

添加帐户时,只需执行以下操作即可

bankAccounts.put(accNum, bankAccount);

and when you want to subtract withdraw amount-

当你想减去提取金额时 -

BankAccount bankAccount = bankAccounts.get(accNum);

BankAccount bankAccount = bankAccounts.get(accNum);

and then you can manipulate amount by using its setters and getters.

然后你可以通过使用它的setter和getter来操纵数量。

#2


0  

Specially: considering the block of code I have given below, once the withdraw_accNum is found in the array list, withdraw_amount must be subtracted from the current balance of withdraw_accNum. And after that that value must be added to the current balance of deposit_accNum. And of course the array should be updated according to the transaction I made so that it can get diplayed with the recent status. case 2: should also follow this basic rules.

特别是:考虑到我在下面给出的代码块,一旦在数组列表中找到withdraw_accNum,就必须从withdraw_accNum的当前余额中减去withdraw_amount。之后,该值必须添加到deposit_accNum的当前余额中。当然,应该根据我所做的事务更新数组,以便它可以显示最近的状态。案例2:还应遵循这一基本规则。

To check whether entered Bank Account number is correct while withdrawing is simple enough. I guess you have no problem with that. You can just loop through all arraylist elements and check whether each bank account's accNum match with given account number.

撤销时检查输入的银行帐号是否正确非常简单。我想你没问题。您可以循环浏览所有arraylist元素,并检查每个银行帐户的accNum是否与给定的帐号匹配。

To manipulate the arraylist elements is very simple.

操纵arraylist元素非常简单。

  1. Get the reference of the bank account object you are interested to manipulate.

    获取您有兴趣操作的银行帐户对象的参考。

    BankAccount b, acc = null;
    for(int x=0; x<bankAccounts.size(); x++){
        b = bankAccounts.get(x);
        if(givenAcc == b.getAccNum())
            acc = b;;
    }
    
  2. If account exist, deduct money from that account

    如果帐户存在,请从该帐户中扣除资金

    //Assume user already input amount to withdraw
    
    if(acc != null) //if account exist
        b.deductAmount(amt);
    else
        System.out.println("Account does not exist");
    

That's all. It is very simple. Just a one line thing.

就这样。这很简单。只是一线的事情。

I advise you to modularize your main into sub-methods. You also seem to have many mutator and accssor methods missing in your BankAccount Class.

我建议你将你的主要模块化为子方法。您的BankAccount类中似乎也缺少许多mutator和accssor方法。

It is advisible to have a deductAmount(double amt) method to deduct amount of money.

建议使用deductAmount(double amt)方法来扣除金额。

#1


0  

Instead of keeping an ArrayList of BankAccount, keep a HashMap with key as account number and value as BankAccount object.

不要保留BankAccount的ArrayList,而是使用密钥作为帐号和值保存HashMap作为BankAccount对象。

Map<int, BankAccount> bankAccounts = new HashMap<int, BankAccount>();

Map bankAccounts = new HashMap (); ,bankaccount> ,bankaccount>

when you add an account, simply do-

添加帐户时,只需执行以下操作即可

bankAccounts.put(accNum, bankAccount);

and when you want to subtract withdraw amount-

当你想减去提取金额时 -

BankAccount bankAccount = bankAccounts.get(accNum);

BankAccount bankAccount = bankAccounts.get(accNum);

and then you can manipulate amount by using its setters and getters.

然后你可以通过使用它的setter和getter来操纵数量。

#2


0  

Specially: considering the block of code I have given below, once the withdraw_accNum is found in the array list, withdraw_amount must be subtracted from the current balance of withdraw_accNum. And after that that value must be added to the current balance of deposit_accNum. And of course the array should be updated according to the transaction I made so that it can get diplayed with the recent status. case 2: should also follow this basic rules.

特别是:考虑到我在下面给出的代码块,一旦在数组列表中找到withdraw_accNum,就必须从withdraw_accNum的当前余额中减去withdraw_amount。之后,该值必须添加到deposit_accNum的当前余额中。当然,应该根据我所做的事务更新数组,以便它可以显示最近的状态。案例2:还应遵循这一基本规则。

To check whether entered Bank Account number is correct while withdrawing is simple enough. I guess you have no problem with that. You can just loop through all arraylist elements and check whether each bank account's accNum match with given account number.

撤销时检查输入的银行帐号是否正确非常简单。我想你没问题。您可以循环浏览所有arraylist元素,并检查每个银行帐户的accNum是否与给定的帐号匹配。

To manipulate the arraylist elements is very simple.

操纵arraylist元素非常简单。

  1. Get the reference of the bank account object you are interested to manipulate.

    获取您有兴趣操作的银行帐户对象的参考。

    BankAccount b, acc = null;
    for(int x=0; x<bankAccounts.size(); x++){
        b = bankAccounts.get(x);
        if(givenAcc == b.getAccNum())
            acc = b;;
    }
    
  2. If account exist, deduct money from that account

    如果帐户存在,请从该帐户中扣除资金

    //Assume user already input amount to withdraw
    
    if(acc != null) //if account exist
        b.deductAmount(amt);
    else
        System.out.println("Account does not exist");
    

That's all. It is very simple. Just a one line thing.

就这样。这很简单。只是一线的事情。

I advise you to modularize your main into sub-methods. You also seem to have many mutator and accssor methods missing in your BankAccount Class.

我建议你将你的主要模块化为子方法。您的BankAccount类中似乎也缺少许多mutator和accssor方法。

It is advisible to have a deductAmount(double amt) method to deduct amount of money.

建议使用deductAmount(double amt)方法来扣除金额。