UCI has just started a program in hotel and restaurant management; its dean has
ID: 666950 • Letter: U
Question
UCI has just started a program in hotel and restaurant management; its dean has established a small "bed and breakfast" hotel as a lab for the program's students. The dean has asked you to write the reservations software for this new inn, which will be called the Anteater BandB.
Your program will keep track of the rooms available for rent (these vary, since sometimes a room is closed for redecoration) and the reservations that guests have made for these rooms.
Your program will be a batch program; this means that you will read all of your input from an external data file and produce output as described below
The input for this program comes from a single text file, which consists of an unlimited number of input command lines. We will describe the various commands below; for each stage, you will implement (or modify) a few more commands
(1) For this stage, your program will keep track of the rooms that are available. This stage implements four commands, as described below. On each command line, the first two nonwhitespace characters are the command; command letters may be upper or lower case.
AB (for "add a new bedroom") followed by an integer room number (in the range 1–999). Add a new bedroom with the specified room number.
BL (for "bedroom list"). Print a list of the bedrooms currently available. The input file may contain any number of these commands in any order; each BL command prints a list of available bedrooms based on what has been added as of that point. See the sample output below for the format of the printed bedroom list. For this stage, it doesn't matter what order the bedrooms appear in.
PL (for "print line"), followed by any text. Simply print (or "echo") a line, copying the input (not counting the PL and leading whitespace) to the output. You'll find it useful in testing, and it's also a simple way to make the program's reports clearer or fancier
** Comment, followed by any text. Like comments in a program, comment lines don't have any effect on the program's behavior; they just serve as annotations in the command file.
Your implementation should include a class called Bedroom. A new instance of the Bedroom class should be created when a new bedroom is added. Each Bedroom object should have instance variables to describe important information associated with each bedroom. You will probably want to maintain a list of Bedroom objects which grows when a bedroom is added.
Below is a sample input file for this stage.
(I put the following in a pastebin link because the formatting here is different.
http://pastebin.com/Jf9jVG0x
Explanation / Answer
/**The class Bedroom that contains a constructor that takes a room number . The method toString that retuns the room number.*/
//Bedroom.java
public class Bedroom
{
private int roomNum;
//default constructor
public Bedroom()
{
roomNum=0;
}
//constructor with room number
public Bedroom(int roomNum)
{
this.roomNum=roomNum;
}
@Override
public String toString() {
return roomNum+"";
}
}//end of Bedroom
-------------------------------------------------------------------------------------
/**The program that reads a input.txt file that contains the commands like AB, BL, PL then creates rooms for AB command with room number followd by AB. If the command is PL then prints a line . If the command is BL then prints a bed room list .
* */
//BedRoomDriver.java
import java.io.File;
import java.util.ArrayList;
import java.util.Scanner;
public class BedRoomDriver
{
public static void main(String[] args)
{
//Create an arraylist of type Bedroom class
ArrayList<Bedroom>roomList=new ArrayList<Bedroom>();
String command = "";
int roomNum = 0;
Scanner scanner=null;
//open an input file using Scanner class
try
{
scanner=new Scanner(new File("input.txt"));
//read coomands from the input file
while(scanner.hasNext())
{
//get command
command=scanner.next();
//check if the command is AB (Add bed room)
if(command.equals("AB"))
{
//read next room number
roomNum=scanner.nextInt();
//create an instance of Bedroom with room num
roomList.add(new Bedroom(roomNum));
}
//check if the command is BL (print bedroom list)
else if(command.equals("BL"))
printBedroomList(roomList);
//check if the command is PL print line
else if(command.equals("PL"))
printLine();
}
}
//catch exception if error occurs
catch (Exception e)
{
System.out.println(e);
}
}
//Method to print a line
private static void printLine()
{
System.out.println("------------------------------------");
}
//Method to print list of rooms under service
private static void printBedroomList(ArrayList<Bedroom> list)
{
System.out.println("Number of bedrooms in service:"+list.size());
for (Bedroom bedroom : list)
{
System.out.println(bedroom);
}
}
}//end of BedRoomDriver class
-------------------------------------------------------------------------------------
Input file name
input.txt
AB 100 AB 101 AB 102 AB 103 BL PL AB 100 AB 201 AB 203 BL
Sample output:
Number of bedrooms in service:4
100
101
102
103
------------------------------------
Number of bedrooms in service:7
100
101
102
103
100
201
203
Hope this helps you
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.