Java Assignment. Fix the issue in my program

SoccerBoss
Category:
Programming
Price: $15 USD

Question description

SystemInventoryMain.doc

SysInventory.txt

 ITSystem.doc

Most of the project is complete. 

I need help with step 5.

Step 6 is done, however, I made a mistake that I cannot solve, the output says There are 4 systems and my output says There are 5 systems i cannot find this minor error.

Note: in the examples I show the output you should get for the given input. If your output does not match this for the same inputs you will receive less credit. For example, after a move command you should display the number of the old room, the systems in the old room, the number of the new room, and the systems in the new room, in the format shown. Output format and details are less important then getting your logic correct and getting the program to work, but they are still important.

For this assignment, we will work with the SysInventory program we are/will be discussing in class; see the provided files. In the linked directory you should find: ITSystem.java, SysInventory.java, and SystemInventoryMain.java 

Here is a run of the provided system. Output like this is what I mean by the results of “interactive tests”.

Welcome to Departmental Network Systems

Enter how many systems: 2

Enter the system's room no: 10

 System is assigned number 1

Enter the system's room no: 20

 System is assigned number 2

Enter system inventory number, 0 to quit: 2 <-- for #4 Enter id number or room number preceded by “room”

System 2 selected

Actions: exit, help, print, record-problem

Enter action: print

ITSystem@1a758cb <-- note useless toString() output: fix this in #1.

Enter action: record-problem

The system now has 1 recorded problems

Enter action: help

Actions: exit, help, print, record-problem

Enter action: exit

Enter system inventory number, 0 to quit: 0

Goodbye from Departmental Network Systems

1.  Add method toString() to ITSystem, including information from all fields. After this, the “print” command should work better in SysInventory. For this part, you only need to edit ITSystem.java, then rerun the whole program to see the improved output. As an example of what should be displayed for the print command issued above (associated with system 2 in the example):

Enter action: print

 Id 2 room 20 problems 0

Enter action:

2. Modify processActionsForITSystem() so as to add a  “in-same-room” action. It should print out all the systems in the same room as the currently selected system, using toString() to print out one system. Devise a series of (interactive) test inputs to exercise your program fully.

After this edit, the system should run (after same setup, so only one system in the same room):

Enter system inventory number, 0 to quit: 2 

System 2 selected

Actions: exit, help, print, record-problem, in-same-room

Enter action: in-same-room

 Systems in the same room as system 2:

 Id 2 room 20 problems 0 (toString output for system 2, can be different in detail)

Enter Action: …

This can be one “interactive test” result, but clearly we need to test with multiple systems in the same room for full testing.

3. Modify processActionsForITSystem() so as to add a “move” action. It should prompt for the number of the room to which the system is being moved, and print a warning if the room is the same as before. You may want to add a method to ITSystem for this. When complete, print out all the systems now in the old and new rooms. Devise a series of (interactive) test inputs to exercise your program fully.

Enter system inventory number, 0 to quit: 2 

System 2 selected

Actions: exit, help, print, record-problem, in-same-room, move

Enter action: move

 Enter room for system 2 to move to: 10

 Systems now in old room 20:

 None

 Systems now in new room 10:

 Id 1 room 10 problems 0

 Id 2 room 10 problems 0

Enter Action: …

4. Currently the user needs to know the id number to work on a system. Modify whichITSystem to allow the user to alternatively enter a room number, by entering “room 22” for example instead of just “12” for system 12. If there are multiple systems in that room, print them out and then ask for the id, and return from whichITSystem with the selected ITSystem object, as in the unmodified case. 

In the case of the original setup (without moves) the room number simply specifies a system, like this:

Enter system inventory number, 0 to quit, or room <n>: room 10

System 1 selected 

Actions: exit, help, print, record-problem, in-same-room, move

In the case of the original setup followed by the move specified above in 3, the room number selects 2 systems, and we need the user to choose between them, like this:

Enter system inventory number, 0 to quit, or room <n>: room 10

Room 10 has 2 systems:

 Id 1 room 10 problems 0

 Id 2 room 10 problems 0

Enter system inventory number from above list, 0 to quit: 2 

System 2 selected

Actions: exit, help, print, record-problem, in-same-room, move

5. Systems don't start out with problems, but develop them over time. Suppose a repairman comes in and wants to know all the systems that have problems. Modify whichITSystem to all the user to alternatively enter a display-problem-systems command. If so, print all systems with problems like this:

Enter system inventory number, display-problem-systems, 0 to quit, or room <n>: display-problem-systems

 None 

Enter system inventory number, display-problem-systems, 0 to quit, or room <n>: 

After the user has recorded 1 problem for systems 1 and 3 (in room 30) but not 2:

Enter system inventory number, display-problem-systems, 0 to quit, or room <n>: display-problem-systems

