The Money Class This class is used to track a USD amount consisting of two integ
ID: 3785448 • Letter: T
Question
The Money Class
This class is used to track a USD amount consisting of two integers to manage dollars and cents. All dollars and cents will be positive or 0, and cents will never exceed 99. The data and methods below will be called from the test driver I’ll use, so be sure to adhere to the names of the functions, as well as the number, order, and type of arguments handed to each function, as indicated below. Also, previous labs and lectures contains hints and details on how to build and test the Money or Date class.
Data Members
You should define a pair of instance variables for use in tracking dollars and cents These should be private and avoid privacy leaks.
Method Members
Money(int dol)
This constructor initializes using dollars input and assumes no cents
Remeber: Cents can never be higher than 99
Money(int dol, int cent)
This constructor initializes dollars and cents accordingly
Money(Money other)
This constructor could redirect to the one above
Getters/setters for dollars and cents
These should be careful to not invalidate class invariants
Also these functions should avoid privacy leaks
double getMoney()
A getter for the total monetary amount, as a double
Returns 5.75, for example
void setMoney(int dol, int cent)
Sets our dollars and cents, accordingly
This function should make use of setters for dollars and cents
void add(int dol)
Adds the int passed into the function to dollars
void add(int dol, int cents)
Adds to dollars and cents the two ints passed into the function.
void add(Money other)
Adds to this object the dollars and cents stored in the other object.
boolean equals(Object o)
Determines if this money object is equal to that Money object
String toString()
Prints out a Money object as a String, such as “$3.75”
The Bill Class
Construct a class for use as a bill that contains data related to an outstanding or paid bill of a specific amount of money. The class should contain the amount of the bill as a Money object, the due date of the bill (a Date object) and a Date object to track the date paid (null if not yet paid). Check to be sure that when paying the bill, the date isn’t past the due date – if so, either quit (System.exit()) with an error message or throw an exception. (Note: either build a Date class(more simple and direct) or use Java’s Date class.)
Data as Instance Variables:
amount – a Money object
dueDate – a Date object
paidDate – a Date object, null if not yet paid
originator – a String containing the company or institution who issued the bill
Methods in the Public Interface:
Bill(Money amount, Date dueDate, String originator)
constructor (null for paidDate)
Bill(Bill toCopy)
copy constructor
Date getDueDate() – return the dueDate*
String getOriginator() – return the originator
boolean isPaid() – return true if bill has been paid
void setPaid(Date onDay) – make the bill paid, save the date*
void setUnpaid() – make the bill unpaid
void setDueDate(Date nextDate) – set the due date of the bill*
void setAmount(Money amount) – change the amount*
Money getAmount() – provide the Money object for the bill that is the amount*
void setOriginator(String originator)* - change the originator
String toString() – build a string that reports the amount, when its due, to whom, whether paid, and if paid, the date paid.
boolean equals(Bill toCompare) – determine if the two bills are identical
*Note: The items starred above are to be considered critical sections in your code where it could be very easy to violate the class invariants that we’ve defined above. Proceed carefully in these functions and be sure that, for example, calling setPaid() with a date that is after the due date will fail, or that calling setDueDate() with a date that is before the paid date will also fail.
The Date Class
This should be similar to the Date classes we’ve covered in class and in the textbook. Do not use any built-in Date classes for this assignment, and provide all of the common class components for this Date. For example, you should add:
Data Members
These data items should be private or final
Constructors & copy constructors
Getters/setters for month, day and year
boolean equals(Object o)
String toString()
Method Members
Follow the Savitch examples for how to build the methods needed in a Date class.
The Driver should test all methods
Explanation / Answer
PROGRAM CODE:
Money.java
public class Money {
private int dollars;
private int cents;
//constructor for the class which sets dollar with this object's dollar and cents with zero
public Money(int dol)
{
dollars = dol;
cents = 0;
}
//sets this object with dollar and cents - constructor for the class
public Money(int dol, int cent)
{
dollars = dol;
if(cents >99)
{
dollars +=1;
cents = cents - 100;
}
else cents = cent;
}
//sets this object with dollar and cents from other object - copy constructor for the class
public Money(Money other)
{
this(other.getDollars(), other.getCents());
}
//returns the dollar for this object
public int getDollars() {
return dollars;
}
//sets the dollar for this object
public void setDollars(int dollars) {
this.dollars = dollars;
}
//returns the cents of this object
public int getCents() {
return cents;
}
//sets the cents for this object
public void setCents(int cents) {
this.cents = cents;
}
//returns the money in double format
public double getMoney()
{
return dollars + cents/100;
}
//sets the dollar and cents
public void setMoney(int dol, int cent)
{
setDollars(dol);
setCents(cent);
}
//adds the dollar to this dollar
public void add(int dol)
{
dollars += dol;
}
//adds the dollar and cent to this dollar and cents
public void add(int dol, int cent)
{
dollars += dol;
cents =+ cent;
if(cents > 99)
{
dollars += 1;
cents = cents-100;
}
}
//adds the other money to this money
public void add(Money other)
{
add(other.getDollars(), other.getCents());
}
//returns true of the other object is equal to this
public boolean equals(Object o)
{
Money other = (Money)o;
if(this.dollars == other.getDollars() && this.getCents() == other.getCents())
return true;
else return false;
}
//returns the money in the format $dollar.cent
public String toString() {
// TODO Auto-generated method stub
return "$" + getMoney();
}
}
Bill.java
public class Bill {
private Money amount;
private Date dueDate;
private Date paidDate;
private String originator;
//- constructor for the class Bill
public Bill(Money amount, Date dueDate, String originator)
{
this.amount = amount;
this.dueDate = dueDate;
this.originator = originator;
this.paidDate = null;
}
//- Copy constructor for the class
public Bill(Bill toCopy)
{
this.amount = toCopy.amount;
this.dueDate = toCopy.dueDate;
this.originator = toCopy.originator;
this.paidDate = toCopy.paidDate;
}
//- returns the dueDate
public Date getDueDate() throws Exception
{
if(paidDate == null)
return dueDate;
else if(paidDate.after(dueDate))
throw new Exception("The due date is before the paid date.");
else return dueDate;
}
//– returns the originator of the bill
public String getOriginator()
{
return originator;
}
// – returns true if bill has been paid
public boolean isPaid()
{
if(paidDate != null)
return true;
else return false;
}
//– make the bill paid, save the date*
public void setPaid(Date onDay) throws Exception
{
if(onDay.before(dueDate))
this.paidDate = onDay;
else throw new Exception("The paid date is after the due date.");
}
// – make the bill unpaid
public void setUnpaid()
{
this.paidDate = null;
}
// – set the due date of the bill*
public void setDueDate(Date nextDate) throws Exception
{
if(nextDate.after(paidDate))
this.dueDate = nextDate;
else throw new Exception("The due date is before the paid date.");
}
// – change the amount*
public void setAmount(Money amount)
{
this.amount = amount;
}
// – provide the Money object for the bill that is the amount*
public Money getAmount()
{
return new Money(amount);
}
//* - change the originator
public void setOriginator(String originator)
{
this.originator = originator;
}
// – build a string that reports the amount, when its due, to whom, whether paid, and if paid, the date paid.
public String toString()
{
String value = "An amount of " + amount + " is due on " + dueDate + " and ";
if(paidDate == null)
value += "is still not yet paid.";
else value += "was paid on " + paidDate;
return value;
}
// – determine if the two bills are identical
public boolean equals(Bill toCompare)
{
if(toCompare.amount == this.amount && toCompare.dueDate == this.dueDate && toCompare.paidDate == this.paidDate && this.originator == toCompare.originator)
return true;
else return false;
}
}
Date.java
public class Date {
private int month;
private int day;
private int year;
/*
* The date mehtod is assumed to be direct and transparent and hence doesn't contain deep validations
*/
Date(int day,int month,int year)
{
this.day = day;
this.month = month;
this.year = year;
}
//getters and setters for the data variables in date class
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
//checks if this date is after other date
public boolean after(Date other)
{
if(this.year > other.year || (this.year == other.year && this.month > other.month)
||(this.year == other.year && this.month == other.month && this.day > other.day))
return true;
else return false;
}
//checks if this date is before the other date
public boolean before(Date other)
{
if(this.year < other.year || (this.year == other.year && this.month < other.month)
||(this.year == other.year && this.month == other.month && this.day < other.day))
return true;
else return false;
}
//checks if this date and other date are equal
public boolean equals(Object o)
{
Date other = (Date)o;
if(this.year == other.year && this.month == other.month && this.day == other.day)
return true;
else return false;
}
//returns the date in DD/MM/YYYY format
public String toString() {
return day + "/" + month + "/" + year;
}
}
MoneyBillTester.java
public class MoneyBillTester {
/**
* @param args
*/
public static void main(String[] args) {
Money phoneAmouunt = new Money(50, 23);
Date duedate = new Date(2, 1, 2016);
Bill phoneBill = new Bill(phoneAmouunt, duedate, "At&T");
Date Date(1, 1, 2016);
System.out.println(phoneBill);
try {
phoneBill.setPaid(oneDay);
} catch (Exception e) {
System.out.println(e.getMessage());
}
System.out.println(phoneBill);
try {
phoneBill.setDueDate(new Date(31,12, 2015));
} catch (Exception e) {
System.out.println(e.getMessage());
}
Bill cableBill = new Bill(phoneBill);
System.out.println("Cable Bill: " + cableBill);
cableBill.setOriginator("TV Company");
cableBill.setUnpaid();
System.out.println("Have I paid the cable bill? " + cableBill.isPaid());
}
}
OUTPUT:
An amount of $50.0 is due on 2/1/2016 and is still not yet paid.
An amount of $50.0 is due on 2/1/2016 and was paid on 1/1/2016
The due date is before the paid date.
Cable Bill: An amount of $50.0 is due on 2/1/2016 and was paid on 1/1/2016
Have I paid the cable bill? false