Assignment 1

The purpose of this assignment is to practice using some features of C# which are different from C++ and also to practice using the Visual Studio IDE. The logic is not especially complicated.


Steps

Start a project of type Console Application in Visual Studio and name it "Assign1".

  1. In your Assign1 namespace, define a class called Person.

    1. The declaration for Person should mention the IComparable interface.

    2. Person should have three private data members, all of type String. These are an individual's name, office number and telephone number.

    3. You should have public properties for each of these data members including Get and Set.

    4. You need a constructor which will initialize a Person to three String values.

    5. To implement the IComparable interface, you will need to have a public method named CompareTo:
           public int CompareTo(object OBJ)

      This method will compare the current instance's name to the name of the Person instance passed to it as OBJ. It returns a negative value for "this less than OBJ", 0 for "equal" or a positive value for "this greater than OBJ". You will need to recast OBJ (of the generic type object) as a Person. (Notice that the office numbers are not involved here.)

  2. In class Program (which can be static, as we have only one program), define a public static array of Person and a public static integer InUse (which keeps track of how many entries we have). Initialize the array to a new array of 20 Person instances. Initialize InUse to 0.

  3. In your Main method:

    1. Call a method which will read a file of data and load the array. You will need to use StreamReader and ReadLine. Keep track of InUse.

    2. In a loop, call a method to print a menu (using WriteLine). The options ('a' to 'h') are (in this order):
      1. Print the list
      2. Add an entry
      3. Search for a name
      4. Search for an office number
      5. Search for a telephone number
      6. Change an office number
      7. Sort the list (by name)
      8. Quit

      Obtain the user's choice as a string (use ReadLine). Use a switch statement to call methods as needed to carry out the designated choice. (The default option is to say "That was not a choice.") The loop ends when "Quit" is chosen.

  4. At the end, write a line "Press Enter to exit" and then use ReadLine. (Otherwise it will all disappear abruptly.)


Notes

You will need "Using" statements for System and System.IO.

When you print the list, line up the three pieces of information in neat columns. You can use the PadRight feature of String to do this.

When you add an entry, ask the user for the name, office number and telephone number (three String values). Now search for the name. Names should be unique. If the name is already present, print an error message. If the name is not found, use something like this:

     YourArrayName[Inuse] = new Person(three strings);
and print a message saying "Done".

When you search for a piece of information, you will need to compare String values. You may need to use the Equals method of String. After searching, print a line containing the name and office number, or print a message saying the target value was not found, as in "The name Harvey was not found" or "Office number 445 was not found". You may find several names for one office number. Print them all.

When you want to change an office number, ask the user for the name and the new office number. (We will assume the telephone number stays the same.) Search the array for the name and change the office number. If you don't find the name, print a message saying so. If the operation is successful, print a message saying "Done".

When you want to sort the array, you can use the Sort method of Array. This is just one line, and you may not even need a separate method for this choice. Array.Sort() uses the CompareTo method mentioned above.

Bear in mind that the user might use a lower-case or an upper-case letter in making a choice.

If you don't like names like InUse, you can change them to something else, within reason.

In searching the array, an option is to use the "foreach" construct. (Go read about if you need to.) Using it is not required on this assignment.

You may want to invent additional methods for the Person class.

In this assignment (though this is not necessarily true for the future), we will not worry about possible exceptions such as "index out of bound" or "file not found". If such things happen, the program will end abnormally. Likewise, we are not going to check the numbers to see if the data makes sense.

All the code for this assignment goes in one file, "Program.cs", which is created for you when you start the project.

You will probably need to look for some details on line. You may find the MSDN web pages useful. In particular, you may want to go read about "IComparable" and "PadRight".

The input file is called data1.txt. It contains three lines for each person lines with one string per line (name followed by office number followed by telephone number). Download it and put a copy in the bin/debug subdirectory of the directory that contains your project.

You should test your work. Make sure each option works.

Make sure your program is adequately documented. You should have a block of comment lines that list the assignment number and your name (and your partner's name, if you are working with a partner). You also need at least some comments for each class and each method.

To submit your work, go to the Blackboard site and locate Assignment 1. Use an FTP program to copy the entire project folder to some other location and then compress it, creating a ZIP file. Attach the ZIP file on Blackboard. The TA will copy the ZIP file to some other location, decompress it and compile it. Your score and the TA's comments will be on Blackboard. if you are working with a partner, the two of your should receive the same grade for the assignment.