Question

In: Computer Science

Assignment 4: Objects that contain objects In this assignment, you will implement a Java application, called...

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.

Solutions

Expert Solution

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());
    }
}


Related Solutions

Assignment 1: JAVA Classes, Objects, and Constructors The goal of this assignment is to get you...
Assignment 1: JAVA Classes, Objects, and Constructors The goal of this assignment is to get you familiar with some of the most used JAVA syntax, as well as constructors objects, objects calling other objects, class and instance attributes and methods. You will create a small program consisting of Musician and Song classes, then you will have Musicians perform the Songs. Included is a file “Assignment1Tester.java”. Once you have done the assignment and followed all instructions, you should be able to...
In Java, design and implement a class called Cat. Each Cat class will contain three private...
In Java, design and implement a class called Cat. Each Cat class will contain three private variables - an integer representing its speed, a double representing its meowing loudness, and a String representing its name. Using the Random class, the constructor should set the speed to a random integer from 0 to 9, the meowing loudness to a random double, and the name to anything you want; the constructor should take no parameters. Write “get” and “set” methods for each...
For this assignment you will implement a dynamic array. You are to build a class called...
For this assignment you will implement a dynamic array. You are to build a class called MyDynamicArray. Your dynamic array class should manage the storage of an array that can grow and shrink. The public methods of your class should be the following: MyDynamicArray(); Default Constructor. The array should be of size 2. MyDynamicArray(int s); For this constructor the array should be of size s. ~MyDynamicArray(); Destructor for the class. int& operator[](int i); Traditional [] operator. Should print a message...
Java - Design and implement an application that creates a histogram that allows you to visually...
Java - Design and implement an application that creates a histogram that allows you to visually inspect the frequency distribution of a set of values. The program should read in an arbitrary number of integers that are in the range 1 to 100 inclusive; then produce a chart similar to the one below that indicates how many input values fell in the range 1 to 10, 11 to 20, and so on. Print one asterisk for each value entered. Sample...
Java 2D Drawing Application. The application will contain the following elements: a) an Undo button to...
Java 2D Drawing Application. The application will contain the following elements: a) an Undo button to undo the last shape drawn. b) a Clear button to clear all shapes from the drawing. c) a combo box for selecting the shape to draw, a line, oval, or rectangle. d) a checkbox which specifies if the shape should be filled or unfilled. e) a checkbox to specify whether to paint using a gradient. f) two JButtons that each show a JColorChooser dialog...
Java 2D Drawing Application. The application will contain the following elements: a) an Undo button to...
Java 2D Drawing Application. The application will contain the following elements: a) an Undo button to undo the last shape drawn. b) a Clear button to clear all shapes from the drawing. c) a combo box for selecting the shape to draw, a line, oval, or rectangle. d) a checkbox which specifies if the shape should be filled or unfilled. e) a checkbox to specify whether to paint using a gradient. f) two JButtons that each show a JColorChooser dialog...
In java, implement a program that returns a value of the area of the geometric objects...
In java, implement a program that returns a value of the area of the geometric objects listed below. Use the technique of method overloading concept and also uses the interfaces for dynamic method invocation. Triangle: √?(? − ?)(? − ?)(? − ?) where s=(a+b+c)/2 a,b,c are the sides of the triangle. Square: A^2 where A is the side Rectangle: (a*b) where a ,b are sides Circle: ? = ??2 where r is the radius Cube: 6a^2 where a is the...
[For Java Programming: Objects Class] Write a Java application program that prompts the user to enter...
[For Java Programming: Objects Class] Write a Java application program that prompts the user to enter five floating point values from the keyboard (warning: you are not allowed to use arrays or sorting methods in this assignment - will result in zero credit if done!). Have the program display the five floating point values along with the minimum and maximum values, and average of the five values that were entered. Your program should be similar to (have outputted values be...
Please do this in java program. In this assignment you are required to implement the Producer...
Please do this in java program. In this assignment you are required to implement the Producer Consumer Problem . Assume that there is only one Producer and there is only one Consumer. 1. The problem you will be solving is the bounded-buffer producer-consumer problem. You are required to implement this assignment in Java This buffer can hold a fixed number of items. This buffer needs to be a first-in first-out (FIFO) buffer. You should implement this as a Circular Buffer...
0. Introduction. In this assignment you will implement a stack as a Java class, using a...
0. Introduction. In this assignment you will implement a stack as a Java class, using a linked list of nodes. Unlike the stack discussed in the lectures, however, your stack will be designed to efficiently handle repeated pushes of the same element. This shows that there are often many different ways to design the same data structure, and that a data structure should be designed for an anticipated pattern of use. 1. Theory. The most obvious way to represent a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT