In: Computer Science
Assignment 4: Objects that contain objects
In this assignment, you will implement a Java application, called
BankApplication, that can be used to manage bank accounts. The
application is to be implemented using three classes, called
Account, Customer, and BankDriver. The description of these classes
is below.
Problem Description
class Account
The Account class keeps track of the balance in a bank account with
a varying annual interest rate. The Account class is identified as
follows:
• two double instance variables, called balance and
annualRate.
• A constructor that takes two input parameters to initialize the
balance and annualRate instance variables.
• No-argument constructor to set both the balance and annualRate to
zero.
• Getters for both instance variables.
• deposit: a method than takes an amount (of type double) and adds
that amount of money to account's balance.
• withdraw: a method than takes an amount (of type double) and
withdraws that amount of money from the balance. If the amount to
be withdrawn is greater than the current balance, then the method
displays as error message and does not change the balance.
• addInterest: a method that adds interest to the balance at the
current rate. The method takes an input (of type int) indicating
how many years-worth of interest to be added. For example, if the
account's balance is 1000 and annualRate is 0.05, the call
addInterest(3) will add 3-years-worth of interest (i.e.,
3*1000*0.05) to the current balance. (Note that interest
calculations can be more complex if the second year's interest is
to be calculated using the first's year balance after adding first
year's interest and so on. However, for simplicity, you are not
required to implement this more complex interest
calculations).
• equals: a method to test the equality of two accounts. Two
accounts are considered to be equal if they have the same balance
and the same annualRate.
• toString: a method that returns a nicely formatted string
description of the account.
class Customer
The Customer class represents a bank customer. A customer is
identified by the following instance variables:
• name: a string that represents customer name
• checkingAccount: an instance variable of type Account
• savingAccount: an instance variable of type Account
Implement the following methods for the Customer class:
• A constructor that takes as input only the customer's name and
creates a customer with zero balance and zero rate in both
accounts.
• A constructor that takes five inputs that represent the
customer's name and four double variables that represent the
customer's checking account's balance, checking account's annual
rate, saving account's balance, and saving account's annual
rate.
• Getter methods for the saving and checking accounts.
• getTotalBalance: a method that calculates and returns the total
balance of a customer (which is the sum of the checking account's
balance and the saving account's balance).
• creditAccount: a method that takes two inputs to represent the
account type and an amount of money to deposit. Note that account
type is either 1 for saving account or 2 for checking account. The
method then calls the appropriate Account's deposit method to add
the specified amount of money. For example, the call
creditAccount(1,500) adds 500 to the saving account while the call
creditAccount(2,500) adds 500 to the checking account. The method
should display an error message if the first input is any number
other than 1 or 2.
• debitAccount: similar to creditAccount but it withdraws money
form the specified account.
• addInterest: similar to creditAccount, this method takes as input
account type and number of years. The method then adds interest to
the specified account.
• toString: a method that returns a string representation of the
customer that includes customer name, details of customer's
accounts and customer’s total balance.
class BankDriver
Your driver class should do the following actions:
• creates two customers with your choice of names, balances, and
annual rate values.
• adds 1 year-worth of interest to all checking accounts.
• adds 3 years-worth of interest to all saving accounts.
• Check whether the checking account of the first customer is equal
to the checking account of the second cusomter.
• prints all customers’ information in a nicely formatted
output.
Important Requirements
The output of our program must be nicely formatted.
• The programs should display your name.
• At the top of the program include your name, a brief description
of the program and what it does and the due date.
• Add appropriate comments to your code.
• All code blocks must be indented consistently and correctly.
Blocks are delimited by opening and closing curly braces. Opening
and closing curly braces must be aligned consistently.
• Variable names should convey meaning.
• The program must be written in Java and submitted via D2L.
Draw a UML diagram of your application that includes the Account
and Customer classes but not the BankDriver class.
Name:- Utkarsh Jain
This is a program for Bank called "BankApplication". It contains three classes named as Account that stores information about your Account, Customer that store information about customers and BankDriver that performs all the operations for your Bank.
// Account Class
public class Account {
private double balance; // Account Balance
private final double annualRate; // Account AnnualRate
// No Args Constructor
public Account() {
this.balance = 0;
this.annualRate = 0;
}
// All Args Constructor
public Account(double balance, double annualRate) {
this.balance = balance;
this.annualRate = annualRate;
}
// Getter for Balance
public double getBalance() {
return balance;
}
// Getter for AnnualRate
public double getAnnualRate() {
return annualRate;
}
// To Deposit your Amount
public void deposit(double amount) {
this.balance += amount;
}
// To Withdraw your Amount
public void withdraw(double amount) {
if (amount > this.balance) {
System.out.println("Insufficient Balance, Your Account have only Rs. "+this.balance+" left.");
}
else {
this.balance -= amount;
}
}
// To Add Interest for particular year
public void addInterest(int years) {
this.balance += (this.balance*this.annualRate*years);
}
// Check whether 2 Accounts are equal or not
public boolean equals(Account account) {
return account.balance == this.balance && account.annualRate == this.annualRate;
}
// Return Account Details
public String toString() {
return "{" +
"balance=" + balance +
", annualRate=" + annualRate +
'}';
}
}
// Customer Class
public class Customer {
private final String name; // Customer Name
private final Account checkingAccount; // Customer Checking Account
private final Account savingAccount; // Customer Saving Account
// 1 params Constructor
public Customer(String name) {
this.name = name;
this.checkingAccount = new Account();
this.savingAccount = new Account();
}
// 5 params Constructor
public Customer(String name,double ch_Balance,double ch_annualRate,double s_Balance,double s_annualRate) {
this.name = name;
this.checkingAccount = new Account(ch_Balance,ch_annualRate);
this.savingAccount = new Account(s_Balance,s_annualRate);
}
// Getter for Checking Account
public Account getCheckingAccount() {
return checkingAccount;
}
// Getter for Saving Account
public Account getSavingAccount() {
return savingAccount;
}
// Get Total Balance of a Customer
public double getTotalBalance() {
return this.checkingAccount.getBalance()+this.savingAccount.getBalance();
}
// Credit Balance to your Account
public void creditAccount(int type,double amount) {
if (type == 1) {
this.checkingAccount.deposit(amount);
}
else if (type == 2) {
this.savingAccount.deposit(amount);
}
else {
System.out.println("Invalid Account Type, It must lies in [1,2].");
}
}
// Debit Balance to your Account
public void debitAccount(int type,double amount) {
if (type == 1) {
this.checkingAccount.withdraw(amount);
}
else if (type == 2) {
this.savingAccount.withdraw(amount);
}
else {
System.out.println("Invalid Account Type, It must lies in [1,2].");
}
}
// Add a Interest to your Account
public void addInterest(int type,int years) {
if (type == 1) {
this.checkingAccount.addInterest(years);
}
else if (type == 2) {
this.savingAccount.addInterest(years);
}
else {
System.out.println("Invalid Account Type, It must lies in [1,2].");
}
}
// Return Customer Details
public String toString() {
return "Customer{" +
"name='" + name + '\'' +
", checkingAccount=" + checkingAccount +
", savingAccount=" + savingAccount +
'}';
}
}
// BankDriver Class
public class BankDriver {
// Main Method
public static void main(String[] args) {
// Creates 1st Customer
Customer customer1 = new Customer("Steve Job",10000,0.02,4000,0.05);
// Creates 2nd Customer
Customer customer2 = new Customer("Utkarsh Jain",100000,0.04,40000,0.06);
// Add Interest in Checking Account of Customer 1 for 1 year
customer1.addInterest(1,1);
// Add Interest in Checking Account of Customer 2 for 1 year
customer2.addInterest(1,1);
// Add Interest in Saving Account of Customer 1 for 3 year
customer1.addInterest(2,3);
// Add Interest in Saving Account of Customer 2 for 3 year
customer2.addInterest(2,3);
// Check whether 2 Customer Checking Accounts are same or not
if (customer1.getCheckingAccount().equals(customer2.getCheckingAccount())) {
System.out.println("Both Customers have Same Checking Account.");
}
else {
System.out.println("Both Customers have Different Checking Account.");
}
// Print Details of Customer 1
System.out.println(customer1.toString());
// Print Details of Customer 2
System.out.println(customer2.toString());
}
}