Question: JAVA PROBLEM JAVA Program Behavior: The classes you make should be able to pass a test case that will be applied to test the functionality
JAVA PROBLEM
JAVA Program Behavior:
The classes you make should be able to pass a test case that will be applied to test the functionality of the classes you have created.
Summary:
This assignment requires you to code three java classes titled: Money.java, Date.java, and Bill.java. These classes will be used to manage a set of outstanding and to manage paid bills. In this problem, well experiment with class composition, privacy leaks, accessors, mutators, toString(), and equals().
For test driver you are required to use at least 3 tests in Junit to get experience w/ the test harness. Overall, you should use the JUnit test model. Below are sample drivers for each style described. Titled: BillMoneDateDriver.java and DateMoneyBillJUnit.java. These drivers however arent complete and must be modified or extended to test all of the methods.
Money Class:
This Money class is used to keep track of a US dollar amount consisting of 2 integer values to manage both dollars and cents values. Amounts will be greater than zero. NO NEGATIVES! Do not return cents greater than .99. Below, will be the methods and data that will be called from the test driver.
If the cent value is larger than 99, we will add to the dollar amount with the appropriate value. For example, the user calls setMoney(3, 455) this would equal to the call of setMoney(7,55);
Money Methods that will be tested:
- Money()
- Constructor, set dollars and cents to the value 0
- Money ( int dol )
- Constructor, sets the dollar amount, and sets cents to the value 0
- Money( int dol, int cent)
- Constructor which initialized dollars and cents.
- Money( Money other)
- Copy Constructor
- int getDollars()
- int getCents()
- void setMoney( int dollars, int cents)
- double getMoney( )
- Gets the money amount as a double
- Returns 5.75 for example
- void add( int dollars);
- void add ( int dollars, int cents);
- void add( Money other)
- Adds the amounts in other to our money object reducing cents appropriately.
- Boolean equals ( Object o)
- String toString()
- Prints out the amount as a string IE $3.75 or $4.00. Note the number of digits displayed for cents.
Date Class:
The date class represents a date in our world. Do not use any built-in date classes in this assignment,
- All days should be between [1-31]
- Each month should be between [1-12]
- Each month has exactly 31 days.
- All years should be between [2016 2026]
- Entering an invalid date should print a special error message to the console, and the program should exit.
Date Methods:
- Date ( int month, int day, int year )
- Constructor
- Date ( Date other)
- Copy Constructor
- int getYear();
- int getMonth();
- int getDay();
- void setYear( int year);
- void setMonth( int month);
- void setDay( int day);
- boolean isAfter( Date compareTo)
- Return true if the compareTodate is after the date.
- WARNING: the direction of this comparison may be the opposite of what youre expecting make sure you get it right.
- boolean equals( Object date)
- String toString()
- Prints out the date in the form mm/dd/yyyy. Note that 2/22/2015 is invalid. It should be 02/22/2015
Bill Class
This class will represent an outstanding or paid bill. The class contains the amount of the bill as a Money object, the due date of the bill as 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 isnt past the due date.
Be careful when returning objects or even setting objects, make sure you dont leak information or access!
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 that initiated the bill.
Bill Methods:
- Bill ( Money amount, Date dueDate, String originator)
- Constructor. ( paidDate would be set to null)
- Bill ( Bill toCopy);
- Money getAmount()
- Date getDueDate()
- String getOriginator();
- boolean isPaid(); - returns true if bill is paid. Else false.
- boolean setPaid( Date datePaid)
- if datePaid is after the dueDate, the call does not update anything and returns false. Else updates the paidDate and returns true
- boolean setDueDate( Date nextDate)
- Resets the due date If the bill is already paid, this call fails and returns false. Else it resets the due date and returns true.
- boolean setAmount( Money amount)
- Change the amount owed. If already paid, returns false and does not change the amount owed else changes the amount and returns true.
- void setOriginator(String originator);
- String toString()
- Build a string that reports the amount, when due, to whom, if paid, and if paid the date paid
- boolean equals ( object toCompare)
- return if the two objects are the same.
Driver Class
Driver classes below can be used as an example for a starting point. JunitTestDriver and MainTestDriver serve as a starting point for testing. SEE BELOW!
Most methods will need to be tested more than once as there is more than one case of interest. For example, make sure that when you set the Amount on a paid bill, nothing changes and the method returns false. or that when you set a paidDate, that an external user cannot change it to an invalid date
Hints:
- Start with the smallest class first (Date or Money) and then build the larger classes (Bill).
- First, use the sample driver with your classes then extend the driver to test each function in Bill, Date and Money.
- Again, make sure you comment all methods and the class. If you have a driver that has code someone else has written (e.g., is based on a skeleton), you have to comment the methods someone else wrote also.
- Watch out for privacy leaks in your getters, setters, and constructors.
BillMoneyDateDriver.java:
public class BillMoneyDateDriver
{
/**
main driver function
pre: none
post: exercises the methods in Bill, Money, and Date (not done)
*/
public static void main(String[] args)
{
//Construct some money
Money money1 = new Money(10);
Money money2 = new Money(money1);
money1.setMoney(30,50);
//TODO: do more functional exercises with the money class
System.out.println("Money objects output:");
System.out.println(money1);
System.out.println(money2);
//Construct some bills
Money amount = new Money(20);
Date dueDate = new Date(4, 30, 2007);
Bill bill1 = new Bill(amount, dueDate, "The phone company");
Bill bill2 = new Bill(bill1);
bill2.setDueDate(new Date(5, 30, 2007));
amount.setMoney(31, 99);
dueDate.setDay(29);
Bill bill3 = new Bill(amount, dueDate, "The record company");
System.out.println("Bill objects output:");
System.out.println(bill1);
System.out.println(bill2);
System.out.println(bill3);
}
}
DateMoneyBillJUnit.java
import static org.junit.Assert.*;
import org.junit.Test;
public class DateMoneyBillJUnit {
@Test
public void ConstructMoneyTest() {
Money money1 = new Money(10);
assertEquals(10, money1.getDollars());
}
@Test
public void SetMoneyTest()
{
Money money1 = new Money();
money1.setMoney(30,50);
assertEquals(30, money1.getDollars());
assertEquals(50, money1.getCents());
}
@Test
public void ConstructMoneyCopyTest() {
Money money1 = new Money();
money1.setMoney(10,40);
Money money2 = new Money(money1);
assertEquals(10, money1.getDollars());
assertEquals(40, money2.getCents());
}
@Test
public void PrintMoneyTest()
{
Money money1 = new Money(10);
money1.setMoney(30,50);
assertEquals("$30.50", money1.toString());
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
