Created
June 25, 2018 00:48
-
-
Save LunaticWolf/e1d51262fd09e55db5f1db2721a6c20d to your computer and use it in GitHub Desktop.
Banking System
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Random; | |
import java.util.Scanner; | |
import static java.lang.System.out; | |
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | |
class Account{ | |
private String accountHolderName; | |
private double accountBalance; | |
private String accountNumber; | |
public Account(){} | |
public Account(String accountHolderName, double accountBalance,String accountNumber){ | |
Random random = new Random(); | |
this.accountNumber = accountNumber + random.nextInt(10) + random.nextInt(10) + random.nextInt(10) + random.nextInt(10) + random.nextInt(10); | |
this.accountHolderName = accountHolderName; | |
this.accountBalance = accountBalance; | |
} | |
void deposit(double val){ | |
this.accountBalance += val; | |
} | |
protected double getAccountBalance(){ | |
return accountBalance; | |
} | |
protected String getAccountNumber(){ | |
return accountNumber; | |
} | |
protected String getaccountHolderName(){ | |
return accountHolderName; | |
} | |
protected void setAccountBalance(double accountBalance){ | |
this.accountBalance = accountBalance; | |
} | |
void display(){ | |
out.println("\n\nThis Account Belongs to - "); | |
out.println("\nAccount Holder - " + this.getaccountHolderName()); | |
out.println("\nAccount Number - " + this.getAccountNumber()); | |
out.println("\nAccount Balance - " + this.getAccountBalance()); | |
} | |
} | |
//---------------------------------------------------------------------------------------------------------------------- | |
class SavingAccount extends Account{ | |
private int interest = 5; | |
private final int minimumbal = 500; | |
private double maxwithDraw; | |
public SavingAccount(String accountHolderName, double accountBalance, double maxwithDraw){ | |
super(accountHolderName, accountBalance, ""); | |
this.maxwithDraw = maxwithDraw; | |
} | |
public double getBalance(){ | |
return getAccountBalance() + (double) this.interest * getAccountBalance(); | |
} | |
void deposit(double deposit){ | |
super.deposit(deposit); | |
} | |
void withdraw(double deposit) | |
{ | |
if(this.maxwithDraw >= deposit && super.getAccountBalance() - deposit >= minimumbal){ | |
super.setAccountBalance((super.getAccountBalance() - deposit)); | |
out.println("\nYour Withdrawl amount is: " + deposit); | |
out.println("\n Your Main Account Balance is: " + getBalance()); | |
} | |
else{ | |
out.println("\nNot Enough Balance in your Account\n" ); | |
} | |
} | |
} | |
//------------------------------------------------------------------------------------------------------------------------ | |
class CurrentAccount extends Account{ | |
private int tradeLicenseNumber; | |
public CurrentAccount(String accountHolderName, double accountBalance, int number){ | |
super(accountHolderName, accountBalance, ""); | |
tradeLicenseNumber = number; | |
} | |
void deposit(double deposit){ | |
super.deposit(deposit); | |
} | |
public double getBalance(){ | |
return super.getAccountBalance(); | |
} | |
void withdraw(double deposit){ | |
if(super.getAccountBalance() >= deposit){ | |
super.setAccountBalance((super.getAccountBalance() - deposit)); | |
out.println("\nYour Withdrawl amount is: " + deposit); | |
out.println("\n Your Main Account Balance is: " + super.getAccountBalance()); | |
} | |
else{ | |
out.println("\nNot Enough Balance in your Account" ); | |
} | |
} | |
} | |
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | |
public class Banking{ | |
public static void main(String[] args){ | |
Scanner scanner = new Scanner(System.in); | |
out.println("\nEnter Account Holder Name - "); | |
String name = scanner.next(); | |
out.println("\nEnter the type of Account you want to open.. \n\nPress 1. To Open a Savings account. or,\nPress 2. To Open a Current Account.\n"); | |
int n = scanner.nextInt(); | |
out.println("\n\nEnter Initial Balance - "); | |
double balance = scanner.nextDouble(); | |
// double bal = balance + 0.05 * balance; | |
char c; | |
double prev; | |
if(1==n){ | |
out.println("Enter the max wthdraw limit"); | |
double maxlimit = scanner.nextDouble(); | |
SavingAccount savingsaccount = new SavingAccount(name, balance, maxlimit); | |
out.println("\n Press 1. To Deposit. \n Press 2. To Withdraw Money. \nPress 3. To Display Account Balance."); | |
int choice = scanner.nextInt(); | |
switch(choice){ | |
case 1: | |
out.println("\nEnter Amount to deposit - "); | |
double deposit = scanner.nextDouble(); | |
prev = savingsaccount.getBalance(); | |
savingsaccount.deposit(deposit); | |
out.println("Do you want to see your balance y/n ?"); | |
c = scanner.next().charAt(0); | |
if(c == 'y'){ | |
out.println("Your balance before deposit is :" + prev); | |
out.println("Your balance after deposit is :" + savingsaccount.getBalance()); | |
} | |
break; | |
case 2 : | |
out.println("\n Enter Withdraw Amount - "); | |
double withdraw = scanner.nextDouble(); | |
prev = savingsaccount.getBalance(); | |
savingsaccount.withdraw(withdraw); | |
out.println("Do you want to see your balance y/n ?"); | |
c = scanner.next().charAt(0); | |
if(c == 'y'){ | |
out.println("Your balance before withdrawl is :" + prev); | |
out.println("Your balance after withdrawl is :" + savingsaccount.getBalance()); | |
} | |
break; | |
case 3: | |
out.println("Your balance is :" + savingsaccount.getBalance()); | |
break; | |
default: out.println("\n Enter a valid option."); | |
} | |
// savingsaccount.display(); | |
return; | |
} | |
else if(2 == n){ | |
out.println("enter the trade license number"); | |
int number = scanner.nextInt(); | |
CurrentAccount currentaccount = new CurrentAccount(name, balance, number); | |
out.println("\n Press 1. To Deposit. \n Press 2. To Withdraw Money. \nPress 3. To Display Account balance. "); | |
int choice = scanner.nextInt(); | |
switch(choice){ | |
case 1: | |
out.println("\nEnter Amount to deposit - "); | |
double deposit = scanner.nextDouble(); | |
prev = currentaccount.getBalance(); | |
currentaccount.deposit(deposit); | |
out.println("Do you want to see your balance y/n ?"); | |
c = scanner.next().charAt(0); | |
if(c == 'y'){ | |
out.println("Your balance before deposit is :" + prev); | |
out.println("Your balance after deposit is :" + currentaccount.getBalance()); | |
} | |
break; | |
case 2: | |
out.println("\n Enter Withdraw Amount - "); | |
double withdraw = scanner.nextDouble(); | |
prev = currentaccount.getBalance(); | |
currentaccount.withdraw(withdraw); | |
out.println("Do you want to see your balance y/n ?"); | |
c = scanner.next().charAt(0); | |
if(c == 'y'){ | |
out.println("Your balance before withdrawl is :" + prev); | |
out.println("Your balance after withdrawl is :" + currentaccount.getBalance()); | |
} | |
break; | |
case 3: | |
out.println("Your balance is :" + currentaccount.getBalance()); | |
break; | |
default: | |
out.println("\n Enter a valid option."); | |
} | |
// currentaccount.display(); | |
return; | |
} | |
else{ | |
out.println("\nEnter a valid option ."); | |
} | |
scanner.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment