我正在为我的Java类编写一个银行帐户程序。我对此仍然非常陌生,这是我们第一次使用数组。所以请原谅任何新秀失误。该任务目前需要3个银行帐户的数组。我需要3个类:银行(包含bankAcct myAcct[] =新bankAcct)、bankUser和bankAcct(其中包含我的取款、存款和查看余额方法)。到目前为止,在我的NullPointerException方法中,每当我试图查看用户类中的余额、存款或withdraw.The错误时,我都会在代码中留下注释,以显示具体位置。如有任何帮助,我们将不胜感激。我正在阅读我的教科书,但并没有发现什么能对我的问题有所帮助。提前谢谢你。
bankAcct类
import java.util.Scanner;
public class bankAcct
{
private double Bal;
private int acctNum;
private String name;
Scanner scannerObject = new Scanner(System.in);
public bankAcct(int pacctNum, double pBal, String pname) {
pBal = Bal;
pacctNum = acctNum;
pname = name;
}
public void makeDeposit()
{
System.out.print( "Hello " + name + ", please, enter amount to deposit $");
double lDep;
lDep = scannerObject.nextDouble();
Bal = Bal + lDep;
System.out.println( " You have deposited $" + lDep);
System.out.println( " Your new balance is $" + Bal);
}
public void makeWithdrawal()
{
System.out.print( "Hello " + name + ", please, enter amount to withdraw $");
double lWDraw;
lWDraw = scannerObject.nextDouble();
if (lWDraw <= Bal){
Bal = Bal - lWDraw;
System.out.println( "You have withdrawn $" + lWDraw);
System.out.println( "Your new balance is $" + Bal);
}else{
System.out.println("Insufficient funds!");
}
}
public void dispBal()
{
System.out.println( "Your current balance is $" + Bal);
}
public void setAcctNum(int pacctNum)
{
pacctNum = acctNum;
}
public int getAcctNum()
{
return acctNum;
}
public void setName(String pname)
{
pname = name;
}
public String getName()
{
return name;
}
}银行级
import java.util.Scanner;
public class Bank
{
int max = 3;
int count;
bankAcct myAcct[] = new bankAcct[max];
bankUser user = new bankUser();
Scanner scannerObject = new Scanner(System.in);
public void openAcct()
{
String lname;
if (count >= max){
System.out.println("Not accepting new customers at this time.");
}else{
System.out.println("Please enter your name: ");
lname = scannerObject.nextLine();
myAcct[count] = new bankAcct(count + 1, 0, lname);
count++;
System.out.println("Thank you " + lname + ", your account number is: " + count);
}
}
public int findAcct()
{
int lnum = -1;
System.out.println("Greetings, please enter your account number: ");
lnum = scannerObject.nextInt();
for(count = 0; count < max; count++){
if (count == lnum)
return count;
}
return lnum;
}
public void seeBal()
{
int lfound = findAcct();
if (lfound == -1)
{
System.out.println("Error!");
}else{
myAcct[lfound].dispBal();
}
}
void Deposit()
{
int lfound = findAcct();
if (lfound == -1)
{
System.out.println("Error!");
}else{
myAcct[lfound].makeDeposit();
}
}
void Withdrawal()
{
int lfound = findAcct();
if (lfound == -1)
{
System.out.println("Error!");
}else{
myAcct[lfound].makeWithdrawal();
}
}
} 用户类
import java.util.Scanner;
public class bankUser
{
public static void main(String[] args)
{
Bank myBank = new Bank();
Scanner scannerObject = new Scanner(System.in);
int Choice;
do
{
dispMenu();
Choice = getChoice(scannerObject);
proChoice(Choice, myBank); ***//Error occurring here***
}
while (Choice !=0);
}
public static void dispMenu()
{
System.out.println( "|==================================|");
System.out.println( "| TONY'S FIRST NATIONAL BANK |");
System.out.println( "|***********Menu Options***********|");
System.out.println( "|__________________________________|");
System.out.println( "| Press 1 To Open New Account |");
System.out.println( "| Press 2 To View Balance |");
System.out.println( "| Press 3 To Make Deposit |");
System.out.println( "| Press 4 To Make Withdrawal |");
System.out.println( "| Press 0 to Exit |");
System.out.println( "|__________________________________|");
System.out.println( "| Please Make Selection Now... |");
System.out.println( "|==================================|");
}
static int getChoice(Scanner scannerObject)
{
int pChoice, Choice;
pChoice = scannerObject.nextInt();
Choice = pChoice;
return Choice;
}
static void proChoice(int Choice, Bank myBank)
{
switch (Choice)
{
case 1: myBank.openAcct();
break;
case 2: myBank.seeBal(); //***Error Here***
break;
case 3: myBank.Deposit(); //***Error Here***
break;
case 4: myBank.Withdrawal(); //***Error Here***
break;
case 0: System.out.println( "Thank you, come again.");
break;
}
}
}发布于 2015-03-21 01:19:17
你有几个问题。我解决了第一个问题,但它将涉及到第二个和第三个。第一个问题是如何创建您的帐户。您当前的构造函数是:
public bankAcct(int pacctNum, double pBal, String pname)
{
pBal = Bal;
pacctNum = acctNum;
pname = name;
} 应该是这样:
public bankAcct(int pacctNum, double pBal, String pname)
{
Bal = pBal;
acctNum = pacctNum;
name = pname;
} 在这里,值被反转,这使得您无法将帐户号、姓氏和余额分配给您创建的帐户。
其次,您的findAcct和seeBalance方法应该如下所示:
public bankAcct findAcct()
{
bankAcct myBankAcct = null;
System.out.println("Greetings, please enter your account number: ");
int acctNum = scannerObject.nextInt();
//make sure you use myAcct.length to ensure you don't get an "ArrayOutOfBoundsIndex" error.
for(count = 0; count < myAcct.length; count++){
myBankAcct = myAcct[count];
if(myBankAcct.getAcctNum() == acctNum){
return myBankAcct;
}
}
return myBankAcct;
}
public void seeBal()
{
bankAcct lfound = findAcct();
if (lfound == null)
{
System.out.println("Error!");
}else{
lfound.dispBal();
}
}您最大的问题是使用findAcct方法。你没找到那个账户。这就是你所做的:
public int findAcct()
{
int lnum = -1;
System.out.println("Greetings, please enter your account number: ");
//you get the users acct #
lnum = scannerObject.nextInt();
for(count = 0; count < max; count++){ //here you iterate, but you never "find" the account
if (count == lnum)
return count; //then you return the "count" rather than the account itself.
}
return lnum;
}如果您做了我所做的更改,它将运行在选择2,但会有一些错误,您将不得不处理。如果你从这篇文章中吸取教训,你应该能够解决你计划中的其他问题。您应该能够使Deposit和Withdraw方法看起来像seeBalance方法。
这里是我运行的输出
|==================================|
| TONY'S FIRST NATIONAL BANK |
|***********Menu Options***********|
|__________________________________|
| Press 1 To Open New Account |
| Press 2 To View Balance |
| Press 3 To Make Deposit |
| Press 4 To Make Withdrawal |
| Press 0 to Exit |
|__________________________________|
| Please Make Selection Now... |
|==================================|
1
Please enter your name:
Blaine
Thank you Blaine, your account number is: 1
|==================================|
| TONY'S FIRST NATIONAL BANK |
|***********Menu Options***********|
|__________________________________|
| Press 1 To Open New Account |
| Press 2 To View Balance |
| Press 3 To Make Deposit |
| Press 4 To Make Withdrawal |
| Press 0 to Exit |
|__________________________________|
| Please Make Selection Now... |
|==================================|
2
Greetings, please enter your account number:
1
Your current balance is $0.0
|==================================|
| TONY'S FIRST NATIONAL BANK |
|***********Menu Options***********|
|__________________________________|
| Press 1 To Open New Account |
| Press 2 To View Balance |
| Press 3 To Make Deposit |
| Press 4 To Make Withdrawal |
| Press 0 to Exit |
|__________________________________|
| Please Make Selection Now... |
|==================================|我希望这有帮助:)
https://stackoverflow.com/questions/29177913
复制相似问题