Final
unknown
java
2 years ago
23 kB
10
Indexable
package Q1; import java.util.*; public class Testing { public static void main(String[] args) { int choice=0,limit=100, i=0,j=0, temp=0, k=0, accnum=0, value=0,owner=-1,receiver,location=-1; //i is for saving, j is for current, k is counter String select=null, namesend, status="default", acctype="N/A"; BankAccount [] s = new SavingAccount[limit]; BankAccount [] c = new CurrentAccount[limit]; Random rand = new Random(); String name,bank; try (Scanner scan = new Scanner(System.in)) { System.out.println("Hey, since this is your first time using this program, we suggest you to create a new account\nignore this message if you have created one"); while (true) { System.out.println("Welcome to Da Bank! What would you like to do today?\n[1- Creating a new account]\n[2- Make Transaction]\n[0- End the program]"); System.out.print("Enter here: "); choice=scan.nextInt(); if(choice==0) break; if(choice==1)//Creating Account { temp = rand.nextInt(9999-1001+1)+1001; System.out.print("\nGreetings, Which Account would you like to create?\n[1- Saving Account]\n[2 -Current Account]\nEnter Here: "); choice=scan.nextInt(); System.out.print("Insert your full name: "); scan.nextLine(); name= scan.nextLine(); System.out.print("Insert your Bank name: "); bank= scan.nextLine(); if(choice==1) { for(int a=0 ; a<k ; a++) { if(c[a]==null) { if(s[a]!=null) { if(temp==s[a].getAccountNumber() ) { while(temp==s[a].getAccountNumber()) { temp = rand.nextInt(9999-1001+1)+1001; if(temp!=s[a].getAccountNumber()) break; } } } } else if(c[a]!=null) { if(s[a]!=null) { if(temp==s[a].getAccountNumber() || temp== c[a].getAccountNumber()) { while(temp==s[a].getAccountNumber() || temp== c[a].getAccountNumber()) { temp = rand.nextInt(9999-1001+1)+1001; if(temp!=s[a].getAccountNumber() || temp!= c[a].getAccountNumber()) break; } } } else if(s[a]==null) { if(temp==c[a].getAccountNumber() ) { while(temp==c[a].getAccountNumber()) { temp = rand.nextInt(9999-1001+1)+1001; if(temp!=c[a].getAccountNumber()) break; } } } } } s[i]= new SavingAccount(name,bank); s[i].setAccountNumber(temp); System.out.println(s[i]); i++; if(k==0) { owner=0; select="s"; } } if(choice==2) { for(int a=0 ; a<k ; a++) { if(s[a]==null) { if(temp==c[a].getAccountNumber() ) { while(temp==c[a].getAccountNumber()) { temp = rand.nextInt(9999-1001+1)+1001; if(temp!=c[a].getAccountNumber()) break; } } } else if(s[a]!=null) { if(c[a]==null) { if(temp==s[a].getAccountNumber()) { while(temp==s[a].getAccountNumber()) { temp = rand.nextInt(9999-1001+1)+1001; if(temp!=s[a].getAccountNumber()) break; } } } else if(c[a]!=null) { if(temp==s[a].getAccountNumber() || temp== c[a].getAccountNumber()) { while(temp==s[a].getAccountNumber() || temp== c[a].getAccountNumber()) { temp = rand.nextInt(9999-1001+1)+1001; if(temp!=s[a].getAccountNumber() || temp!= c[a].getAccountNumber()) break; } } } } } c[j]= new CurrentAccount(name,bank); c[j].setAccountNumber(temp); System.out.println(c[j]); j++; if(k==0) { owner=0; select="c"; } } k++; choice =0; } if(choice == 2)//Make a transaction { System.out.print("\nHi! What transaction would you like to make?\n1- Display Credentials\n2- Deposit\n3- Withdraw\n4- Transfer\n\nEnter here: "); choice=scan.nextInt(); if(choice==1)//Display stuffs { if(select == "s") { System.out.println("Here are your details"); System.out.println(s[owner]); } if(select=="c") { System.out.println("Here are your details"); System.out.println(c[owner]); } } if(choice==2)//Deposit { if(select == "s") { System.out.println("How much do you want to deposit?"); System.out.print("\nEnter here: "); value=scan.nextInt(); s[owner].setBalance(s[owner].getBalance()+(double)value); } if(select=="c") { System.out.println("How much do you want to deposit?"); System.out.print("\nEnter here: "); value=scan.nextInt(); c[owner].setBalance(c[owner].getBalance()+(double)value); } } if(choice==3)//Withdrawal { if(select == "s") { while(true) { try { System.out.println("How much do you want to withdraw?"); System.out.print("\nEnter here: "); value=scan.nextInt(); if(value > s[owner].getBalance()) throw new Exception("Withdrawal amount exceeded the current balance"); System.out.println("Status: Successfully Withdraw "); break; }catch(InputMismatchException e){ System.out.println("Invalid Entry"); }catch(Exception e){ System.out.println("Status: Incompleted. Reason: " + e.getMessage()); } } s[owner].setBalance(s[owner].getBalance()-(double)value); } if(select=="c") { while(true) { try { System.out.println("How much do you want to withdraw?"); System.out.print("\nEnter here: "); value=scan.nextInt(); if(value > c[owner].getBalance()) throw new Exception("Withdrawal amount exceeded the current balance"); System.out.println("Status: Successfully Withdraw "); break; }catch(InputMismatchException e){ System.out.println("Invalid Entry"); }catch(Exception e){ System.out.println("Status: Incompleted. Reason: " + e.getMessage()); } } c[owner].setBalance(c[owner].getBalance()-(double)value); } } if(choice==4)//Transfer { while(true) { try { System.out.print("Please enter an amount you want to transfer: "); value =scan.nextInt(); scan.nextLine(); System.out.print("Please enter name of a sender: "); namesend =scan.nextLine(); System.out.print("Please enter the receiver account number: "); receiver = scan.nextInt(); if(select =="s") { if(value>s[owner].getBalance()) { throw new Exception("Withdrawal Exceeded Balance"); } for(int a=0 ; a<k ;a++) { if(c[a]==null) { if(receiver!=s[a].getAccountNumber()) status="No"; else if(receiver==s[a].getAccountNumber()) { status="Yes"; acctype="s"; location=a; break; } } else if(c[a]!=null) { if(receiver!=s[a].getAccountNumber()) status="No"; else if(receiver==s[a].getAccountNumber()) { status="Yes"; acctype="s"; location=a; break; } if(receiver!=c[a].getAccountNumber()) status="No"; else if(receiver==c[a].getAccountNumber()) { status="Yes"; acctype="c"; location=a; break; } } } if(status=="No") throw new Exception("Account is not exist"); } if(select =="c") { if(value>c[owner].getBalance()) { throw new Exception("Withdrawal Exceeded Balance"); } for(int a=0 ; a<k ;a++) { if(c[a]==null) { if(receiver!=s[a].getAccountNumber()) status="No"; else if(receiver==s[a].getAccountNumber()) { status="Yes"; acctype="s"; location=a; break; } } else if(c[a]!=null) { if(receiver!=s[a].getAccountNumber()) status="No"; else if(receiver==s[a].getAccountNumber()) { status="Yes"; acctype="s"; location=a; break; } if(receiver!=c[a].getAccountNumber()) status="No"; else if(receiver==c[a].getAccountNumber()) { status="Yes"; acctype="c"; location=a; break; } } } if(status=="No") throw new Exception("Account is not exist"); } System.out.println("Status: Completed"); break; }catch(InputMismatchException e){ System.out.println("Invalid Entry"); }catch(Exception e){ System.out.println("Status: Incompleted " + e.getMessage()); } } if(select=="s") { if(acctype=="s") { s[owner].setBalance(s[owner].getBalance()-(double)value); s[location].setBalance(s[location].getBalance()-(double)value); } else if(acctype=="c") { s[owner].setBalance(s[owner].getBalance()-(double)value); c[location].setBalance(c[location].getBalance()-(double)value); } } if(select=="c") { if(acctype=="s") { c[owner].setBalance(c[owner].getBalance()-(double)value); s[location].setBalance(s[location].getBalance()-(double)value); } else if(acctype=="c") { c[owner].setBalance(c[owner].getBalance()-(double)value); c[location].setBalance(c[location].getBalance()-(double)value); } } } } System.out.print("\nWould you like to continue? [1- Yes || 0-No]\nEnter Here: "); choice=scan.nextInt(); if(choice == 0) break; } } } } class BankAccount { protected String fullName, BankName,accType; protected int accountNumber; protected Double interestRate, balance; public BankAccount(String fullName, String bankName) { this.fullName = fullName; BankName = bankName; } public BankAccount() { } public String getFullName() { return fullName; } public void setFullName(String fullName) { this.fullName = fullName; } public String getBankName() { return BankName; } public void setBankName(String bankName) { BankName = bankName; } public int getAccountNumber() { return accountNumber; } public void setAccountNumber(int accountNumber) { this.accountNumber = accountNumber; } public String getAccType() { return accType; } public void setAccType(String accType) { this.accType = accType; } public Double getInterestRate() { return interestRate; } public void setInterestRate(Double interestRate) { this.interestRate = interestRate; } public Double getBalance() { return balance; } public void setBalance(Double balance) { this.balance = balance; } @Override public String toString() { return "\nYour Details \nfullName=" + fullName + "\nBankName=" + BankName + "\naccountNumber=" + accountNumber + "\naccType=" + accType + "\ninterestRate=" + interestRate + "\nbalance=" + balance ; } } class SavingAccount extends BankAccount { double Newbalance=50; double NewinterestRate = 0.0005; public SavingAccount(){ super(); } public SavingAccount(String name, String BankName){ super(name, BankName); this.setBalance(Newbalance); this.setInterestRate(NewinterestRate); this.setAccType("Saving Account"); } } class CurrentAccount extends BankAccount { double Newbalance=200; double NewinterestRate = 0.0001; public CurrentAccount(){ super(); } public CurrentAccount(String name, String BankName) { super(name, BankName); this.setBalance(Newbalance); this.setInterestRate(NewinterestRate); this.setAccType("Current Account"); } }
Editor is loading...