Recent Question/Assignment

Applications Programming
Assignment 1
Topics: OO design, standard patterns, lists
Due date: 11:59pm Monday 19th of September 2016 Weight: 30%
Specification
A secure research facility has hired you to develop a new system for the lifts of its secure building. The system needs to know where everyone in the building is at every moment. To use the lifts, a person must be added to the system and given an ID card. This ID card must be swiped when making a trip on the lift so that the lift can track the movement of that person.
The building has 6 floors and 3 lifts. Each lift services a different range of levels: Lift 1 services levels 1­6, Lift 2 services levels 2­6 and Lift 3 services levels 2­5. The building entrance is on level 2 and the initial resting positions of all 3 lifts is level 2.
When a person registers for an ID card, the system records their initial level as 2. A person can also be removed from the system, but only if they are on level 2 and not currently waiting for or aboard a lift.
When calling a lift, a person swipes his/her ID card, selects a destination level/floor, and then waits. The system uses an algorithm to select the most suitable lift and reports the number of that lift to the caller. When the lift reaches the caller’s current level, the caller boards the lift unless the lift is heading in the opposite direction to that of the caller’s destination. When the lift reaches the caller’s destination, the caller alights from the lift. The lift will stop at a level if any person wants to board or alight.
The most suitable lift for a caller is determined by a suitability function. If a lift’s service range doesn’t include the caller’s current level or destination level, that lift’s suitability is 0. Otherwise if a lift is moving away from the caller, its suitability is 1. Otherwise if a lift is moving in the opposite direction that the caller wants to go in, then its suitability is N + 1 ­ D where N is the highest minus the lowest level of the building and D is the distance in levels between the lift and the caller. Otherwise, the lift’s suitability is N + 2 ­ D. The most suitable lift for a caller is the lift with the highest suitability.
The User Interface
Below is a sample I/O trace. Not every conceivable scenario is shown below and you should submit your code to PLATE to see what specific scenarios are tested.
Basic usage
When the program launches, you will see a mode prompt which can be used to change the behaviour of the program.
Mode:
For the moment, let’s leave this blank and press enter. The menu is now shown. Inputting an unrecognised command results in the help being shown:
Choice (a/r/p/c/l/o/x): ?
Menu a = add person r = remove person p = show people c = call lift l = show lifts o = operate
The user can add, remove and show people registered in the system with the ‘a’, ‘r’ and ‘p’ options. Each person must have a unique ID:
Choice (a/r/p/c/l/o/x): a
Person ID: 1
Name: Sam Worthington
Choice (a/r/p/c/l/o/x): a Person ID: 1
ID already exists
Choice (a/r/p/c/l/o/x): a Person ID: 2
Name: Nicole Kidman
Choice (a/r/p/c/l/o/x): a Person ID: 3
Name: Hugh Jackman
Choice (a/r/p/c/l/o/x): p Sam Worthington(1) on level 2
Nicole Kidman(2) on level 2
Hugh Jackman(3) on level 2
Choice (a/r/p/c/l/o/x): r Person ID: 1
Choice (a/r/p/c/l/o/x): r Person ID: 1
No such person
Choice (a/r/p/c/l/o/x): p
Nicole Kidman(2) on level 2
Hugh Jackman(3) on level 2
The lifts are shown with the ‘l’ option:
Choice (a/r/p/c/l/o/x): l
Lift 1 |­0­­­­|
Lift 2 |0­­­­| Lift 3 |0­­­|
Each lift is displayed as a string of dashes with the left side representing the bottom of the lift and the right side representing the top of the lift. One of the dashes is replaced by a number indicating how many passengers are currently aboard that lift. The position of this number also indicates the location of the lift within the shaft. Notice that Lift 2 and Lift 3 are shifted right by one level because those lifts start at level 2. Your code must not have any special cases to deal with different lifts. For example, you cannot write code like this: if (liftNumber != 1) { add an extra space }.

