在我的银行应用程序中,用户可以创建多个帐户。现在我添加了一个额外的功能,以便它可以在不同的帐户内以及同一帐户内执行交易。在这个时刻,总之,我不知道如何实现这一目标。
特别是:考虑到我在下面给出的代码块,一旦在数组列表中找到withdraw_accNum
,就必须从当前余额withdraw_amount
中减去withdraw_accNum
1}}。之后,该值必须添加到deposit_accNum
的当前余额中。当然,阵列应该根据我所做的交易进行更新,以便能够显示最近的状态。
case 2:
也应遵循此基本规则。
这是我对此应用程序的完整代码:
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;
}
}
答案 0 :(得分:0)
不要保留ArrayList
BankAccount
,而是将HashMap
作为帐号和值保存为BankAccount对象。
Map<int, BankAccount> bankAccounts = new HashMap<int, BankAccount>();
添加帐户时,只需执行 -
即可 bankAccounts.put(accNum, bankAccount);
当你想减去提取金额时 -
BankAccount bankAccount = bankAccounts.get(accNum);
然后您可以使用amount
和setters
来操纵getters
。
答案 1 :(得分:0)
特别是:考虑到我在下面给出的代码块,一旦在数组列表中找到withdraw_accNum,就必须从withdraw_accNum的当前余额中减去withdraw_amount。之后,该值必须添加到deposit_accNum的当前余额中。当然,阵列应该根据我做的事务进行更新,以便它可以显示最近的状态。案例2:也应遵循这一基本规则。
在退出时检查输入的银行帐号是否正确是否足够简单。我想你没问题。您可以遍历所有的arraylist元素,并检查每个银行帐户的accNum是否与给定的帐号匹配。
操纵arraylist元素非常简单。
获取您有兴趣操作的银行帐户对象的参考。
BankAccount b, acc = null;
for(int x=0; x<bankAccounts.size(); x++){
b = bankAccounts.get(x);
if(givenAcc == b.getAccNum())
acc = b;;
}
如果帐户存在,请从该帐户中扣除款项
//Assume user already input amount to withdraw
if(acc != null) //if account exist
b.deductAmount(amt);
else
System.out.println("Account does not exist");
这就是全部。这很简单。只是一行。
我建议你将你的主要模块化为子方法。您的BankAccount类似乎也缺少许多mutator和accssor方法。
建议采用deductAmount(double amt)
方法扣除金额。