Solved

Container Classes and Linked Lists - More

Posted on 1998-08-09
9
332 Views
Last Modified: 2010-04-10
Hello good people and thanks for all your patience and help.

I'm working on yet another C++ project which involves a class to 'contain' and a linked
list. I need some more help breaking down the program into its component pieces.

This program is to produce an information manager similar to a rolodex, which is made
up of cards arranged in alphabetical order. Each card contains information, usually
names, addresses, phone numbers and type of the person's business(the reason
why the person is in the rolodex to begin with). i.e. :

First:     Last:       Occupation:                 Address:                                  Phone:

Tony     Hansen    Writer                         12 E. St. NY, NY   33333       555-9999

When the last card is read, the "rolodex" is at the start of the list since a rolodex
is built on a circular track.

The rolodex should be stored in alphabetic sorted order by last name.
The program needs to demonstrate that entries can be added, deleted, and
changed. Also, show that you can search the rolodex for specific information.
The list should also be able to be printed/displayed.

A string class should be used for the individual pieces of information. The cards
should also be represented by a user-defined class. This means you should use the
idea of a container class.

One class should represent a Card entry, anf the Rolodex class is the "container"
which 'contains' a linked list of card entries, and manages them as they are added/removed.
Thus, Rolodex has pointers to a linked list of Card objects. Each Card object
represents one item in the rolodex.
To add a new item to the rolodex, first create and populate the new Card entry,
then pass it to the rolodex addItem() member(which maintains the list in sorted
order). When finding an entry, the rolodex returns a Card* to one of its entries.

Here's what I have thus far........................................................................

HEADER FILE
class rolodex {

      public:

            void initialize_rolodex();
            void addEntry();
            void deleteEntry();
            void displayRolodex();xz
            rolodex changeEntry(rolodex person);
            rolodex(); // default CTOR
            ~rolodex();  // default DTOR

      private:

            char firstName;
            char LastName;
            char occupation;
            char address;
            char phone_number;
            int rolodex_record_number;
};


IMPLEMENTATION FILE
#include <iostream.h>
#include "project2.hpp"

int main()

{
      rolodex R1;
      R1.addEntry();
  return(0);
}

// Initilaize the rolodex
void rolodex::initialize_rolodex()
{
   rolodex Rdx; // initial rolodex object, Rdx
}

// Add an entry to the rolodex
void rolodex::addEntry()
{
  cout << "Enter first name:  ";
  cin >> firstName;
  cout << endl;
  cout << "Enter last name:  ";
  cin >> LastName;
  cout << endl;
  cout << "Enter person's occupation:  ";
  cin >> occupation;
  cout << endl;
  cout << "Enter person's telephone number:  ";
  cin >> phone_number;
  cout << endl;
}

// Delete an entry from the rolodex
void rolodex::deleteEntry()
{
  cout << "Enter the record number of the rolodex entry you wish to delete  ";
  cin >> rolodex_record_number;
  cout << "The record number you entered was " << rolodex_record_number << endl;

}

// Change an entry held in the rolodex

rolodex rolodex::changeEntry(rolodex person)
{
  int choice = 0;  // used for selection of rolodex entry to be updated.
  char yes_no;
  cout << "Please enter the desired field to be changed for " << person.LastName << endl;
  cout << "1: First name, 2: last name, 3: occupation, 4: address, 5: phone number  ";
  cin >> choice;
  switch (choice) {
     case 1: cout << "Change " << person.firstName << "? y/n";
           cin >> yes_no;
           if ( yes_no == 'y' )  {
            cout << "Enter new first name ";
            cin >> person.firstName; }
            else break;
            break;

      case 2: cout << "Change " << person.LastName << "? y/n";
           cin >> yes_no;
           if ( yes_no == 'y' )  {
            cout << "Enter new last name ";
            cin >> person.LastName;  }
            else break;
            break;

      case 3: cout << "Change " << person.occupation << "? y/n";
           cin >> yes_no;
           if ( yes_no == 'y' )  {
            cout << "Enter new occupation ";
            cin >> person.occupation;  }
            else break;
            break;

      case 4: cout << "Change " << person.address << "? y/n";
           cin >> yes_no;
           if ( yes_no == 'y' )  {
            cout << "Enter new address ";
            cin >> person.address;  }
            else break;
            break;

      case 5: cout << "Change " << person.phone_number << "? y/n";
           cin >> yes_no;
           if ( yes_no == 'y' )  {
            cout << "Enter new telephone number ";
            cin >> person.phone_number; }
            else break;
            break;


      default: break;
      }  // end switch block
  return(person);
} // end changeEntry()