A lift can be called with the ‘c’ option:
Choice (a/r/p/c/l/o/x): c
Person ID: 1
No such ID
Choice (a/r/p/c/l/o/x): c
Person ID: 2
Destination level: 4
Choice (a/r/p/c/l/o/x): p
Nicole Kidman(2) on level 2 waiting to go to level 4
Hugh Jackman(3) on level 2
Choice (a/r/p/c/l/o/x): l
Lift 1 |­0­­­­|
Lift 2 |0­­­­| Lift 3 |0­­­|
The most suitable lift is chosen and the caller is added to the lift’s queue. In this case, all 3 lifts are equally suitable so the first lift will be chosen. If there was no suitable left (e.g. the destination was not serviceable by any lift), the call is discarded.
The lifts are operated via the ‘o’ option. Here, each lift use this algorithm:
? If the lift is currently stationary, check if there is anyone in the queue to be picked up or if there is any passenger to be dropped off, and set the direction of the lift toward the pickup or dropoff point.
? Before moving, let any passengers who want to alight here alight (they are then removed from the passengers list), and let any people in the queue board if they’re heading in the same direction as the lift and they’re on the same level that the lift is currently on (they are then removed from the queue).
? If the lift is now empty, the lift becomes idle (set the direction to 0).
? Otherwise, move the lift and all its passengers one level in the current direction. The lift stops if it reaches the top or bottom (set the direction to 0).
Now that Nicole has made a call for a lift, the operate option will work as follows:
Choice (a/r/p/c/l/o/x): o
Choice (a/r/p/c/l/o/x): p
Nicole Kidman(2) on level 2 going to level 4
Hugh Jackman(3) on level 2
Choice (a/r/p/c/l/o/x): l
Lift 1 |­1­­­­| UP
Lift 2 |0­­­­| Lift 3 |0­­­|
Nicole has boarded lift 1 and is now going UP to level 4, but the lift hasn’t moved yet. Pressing ‘o’ again: Choice (a/r/p/c/l/o/x): o
p
Nicole Kidman(2) on level 3 going to level 4
Hugh Jackman(3) on level 2
Choice (a/r/p/c/l/o/x): l
Lift 1 |­­1­­­| UP
Lift 2 |0­­­­| Lift 3 |0­­­|
The lift has moved up to level 3, and Nicole has moved along with the lift. Again:
Choice (a/r/p/c/l/o/x): o
Choice (a/r/p/c/l/o/x): p
Nicole Kidman(2) on level 4 going to level 4
Hugh Jackman(3) on level 2
Choice (a/r/p/c/l/o/x): l
Lift 1 |­­­1­­| UP
Lift 2 |0­­­­| Lift 3 |0­­­|
The lift with Nicole have moved up to level 4 and has stopped, but Nicole hasn’t alighted yet. Again:
Choice (a/r/p/c/l/o/x): o
Choice (a/r/p/c/l/o/x): p
Nicole Kidman(2) on level 4
Hugh Jackman(3) on level 2
Choice (a/r/p/c/l/o/x): l
Lift 1 |­­­0­­|
Lift 2 |0­­­­|
Lift 3 |0­­­|
Nicole has now alighted and there are zero passengers aboard.
The ‘x’ option exits the program.
Choice (a/r/p/c/l/o/x): x
Modes
When starting the program, the mode prompt allows for the input of a string of special mode codes in any order.
? ‘w’ means show how many people are waiting for a lift on each level of the building.
? ‘i’ means show how many people are idle on each level (i.e. not aboard a lift and not waiting for a lift).
? ‘a’ means the lifts will be automatically be displayed along with the program’s main menu.
For example, if you input “wi” or “iw” as the mode, then the waiting and idle numbers will be displayed for each level:
Idle: 1 2
Waiting: 1
Lift 1 |­0­­­­|
Lift 2 |0­­­­|
Lift 3 |0­­­|
This indicates that 1 person is idle on level 1, 2 people are idle on level 5 and 1 person is waiting for a lift on level 3. If the mode is “w”, then just the waiting numbers will be displayed:
Waiting: 1
Lift 1 |­0­­­­|
Lift 2 |0­­­­|
Lift 3 |0­­­|
Let’s run the program with all mode options enabled:
Mode: iwa Idle:
Waiting:
Lift 1 |­0­­­­|
Lift 2 |0­­­­|
Lift 3 |0­­­|
Choice (a/r/p/c/l/o/x):
As you can see, the lifts are automatically shown along with the menu. With the ‘a’ mode turned on, there is no longer any need to use the ‘l’ option to view the lifts. Also, with the ‘w’ and ‘i’ modes turned on, there is little reason to use the ‘p’ option to view the people. This allows us to run a scenario just with a combination of the call and operate menu options (after first adding some people):
Choice (a/r/p/c/l/o/x): a
Person ID: 1
Name: Bridget Jones
Idle: 1
Waiting:
Lift 1 |­0­­­­|
Lift 2 |0­­­­|
Lift 3 |0­­­|
Choice (a/r/p/c/l/o/x): a Person ID: 2
Name: Mark Darcy
Idle: 2
Waiting:
Lift 1 |­0­­­­|
Lift 2 |0­­­­|
Lift 3 |0­­­|
c
Person ID: 1
Destination level: 3
Idle: 1
Waiting: 1
Lift 1 |­0­­­­|
Lift 2 |0­­­­|
Lift 3 |0­­­|
Choice (a/r/p/c/l/o/x): c Person ID: 2 Destination level: 6 Idle:
Waiting: 2
Lift 1 |­0­­­­|
Lift 2 |0­­­­|
Lift 3 |0­­­| Choice (a/r/p/c/l/o/x): o Idle:
Waiting:
Lift 1 |­2­­­­| UP
Lift 2 |0­­­­|
Lift 3 |0­­­| Choice (a/r/p/c/l/o/x): o Idle:
Waiting:
Lift 1 |­­2­­­| UP
Lift 2 |0­­­­|
Lift 3 |0­­­|
Choice (a/r/p/c/l/o/x): o
Idle: 1
Waiting:
Lift 1 |­­1­­­| UP
Lift 2 |0­­­­|
Lift 3 |0­­­|
Choice (a/r/p/c/l/o/x): o
Idle: 1
Waiting:
Lift 1 |­­­1­­| UP
Lift 2 |0­­­­|
Lift 3 |0­­­|
Choice (a/r/p/c/l/o/x): o
Idle: 1
Waiting:
Lift 1 |­­­­1­| UP
Lift 2 |0­­­­|
Lift 3 |0­­­|
Choice (a/r/p/c/l/o/x): o
Idle: 1
Waiting:
Lift 1 |­­­­­1|
Lift 2 |0­­­­|
Lift 3 |0­­­|
Choice (a/r/p/c/l/o/x): o
Idle: 1 1
Waiting:
Lift 1 |­­­­­0|
Lift 2 |0­­­­|
Lift 3 |0­­­|
Choice (a/r/p/c/l/o/x): c
Person ID: 1
Destination level: 6
Idle: 1
Waiting: 1
Lift 1 |­­­­­0|
Lift 2 |0­­­­|
Lift 3 |0­­­|
Choice (a/r/p/c/l/o/x): o
Idle: 1
Waiting: 1
Lift 1 |­­­­­0|
Lift 2 |­0­­­| UP
Lift 3 |0­­­|
Choice (a/r/p/c/l/o/x): o
Idle: 1
Waiting:
Lift 1 |­­­­­0|
Lift 2 |­1­­­| UP
Lift 3 |0­­­|
Choice (a/r/p/c/l/o/x): o
Idle: 1
Waiting:
Lift 1 |­­­­­0|
Lift 2 |­­1­­| UP
Lift 3 |0­­­|
Choice (a/r/p/c/l/o/x): o
Idle: 1
Waiting:
Lift 1 |­­­­­0|
Lift 2 |­­­1­| UP
Lift 3 |0­­­|
Choice (a/r/p/c/l/o/x): o
Idle: 1
Waiting:
Lift 1 |­­­­­0|
Lift 2 |­­­­1|
Lift 3 |0­­­|
Choice (a/r/p/c/l/o/x): o
Idle: 2
Waiting:
Lift 1 |­­­­­0|
Lift 2 |­­­­0|
Lift 3 |0­­­|
x

