Click here to Skip to main content
15,891,689 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Basically I'm working on an assignment for my first Java class. I have most of it completed except for the part of the program that asks for area in sq. ft. for each room. I'm struggling with thinking of a way to ask the user for only the rooms specified.

Java
public class PropertyAssessment {
    public static void main(String[] args) {
        // prompt user to input number, type, and size of rooms
        // create 5 room variables
        // display output with all the information
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter street name: ");
        String streetName = sc.nextLine();
        System.out.print("Enter street number: ");
        int streetNumber = sc.nextInt();
        System.out.print("Enter number of rooms: ");
        int lineCount = 0;
        int numberRooms = sc.nextInt();
        String r1 = "";
        String r2 = "";
        String r3 = "";
        String r4 = "";
        String r5 = "";
        
        if (numberRooms > 0) {
            System.out.print("Type of first room: ");
            r1 = sc.next();
        }
        if (numberRooms > 1) {
            System.out.print("Type of second room: ");
            r2 = sc.next();
        }
        if (numberRooms > 2) {
            System.out.print("Type of third room: ");
            r3 = sc.next();
        }
        if (numberRooms > 3) {
            System.out.print("Type of fourth room: ");
            r4 = sc.next();
        }
        if (numberRooms > 4) {
            System.out.print("Type of fifth room: ");
            r5 = sc.next();
        }
        lineCount = lineCount + 1;
        System.out.print(lineCount + ". ");
        System.out.print("         Street: ");
        System.out.print(streetName);
        System.out.print(" #" + streetNumber);
        System.out.println();
        
        lineCount = lineCount + 1;
        System.out.print(lineCount + ".");
        System.out.print("          Total Rooms: ");
        System.out.print(numberRooms);
        System.out.println();
        
        lineCount = lineCount + 1;
        System.out.print(lineCount + ".");
        System.out.print("          Room Types: ");
        System.out.print(r1 + ", " + r2 + ", " + r3 + ", " + r4 + ", " + r5);
        System.out.println();
    }
}


I figure it will require some type of loop but I can't figure out what. If the user inputs two rooms and specifies what the rooms are, I want it to ask for the area of the rooms individually so it can later calculate total area. Nothing I've tried has worked.

I also want to know how I can make it so if I don't have a total of 5 rooms it would leave out commas instead of adding the commas for unspecified rooms in the output. For example if less than 5 rooms are inputted it will show up like "bedroom, bathroom, Dining, , "

This is the assignment in question.
Declare variables to hold the data fields for:
Street number
Street name
The number of rooms in the house
Five string variables for types of rooms: (living, dining, bedroom1, bedroom2, kitchen, bathroom, etc.)
Five integer variables for the area of each room in sq. ft.
Price per sq. ft., for example, $150.50 (store this value as double.)
Prompt the user for each of the above fields. Read console input and store entered values in the corresponding variables.
IMPORTANT: Make sure that the street name can be entered with embedded spaces, for example, "Washington St" and "Park Ave" are both valid street names.
Compute total area of the house (total sq. ft.) and multiply it by the Price per sq. ft. to compute the estimated property value.
Display the results in the following format. IMPORTANT: Your program should keep the line count in a separate variable and print the line numbers in your report using that variable as shown:
1. Street: ______ #____
2. Total Rooms: _____ (list entered rooms here)
3. Total Area: _____ sq. ft.
4. Price per sq. ft: $_______
5. Estimated property value: $_______

These are the requirements I need to meet. I know how to calculate the area and price but I simply don't know how to ask for the area of only the rooms specified by the user. Any help I can get would be much appreciated!

What I have tried:

I've tried initializing values and asking for room sizes but it's not applying only to the given number of rooms.
Posted
Updated 2-Nov-18 23:39pm

1 solution

Create a Room class that can hold the details of each room (name, area). Then create an array or List that can hold the Room objects instead of repeating the same code in a set of if statements. Then write a loop that repeats for the number of rooms, each time asking for the name and dimensions, creating a new Room object and adding it to the array. You can then iterate the array later to add up the totals.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900