Skip to content

Instantly share code, notes, and snippets.

@situnamrit
Created May 27, 2017 09:48
Show Gist options
  • Save situnamrit/9ebcc1095afe19b30a0b2cf32d2432c5 to your computer and use it in GitHub Desktop.
Save situnamrit/9ebcc1095afe19b30a0b2cf32d2432c5 to your computer and use it in GitHub Desktop.
import java.util.Scanner;
class BankAccount
{
String customerName;
String address;
int accountNumber;
double accountBalance;
boolean canWithdraw;
BankAccount()
{
customerName = "";
address = "";
accountNumber = 1;
accountBalance = 0.0;
}
BankAccount(String name, String addr, int acNum, double balance)
{
customerName = name;
address = addr;
accountNumber = acNum;
accountBalance = balance;
canWithdraw = true;
}
double getBalance()
{
return accountBalance;
}
void deposit(double amount) {
if(accountBalance < 0.0)
accountBalance = 0.0;
accountBalance += amount;
}
void withdraw(double amount)
{
if(amount < accountBalance && canWithdraw)
accountBalance -= amount;
else
System.out.println("Insufficient funds! Your withdrawal amount exceeds your balance by " + (amount - accountBalance));
}
}
class SavingsAccount extends BankAccount {
SavingsAccount(String name, String addr, int acNum, double bal)
{
super(name, addr, acNum, bal);
}
/* s = value after t periods
P = principal amount (initial investment)
j = annual nominal interest rate
m = number of times the interest is compounded per year
t = number of years
*/
void computeInterest(double rate) {
double s=0.0, t=0.0, principalAmount = accountBalance;
int m=0;
Scanner read = new Scanner(System.in);
System.out.println("Enter the number of times the interest is compounded yearly : ");
m = read.nextInt();
System.out.println("Enter the number of years : ");
t = read.nextDouble();
s = principalAmount * Math.pow(( (1 + (rate / m))), (m * t));
accountBalance = s;
}
}
class CurrentAccount extends BankAccount
{
static double serviceCharge = 0.05;
CurrentAccount(String name, String addr, int acNum, int bal)
{
super(name, addr, acNum, bal);
}
void checkMinimum()
{
if(accountBalance < 1000.0)
{
accountBalance -= (serviceCharge * accountBalance);
canWithdraw = false;
}
}
}
public class AccountDetails {
static void printSavingsBalance(SavingsAccount sav1,SavingsAccount sav2)
{
System.out.println("Balance = " + sav1.getBalance());
System.out.println("Balance = " + sav2.getBalance());
}
public static void main(String[] args) {
String name, address;
int accountNumber;
double annualInterestRate;
Scanner sc = new Scanner(System.in);
System.out.println("Savings Account 1 : ");
System.out.println("Enter your name: ");
name = sc.nextLine();
System.out.println("Enter your address: ");
address = sc.nextLine();
System.out.println("Enter your account number: ");
accountNumber = sc.nextInt();
SavingsAccount saver1 = new SavingsAccount(name, address, accountNumber, 50000.0);
System.out.println("\nSavings Account 2 - ");
System.out.println("Enter your name: ");
name = sc.next();
System.out.println("Enter your address: ");
address = sc.next();
sc.nextLine();
System.out.println("Enter your account number: ");
accountNumber = sc.nextInt();
SavingsAccount saver2 = new SavingsAccount(name, address, accountNumber, 80000);
annualInterestRate = 0.042;
System.out.println("Savings Account 1 : ");
saver1.computeInterest(annualInterestRate);
System.out.println("Savings Account 2 : ");
saver2.computeInterest(annualInterestRate);
printSavingsBalance(saver1, saver2);
System.out.println("\nCurrent Account 1 - ");
System.out.println("Enter your name: ");
name = sc.next();
System.out.println("Enter your address: ");
address = sc.next();
System.out.println("Enter your account number: ");
accountNumber = sc.nextInt();
sc.close();
CurrentAccount current1 = new CurrentAccount(name, address, accountNumber, 10000);
current1.checkMinimum();
System.out.println("Balance = " + current1.getBalance());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment