Cis296 Proj1 Fa24
Cis296 Proj1 Fa24
Objectives
• To solve problems using ArrayLists
• To perform file I/O
Introduction
• Hotel
o Contains the data and some business logic related to an individual hotel
• HotelDemo
o Contains the main method
o Do not use the newer Java features for “unnamed classes” (with implicit class
declarations) or instance main methods
For this assignment, you will implement a class named Hotel. The class should contain:
Data fields
• A private int data field named numRooms representing the total number of rooms available for
the given Hotel
• A private int data field named numOccupied, representing the number of rooms currently
occupied
• A private String data field named hotelName, with the name of the hotel
Methods
• A no-arg constructor that creates a Hotel object with the name “Default Hotel”, and 10 rooms
total along with 0 rooms occupied
• A constructor that creates a Hotel object based on three parameters: hotelName, numRooms,
and numOccupied
o These parameters correspond to the fields
o The fields should be set to the corresponding parameter values
• Accessor (getter) and mutator (setter) methods for all data fields
1
• A getOccupancyRate method that returns the occupancy rate for the hotel
o Occupancy rate = number of rooms occupied / total number of rooms
o When you eventually display the value, you should display up to two decimal places
▪ Hint: The DecimalFormat class or using printf could help with this
Input
Each line will contain either one or three values. Each line represents a separate Hotel, whose data
should be made into an object. If there’s one value (meaning anything that doesn’t have any commas in
it – could be a string, integer, double, etc.), you just create a Hotel object using the no-arg constructor.
If there are three values provided, you create a Hotel object, with the following conditions:
Regardless, if the input is a valid hotel, you should create the Hotel object and add the object’s
reference to an ArrayList of Hotel objects, maintained in the same file as main.
Input Format
If one value:
If three values:
Hints
• Consider that the nextLine method of a Scanner can be used to read in the entire line of
data as a String
• The String class has a method called split that will return a String array, with values
separated by a delimiter
o Default delimiter is space, but this can be changed
Output
As soon as the file is read into memory and the data is placed in the appropriate data structure(s), the
following will be printed to standard output (the console), in general form:
2
<Hotel Name> : <occupancy rate>
At the very bottom of the output, you should display the overall occupancy rate across all hotels that
were read in from file (only count the valid ones, of course).
Example
(Note that line 2 should be default because there aren’t three values, and the last line is invalid because
the occupants outnumber the total number of rooms)
Sample Output
Note: Display the values as a percentage
(Total occupancy was calculated by summing all the total occupancy at each hotel, and the total
available rooms, and then dividing number of occupied rooms by total number of rooms at each hotel –
it only considered the valid hotels. So, this was 140/195). The default hotel had 10 rooms.
Deliverables
To turn in the assignment, please upload a zip file of a folder containing the .java files necessary for the
program to run, as well as screenshots of your program running. Upload the zip file to to the
appropriate assignment directory on Canvas.