I cant get this program working. Cant really sort out where i am wrong. The program works fine without the save/load object feature. Please help!!
import java.io.*;
class Account
{
String name ,accno;
double bal;
void withdraw(double amt)
{
System.out.print("Processing");
for(double pr = 0; pr<500000000; pr++);
System.out.print(".");
for(double pr = 0; pr<500000000; pr++);
System.out.print(".");
for(double pr = 0; pr<500000000; pr++);
System.out.println(".");
if(bal-amt >=500)
{
bal -= amt;
System.out.println(name + " , Amount has been successfully withdrawn,your current balance = " + bal);
}
else
{
System.out.println("Not enough balance");
}
}
void deposit(double amt)
{
System.out.print("Processing");
for(double pr = 0; pr<500000000; pr++);
System.out.print(".");
for(double pr = 0; pr<500000000; pr++);
System.out.print(".");
for(double pr = 0; pr<500000000; pr++);
System.out.println(".");
bal += amt;
System.out.println(name + " , Amount successfully deposited, Your current balance = " + bal);
}
void view()
{
System.out.print("Processing");
for(double pr = 0; pr<500000000; pr++);
System.out.print(".");
for(double pr = 0; pr<500000000; pr++);
System.out.print(".");
for(double pr = 0; pr<500000000; pr++);
System.out.println(".");
System.out.println("Name : " + name + "\nAccount No.:" + accno + "\nBalance : " + bal);
}
void newAcc(int i)throws IOException
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter Name:");
name = in.readLine();
System.out.println("Enter the amount you want to deposit (min Rs 500)");
double deposit = Double.parseDouble(in.readLine());
if(deposit >500)
{
bal = deposit;
int j = i;int k = 1;
while(j>9)
{
j=j/10;
k++;
}
accno = "BK";
for(int w = k;w<5;w++)
{
accno = accno + "0";
}
accno = accno + Integer.toString(i+1);
System.out.print("Processing");
for(double pr = 0; pr<500000000; pr++);
System.out.print(".");
for(double pr = 0; pr<500000000; pr++);
System.out.print(".");
for(double pr = 0; pr<500000000; pr++);
System.out.println(".");
System.out.println("Account Successfully created! Your account no is = " + accno);
}
else
{
System.out.println("Sorry, the minimum amount must be Rs 500");
}
}
public static void main()
{
try
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
FileInputStream n = new FileInputStream("savefile.sav");
ObjectInputStream lo = new ObjectInputStream(n);
Account accarray[] = (Account[])lo.readObject();
int i = 0;
System.out.println("Welcome to Soumya Bank! ☺");
for(;;)
{
for(double pr = 0; pr<500000000; pr++);
System.out.println("Enter 1 if you're an user \nEnter 2 if you are an employee \nPress 0 to exit");
int ch1 = Integer.parseInt(in.readLine());
if(ch1 == 0)
{
try
{
FileOutputStream fos = new FileOutputStream("savefile.sav");
ObjectOutputStream os = new ObjectOutputStream(fos);
os.writeObject(accarray);
os.flush();
os.close();
}
catch(Exception e)
{
}
break;
}
switch(ch1)
{
case 1:
for(;;)
{
System.out.println("Press 1 to create a new Account. \nPress 2 to view current account status. \nPress 3 to withdraw amount. \nPress 4 to deposit amount");
int ch2 = Integer.parseInt(in.readLine());
String ac ="";
if(ch2 != 1 && ch2 <=4)
{
System.out.println("Enter account no.");
ac = in.readLine();
}
switch (ch2)
{
case 1:
accarray[i] = new Account();
accarray[i].newAcc(i);
i++;
break;
case 2:
int c = -1;
for(int a = 0; a<i;a++)
{
if(ac.equals(accarray[a].accno) == true)
{
accarray[a].view();
c=a;
break;
}
}
if(c == -1)
{
System.out.println("Not found!");
}
break;
case 3:
int c1 = -1;
for(int a = 0; a<i;a++)
{
if(ac.equals(accarray[a].accno) == true)
{
System.out.println("Welcome, " + accarray[a].name + ", Enter amount to be withdrawn");
accarray[a].withdraw(Double.parseDouble(in.readLine()));
c1=a;
}
}
if(c1 == -1)
{
System.out.println("Not found!");
}
break;
case 4:
int c2 = -1;
for(int a = 0; a<i;a++)
{
if(ac.equals(accarray[a].accno) == true)
{
System.out.println("Welcome, " + accarray[a].name + ", Enter amount to be deposited");
accarray[a].deposit(Double.parseDouble(in.readLine()));
c2=a;
}
}
if(c2 == -1)
{
System.out.println("Not found!");
}
}
System.out.println("Press 0 for access menu,9 to exit to main menu");
int chx = Integer.parseInt(in.readLine());
if(chx == 0)
{
continue;
}
else
{
break;
}
}
break;
case 2:
System.out.print("Enter your 8 digit Verification ID :");
int vid = Integer.parseInt(in.readLine());
int sum=0;
while(vid>0)
{
sum = sum + vid%10;
vid = vid/10;
}
if(sum == 35)
{
System.out.println("Access Granted!");
for(;;)
{
System.out.println("Enter 1 to know the no of members \nEnter 2 to know the total amount deposited \nEnter 3 to display the list of members along with their account balance");
int ch3 = Integer.parseInt(in.readLine());
switch(ch3)
{
case 1:
System.out.println("Total no of members = " + i);
break;
case 2:
double s=0;
for(int pr = 0; pr<i;pr++)
{
s = s + accarray[pr].bal;
}
System.out.println("Total amount deposited = " + s );
break;
case 3:
System.out.println("Name Account No Balance");
for(int pr = 0; pr<i;pr++)
{
System.out.println(accarray[pr].name + " " + accarray[pr].accno + " " + accarray[pr].bal);
}
break;
}
System.out.println("Press any key to continue or 0 to exit to menu");
int ch4 = Integer.parseInt(in.readLine());
if(ch4==0)
{
break;
}
else
{
continue;
}
}
}
else
{
System.out.println("Access Denied, Invalid Verification ID");
}
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}