Id 1 room 10 problems 1

 Id 3 room 30 problems 1

Enter system inventory number, display-problem-systems, 0 to quit, or room <n>:

6. 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.

Extra credit: the user might want to know all the systems that exist. Modify whichITSystem so as to add a “print-all” action to print out all the systems that exist (using toString() to print them out). This action should only be available at the “Enter system inventory number” prompt, as the user should not be required to enter the system number to see what systems exist. You might want to create your own method for this that gets called from whichITSystem. The command should print the systems out in order of the room number they are in (lowest to highest). For systems in the same room you can print them out in any order. You can do this in similar manner to the one in AutomobileMain.java, where first the Automobile objects are sorted, and then printed out. You will also have to implement a compareTo function in ITSystem.java, and make the ITSystem class implement the Comparable interface, very similarly to how Automobile.java does this. Note that the systems in the array start at index 1 and are ordered by system id, not room number. This causes two types of problems: first, the sort method for arrays requires that data start at index 0 of the array; and second, if we sort the data in the array, modifying it so the array is no longer sorted by system id, other code in the program will break. In order to get Arrays.sort to work, and not to change all the other code you may want to create another array of ITSystem, just for this problem, which should start at index 0. If so, copy the standard ITSystem array to this new one, (but going from index 1 (old) to index 0 (new), index 2 (old) to index 1 (new), etc), and then sort the new array before printing it. Modify your narrative to include this problem as well. The interactive test might look like this:

Welcome to Departmental Network Systems

Enter how many systems: 4

Enter the system's room no: 10

 System is assigned number 1

Enter the system's room no: 10

 System is assigned number 2

Enter the system's room no: 5

 System is assigned number 3

Enter the system's room no: 2

 System is assigned number 4

Enter system inventory number, print-all, display-problem-systems, 0 to quit, or room <n>: print-all

There are 4 systems ******* On my code it says ((There are 5 systems))

 Id 4 room 2 problems 0

 Id 3 room 5 problems 0

 Id 1 room 10 problems 0

 Id 2 room 10 problems 0

Enter system inventory number, print-all, display-problem-systems, 0 to quit, or room <n>: 0

Goodbye from Departmental Network Systems



Just in case the code is bellow.

*****************************************************************************************************************************************

import java.util.Scanner;

public class SystemInventoryMain {

    public static void main( String[] args )

    {

        Scanner input = new Scanner(System.in);

        SysInventory sysInventory = new SysInventory( "Departmental Network Systems, version 2.0" );

        sysInventory.open(input);

        sysInventory.doCommands();    

    }

}

********************************************************************************************************************************

/**

 * A SysInventory object keeps track of computer systems for a department

 *

 * @see SystemInventoryMain

 * @see ITSystem

 * Note: with rare exceptions, getting input from the user is handled in one method

 * (processActionsForITSystem, whichITSystem), but actually acting on te user input

 * is done in a different, helper, method (pickSystemFromRoom, printAllSystems,

 * printSystemsInRoom, moveSystem).  It's good practice, whenever the code gets long 

 * for a method you are working on, to think of what tasks this method does and 

 * make additional helper methods for each of those tasks.  It's also good practice, 

 * if you do the same or similar task in different places, to replace the code for 

 * it in those places with a method which you call from those places.  For

 * example: printSystemsInRoom.

 */

import java.util.*;

public class SysInventory

{

  private String name;             // the name of this SysInventory 

  private Scanner scanner;         // for getting input from the user  

  private ITSystem [] systems;     // the systems we're tracking

  private static final String HELPSTRING = "Actions: exit, help, print, record-problem, in-same-room, move";

  /**

   * Construct a SysInventory with the given name.

   * @param name the name of the SysInventory.

   */

  public SysInventory( String name )

  {

    this.name = name;

  }

  /**

   * open the SysInventory: get it set up with IT systems

   * @param input for user input.

   */

  public void open( Scanner input)

  {

    scanner = input;

    System.out.println( "Welcome to " + name );

   

    // Construct ITSystems systems. (Eventually replace by reading a file)

    int n = promptInt( "Enter how many systems: " );

    systems = new ITSystem[n + 1];  // avoid "system 0"

    for (int id = 1; id <= n; id++) {

      int r = promptInt( "Enter the system's room no: " );

      systems[id] = new ITSystem( id, r , 0 );  // assume 0 problems to start

      System.out.println(" System is assigned number " + id);  

    }  

  }

  /**

   * process commands for the SysInventory

   * 

   */

  public void doCommands() {

    while ( true ) {

      ITSystem sys = whichITSystem();

      if ( sys == null ) {

        break; // all done

      }

      else {

        processActionsForITSystem(sys);

      }

    }      

    System.out.println( "Goodbye from " + name );

  }

  // Prompt the user for an system number and return the

