In: Computer Science
For an application of your choice, describe the problem you aim to solve, design the necessary classes, and implement your design using Java. Your programs should contain at least two classes, and a driver class in which you demonstrate the functionality of your application.
You are required to submit the following documents at the completion of your project in Module 8:
SOLUTION :-
Created 3 classes BankAccount, SavingsAccount, CheckingAccount and a driver class Driver.java.
BankAccount.java consists of basic bank account details including account number, balance, created date and a static variable numAccounts which keeps the track of created accounts. It has methods to deposit, withdraw and the required getters and a toString method.
SavingsAccount.java is extended from BankAccount. So it has an additional variable interestRate and a method to calculate interest rate as per the current balance.
CheckingAccount.java is also extended from BankAccount, with an additional monthlyFee attribute and deductFee() method.
The driver class will prompt the user to create one SavingsAccount object and one CheckingAccount object with initial balance, and performs several operations with it.
Javadoc comments are included and feel free to ask if you have any doubts.
//BankAccount.java
import java.util.Calendar;
import java.util.Date;
public class BankAccount{
private static int numAccounts;
protected int accNumber;
protected double balance;
protected Date createdDate;
public BankAccount(double initbalance) {
numAccounts++;
accNumber = numAccounts;/*
* so that each time an account gets created, it'll
* get a new ID
*/
balance = initbalance;
createdDate = Calendar.getInstance().getTime();
}
/**
* method to deposit amount
*/
public void deposit(double add) {
balance += add;
}
/**
* method to withdraw amount
*/
public void withdraw(double minus) {
balance -= minus;
}
/**
* required getters
*/
public int getAccountNumber() {
return accNumber;
}
public double getBalance() {
return balance;
}
public Date getCreatedDate() {
return createdDate;
}
/**
* returns a string containing all data
*/
@Override
public String toString() {
return "Account Number: " + accNumber + ", Balance: " + balance
+ ", Date Created: " + createdDate.toString();
}
}
//SavingsAccount.java
/**
* basic savings account class
*/
public class SavingsAccount extends BankAccount {
private double interestRate;
public SavingsAccount(double initbalance, double interestRate) {
super(initbalance);
this.interestRate = interestRate;
}
/**
* returns the calculated interest amount (wont update the balance)
*/
public double calculateInterest() {
return getBalance() * interestRate/100;
}
/**
* getter and setter for interest rate
*/
public double getInterestRate() {
return interestRate;
}
public void setInterestRate(double interestRate) {
this.interestRate = interestRate;
}
/**
* returns a string containing all data
*/
@Override
public String toString() {
return super.toString() + ", Interest Rate: " + interestRate;
}
}
//CheckingAccount.java
/**
* checking account class
*/
public class CheckingAccount extends BankAccount {
private double monthlyFee;
public CheckingAccount(double initbalance, double monthlyFee) {
super(initbalance);
this.monthlyFee = monthlyFee;
}
public void deductFee() {
balance -= monthlyFee;
}
public double getMonthlyFee() {
return monthlyFee;
}
public void setMonthlyFee(double monthlyFee) {
this.monthlyFee = monthlyFee;
}
/**
* returns a string containing all data
*/
@Override
public String toString() {
return super.toString() + ", Monthly Fee: " + monthlyFee;
}
}
//Driver.java
import java.util.Scanner;
public class Driver {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
try {
System.out.println("Enter the amount of money to create a SavingsAccount");
double amount = Double.parseDouble(scanner.nextLine());
System.out.println("Enter the interest rate of the SavingsAccount");
double interest = Double.parseDouble(scanner.nextLine());
/**
* creating a saving account using input data
*/
SavingsAccount savings = new SavingsAccount(amount, interest);
System.out.println("Savings account created,\n" + savings);
System.out.println("Enter the amount of money to create a CheckingAccount");
amount = Double.parseDouble(scanner.nextLine());
System.out.println("Enter the monthly fee of the CheckingAccount");
double fee = Double.parseDouble(scanner.nextLine());
/**
* creating a checking account using input data
*/
CheckingAccount checking = new CheckingAccount(amount, fee);
System.out.println("Checking account created,\n" + checking);
System.out.println("Amount to deposit into the SavingsAccount?");
amount = Double.parseDouble(scanner.nextLine());
/**
* depositing amount to savings account
*/
savings.deposit(amount);
System.out.println("Amount deposited,\n" + savings);
System.out.println("Amount to withdraw from the CheckingAccount?");
amount = Double.parseDouble(scanner.nextLine());
/**
* withdrawing amount from checking account
*/
checking.withdraw(amount);
System.out.println("Amount withdrawn,\n" + checking);
System.out.println("Calculating and adding interest of SavingsAccount.");
/**
* calculating the interest amount depositing amount to savings
* account
*/
double i = savings.calculateInterest();
savings.deposit(i);
System.out.println("Deducting monthly fee from CheckingAccount.");
/**
* deducting monthly rate from checking acc
*/
checking.deductFee();
/**
* displaying final summary
*/
System.out.println(savings);
System.out.println(checking);
} catch (Exception e) {
System.out.println("Invalid input, program is quitting...");
System.exit(1);
}
}
}
/*output*/
Enter the amount of money to create a SavingsAccount
120
Enter the interest rate of the SavingsAccount
7
Savings account created,
Account Number: 1, Balance: 120.0, Date Created: Tue Dec 05 09:19:35 IST 2017, Interest Rate: 7.0
Enter the amount of money to create a CheckingAccount
250
Enter the monthly fee of the CheckingAccount
40
Checking account created,
Account Number: 2, Balance: 250.0, Date Created: Tue Dec 05 09:19:47 IST 2017, Monthly Fee: 40.0
Amount to deposit into the SavingsAccount?
300
Amount deposited,
Account Number: 1, Balance: 420.0, Date Created: Tue Dec 05 09:19:35 IST 2017, Interest Rate: 7.0
Amount to withdraw from the CheckingAccount?
200
Amount withdrawn,
Account Number: 2, Balance: 50.0, Date Created: Tue Dec 05 09:19:47 IST 2017, Monthly Fee: 40.0
Calculating and adding interest of SavingsAccount.
Deducting monthly fee from CheckingAccount.
Account Number: 1, Balance: 449.4, Date Created: Tue Dec 05 09:19:35 IST 2017, Interest Rate: 7.0
Account Number: 2, Balance: 10.0, Date Created: Tue Dec 05 09:19:47 IST 2017, Monthly Fee: 40.0