Question: In Java, code the following object oriented program focused around inheritance. See below code for classes that are already written. Note: Do not use instanceof
In Java, code the following object oriented program focused around inheritance. See below code for classes that are already written.
Note: Do not use instanceof at any point, but do use the Collections.sort() method for the ArrayLists as well as the DateFactory class.
Implement the following methods:
Class: Deposit
Type: Concrete
Superclass: Transaction
Fields: none
Constructor: Deposit (Date date, double amount, String description) passes the provided information on to Deposits superclass.
Methods: @Override public String toString() formats the deposit according to the sample
output provided. The String should contain the word Deposit as well as the date, description,
and amount (as a positive number) of the transaction.
Class: Withdrawal
Type: Abstract
Superclass: Transaction
Fields: none
Constructor: Withdrawal (Date date, double amount, String description) passes the
provided information on to Withdrawals superclass. Note that the amount passed on to the
superclass must be a negative number, even though amount will be specified as a positive
value.
Methods: none
Class: Debit
Type: Concrete
Superclass: Withdrawal
Fields: none
Constructor: Debit (Date date, double amount, String description) passes the provided information on to Withdrawals superclass.
Methods: @Override public String toString() formats the debit according to the sample
output provided. The String should contain the word Debit as well as the date, description,
and amount (as a positive number) of the transaction.
Class: Check
Type: Concrete
Superclass: Withdrawal
Fields: int mCheckNum the number of the check
String mMemo an additional text field that holds additional information about the check
Constructor: Check (Date date, double amount, String payee, int checkNum, String
memo) passes the first 3 parameters provided on to Checks superclass, and sets the other two parameters into the Checks fields.
Methods: Accessors and Mutators for each of the mCheckNum and mMemo fields. @Override public String toString() formats the check according to the sample output provided. The String should contain the word Check as well as the check number, date, payee (description), amount (as a positive number), and memo of the check.
Class: Account
Type: Concrete
Superclass: none (Object)
Fields: double mOpeningBalance specified via the constructor, specifies the initial balance in this account. double mBalance maintains the running balance in the account. List
Constructors: Account() no arguments constructor. Sets the opening balance to 0.0. Account(double openingBalance) constructor that sets the opening balance to the
specified amount.
Methods: Accessors for mOpeningBalance and mBalance. public void addTransaction(Transaction txn) adds the specified Transaction to
the list of transactions. public void printStatement() prints the statement according to the sample output and the rules below.
Bank Statement Format
The bank statement should be generated with the transactions ordered by transaction date. Since Transaction implements the Comparable
Your statement should begin with the opening balance, then list the details of each transaction (using the toString() method, or individual accessors if you prefer) and the running balance after that transaction. The formatting of your report should be similar to that shown below. If the transaction description is longer than 80 characters, it should be truncated to fit. The amounts should be displayed with two decimal points.
Already written:
Class: DateFactory
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateFactory
{
//declare a private constructor so that this class can't be
//instantiated.
private DateFactory(){}
//returns a date if the specified String can be converted to one,
//returns null otherwise. The input string must be in either "MM-DD-YYYY"
//or "MM/DD/YYYY" format.
public static Date stringToDate(String dateString) {
DateFormat df = new SimpleDateFormat("MM-dd-yyyy");
Date date = null;
try {
date = df.parse(dateString);
} catch (ParseException e) {
}
//maybe we were passed MM/DD/YYYY
if (date == null) {
df = new SimpleDateFormat("MM/dd/yyyy");
try {
date = df.parse(dateString);
} catch (ParseException e) {
}
}
return date;
}
//returns a String representing the short form of a specified
//Date ("MM/DD/YYYY")
public static String dateToString(Date date) {
DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
return df.format(date);
}
}
Class: Transaction
import java.util.Date;
//TODO: implement the Comparable
public abstract class Transaction implements Comparable
private Date mDate;
private double mAmount;
private String mDescription;
//constructor requires a txn date, amount (positive or negative), and a description
//There is no no-args constructor, so subclasses must call this constructor
public Transaction(Date date, double amount, String description) {
mDate = date;
mAmount = amount;
mDescription = description;
}
//accessors and mutators
public void setDate(Date date) {mDate = date;}
public Date getDate(){return mDate;}
public void setAmount(double amount){mAmount = amount;}
public double getAmount(){return mAmount;}
public void setDescription(String description){mDescription = description;}
public String getDescription(){return mDescription;}
//TODO: add the compareTo() method required by the Comparable interface
//to compare two transactions by date. This will allow us to sort a list of
//transactions by calling Collections.sort on our List
//our Account class. Hint: the Date class already has a compareTo() method - use it.
//optional abstract method if we want to provide an alternative to the toString method
//that would provide a description that could be used in the second Statement report
//public abstract String getReportDesc();
}
Thank you!
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