  // corresponding ITSystem object. Return null when

  // the user is done.

  private ITSystem whichITSystem()

  {

    while (true) {

      System.out.print("Enter system inventory number, print-all, 0 to quit, or room <n>: ");

      if (scanner.hasNextInt()) { 

        int idNumber = scanner.nextInt();

        if (idNumber > 0 && idNumber < systems.length) {

          return systems[idNumber];  // found matching id

        }

        else if ( idNumber == 0 ) {

          return null;  // user says to quit

        }

        else {

          System.out.println( "No system numbered " + 

                             idNumber + "; try again." );

        }

      } else { // picked room number  or print-all 

        String pickingSystem = scanner.next();

        if ( pickingSystem.equals( "room" ) ) { // picked room number

          int roomNo = scanner.nextInt(); 

          int idNumber = pickSystemFromRoom(roomNo);

          if (idNumber == 0) { // user quitting after all

            return null; 

          } else if (idNumber > 0 && idNumber < systems.length) {

            return systems[idNumber];  

          } 

        } else if ( pickingSystem.equals( "print-all" ) ) { // picked print-all

          printAllSystems();

        } else {

          System.out.println("sorry, unknown action" );

        }

      }

    }

  }

  // Prompt the user for action to process.

  // and do it,

  private void processActionsForITSystem( ITSystem system) 

  {

    System.out.println("System " + system.getId() + " selected");

    System.out.println( HELPSTRING );

   

    boolean moreActions = true;

    while ( moreActions ) {

      String command = prompt( "Enter action: " );

      if ( command.equals( "exit" ) ) {

        moreActions = false;

      }

      else if ( command.equals( "help" ) ) {

        System.out.println( HELPSTRING );

      }

      else if ( command.equals( "print" ) ) {

        System.out.println(system);  // calls toString()

      }

      else if ( command.equals( "record-problem" ) ) {

        system.recordProblem( );

        System.out.println("The system now has " + system.getProblemCount() + " recorded problems");

      }

      else if ( command.equals( "in-same-room") ) {

        System.out.println("  Systems in the same room as system " + system.getId() + ":");

        printSystemsInRoom(system.getRoomNo());

      }

      else if ( command.equals( "move") ) {

        moveSystem(system);

      }

      else {

        System.out.println("sorry, unknown action" );

      }

    }

  }

  // helper methods using the private Scanner  

  private String prompt( String prompt) {

    System.out.print(prompt);

    return scanner.next();

  }

  private int promptInt( String prompt) {

    System.out.print(prompt);

    return scanner.nextInt();

  }

  // more helper methods

  // Print all the systems for a given room.  Used lots of places.

  private void printSystemsInRoom(int roomNo) {

    boolean foundOne = false;

    for (int id = 1; id < systems.length; id++) {

      if(systems[id].getRoomNo() == roomNo) {

        System.out.println("  " + systems[id]);

        foundOne = true;

      }

    }

    if(!foundOne) { // no systems in roomNo

      System.out.println("  None");

    }

  }

  // move the given system from one room to another.

  private void moveSystem(ITSystem system) {

    int oldRoomNo = system.getRoomNo();

    int newRoomNo;

    do {

      newRoomNo = promptInt("Enter room for system " + system.getId() + " to move to: ");

      if(newRoomNo == oldRoomNo) {

        System.out.println("System " + system.getId() + " is already in this room.");

      }

    } while(newRoomNo == oldRoomNo);

    // moves systems from old room to new room with additional method in ITSystem

    // Note that we could have done the same thing with:

    // int i = system.getId();

    // systems[i] = new System(system.getId(), newRoomNo, system.getProblemCount());

    // I felt creating a setRoomNo method in ITSystem and using it was a little simpler.

    system.setRoomNo(newRoomNo); 

    System.out.println("  Systems now in old room " + oldRoomNo + ":");

    printSystemsInRoom(oldRoomNo);

    System.out.println("  Systems now in new room " + newRoomNo + ":");

    printSystemsInRoom(newRoomNo);

  }

  // select a system from the systems in the room, given a room number

  // returns 0 if user wants to quit, or -1 if no systems available in room

  // we could have made this more efficient at the cost of making it harder 

  // to understand.  This code loops over the systems once to count how

  // many are in the same room as the parameter, and if there are

  // more then 1, loops over the systems again in printSystemsInRoom method 

  // to print them out.  Instead, during the first loop we could have just tracked 

  // the first system we found, and if/when we found a second system, printed out the 

  // first and second systems, and any subsequent ones.

  // More efficient, as it would use only one loop over the systems array (much more 

  // efficient if we have millions of systems), but harder to read and understand for 

  // subsequent programmers.

