In: Computer Science
You are writing a program for an architectural firm that needs to enter the areas of all the rooms in a building project they’re working on. Some rooms are square, some are round, and some are rectangular. Each room in the building has an ID number to help prevent adding the same room more than once. The program needs to prompt the user for the room ID number, the shape of the room, and the dimensions needed to calculate the area. It should store the ID number and the calculated area in sq. ft. (rounded to the nearest foot) of each room in a single integer ArrayList in the order: ID, area, ID, area, ... If the room ID has already been entered, it should let the user know and prompt for another. The program should continue running until the user chooses to close the program. It should also allow the user to view a list of the current rooms’IDs and areas, the total area, and the average room area. The program will need at least the following methods. The only global variable should be a Scanner object.•public static void main(String[] args)controls the flow of the program and manages the integer ArrayList.It will present the user with the choice to enter a new room, view room statistics, or exit the program. If an invalid choice is made, it should just repeat the menu of choices.•public static void getRoomId(ArrayList<Integer> data)asks the user for the ID of the room they’re reporting on, checks to make sure that ID has not already been entered, and adds the ID to the ArrayList. It should bulletproof input and allow the user to keep trying until a valid, unique ID is entered.•public static int getRoomShape() gets the room shape selection from the user by presenting a numbered menu (such as 1 –square, 2 –round, 3 –rectangular.) It should bulletproof input and allow the user to keep trying until a valid choice is entered.•public static int calcArea(int roomShape)calculates the room area based on the shape of the room selected, prompting the user for the data needed to do the calculation (width for square, diameter for round, length and widthrectangular.)•public static void displayStats(ArrayList<Integer> data) reads all the data stored in the ArrayList, prints out the entire list of room IDs and areas, followed by the total area of all the rooms added together and the average room area based on the number of rooms currently entered.You are welcome to add more methods if necessary, but you have to have the above methods. The program should be error free and user friendly. Proper indentation and spacing is expected, but you do not have to add JavaDoc comments.
CODE -
import java.util.ArrayList;
import java.util.Scanner;
public class rooms {
// Create scanner object globally
static Scanner keyboard = new Scanner(System.in);
// Function to get room ID from the user
public static void getRoomId(ArrayList<Integer> data)
{
int roomId;
// Take room ID as input
System.out.print("Enter Room ID: ");
roomId = keyboard.nextInt();
// Prompt user for a unique room ID and take room ID as input until user enters an unique room ID
while (data.contains(roomId))
{
System.out.println("\nRoom already added! Try again\n");
System.out.print("Enter Room ID: ");
roomId = keyboard.nextInt();
}
// Add room ID to the ArrayList
data.add(roomId);
}
// // Function to get room shape from the user
public static int getRoomShape()
{
int roomShape;
// Display the choice of shapes available
System.out.println("Select the shape of the room");
System.out.println("1. Square");
System.out.println("2. Round");
System.out.println("3. Rectangular");
// Take the user's choice of shape as input
System.out.print("\nEnter your choice: ");
roomShape = keyboard.nextInt();
// Take input from the user until user enters an available shape as his/her choice.
while(roomShape<1 || roomShape>3)
{
System.out.println("\nNo such shape available! Select from the avilable choices.\n");
System.out.print("Enter your choice: ");
roomShape = keyboard.nextInt();
}
// Return the shape
return roomShape;
}
// // Function to get dimensions of the room as input based on the room shape and calculate the area of the room
public static int calcArea(int roomShape)
{
int area = 0;
if (roomShape == 1)
{
// Take width of the room as input if shape of the room is square
System.out.print("Enter the width of the room: ");
float width = keyboard.nextFloat();
// Calculate area of the room
area = Math.round(width * width);
}
else if (roomShape == 2)
{
// Take diameter of the room as input if shape of the room is round
System.out.print("Enter the diameter of the room: ");
float diameter = keyboard.nextFloat();
// Calculate area of the room
area = (int)Math.round(Math.PI * (diameter/2) * (diameter/2));
}
else if(roomShape == 3)
{
// Take length and width of the room as input if shape of the room is rectangular
System.out.print("Enter the length of the room: ");
float length = keyboard.nextFloat();
System.out.print("Enter the width of the room: ");
float width = keyboard.nextFloat();
// Calculate area of the room
area = Math.round(length * width);
}
// Return the area of the room
return area;
}
public static void displayStats(ArrayList<Integer> data)
{
int totalArea = 0;
// Display ID and area of all the room and also calculate the total area
for (int i=0; i<data.size(); i+=2)
{
System.out.println("Room ID: " + data.get(i));
System.out.println("Room area: " + data.get(i+1) + "\n");
totalArea += data.get(i+1);
}
// Calculate average area of all the rooms
int avgArea = Math.round(totalArea / (data.size()/2));
// Display total area of all the rooms and the average area
System.out.println("Total area of all the rooms: " + totalArea);
System.out.println("Average room area: " + avgArea);
}
public static void main(String[] args)
{
int choice;
ArrayList<Integer> data = new ArrayList<>();
int roomShape, area;
// Loop to run until user chooses to quit
while(true)
{
// Display menu
System.out.println("\nBuilding Project Menu");
System.out.println("1. Enter a new Room");
System.out.println("2. View Room Statistics");
System.out.println("3. Quit the Program");
// Take choice as input from user
System.out.print("\nEnter your choice: ");
choice = keyboard.nextInt();
// Perform action based on user's choice
if(choice == 1)
{
getRoomId(data);
roomShape = getRoomShape();
area = calcArea(roomShape);
data.add(area);
}
else if(choice == 2)
displayStats(data);
else if(choice == 3)
{
System.out.println("\nThank You for using our Program\n");
break;
}
else
System.out.println("\nInvalid Choice!\n");
}
}
}
SCREENSHOTS -
CODE -
OUTPUT -
If you have any doubt regarding the solution, then do
comment.
Do upvote.