Requirements
? Your design must consist of exactly the following set of classes and fields. All fields must be declared private. You may not add or remove classes or fields. You may add constructors, functions and procedures. (Copy and paste this into a new project)
public class Building { private int entrance = 2; private int bottom = 1; private int top = 6;
private LinkedList Lift lifts = new LinkedList Lift (); private LinkedList Person people = new LinkedList Person ();
private String mode;
}
public class Lift { private int number; private int bottom; private int top; private int level; private int direction;
private LinkedList Person passengers = new
LinkedList Person (); private LinkedList Person queue = new LinkedList Person ();
}
public class Person { private int id; private String name; private int level; private int destination; private boolean aboard;
}
public class In { private static final Scanner scanner = new Scanner(System.in);
}
? Constructor requirements:
? The Person constructor must initialise ALL 5 fields from parameters.
? The Lift constructor must initialise the number, bottom, top and level fields from parameters. The direction always starts at 0.

? The Building constructor must initialise the mode field by reading it from the user, and must add the 3 lifts to the list of lifts.
? Constructor parameters must be listed in the same order as the fields. ? Field requirements:
? The lift direction is always either -1 (down), 0 (stationary) or 1 (up).
? The lift queue contains the list of people waiting for that particular lift.
? The lift passengers list contains the people currently inside that particular lift. ? toString() function requirements:
? The Person toString() function must return a string in one of these forms depending on whether the person is (1) currently on board a lift, (2) waiting for a lift, or (3) neither on board a lift nor waiting for a lift:
¦ Ryan Heise (1) on level 2, going to level 4
¦ Ryan Heise (1) on level 2, waiting to go to level 4
¦ Ryan Heise (1) on level 2 where “Ryan Heise” and 1 indicate the person’s name and ID.
? The Lift toString() function must return a string beginning with the word “Lift”, then a space, then the lift number, then two spaces. If the bottom of the lift is at level 1, then the next part of the string will be “|” followed by a string of dashes “­­-...etc” followed by another “|”. The number of dashes is determined by how many levels are in the lift’s range, and one of the dashes should be replaced by a number indicating how many passengers are currently aboard that lift. The particular dash that gets replaced by this number depends on the current location of the lift. If the lift is currently at its bottom, then replace the leftmost dash. If the lift is currently at the top, replace the rightmost dash. If the lift is somewhere in the middle, replace the dash at the corresponding position. After the final “|”, the string may contain a space followed by the word “UP” or “DOWN” depending on whether the lift is currently moving up or down. This part of the string is absent if the lift is stationary. If the bottom of the lift is N levels higher than 1, then it should be shifted right N spaces so that it all are aligned at the corresponding levels. Examples:
¦ Lift 1 |­2­­­­| UP
¦ Lift 2 |­­­0­|
¦ Lift 3 |­­­1| DOWN
? Your main method must be defined in the Building class.
? You must not use hard coded literals. i.e. Don’t write the numbers 1, 6 or 2 anywhere in your code. Always refer to the bottom, top and entrance variables.
? You must submit your program to PLATE and match PLATE’s output to receive marks.
Beyond this, you are free to make your own design decisions, however, a design guide will be posted to PLATE in week 5 which will offer strong recommendations on how you should design and build your solution for best results.
Take a short break, and then continue reading. What follows is important!
Marking scheme
Task Mark
Class and field declarations 5%
Constructors 5%
toString functions 10%
Menu help 5%
Add person 5%
Show people 3%
Remove person 5%
Show lifts 2%
Call lift 1 10%
Operate lift 1 15%
Modes 13%
Suitability function 10%
Call and operation of multiple lifts 12%