COSC 502: Objects and Algorithms

Project 4
Spring 2011

Due: Thu, Apr 14 @ 5 PM
10 points

For this project, more dog food. Woof! We're going to use Payment (and Date) to develop an interactive program for manipulating payments. Through a menu, users will be able to add, print, and remove payments. This will require slight modifications to Payment and the development of two new classes: Payments and HSBCApp.

The first modification you need to make to Payment is to make it work for names for payees that contains spaces. In your data files, payees should be delimited with double quotes. Take my data file as an example:

350 02/04/2011 "Tombs" 17.95
351 02/05/2011 "Wisemiller's" 9.75
352 02/07/2011 "Starbucks" 2.95
353 02/07/2011 "Georgetown Bookstore" 122.25
You need to modify operator>> to read the delimited string using the getline function. You then need to modify operator<< to replace those double quotes when writing a payee to a file. Two crucial points: The double quotes should not be included in the name of the payee, and application must read and write the same file format.

The Payments (plural) class has a C++ vector. Its methods will store and manipulate Payment (singular) objects. There should be methods that add a new payment to the vector, get the total of the payments in the vector, print the payments in the vector, remove a payment from the vector based on its number, read payments from a file, and write payments to a file. The definition for Payments:

class Payments
{
  public:
    Payments();
    void add( const Payment& payment );
    double getTotal() const;
    void print() const;
    void read( string filename );
    void read( istream& in );
    void remove( int number );
    void write( string filename ) const;
    void write( ostream& out ) const;

  private:
    vector<Payment> payments;
    //...

}; // Payments class

These methods work pretty much as you might imagine. The add method adds the specified payment to the vector, but it also sorts the vector after the addition using payment numbers, so payments are printed and written in order. The add method should not add a payment if there is a payment with the same number in the vector. In this case, it should print an error message indicating that there is already a payment with that number in the vector.

The remove method removes the payment with the specified number from the vector. If no such payment exists, then it should print an appropriate message.

The read method reads all of the payments from the specified file and stores them in the vector. The read operations are destructive, rather than additive, so the read method should clear the vector if it is not empty before reading and storing new payments in the vector. If the read method fails to open the specified file, then it should print an appropriate error message.

The print and write methods output the payments in the vector, and they print an appropriate message if there are no payments.

To sort payments for add and to find payments for remove —that is, find that they are not present—you can either use the sort and find functions from the C++ Standard Template Library, or implement private methods for sort and find in the Payments class. If you do the former, then to use sort, you need to overload Payment::operator< and to use find, you need to overload Payment::operator==. These operators should use only a payment number for comparison, since it is the unique identifier for payments.

Rather than implementing the application logic in the main function, you are going to implement a class that encapsulates the application logic. I named mine HSBCApp, which has one public method that reads payments from the file payments.dta, lets users execute commands, executes those commands, and upon termination, writes payments to the file payments.out. (A good test is to see if your application can read the file that it writes.)

The HSBCApp also has a private method named printMenu, which prints the menu of options for the application. The main function, therefore, simply creates an instance of the application and kicks everything off:

int main()
{
  HSBCApp theApp;
  theApp.gitErDone();
  return 0;
} // main
With the application, users can add payments, print all of the payments, and remove a payment. A transcript of a session appears below with user input set in bold-face type:
seva% a.out
Menu:
  [A]dd Payment
  [P]rint Payments
  [R]emove Payment
  [E]xit Program
: p
Number: 350
Date: 02/04/2011
Payee: Tombs
Amount: 17.95

Number: 351
Date: 02/05/2011
Payee: Wisemiller's
Amount: 9.75

Number: 352
Date: 02/07/2011
Payee: Starbucks
Amount: 2.95

Total: 30.65
Menu:
  [A]dd Payment
  [P]rint Payments
  [R]emove Payment
  [E]xit Program
: x
Invalid command: X
Menu:
  [A]dd Payment
  [P]rint Payments
  [R]emove Payment
  [E]xit Program
: a
Enter a payment:
Number: 350
Date: 02/05/2011
Payee: Bookstore
Amount: 45.67
Payment 350 already exists!
Menu:
  [A]dd Payment
  [P]rint Payments
  [R]emove Payment
  [E]xit Program
: a
Enter a payment:
Number: 353
Date: 02/05/2011
Payee: Bookstore
Amount: 45.67
Menu:
  [A]dd Payment
  [P]rint Payments
  [R]emove Payment
  [E]xit Program
: p
Number: 350
Date: 02/04/2011
Payee: Tombs
Amount: 17.95

Number: 351
Date: 02/05/2011
Payee: Wisemiller's
Amount: 9.75

Number: 352
Date: 02/07/2011
Payee: Starbucks
Amount: 2.95

Number: 353
Date: 02/05/2011
Payee: Bookstore
Amount: 45.67

Total: 76.32
Menu:
  [A]dd Payment
  [P]rint Payments
  [R]emove Payment
  [E]xit Program
: r
Enter the number of the payment to remove: 354
Found no payment with number 354
Menu:
  [A]dd Payment
  [P]rint Payments
  [R]emove Payment
  [E]xit Program
: r
Enter the number of the payment to remove: 351
Menu:
  [A]dd Payment
  [P]rint Payments
  [R]emove Payment
  [E]xit Program
: p
Number: 350
Date: 02/04/2011
Payee: Tombs
Amount: 17.95

Number: 352
Date: 02/07/2011
Payee: Starbucks
Amount: 2.95

Number: 353
Date: 02/05/2011
Payee: Bookstore
Amount: 45.67

Total: 66.57
Menu:
  [A]dd Payment
  [P]rint Payments
  [R]emove Payment
  [E]xit Program
: e
seva% exit

Be sure to incrementally develop and test. For this project, you do not need to write doc comments. If your implementation of the Payment and Date classes from p3 are a little shaky, try to fix them using the clarity that comes only after you've submitted an assignment. If that doesn't work, come see me.

Getting Started

The first thing you want to do is make a copy of your project from p3. To do this, at the seva prompt, type:

cp p3.cc p4.cc

Instructions for Submission

At the top of the file containing your source code (i.e., the file containing the C++ instructions), include the following header comment with the appropriate modifications:
//
// COSC-071 Project 4
// Name: <your name>
// E-mail: <e-mail address>
// Instructor: Maloof
// Spring 2011
//
// In accordance with the class policies and Georgetown's Honor Code,
// I certify that, with the exceptions of the lecture notes and those
// items noted below, I have neither given nor received any assistance
// on this project.
//

When you are ready to submit your program for grading, use the submit program, just like you did for p3. Both submit.jar and the file containing your program should be in the same directory. You can confirm this by listing the files in your home directory:

seva% ls
p4.cc    submit.jar
You may see other files from previous assignments in your home directory, but p4.cc and submit.jar should be among them.

This assignment's label is p4. Assuming the file you want to submit is p4.cc, then to submit you enter the command

seva% java -jar submit.jar -a p4 -f p4.cc
Make sure you submit the file containing your program's source, not its executable code; that is, do not submit a.out.

Once you've submitted your project, it is a good idea to keep a copy on seva, which preserves the modification date and time and ensures that you will have a backup copy of your project. If we lose your project or submit breaks, then we will need to look at the modification date and time of your project to ensure that you submitted it before it was due.

The TAs who will be grading your projects this semester are listed on the main page. You must submit your project before 5 PM on the due date.