CSIT115 Programming Assignment 3 (p3)
 Using ArrayList<ITSystem>, Reading and writing Data to/from Files

Due Friday, Apr. 26 in it115/p3.  Extended to Sun., Apr. 28 due to University closings

For this assignment, we will continue to work with the SysInventory program we developed in the last programming project. See the provided files. Note that ITSystem now has an equals method, coded following equals for Point, pg. 577.

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:

 

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 inventory number, 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, 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 pg. 401. 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, 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

Enter system inventory number, 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, 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, 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).

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.