Top-Rated Free Essay
Preview

Using ArrayListITSystem Reading And Writing Data

Powerful Essays
1222 Words
Grammar
Grammar
Plagiarism
Plagiarism
Writing
Writing
Score
Score
Using ArrayListITSystem Reading And Writing Data
Using ArrayList<ITSystem>, Reading and writing Data to/from Files
Note: slight changes were made on 11/14 (1 day after this was put up). Specifically, the source files are slightly different from the solution to p2. When comparing two ITSystems we are now only concerned with ID. Highlighted text reflects changes made to the assignment.
For this assignment, we will continue to work with the SysInventory program we developed in the last programming project, with a few minor changes/additions. See the provided files. Note thatITSystem now has an equals method, coded following equals for Point, pages 595-596. If you completed all the problems for project 2 you may use your own files for project 3. If you do, you will be expected to complete the same work, but some of the advice on how to accomplish it may be incorrect. If you did not complete all the problems, or you would prefer a fresh start, you You should use the files provided to start with.

1. Convert the ITSystem array of SysInventory to ArrayList<ITSystem> systems. We will no longer use an empty 0 slot as we did with the array, but instead look up ITSystems by their id using the helper method findSystem(id). We want to eventually allow ids like 100, 101, 120, etc,, as well as the simple sequence 1, 2, 3, …. The helper method findSystem(id) can be written as follows: ArrayList<ITSystem> systems = new ArrayList<ITSystem>(findSystem(id));

private ITSystem findSystem(int id) { return systems.get(systems.indexOf(new ITSystem(id, 0, 0)));
}

This works because ITSystem has equals based on the id, so indexOf searches for the matching id. The “0, 0” arguments are necessary to make the constructor work, but these zero values are never used in the search. The challenge here is finding all the places that use the array and changing them over to using the ArrayList. For example, in whichITSystem() we have “return systems[idNumber]”, but now we want “return findSystem(idNumber)”. These changes will not affect the user experience, except that it is no longer necessary to ask how many systems will be set up (Enter how many systems: 2), since we can just start with an empty ArrayList and add any number of systems to it. We now need to let the user say when he/she is done adding systems. After this part is complete, the system should run as follows (for example):
Welcome to Departmental Network Systems
Enter system's room no, or 0 to quit adding systems: 10 System is assigned number 1
Enter system's room no, or 0 to quit adding systems: 20 System is assigned number 2
Enter system's room no, or 0 to quit adding systems: 0
Enter system inventory number, print-all, room <n>, or 0 to quit: 2
System 2 selected
Actions: exit, help, print, record-problem, in-same-room, move
Enter action: record-problem
The system now has 1 recorded problems
Enter action: help
Actions: exit, help, print, record-problem, in-same-room, move
Enter action: exit
Enter system inventory number, print-all, room <n>, or 0 to quit: 0
Goodbye from Departmental Network Systems 2. See the provided file systems.dat.
File systems.dat:
100 100 1
101 200 0
120 100 2
This file is describing three ITSystems, with id first, room number second, and number of problems third on the line. We want to read this file in our system and put the three ITSystem objects in the ArrayList.
a. Change the method open by adding a filename parameter: public void open(Scanner input, String filename) and change the call in SystemInventoryMain to open(input, “systems.dat”);
Further, have open call private void load(String filename) to read the named file into systems. To start with, just have load print “loading…”.
b. Now fill out load to actually read the file. Note that systems is directly available to all object methods because it is a SysInventory field. Method load should read the file, line by line, and call method processLine to interpret one line and put its values into a new ITSystem object and return it. See somewhat similar code on pages 410-411. Then load should add the new ITSystem object into the growing ArrayList. When load returns to open, open should report the number of ITSystems now in systems. Then the user should be invited to add more entries using the old code. After this part is complete, the system should run as follows (for example):
Welcome to Departmental Network Systems
3 systems read from systems.dat
Enter additional systems as needed now
Enter system's room no, or 0 to quit adding systems: 100 System is assigned number 121 (adding 1 to highest id so far)
Enter system's room no, or 0 to quit adding systems: 0 Enter system inventory number, print-all, room <n>, or 0 to quit: 100
System 100 selected
Actions: exit, help, print, record-problem, in-same-room, move
Enter action: in-same-room Systems in the same room as system 100:
ID: 100, Room: 100, Problems Reported: 1
ID: 120, Room: 100, Problems Reported: 2
ID: 121, Room: 100, Problems Reported: 0
Enter Action: exit
Enter system inventory number, print-all, room <n>, or 0 to quit: 0 Goodbye from Departmental Network Systems

3. Similar to load(String filename), implement save(filename) to save the ArrayList contents to a file. Add a method close(filename) to SysInventory that asks the user whether to save the file or not, and if so, calls save(filename). Call close(“systems1.dat”) from SystemInventoryMain after the call to doCommands(). Note the example MakeFile.java in the p3 files area. It shows how to create a new named file and use println to specify the contents.
Welcome to Departmental Network Systems
3 systems read from systems.dat
Enter additional systems as needed now
Enter system's room no, or 0 to quit adding systems: 100 System is assigned number 121 (adding 1 to highest id so far)
Enter system's room no, print-all, or 0 to quit adding systems: 0
Enter system inventory number, room <n>, or 0 to quit: 100
System 100 selected
Actions: exit, help, print, record-problem, in-same-room, move
Enter action: in-same-room Systems in the same room as system 100:
ID: 100, Room: 100, Problems Reported: 1
ID: 101, Room: 100, Problems Reported: 2
ID: 120, Room: 100, Problems Reported: 0
Enter Action: exit
Save ITSystems to file systems1.dat? Enter y or n: y
Saved 4 ITSystems to file systems1.dat. Resulting file systems1.dat:
100 100 1
101 200 0
120 100 2
121 100 0 4. Write a narrative that describes how you went about designing and implementing these modifications, any problems you encountered and how yu solved them, how you tested your program (how test cases were chosen and why) and a (cut and pasted) result for these tests. Put this in a text file called memo.txt. Delivery
Create a subdirectory p3 (not P3) in your it115 subdirectory on your CS Unix network account, and transfer a copy of your resulting Java files and narrative there, in plain text file memo.txt
Delivery files: Make sure the names are the same as specified here, including their case (Not Memo.txt for example)
1. ITSystem.java (as provided, or your own if preferred).
2. SysInventory.java with added code as specified above.
3. SystemInventoryMain.java: changed to call open and close with a filename argument.
4. memo.txt, with interactive tests shown. In addition, make a paper copy of memo.txt and bring it to class on November 25. Hand it in at the beginning of class. memo.txt should have the name of the file on it, your name, and the assignment (p3). It should be either paper-clipped or stapled together. In addition, make a copy of p3.gradesheet.htm and hand it in with memo.txt in class, at the beginning of class on November 25. Your name should be on p3.grade_sheet.htm. Nothing else should be filled in.

You May Also Find These Documents Helpful

Related Topics