void rolodex::displayRolodex()
{
// display the contents of the rolodex
cout << "First,    Last,    Occupation,    Address,   Telephone Number" << endl;

}
// Default constructor for the class rolodex
rolodex::rolodex()
{
}

rolodex::~rolodex()
{

// Default destructor; takes no action

}


Could someone please help me with some direction again? As with my other
question regarding containers and linked lists I am equally confused. If I could
just get setup and explanation, I'm sure I could do it.

S.
0
Comment
Question by:swright243
  • 5
  • 4
9 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 400 total points
ID: 1169778
first of all you need a string class for storing the string information.

thus the rolodex class should look like,

class rolodex {

   public:
//  removed for space
   private:

   String  firstName;
   String LastName;
   String occupation;
   String  address;
   String  phone_number;
   int rolodex_record_number;
   };
0
 
LVL 22

Expert Comment

by:nietod
ID: 1169779
Actually you have the same problem here as you did in the zoo.  You need more than one class.  One class represents the Rolodex file and the other represents the cards in it, like

class RolodexFile
{
   public:

   void initialize_rolodex();  // Do you really need this?  Or is this what the CTOR doesj?
   void addEntry();
   void deleteEntry();
   void displayRolodex();xz
   rolodex changeEntry(rolodex person);
  RolodexFile(); // default CTOR
   ~RolodexFile();  // default DTOR

   private:
// here you need linked list type data.
   RolodexCard *First;
   RolodexCard *Last;
};

class RolodexCard {

   public:

   void displayRolodexCard();
  RolodexCard(); // default CTOR
   ~RolodexCard();  // default DTOR

   private:

   String firstName;
   String LastName;
   String occupation;
   String address;
   String phone_number;
   int rolodex_record_number;
   friend RoladexFile;
};
0
 
LVL 22

Expert Comment

by:nietod
ID: 1169780
Now that should give you some idea where you are going.  But don't try to get there all at once.

When you get a big problem like this try to outline the general approach you are going to take, think about all the classes you will need and what their resposibilities will be.  Then try to wrote the classes a little bit at a time.  Don't try to tackle the whole problem at once.  Here is what I see when I think about the problem.

Three classes are needed:
A string class (called String?) for storing names, addresses, phone numbers and other information.
A Rolodex card information class for storing the information that goes on a single card.
A Roladex file information for storing all the rolodex cards objects.

The reposibilities:

String class:
Can store string of any desired length.  
Can be used to compare two strings.  (needed when searching the rolodex)
Can be use to read and write strings from a stream.

Rolodex card:
Stores information for a single contact.
Can be used to read and write contact information from a stream
Contact information can be obtained or set using functions, like GetName() or SetName().

Rolodex File:
Stores a list of Rolodex cards in alphabetical order.
Rolodex card objects can be added or deleted.
Rolodex cards can be access in sequential order.

Now you have an idea of the different classes and responsibilities.  Looking at this it should be obvious that the first thing you need is a working string class?  do you have one?  If so, let me see it, if not lets start on that.

When does all this have to be done?
0
 
LVL 22

Expert Comment

by:nietod
ID: 1169781
One more thing.  Once you have come up with your list of classes and their responsibilities.  The next things you ned to think about, and these are all interelated are:
1.  What data is stored in the class.
2.  How is it obtained from the class.(if allowed)
3.  How is it set by users fo the class (if allowed)
4.  How does the constructor initalize the data.
5.  How does the destructor clean-up the data.

For a class like the RolodexCard, you might have a Name member that is a string.
You would need to provide a function to set the name to a new value, like SetName(), one to get the current value of the name, like GetName().  The constructor(s) needs to initialize this.  Soem constructors may initialize it to an empty string, others might take a parameter that is a name string and set the stored name string to this string.  The destrucor must destroy the name.  If the name is stored in a string class, the string class should take care of that automatically, so actually the destructor doesn't need to do anything.

This sort of thinking should be applied to all the classes and all the data they store.  Can you handle that?  (Bear in mind that you won't get it perfect at the start, you will add new data members and change others etc as time goes on, but this is a good way to start.)
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:swright243
ID: 1169782
Hello again nietod and thank you again for responding. I'll get you some of the answers you asked for in the next comment, probably tonight, Monday.
S.
0
 

Author Comment

by:swright243
ID: 1169783
08/23/98
0
 

Author Comment

by:swright243
ID: 1169784
09/05/98
0
 
LVL 22

Expert Comment

by:nietod
ID: 1169785
9/25/98
0
 

Author Comment

by:swright243
ID: 1169786
Adjusted points to 400
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now