Hello I need help my Object Oriented Programming Project. Here is my code: import java.util.*; import java.io.*; class Bnk_Account{ String Acc_name; String Acc_type; int Acc_num; int Acc_Balance; Bnk_Account(){ } Bnk_Account(String name,int Pin_Code,int Bal,String type){ Acc_name=name; Acc_num=Pin_Code; Acc_Balance=Bal; Acc_type=type; } } class create_account extends Bnk_Account{ create_account(String name,int Pin_Code,int Bal,String type){ Acc_name=name; Acc_num=Pin_Code; Acc_Balance=Bal; Acc_type=type; } create_account(){ super(); } void insert(String name,int Pin_Code,String type,int Bal){ Acc_name=name; Acc_type=type; Acc_num=Pin_Code; Acc_Balance=Bal; } void display_details(){ System.out.println("\t\tDepositor Name :" +Acc_name); System.out.println("\t\tAccount Number : "+Acc_num); System.out.println("\t\tAccount Balance : ₱"+Acc_Balance); System.out.println("\t\tAccount Type : "+Acc_type); } int deposite(int Balance){ Acc_Balance=Acc_Balance+Balance; return Balance; } int Withdraw_Acc(int Withdraw){ Acc_Balance=Acc_Balance-Withdraw; return Acc_Balance; } } public class Main{ public static void main(String[] args) { String user_name=null,type=null; int Balance=0,Pin=0; int Withdraw=0,Curr_Bal=0; int aNumber = 0; aNumber = (int)((Math.random() * 9000)+1000); create_account user = new create_account("user",0,0,"savings"); Scanner in = new Scanner(System.in); Scanner strng=new Scanner(System.in); int userChoice; boolean quit = false; do { System.out.println("Banking System: "); System.out.println("1. Create Account"); System.out.println("2. Deposit money"); System.out.println("3. Withdraw money"); System.out.println("4. Check Balance"); System.out.println("5. Display Account Details"); System.out.println("0. Quit Program: \n"); System.out.print("Enter Your Choice : "); userChoice = in.nextInt(); switch (userChoice) { case 1: System.out.print("\033[H\033[2J"); System.out.flush(); System.out.println("*******************************"); System.out.println("\tAccount Details"); System.out.println("*******************************"); System.out.print("Enter your Name: "); user_name=strng.nextLine(); System.out.print("Enter Accout Type: "); type=in.next(); System.out.print("Enter Accout Balance: ₱"); Balance=in.nextInt(); user.insert(user_name, aNumber, type, Balance); System.out.print("\033[H\033[2J"); System.out.flush(); System.out.println("\t\tHere Are Your Account Details"); System.out.println("\tDont Forget Your Assigned Account Number"); System.out.println("========================================================"); user.display_details(); break; case 2: System.out.print("\033[H\033[2J"); System.out.flush(); System.out.print("Enter your account Number: "); Pin=in.nextInt(); if(Pin==user.Acc_num){ System.out.print("Enter Amount Of Money To Deposite: ₱"); Balance=in.nextInt(); user.Acc_Balance=Balance; Balance = user.deposite(Balance); System.out.print("\033[H\033[2J"); System.out.flush(); System.out.println("\n\tMoney Successfully Deposited."); } else System.out.println("Wrong Accoount Number."); break; case 3: System.out.print("\033[H\033[2J"); System.out.flush(); System.out.print("Enter your account Number: "); Pin=in.nextInt(); if(Pin==user.Acc_num){ if(user.Acc_Balance==0) System.out.print("Your Account is Empty."); else{ System.out.print("Enter Amout Of Money You Want To Withdraw: ₱"); Withdraw=in.nextInt(); if(Withdraw>user.Acc_Balance){ System.out.print("Enter Valid Amout of Money: ₱"); Withdraw=in.nextInt(); } else Curr_Bal= user.Withdraw_Acc(Withdraw); System.out.println("Your Current Balance: ₱"+ Curr_Bal); } } else System.out.println("Wrong Accoount Number."); break; case 4: System.out.print("\033[H\033[2J"); System.out.flush(); System.out.print("Enter your Account Number : "); Pin=in.nextInt(); if(Pin==user.Acc_num){ System.out.println("Your Current Balance: ₱"+user.Acc_Balance); } else System.out.println("Wrong Accoount Number."); break; case 5: System.out.print("\033[H\033[2J"); System.out.flush(); System.out.print("Enter your Account Number:"); Pin=in.nextInt(); if(Pin==user.Acc_num){ try{ FileOutputStream fout = new FileOutputStream("AccountDetails.txt"); PrintWriter cout = new PrintWriter(fout); cout.println("Current Account Details: "); cout.println("Depositor Name :" + user.Acc_name); cout.println("Account Number : " + user.Acc_num); cout.println("Account Balance : ₱" + user.Acc_Balance); cout.println("Account Type : " + user.Acc_type); cout.close(); FileInputStream fin = new FileInputStream("AccountDetails.txt"); Scanner details = new Scanner(fin); while (details.hasNext()) { System.out.println(details.nextLine()); } System.out.println(); } catch(Exception e){System.out.println(e);} }else System.out.println("Wrong Accoount Number."); break; case 0: quit = true; break; default: System.out.println("Invalid Input."); break; } System.out.println("\n"); } while (!quit); System.out.print("\033[H\033[2J"); System.out.flush(); System.out.println("Please Come Again Next Time!"); } } The problem is that if i create a second account the file handling rewrites the first account. how do I fix it?
Hello I need help my Object Oriented Programming Project.
Here is my code:
import java.util.*;
import java.io.*;
class Bnk_Account{
String Acc_name;
String Acc_type;
int Acc_num;
int Acc_Balance;
Bnk_Account(){
}
Bnk_Account(String name,int Pin_Code,int Bal,String type){
Acc_name=name;
Acc_num=Pin_Code;
Acc_Balance=Bal;
Acc_type=type;
}
}
class create_account extends Bnk_Account{
create_account(String name,int Pin_Code,int Bal,String type){
Acc_name=name;
Acc_num=Pin_Code;
Acc_Balance=Bal;
Acc_type=type;
}
create_account(){
super();
}
void insert(String name,int Pin_Code,String type,int Bal){
Acc_name=name;
Acc_type=type;
Acc_num=Pin_Code;
Acc_Balance=Bal;
}
void display_details(){
System.out.println("\t\tDepositor Name :" +Acc_name);
System.out.println("\t\tAccount Number : "+Acc_num);
System.out.println("\t\tAccount Balance : ₱"+Acc_Balance);
System.out.println("\t\tAccount Type : "+Acc_type);
}
int deposite(int Balance){
Acc_Balance=Acc_Balance+Balance;
return Balance;
}
int Withdraw_Acc(int Withdraw){
Acc_Balance=Acc_Balance-Withdraw;
return Acc_Balance;
}
}
public class Main{
public static void main(String[] args) {
String user_name=null,type=null;
int Balance=0,Pin=0;
int Withdraw=0,Curr_Bal=0;
int aNumber = 0;
aNumber = (int)((Math.random() * 9000)+1000);
create_account user = new create_account("user",0,0,"savings");
Scanner in = new Scanner(System.in);
Scanner strng=new Scanner(System.in);
int userChoice;
boolean quit = false;
do {
System.out.println("Banking System: ");
System.out.println("1. Create Account");
System.out.println("2. Deposit money");
System.out.println("3. Withdraw money");
System.out.println("4. Check Balance");
System.out.println("5. Display Account Details");
System.out.println("0. Quit Program: \n");
System.out.print("Enter Your Choice : ");
userChoice = in.nextInt();
switch (userChoice) {
case 1:
System.out.print("\033[H\033[2J");
System.out.flush();
System.out.println("*******************************");
System.out.println("\tAccount Details");
System.out.println("*******************************");
System.out.print("Enter your Name: ");
user_name=strng.nextLine();
System.out.print("Enter Accout Type: ");
type=in.next();
System.out.print("Enter Accout Balance: ₱");
Balance=in.nextInt();
user.insert(user_name, aNumber, type, Balance);
System.out.print("\033[H\033[2J");
System.out.flush();
System.out.println("\t\tHere Are Your Account Details");
System.out.println("\tDont Forget Your Assigned Account Number");
System.out.println("========================================================");
user.display_details();
break;
case 2:
System.out.print("\033[H\033[2J");
System.out.flush();
System.out.print("Enter your account Number: ");
Pin=in.nextInt();
if(Pin==user.Acc_num){
System.out.print("Enter Amount Of Money To Deposite: ₱");
Balance=in.nextInt();
user.Acc_Balance=Balance;
Balance = user.deposite(Balance);
System.out.print("\033[H\033[2J");
System.out.flush();
System.out.println("\n\tMoney Successfully Deposited.");
}
else
System.out.println("Wrong Accoount Number.");
break;
case 3:
System.out.print("\033[H\033[2J");
System.out.flush();
System.out.print("Enter your account Number: ");
Pin=in.nextInt();
if(Pin==user.Acc_num){
if(user.Acc_Balance==0)
System.out.print("Your Account is Empty.");
else{
System.out.print("Enter Amout Of Money You Want To Withdraw: ₱");
Withdraw=in.nextInt();
if(Withdraw>user.Acc_Balance){
System.out.print("Enter Valid Amout of Money: ₱");
Withdraw=in.nextInt();
}
else
Curr_Bal= user.Withdraw_Acc(Withdraw);
System.out.println("Your Current Balance: ₱"+ Curr_Bal);
}
}
else
System.out.println("Wrong Accoount Number.");
break;
case 4:
System.out.print("\033[H\033[2J");
System.out.flush();
System.out.print("Enter your Account Number : ");
Pin=in.nextInt();
if(Pin==user.Acc_num){
System.out.println("Your Current Balance: ₱"+user.Acc_Balance);
}
else
System.out.println("Wrong Accoount Number.");
break;
case 5:
System.out.print("\033[H\033[2J");
System.out.flush();
System.out.print("Enter your Account Number:");
Pin=in.nextInt();
if(Pin==user.Acc_num){
try{
FileOutputStream fout = new FileOutputStream("AccountDetails.txt");
PrintWriter cout = new PrintWriter(fout);
cout.println("Current Account Details: ");
cout.println("Depositor Name :" + user.Acc_name);
cout.println("Account Number : " + user.Acc_num);
cout.println("Account Balance : ₱" + user.Acc_Balance);
cout.println("Account Type : " + user.Acc_type);
cout.close();
FileInputStream fin = new FileInputStream("AccountDetails.txt");
Scanner details = new Scanner(fin);
while (details.hasNext())
{ System.out.println(details.nextLine()); }
System.out.println();
}
catch(Exception e){System.out.println(e);}
}else
System.out.println("Wrong Accoount Number.");
break;
case 0:
quit = true;
break;
default:
System.out.println("Invalid Input.");
break;
}
System.out.println("\n");
} while (!quit);
System.out.print("\033[H\033[2J");
System.out.flush();
System.out.println("Please Come Again Next Time!");
}
}
The problem is that if i create a second account the file handling rewrites the first account. how do I fix it?
Step by step
Solved in 3 steps with 1 images