JAVA array problem ResortManagement /* CS141 Assignment 21 Start file * * Part 1
ID: 3859948 • Letter: J
Question
JAVA array problem
ResortManagement
/* CS141 Assignment 21 Start file
*
* Part 1
*
* This program is an object file that is designed to
* manage a resort of building and their available rooms.
*/
/* Students should not modifiy this file, but add the
* neccessary object files to implement it
*/
/* Also this file contains methods/algorithms that you may
* want to copy when completing part 2. Permission is given
* for this assignment to plagerize this work to help you
* with part 2.
*/
import java.util.Scanner;
public class ResortManagement
{
public static void main(String[] args)
{
Building[] resort = new Building[4];
resort[0] = new Building('A', 5, 150);
resort[1] = new Building('B', 4, 250);
resort[2] = new Building('C', 6, 100);
resort[3] = new Building('D', 10, 75);
runLoop(resort);
}
public static void runLoop(Building[] resort )
{
boolean keepGoing = true;
Scanner keyboard = new Scanner(System.in);
while(keepGoing)
{
System.out.println("What would you like to do?");
System.out.println(" 1. Rent a room?");
System.out.println(" 2. Check out a room?");
System.out.println(" 3. Print a summary");
System.out.println(" 4. Print a large overview");
System.out.println(" 0. Quit");
int x = keyboard.nextInt();
if(x == 1)
{rentARoom(resort, keyboard) ;}
else if(x == 2)
{ checkOutOfARoom(resort, keyboard); }
else if(x == 3)
{printResortSmallStatus(resort); }
else if(x == 4)
{printResortLargeStatus(resort); }
else if(x == 0)
{keepGoing = false;}
else {}
try {Thread.sleep(500);} catch(InterruptedException ex) {Thread.currentThread().interrupt();}
}
}
public static void checkOutOfARoom(Building[] resortList, Scanner in)
{
printOccupiedRooms(resortList);
System.out.println("What Room would you like to check out?");
String room = in.next().toUpperCase();
checkout(resortList, room);
}
public static void rentARoom(Building[] resortList, Scanner in)
{
System.out.println("What building would you like to rent from?");
char let = in.next().toUpperCase().charAt(0);
int num = let - 'A';
rentRoom(resortList, num);
}
public static void printOccupiedRooms(Building[] resortList)
{
System.out.print("The currently occupied rooms are : ");
for (int i = 0; i < resortList.length ; i ++)
{
System.out.print(resortList[i].listOfNonEmpty() );
}
System.out.println();
}
public static void checkout(Building[] resortList, String roomName)
{
boolean removed = false;
for (int i = 0; i < resortList.length ; i ++)
{
if (resortList[i].checkOut(roomName))
{
removed = true;
}
}
if (removed) System.out.printf("Room %s was cleared. %n",roomName);
else System.out.printf("No Room with that name was found.%n");
}
public static void rentRoom(Building[] resortList, int buildingNumber)
{
if (buildingNumber >= 0 && buildingNumber < resortList.length)
{
String x;
x = resortList[buildingNumber].rentRoom();
if (!x.equals("error")) System.out.printf("Room %s was rented out.%n",x);
else System.out.println("Sorry, no rooms in that building available.");
}
else System.out.println("Sorry, no building of that letter found.");
}
public static double getValue(Building[] resortList)
{
double val = 0;
for (int i = 0; i < resortList.length ; i ++)
{
val += resortList[i].getValue();
}
return val;
}
public static int getEmpty(Building[] resortList)
{
int val = 0;
for (int i = 0; i < resortList.length ; i ++)
{
val += resortList[i].currentEmpty();
}
return val;
}
public static void printResortSmallStatus(Building[] resortList)
{
System.out.println("************************************");
System.out.println("** Quick Status of the Resort");
for (int i = 0; i < resortList.length ; i ++)
{
System.out.println("** " + resortList[i]);
}
System.out.println("************************************");
System.out.print("** ");
printOccupiedRooms(resortList);
System.out.printf("** The current value of the resort is $%5.2f.%n",getValue(resortList));
System.out.printf("** There are %4d empty rooms.%n",getEmpty(resortList));
System.out.println("************************************");
}
public static void printResortLargeStatus(Building[] resortList)
{
System.out.println("**********Expanded Status of the Resort*******");
for (int i = 0; i < resortList.length ; i ++)
{
resortList[i].printCurrentStatus();
}
System.out.println("#####");
System.out.println("#####");
}
}
ResortManagement2
/* CS141 Assignment 21 Start file
*
* Part 2
*
* This program is an object file that is designed to
* manage a resort of building and their available rooms.
*/
/* Students should not modifiy this file, but add the
* neccessary object files to implement it
*
* This file assumes that you completed part 1 and have the
* room and building classes done, and are ready to
* implement the resort class.
*
*/
import java.util.Scanner;
public class ResortManagement2
{
public static void main(String[] args)
{
Resort myResort = new Resort(4);
myResort.setBuilding(0,'A', 5, 150);
myResort.setBuilding(1,'B', 3, 166);
myResort.setBuilding(2,'C', 7, 250);
myResort.setBuilding(3,'D', 10, 750);
runLoop(myResort);
}
public static void runLoop(Resort resortVar )
{
boolean keepGoing = true;
Scanner keyboard = new Scanner(System.in);
while(keepGoing)
{
System.out.println("What would you like to do?");
System.out.println(" 1. Rent a room?");
System.out.println(" 2. Check out a room?");
System.out.println(" 3. Print a summary");
System.out.println(" 4. Print a large overview");
System.out.println(" 0. Quit");
int x = keyboard.nextInt();
if(x == 1)
{
System.out.println("What building would you like to rent from?");
char let = keyboard.next().toUpperCase().charAt(0);
resortVar.rentRoom(let);
}
else if(x == 2)
{
resortVar.printOccupiedRooms();
System.out.println("What Room would you like to check out?");
String room = keyboard.next().toUpperCase();
resortVar.checkout(room);
}
else if(x == 3)
{
resortVar.printResortSmallStatus();
}
else if(x == 4)
{
resortVar.printResortLargeStatus();
}
else if(x == 0)
{
keepGoing = false;
}
else {}
try {Thread.sleep(500);} catch(InterruptedException ex) {Thread.currentThread().interrupt();}
}
}
}
Explanation / Answer
Building.java and Room.java for 1st assignment
Resort.java is 2nd assignment
Building.Java
public class Building {
private Room[] list;
private char buildingLetter;
private int size;
public Building(char buildingLetter, int size, double price)
{
this.buildingLetter = buildingLetter;
this.size = size;
list = new Room[size+1];
for(int i = 1 ;i<size+1;i++)
{
list[i] = new Room(price, i, buildingLetter);
}
}
public String toString()
{
return "Building " + String.valueOf(buildingLetter) + " has " + size + " rooms: (" + String.valueOf(currentEmpty()) + ") empty";
}
public void printCurrentStatus()
{
System.out.println("Building " + String.valueOf(buildingLetter));
for(int i = 1; i<size+1 ; i++)
{
System.out.println(list[i]);
}
}
public double getValue()
{
double sum = 0;
for(int i =1 ;i<size+1; i++)
{
if(list[i].getOccupied())
sum += list[i].getCost();
}
return sum;
}
public String listOfEmpty()
{
StringBuilder str = new StringBuilder();
for(int i = 1; i< size+1; i++)
{
if(!list[i].getOccupied())
str.append(list[i].getRoomName() + " ");
}
return str.toString();
}
public String listOfNonEmpty()
{
StringBuilder str = new StringBuilder();
for(int i = 1; i< size+1; i++)
{
if(list[i].getOccupied())
str.append(list[i].getRoomName() + " ");
}
return str.toString();
}
public int currentEmpty()
{
int num = 0;
for(int i = 1; i< size+1; i++)
{
if(!list[i].getOccupied())
num++;
}
return num;
}
public String rentRoom()
{
for(int i =1 ; i< size+1; i++)
{
if(!list[i].getOccupied())
{
list[i].rentRoom();
return list[i].getRoomName();
}
}
return "error";
}
public boolean checkOut(String roomName)
{
boolean ret = false;
if(roomName.length() == 2 )
{
char buildingName = roomName.charAt(0);
int roomNumber = Integer.parseInt(String.valueOf(roomName.charAt(1)));
if(buildingLetter == buildingName && roomNumber < size+1 && roomNumber > 0)
{
list[roomNumber].emptyRoom();
ret = true;
}
}
return ret;
}
}
Room.java
public class Room {
private double cost;
private int roomNumber;
boolean occupied;
char building;
public Room(double cost, int roomNumber, char building) {
super();
this.cost = cost;
this.roomNumber = roomNumber;
this.building = building;
this.occupied = false;
}
public String getRoomName()
{
return String.valueOf(building) + String.valueOf(roomNumber);
}
public double getCost() {
return cost;
}
public boolean getOccupied() {
return occupied;
}
public String toString() {
if(occupied)
{
return "Room " + getRoomName()+ " : is occupied";
}
else
{
return "Room " + getRoomName()+ " : is empty";
}
}
public void rentRoom()
{
occupied = true;
}
public void emptyRoom()
{
occupied = false;
}
}
Resort.java
public class Resort {
private Building[] resortList;
public Resort(int size)
{
resortList = new Building[size];
}
public void setBuilding(int buildingNumber, char buildingName, int size, double price)
{
if(buildingNumber >=0 && buildingNumber < resortList.length)
resortList[buildingNumber] = new Building(buildingName, size, price);
}
public void rentRoom(char let)
{
int num = let - 'A';
if (num >= 0 && num < resortList.length)
{
String x;
x = resortList[num].rentRoom();
if (!x.equals("error")) System.out.printf("Room %s was rented out.%n",x);
else System.out.println("Sorry, no rooms in that building available.");
}
else System.out.println("Sorry, no building of that letter found.");
}
public void checkout(String roomName)
{
boolean removed = false;
for (int i = 0; i < resortList.length ; i ++)
{
if (resortList[i].checkOut(roomName))
{
removed = true;
}
}
if (removed) System.out.printf("Room %s was cleared. %n",roomName);
else System.out.printf("No Room with that name was found.%n");
}
public void printResortSmallStatus()
{
System.out.println("************************************");
System.out.println("** Quick Status of the Resort");
for (int i = 0; i < resortList.length ; i ++)
{
System.out.println("** " + resortList[i]);
}
System.out.println("************************************");
System.out.print("** ");
printOccupiedRooms();
System.out.printf("** The current value of the resort is $%5.2f.%n",getValue(resortList));
System.out.printf("** There are %4d empty rooms.%n",getEmpty(resortList));
System.out.println("************************************");
}
public void printResortLargeStatus()
{
System.out.println("**********Expanded Status of the Resort*******");
for (int i = 0; i < resortList.length ; i ++)
{
resortList[i].printCurrentStatus();
}
System.out.println("#####");
System.out.println("#####");
}
public void printOccupiedRooms()
{
System.out.print("The currently occupied rooms are : ");
for (int i = 0; i < resortList.length ; i ++)
{
System.out.print(resortList[i].listOfNonEmpty() );
}
System.out.println();
}
public static double getValue(Building[] resortList)
{
double val = 0;
for (int i = 0; i < resortList.length ; i ++)
{
val += resortList[i].getValue();
}
return val;
}
public static int getEmpty(Building[] resortList)
{
int val = 0;
for (int i = 0; i < resortList.length ; i ++)
{
val += resortList[i].currentEmpty();
}
return val;
}
}
//Sample output
What would you like to do?
1. Rent a room?
2. Check out a room?
3. Print a summary
4. Print a large overview
0. Quit
1
What building would you like to rent from?
a
Room A1 was rented out.
What would you like to do?
1. Rent a room?
2. Check out a room?
3. Print a summary
4. Print a large overview
0. Quit
1
What building would you like to rent from?
a
Room A2 was rented out.
What would you like to do?
1. Rent a room?
2. Check out a room?
3. Print a summary
4. Print a large overview
0. Quit
1
What building would you like to rent from?
b
Room B1 was rented out.
What would you like to do?
1. Rent a room?
2. Check out a room?
3. Print a summary
4. Print a large overview
0. Quit
2
The currently occupied rooms are : A1 A2 B1
What Room would you like to check out?
a2
Room A2 was cleared.
What would you like to do?
1. Rent a room?
2. Check out a room?
3. Print a summary
4. Print a large overview
0. Quit
3
************************************
** Quick Status of the Resort
** Building A has 5 rooms: (4) empty
** Building B has 3 rooms: (2) empty
** Building C has 7 rooms: (7) empty
** Building D has 10 rooms: (10) empty
************************************
** The currently occupied rooms are : A1 B1
** The current value of the resort is $316.00.
** There are 23 empty rooms.
************************************
What would you like to do?
1. Rent a room?
2. Check out a room?
3. Print a summary
4. Print a large overview
0. Quit
4
**********Expanded Status of the Resort*******
Building A
Room A1 : is occupied
Room A2 : is empty
Room A3 : is empty
Room A4 : is empty
Room A5 : is empty
Building B
Room B1 : is occupied
Room B2 : is empty
Room B3 : is empty
Building C
Room C1 : is empty
Room C2 : is empty
Room C3 : is empty
Room C4 : is empty
Room C5 : is empty
Room C6 : is empty
Room C7 : is empty
Building D
Room D1 : is empty
Room D2 : is empty
Room D3 : is empty
Room D4 : is empty
Room D5 : is empty
Room D6 : is empty
Room D7 : is empty
Room D8 : is empty
Room D9 : is empty
Room D10 : is empty
#####
#####
What would you like to do?
1. Rent a room?
2. Check out a room?
3. Print a summary
4. Print a large overview
0. Quit
0
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.