In: Computer Science
JAVA
A simple Class in a file called Account.java is given below. Create two Derived Classes Savings and Checking within their respective .java files. (modify display() as needed )
1. Add New private String name (customer name) for both, add a New int taxID for Savings only.
2. Add equals() METHOD TO CHECK any 2 accounts
Demonstrate with AccountDemo.java in which you do the following:
3. Create 1 Savings Account and 3 Checking Accounts, where 2 checkings are the same. Check 2 equal accounts and 2 different accounts.
4. Withdraw 100 dollars from savings , deposit the same amount to a checking , then display accounts.
import java.util.*;
public class Account
{private int accNum; double balance;
public Account (int accNum, double balance)
{
this.accNum = accNum;
this.balance = balance;
}
public int getAccNum()
{
return accNum;
}
public double getBalance()
{
return balance;
}
public void deposit (double depositAmt)
{
double newBalance = balance + depositAmt;
}
public void withdraw (double withdrawAmt)
{…………….fill here………………….}
public void display()
{
System.out.println();
System.out.println("Account Number :" + accNum );
System.out.println("Balance :" + balance);
}
}
CODE:
package com.company;
import java.io.*;
import java.util.Objects;
class Account {
private int accNum;
double balance;
//constructor
public Account (int accNum, double balance) {
this.accNum = accNum;
this.balance = balance;
}
//getters and setters
public int getAccNum() {
return accNum;
}
public double getBalance() {
return balance;
}
//deposit method
public void deposit (double depositAmt) {
double newBalance = balance + depositAmt;
balance = newBalance;
}
//withdraw method
public void withdraw (double withdrawAmt) {
//withdrawal Amount will be deducted from the balance only
//when it is less than equal to the balance
if(withdrawAmt <= balance)
balance -= withdrawAmt;
else{
System.out.println("Withdraw amount greater than the balance");
}
}
//display method
public void display() {
System.out.println();
System.out.println("Account Number :" + accNum );
System.out.println("Balance :" + balance);
}
}
class Savings extends Account{
private String name;
private int taxId;
//constructor
public Savings(String name,int taxId, int accNum, double balance) {
super(accNum, balance);
this.name = name;
this.taxId = taxId;
}
@Override
public boolean equals(Object o) {
//if the object is equal to this then return true
if (this == o)
return true;
//if the object is not an instance of Savings
if (!(o instanceof Savings))
return false;
Savings savings = (Savings) o;
//if all the parameters are same then returns true
return taxId == savings.taxId && Objects.equals(name, savings.name)
&& getAccNum() == savings.getAccNum() && getBalance() == savings.getBalance();
}
@Override
public void display() {
super.display();
System.out.println("Name: "+name);
System.out.println("TaxId: "+taxId);
}
}
class Checking extends Account{
private String name;
//constructor
public Checking(String name, int accNum, double balance) {
super(accNum, balance);
this.name = name;
}
@Override
public boolean equals(Object obj) {
//if the object is equal to this then return true
if (this == obj)
return true;
//if the object is not an instance of Checking
if (!(obj instanceof Checking))
return false;
Checking checking = (Checking) obj;
//if all the parameters are same then returns true
return Objects.equals(name, checking.name)
&& getAccNum() == checking.getAccNum() && getBalance() == checking.getBalance();
}
//display method
@Override
public void display() {
super.display();
System.out.println("Name: "+name);
}
}
//main driver class
class Main {
//main method
public static void main(String[] args) throws IOException {
//1 savings account
Savings savings = new Savings("John",124,1626,1352.7);
//3 checking account
Checking checking1 = new Checking("Harry",5156,363.2);
Checking checking2 = new Checking("Matt",5176,3623.2);
Checking checking3 = new Checking("Harry",5156,363.2);
//printing the result of two equals call methods
System.out.println(checking1.equals(checking3));
System.out.println(checking1.equals(checking2));
//withdrawing $100 from savings
savings.withdraw(100);
//depositing $100 to checking
checking2.deposit(100);
//displaying two accounts
savings.display();
checking2.display();
}
}
___________________________________________
CODE IMAGES:




_______________________________________________
OUTPUT:

________________________________________________
Feel free to ask any questions in the comments section
Thank You!