我无法在帐户的对象中创建,我不知道问题出在哪里。我的输出只显示最近输入的值。我想看看我创建的印刷细节(特别是不同的id),我不知道该怎么做。
请检查我的代码,因为我认为我的循环中有一个错误,有些显示不需要显示的显示消息。
BankSystem.java:
import java.io.*;
import java.util.Scanner;
public class BankSystem {
public static void main(String[] args) throws IOException {
Scanner in = new Scanner(System.in);
BankAccount account[] = new BankAccount[5];
BankClient client[] = new BankClient[5];
BankSystem myBankSystem = new BankSystem();
String MainMenu;
do {
System.out.println("");
System.out.println("------PLP BANK SYSTEM------");
System.out.println("Main Menu: ");
System.out.println("[A]ccount Management" );
System.out.println("[C]lient Management" );
System.out.println("[Q]uit" );
System.out.println("");
System.out.print("Please select letter: ");
MainMenu = in.nextLine();
switch (MainMenu.toLowerCase()) {
case "a":
System.out.println("");
System.out.println("------PLP BANK SYSTEM------");
System.out.println("Account Management:" );
System.out.println("[N]ew Account" );
System.out.println("[A]Apply Interest to All Accounts" );
System.out.println("[L]ist All Accounts" );
System.out.println("[F]ind an Account" );
System.out.println("[D]eposit to an Account" );
System.out.println("[W]ithdraw from an Account" );
System.out.println("[R]eturn to Main Menu" );
System.out.println("");
System.out.print("Please select letter: " );
String AccountManagement = in.nextLine();
switch (AccountManagement.toLowerCase()) {
case "n": //NEW ACCOUNT
System.out.print("Please input your desired ID number: " );
int id = in.nextInt();
System.out.print("Please input your desired Balance: " );
double balance = in.nextDouble();
System.out.print("Please input your desired Interest Rate: " );
double interestRate = in.nextDouble();
// IS THE PROBLEM IS HERE ON MY LOOP?
for (int i=0; i<account.length; i++) {
account[i] = new BankAccount(id, balance, interestRate);
}
break;
case "a": //APPLY INTEREST TO ALL ACCOUNTS
System.out.println("For all accounts, compute and compound:" );
System.out.println("[M]onthly" );
System.out.println("[Q]uarterly" );
System.out.println("[A]nnually");
System.out.println("[C]ancel");
break;
case "l": //LIST ID NUMBERS OF ALL ACCOUNTS
System.out.println("List of all Accounts: ");
account[1].printDetails(); // OR THE PROBLEM IS HERE?
account[2].printDetails();
break;
case "f": //FIND ACCOUNT
System.out.println("Enter ID number: " );
break;
case "d": //DEPOSIT
System.out.println("Enter ID number: " );
System.out.println("Enter Deposit amount: " );
break;
case "w": //WITHDRAW
System.out.println("Enter ID number: " );
System.out.println("Enter Withdraw amount: " );
break;
case "r": // RETURN
break;
}
case "c":
System.out.println("");
System.out.println("------PLP BANK SYSTEM------");
System.out.println("Client Management:");
System.out.println("[N]ew Client" );
System.out.println("[L]List All Clients" );
System.out.println("[F]ind a Client" );
System.out.println("[R]eturn to Main Menu" );
System.out.println("");
System.out.print("Please select letter: " );
String ClientManagement = in.nextLine();
switch (ClientManagement.toLowerCase()) {
case "n": //NEW CLIENT
System.out.println("Enter ID number: " );
System.out.println("Please input your Name: " );
System.out.println("Please input your account ID number: " );
break;
case "l": //LIST ALL CLIENT
break;
case "f": //FIND A CLIENT
System.out.println("Enter ID number: " );
break;
case "r":
break;
default:
System.out.println("Invalid entry, Please try again!");
break;
}
default:
System.out.println("Invalid entry, Please try again!");
}
} while (!MainMenu.equals("q"));
System.out.println("Thank you for using my program!");
}
}
BankAccount.java:
public class BankAccount {
private double balance;
private double interestRate;
private int id;
public BankAccount (int id, double initialDeposit, double initialIntRate){
//Constructor
this.id=id;
this.balance=initialDeposit;
this.interestRate=initialIntRate;
}
public double getBalance(){
return balance;
}
public double getInterestRate(){
return interestRate;
}
public int getIDNumber(){
return id;
}
public void printDetails() {
System.out.println("ID Number is: " +id );
System.out.println("Current balance is: "+balance );
System.out.println("Interest rate is: "+interestRate+"%");
}
public double computeMonthlyInterest(){
interestRate=balance*interestRate;
return interestRate;
}
public void applyMonthlyInterest(){
this.balance=balance+interestRate;
}
public void applyQuarterlyInterest(){
this.balance=balance+(interestRate*3);
}
public void applyAnnualInterest(){
this.balance=balance+(interestRate*12);
}
public void deposit(double amount){
this.balance += amount;
}
public boolean withdraw (double amount) {
if (amount>balance) {
System.out.println("Withdraw amount is more than balance, Please try again.");
return false;
}
else
{
this.balance -= amount;
return true;
}
}
}
答案 0 :(得分:2)
你应该考虑将你的代码分成子程序;分解它将有助于区分不同的功能。
关于你的错误;我想你是说所有账户都有相同的价值,你并不期望这样。
创建新帐户时,您的代码如下:
for (int i=0; i<account.length; i++) {
account[i] = new BankAccount(id, balance, interestRate);
}
每次运行该行时,都会使用新的BankAccount对象替换数组中的每个元素。
答案 1 :(得分:0)
您的问题是,您使用最新输入的银行帐户填写了整个银行帐户。
for (int i=0; i<account.length; i++) {
account[i] = new BankAccount(id, balance, interestRate);
}
相反,在收到银行帐户的信息后,您应该只创建一个新的BankAccount,并且该数组的索引应该是当前的银行帐户数量+1。
所以在do语句之前创建一个变量来存储当前的银行账户数量。
int amountOfBankAccounts = 0;
在for的位置使用它并删除for
if(amountOfBankAccounts < 5) {
account[amountOfBankAccounts++] = new BankAccount(id, balance, interestRate);
} else {
System.out.println("Can not create a new account anymore, array is full!");
}
那应该解决它。一旦有效,我建议你花一些时间来创建函数,使你的代码更具可读性和可重用性。