You are to write a program that maintains a list of people. The list will be implemented as a resizable array. You should be able to handle insertions and removals into the list, printing the list, and getting the size of the list. You should also allow the user to specify (by means of a command-line argument) whether to keep the list sorted or unsorted. After running, the program will loop, asking the user to repeatedly enter the option of inserting, removing, printing, getting the size, or quitting. Your program should continue this loop until the user selects the quit option. Below is a sample run of the program using the sorted list option:
If your program is invoked with the –unsorted option instead, the information should be stored in the order they are added (not sorted by last name).
Implementation Requirements:
Your program must include the following six classes/interfaces:
·[FONT="]List[/FONT]
·[FONT="]ArrayList[/FONT]
·[FONT="]ArrayListSorted[/FONT]
·[FONT="]Person[/FONT]
·[FONT="]IO[/FONT]
·[FONT="]FinalProject[/FONT]
[FONT="]List[/FONT] is an interface that defines a list of objects. It must define the following methods:
[FONT="]void[/FONT][FONT="] add(object x); [/FONT]
[FONT="]- add x to the list[/FONT]
[FONT="] bool remove(object x); [/FONT]
[FONT="]- remove x from the list (return whether x was in list) [/FONT]
[FONT="]int[/FONT][FONT="] size();[/FONT]
[FONT="] - return the number of elements in the list[/FONT]
[FONT="]ArrayList[/FONT] should implement [FONT="]List[/FONT]. (Remember that if a class
implements an interface, it must include implementations of all the methods in the interface.) [FONT="]ArrayList[/FONT] should contain an [FONT="]object[/FONT] array instance variable that will store the list elements, as well as the current size of the array. This class should store the elements in unsorted order. Also, because the list should not have a size limit, you will need to resize the array as the number of elements grows and shrinks.
[FONT="]ArrayListSorted[/FONT] should also implement [FONT="]List[/FONT]. It should be designed similarly to [FONT="]ArrayList[/FONT], but it should store the elements in sorted order.
You may need to see if one object is “less than” another, so you can determine where to place objects to put them in sorted order. To see if [FONT="]object obj1[/FONT] is less than (should come before) [FONT="]object obj2[/FONT], do:
[FONT="]if[/FONT][FONT="] (((IComparable)obj1).CompareTo(obj2) < 0)[/FONT]
(This is actually a bit of a hack, but that’s OK for this class.)
[FONT="]Person[/FONT] should represent a person, including their first name, last name, and age. It should implement the IComparable interface and the CompareTo method, and should override the Equals and ToString methods from object. You will also need to add the following method to Person (just to make it compile):
[FONT="]public[/FONT][FONT="] override int GetHashCode() {[/FONT]
[FONT="] return 0;[/FONT]
[FONT="] }[/FONT]
[FONT="]IO[/FONT] should handle all printing and user input. Make a list of all the different things that need printed, and all the different kinds of user input you want. Make a separate method in [FONT="]IO[/FONT] for each item on your list.
[FONT="]FinalProject[/FONT] is the controller portion of the program that will contain the Main method. It should contain the loop that repeatedly asks the user what they want to do with the array. Remember, [FONT="]FinalProject[/FONT] should call [FONT="]IO[/FONT] to display things or get user input, and it should call [FONT="]List[/FONT] to handle list actions (adding elements, etc.). Remember that when inserting, you should first get the information from the user, then create a new [FONT="]Person[/FONT] object, and then pass that object to [FONT="]List[/FONT] to insert. [FONT="]Final[/FONT] should declare the following variable:
[FONT="]List list = null;[/FONT]
Then, depending on what command-line arguments the user supplies, it will instantiate [FONT="]list[/FONT] to be either a new [FONT="]ArrayList[/FONT] (unsorted) or a new [FONT="]ArrayListSorted[/FONT] (sorted). It should also create an [FONT="]IO[/FONT] object and call its methods for printing and getting input.
Array resizing should be done whenever the number of elements is the same as the current array size. In this case, double the size of the array. You should also resize the array any time the current array size is at least twice as big as the number of elements. In this case, decrease the size of the array by half. The array should start with 10 spots, and should never get smaller than that (even if it is less than half full).
Inserting and removing elements may require you to shift elements in your array using [FONT="]Array.Copy[/FONT]. If inserting into an unsorted list, the correct spot to put a new element is the next available spot in the list. If you are inserting into a sorted list, first find the correct position in the array. Then, shift all elements beyond that position up one spot, and insert the element. When removing (sorted or unsorted), find the correct position of the element. Then, shift all elements beyond that position down one spot to fill in the element you’re removing. Your array should NEVER contain empty spaces between elements.
All
command-line arguments to a program are stored in the [FONT="]string[[/FONT][FONT="]] args[/FONT] array (which is a parameter to the main method). For example, if the program is invoked with:
[FONT="]Final -sorted[/FONT]
Then the “-sorted” option is stored in [FONT="]args[/FONT][FONT="][[/FONT][FONT="]0][/FONT].[FONT="].[/FONT] You can get the number of command-line arguments by saying “[FONT="]args.Length[/FONT]”.
You should print an error message if any of the following occur:
·Your program should handle and print warning messages for the following errors:
·User forgets to select –sorted or –unsorted command-line option
·User tries to remove an element not in the list
·User inputs an invalid option in the main loop
·User does not provide last name, first name, and age when inserting
You should allow people to be stored with the same names. (If their last names are the same, you should sort by first name.) If the user specifies that last name to remove, remove the FIRST person with that last name.
You should be able to compile your code from the command-line like this:
[FONT="]csc[/FONT][FONT="] *.cs[/FONT]