Hi,
I am trying out a bank account program using the corejava package.
I am getting a run-time eception when I create a new account & am unable to trace where the error is.
Plz help. I am giving the code below.
import corejava.Console;
/****************************************************************
**cLASS bANKaCCOUNT REPRESENTING THE RUN-TIME MULTI-USER SYSTEM**
****************************************************************/
public class BankAccount
{
/****************************
**DATA OF bANKaCCOUNT CLASS**
****************************/
private static final int MAXACC=10;
private static int acc_logged_in;
private static int no_acc_created;
private static Account[] database;
/*******************************
**METHODS OF bANKaCCOUNT CLASS**
*******************************/
// initialisation block
{
acc_logged_in=-1;
no_acc_created=0;
database=new Account[MAXACC];
}
private static void createnew()
{
String str;
no_acc_created++;
acc_logged_in=no_acc_created;
str=Console.readLine("\nEnter your name : ");
database[acc_logged_in].SETname(str);
database[acc_logged_in].SETbalance(Console.readInt("Enter your initial balance : "));
database[acc_logged_in].SETaccountid((int)(Math.random()*32000));
System.out.println("Your account I.D. is " + database[acc_logged_in].GETaccountid());
System.out.println("Account created successfully...\n\n");
}
private static void login()
{
int i,no;
boolean found=false;
no=Console.readInt("\nEnter your account I.D. : ");
for(i=0;i<no_acc_created;i++)
{
if(no==database[i].GETaccountid())
{
acc_logged_in=i;
found=true;
break;
}
}
if(found==true)
System.out.println("Logged in successfully as " + database[acc_logged_in].GETname() + "...\n\n");
else
System.out.println("Error : The I.D. did not match with any of the accounts !\n\n");
}
private static void logoff()
{
if(acc_logged_in==-1)
{
System.out.println("\nNo account is currently logged in !\n\n");
}
else
{
acc_logged_in=-1;
System.out.println("\nLogged off your account successfully...\n\n");
}
}
private static int menu()
{
int choice;
System.out.println("\n\t\tBANK-ACCOUNT");
System.out.println("\t\t~~~~~~~~~~~~");
System.out.println("");
System.out.println("\t<1> Create new account");
System.out.println("\t<2> Log in your account");
System.out.println("\t<3> Log off your account");
System.out.println("\t<4> View current status");
System.out.println("\t<5> Deposit an amount");
System.out.println("\t<6> Withdraw an amount");
System.out.println("\t<7> Exit BankAccount.java");
do
{
choice=Console.readInt("\n\t\tEnter your choice (1 to 7): ");
}
while(choice<1||choice >7);
return choice;
}
public static void main(String[] args)
{
int choice;
boolean want_to_exit=false;
while(want_to_exit==false)
{
choice=menu();
switch(choice)
{
case 1:
createnew();
break;
case 2:
login();
break;
case 3:
logoff();
break;
case 4:
if(acc_logged_in==-1)
{
System.out.println("Error : No account is logged in !");
break;
}
database[acc_logged_in].view();
break;
case 5:
if(acc_logged_in==-1)
{
System.out.println("Error : No account is logged in !");
break;
}
database[acc_logged_in].deposit();
break;
case 6:
if(acc_logged_in==-1)
{
System.out.println("Error : No account is logged in !");
break;
}
database[acc_logged_in].withdraw();
break;
case 7:
want_to_exit=true;
break;
}
}
}
}
/********************************************************************
**cLASS aCCOUNT REPRESENTING EVERY INDIVIDUAL ACCOUNT & ITS DETAILS**
********************************************************************/
// do not mention public\private when a class does not contain the main() to be executed
class Account
{
/************************
**DATA OF aCCOUNT CLASS**
************************/
private String name;
private int accountid;
private int balance;
/***************************
**METHODS OF aCCOUNT CLASS**
***************************/
public Account()
{
name="";
accountid=-1;
balance=0;
}
public int GETaccountid()
{
return accountid;
}
public String GETname()
{
return name;
}
public void SETname(String str)
{
name=str;
}
public void SETaccountid(int id)
{
accountid=id;
}
public void SETbalance(int bal)
{
balance=bal;
}
public void view()
{
System.out.println("\nName : " + name);
System.out.println("Balance : " + balance + " Rs.\n\n");
}
public void deposit()
{
int dep=Console.readInt("\nEnter the amount you want to deposit : ");
balance+=dep;
System.out.println("Amount deposited successfully...\n\n");
}
public void withdraw()
{
int wit=Console.readInt("\nEnter the amount you want to withdraw : ");
if(wit<=balance)
{
balance-=wit;
System.out.println("Amount withdrawn successfully...\n\n");
}
else
{
System.out.println("Error : The amount you are attempting to");
System.out.println(" withdraw is greater than your balance !\n\n");
}
}
}