  private int pickSystemFromRoom (int roomNo) {

    int idNumber = 0;

   

    // count number of systems in roomNo

    int numSystemsFound = 0;

    for (int id = 1; id < systems.length; id++) {

      if(systems[id].getRoomNo() == roomNo) {

        if(++numSystemsFound == 1) {

          idNumber = id; // track first system found in case only 1.

        } 

      }

    }

   

    if(numSystemsFound == 1) { // only one system found, return it.

      return idNumber;

    } else if(numSystemsFound > 1) { // more then one system found, allow user to pick among them.

      do {

        System.out.println("Room " + roomNo + " has " + numSystemsFound + " systems:");

        printSystemsInRoom(roomNo);

        idNumber = promptInt("Enter system inventory number from above list, 0 to quit:");              

        if (idNumber == 0) { // user doesn't like these systems, quitting.

          return 0;

        } else if (idNumber < 1 || idNumber >= systems.length) { // invalid system ID 

          System.out.println( "No system numbered " + idNumber + "; try again." );

        } else if (systems[idNumber].getRoomNo() != roomNo) { // system not in room selected.

          System.out.println("System " + idNumber + " is not in room " + roomNo + "; try again.");

        } else { // valid selection, return it.

          return idNumber;

        }

      } while (true);

    } else { // no systems found

      System.out.println("No systems found in room " + roomNo + ".");

      return -1;

    }

  }

  // prints all systems using Arrays.sort which, internally, uses compareTo method for sorting systems.

  // Note: sort needs an array with 0-based indexing, which it then changes the order of.

  // our current systems array starts at index 1 (and so will break sort method), and everywhere else

  // in the code it relies on the index of each system matching the id of the system, so 

  // even if we could work with a 1-based index array, it would cause massive problems elsewhere to sort it.

  // So we copy the contents to a new temporary array, just for display here, starting at index 0 of the new array

  // and the new array has no reliancce on index == id.

  private void printAllSystems() {

    ITSystem[] sortedSystems = new ITSystem[systems.length - 1]; // deep copy of systems to make it 0-indexed and 

    // allow sorting to change it so system id no longer

    // matches index.

    for(int id = 1; id < systems.length; id++) { // for each system

      sortedSystems[id - 1] = systems[id]; // make a reference to the system in the new array we can monkey around with

    }

    Arrays.sort(sortedSystems); // sort systems in the new array according to compareTo method.

    System.out.println("there are " + systems.length + " systems.");

    for(int id = 0; id < sortedSystems.length; id++) { // new array has 0-based indexing

      System.out.println(" " + sortedSystems[id]);

    }      

  }

}

***********************************************************************************************************************

public class ITSystem implements Comparable<ITSystem>

{

  private int id;      // local inventory no.

  private int roomNo;  // location

  private int problems; // count of reported problems

  /**

   * A constructor for creating a new ITSystem.

   *

   * @param id  unique id of system

   * @param roomNo  location of system

   * @param problems number of reported problems for system

   */

  public ITSystem( int id, int roomNo, int problems )

  {

    this.id = id;

    this.roomNo = roomNo;

    this.problems = problems;

  }

  /**

   * Record problem

   *

   */

  public void recordProblem()

  {

    problems++;

  }

  public int getId()

  {

    return id;

  }

  public int getRoomNo()

  {

    return roomNo;

  }

  // Setter for room number.  Needed for move command.

  public void setRoomNo(int newRoomNo) {

    roomNo = newRoomNo;

  }

  public int getProblemCount()

  {

    return problems;

  }

  // Had to follow the format for what an ITSystem looked like when printed from the project

  // writeup.

  public String toString() 

  {

    return "Id " + id + " room " + roomNo + " problems " + problems;

  }

  // allows for sorting based strictly on room number.

  // compareTo must return a negative integer, zero, or a positive integer as "this"

  // object is less than, equal to, or greater than the specified "other" object. 

  public int compareTo(ITSystem other) {

      return roomNo - other.roomNo;

  }

}




Tutor Answer

(Top Tutor) Daniel C.
(997)
School: UIUC
PREMIUM TUTOR
Studypool has helped 1,244,100 students
Ask your homework questions. Receive quality answers!

Type your question here (or upload an image)

1825 tutors are online

Brown University





1271 Tutors

California Institute of Technology




2131 Tutors

Carnegie Mellon University




982 Tutors

Columbia University





1256 Tutors

Dartmouth University





2113 Tutors

Emory University





2279 Tutors

Harvard University





599 Tutors

Massachusetts Institute of Technology



2319 Tutors

New York University





1645 Tutors

Notre Dam University





1911 Tutors

Oklahoma University





2122 Tutors

Pennsylvania State University





932 Tutors

Princeton University





1211 Tutors

Stanford University





983 Tutors

University of California





1282 Tutors

Oxford University





123 Tutors

Yale University





2325 Tutors