Fourth Assignment: The Whole Database (v1)

Due on or before Saturday 27th March 1999.



The original idea was that the Goldfish Club has a big book listing all the members with their personal data, and all their pet goldfish with their piscinal data. We have spent a long time implementing representations of individual people and fish. Now you are to implement the whole big book.

The assignment is to implement in C++ a class that represents to goldfish club's membership book. The class must contain (at least) a list of people, and a list of goldfish. You should use the Person class that we created ages ago to represent the individual people, and the Goldfish class that you created for your last homework to represent the individual fish.

Your club class should have (as a minimum) a constructor that just sets up a valid but empty club, a method for adding a new person to the club, a method for adding a new goldfish to the club, a method for finding a person given their name, a method for finding a goldfish given its name, a method for reading all the data from a file, and a method for saving all the data to a file. Loading and Saving from/to a file should be a user-selectable option, not an automatic requirement. Making it automatic will make testing much harder.

You need to provide a main function to make it into a runnable program. There is no need to be user-friendly, but do make it fully functional. The simplest and probably best way is to make it menu driven. A sample session may look something like this:
Welcome to the GOP membership database
Enter your selection from the following menu:
1: Load data from file
2: Save data to file
3: Insert new Person
4: Insert new Goldfish
5: Find details of Person
6: Find details of Goldfish
7: Change Person's address
8: Change Goldfish name
9: Exit

Selection? 1
Filename? gop.dat
OK, I read 9 Persons and 9 Goldfish from "gop.dat"

Selection? 5
Person's Lastname? Pudding
Member #7: Mrs Semolina Pudding
           1234 Gingivitis Street, Morbid, OH 37282
           Phone: 136-373-1762
           Membership Fee: $25    due: 1999-06-23
           Goldfish name: Goldie

Selection? 6
Fish name? Goldie
Goldfish #4: Goldie Pudding
             Born: 1958
             Status: Alive
             Owner: Pudding, Mrs. Semolina

Selection? 5
Person's Lastname? Droopy
No such member found.

Selection? 3
Title? Mr
Firstname? Leonard
Lastname? Droopy
Address? 221b Baker Street
etc etc etc
Fish name? Princess Fluffy VIII
Person #10 added OK.

Selection? 5
Person's Lastname? Droopy
Member #10: Mr Leonard Droopy
            221b Baker Street, London, Ontario LO62AB 
            Phone: 928-237-2838
            Membership fee: $75    due: 1984-03-21
            Goldfish name: Princess Fluffy VIII

Selection? 2
Filename? gop.dat
Saved 10 Person records and 9 Goldfish recoords in file "gop.dat"

Selection? 9
OK, bye.
There is no need to duplicate this example. It is just to give you the idea of the kind of thing that is expected.


Inside the main() function of your program, you should first create a new GoldfishClub object, and print out the menu of choices, before entering the loop:
{ GoldfishClub club;
  printf("Welcome blah blah blah\n");
  ...
Most of the choices will be very easy to implement. For example, to find the personal details of a member, it would use the club's method for finding a person, then use that person's method for printing itself:
case 5: { char name[100];
          Person *p;
          printf("Person's Lastname? ");
          gets(name);
          p=club.findPerson(name);
          if (p==NULL)
            printf("No such person as \"%s\"\n", name);
          else
            p->print();
          break; }
You are allowed to make resonable modifications to the definitions of Person and Goldfish to make them more suitable for this assignment. It is quite likely that you will want to change the way they deal with files.
        You should probably give the club class a method for writing everything to a file. Sensibly, it would just take one parameter (the file's name) and just get on with it. It would open the file, then run through the array of members, printing each one into the already open file. The person class should have a "print to file" method suitable for this. It would also run through the array of goldfish, printing each of them to the file. How you arrange the file is your own decision. Perhaps you'll want to alternate object types, first writing a person then a goldfish, then another person, then another goldfish and so on. A more flexible idea is to make sure that the information for a person always begins with the letter 'P' and the information for a goldfish always begins with the letter 'G', then you can write them in any order you like, and never have to worry about confusion when reading them back in later. It might look something like this:
void GoldfishClub::save(char *filename)
{ FILE *fil;
  fil=fopen(filename,"w");
  if (fil==null)
  { complain complain complain }
  int i;
  for (i=0; i<num_people; i+=1)
    people[i]->save_in(fil);
  for (i=0; i<num_fish; i+=1)
    fish[i]->save_in(fil);
  fclose(fil); }
The "save_in" method of both Person and Goldfish is being given an open file, not a filename, so you don't need to do any opening or closing. Just remember two things: write the 'P' or 'G' first, and make sure that you write the real information out in a way that you can easily read back in. You already have a constructor for both Person and Fish that accepts one big string with colons separating the important parts, so that would be an ideal format to write the data in.
void Person::save_in(FILE *f)
{ fprintf(f,"P%s:%s:%s:.....%s\n", blah, blah, blah); }
Pretty easy.

The function for reading data from a file just does the opposite: open the file, keep reading lines one at a time. If the line begins with 'P', pass the rest of it to the constructor for a Person, use the club's addPerson method on the result. Remember that fgets returns NULL when you attempt to read past the end of a file.