Question: Write the definition of a class named Money whose state is an int representing the number of cents that a Money object represents. The Money

Write the definition of a class named Money whose state is an int representing the number of cents that a Moneyobject represents. The Money class offers two constructors and three methods, add, toString, and equals. One constructor gets no arguments and sets the object's value to zero. The other constructor gets a reference to a Scannerobject. It uses the Scanner to read in a single String token that is expected to be in the form: "$dddd.cc", in other words the standard representation of U.S. money (but with no commas). Examples include: "0.07" or "41242.75" or "32.00". The constructor reads this string and parses it and figures out the total number of cents it represents in order to initialize the objects state.

The methods toString and equals are standard. The toString method returns a String that represents the value of the object in the "standard" way (see the discussion above). The equals method gets, as an argument, a reference to another (generally different) Money object and returns true if an only if that object's state matches the receiver's. The add method also gets, as an argument, a reference to another Money object, and adds that object's value to the receiver's.

Then, write a program named CashRegister that reads a two files. The first file, named balance, contains a single string, a money value, that represents the starting balance. The program creates a Scanner for that file, and constructsa Money object, using that Scanner, to represent the balance, and prints the value out. The second file, namedtransactions, contains multiple strings representing monetary values. The program creates a Scanner for that file, and then as long as there are more string tokens in the file to be read, it keeps creating Money objects, adding them all up, so that after there is no more money data to read from the transactions file, the program updates the balance file, replacing the old balance value with the new one. In addition, the program prints to standard output the number of transactions read, the total value of the transactions and the closing balance. For example, the output might look like this:

Starting balance: $400.23 3 transactions Transaction Total: $9030.77 Closing balance: $9431.00

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!