In: Computer Science
JAVA programming- please answer all prompts as apart of the same java project
Enums
Part A
Create an enumeration AptType representing several different types of apartment. Each type of apartment has a description, a size (square footage), and a rent amount. In the enumeration, provide public final instance variables for these. Also write a parameterized constructor that sets the values of these variables (doing this is just part of seeing up the enum) The apartment types are
description | size (sq ft) | rent ($/mo) | |
studio | "Studio" | 520 | 480 |
one bedroom | "1 Bedroom" | 810 | 720 |
two bedroom | "2 Bedroom" | 1120 | 1030 |
luxury | "Luxury" | 1720 | 1780 |
Create the possible values for the enumeration, one for each type of apartment, in this order. Give these brief all-caps names.
Part B
☑ Create a class Apartment. Each Apartment has an instance variable to store its apartment type (use your enumeration). Also add a boolean indicating whether it is rented or not. Apartments should start off unrented.
Add a private static final variable building in Apartment, which is a 2-dimensional array of Apartments.
☑ Add a constant FLOORS, which is 10, and another APTSPERFLOOR, which is the number of types of apartment in the enum. We only need one of each of these for the whole class, and they will be availble to other classes
☑ In the static block, set up building to be FLOORS x APTSPERFLOOR apartments in size, and fill in all the apartments so that there is one apartment with each type on each floor, in the same order as the types were defined. To do this, use the .values() method of the enumeration so you can get the appropriate type of apartment for the position on the floor without having to use a conditional.
☑ Add a static method rent(floor, num) which, if the apartment at that position in building is currently free, sets it as rented and (using the values from AptType) prints a message like
You are renting a 1120 square foot 2 Bedroom apartment for $1030 / month. Welcome to our building.
If the apartment is already rented, print an apologetic message saying the apartment is taken.
If the number requested is not valid in building (e.g., 15th floor) print an error message.
Part C
☑ In a class with a main,check that you can choose an apartment of each type from the building and rent it, but if you try to rent it again, you get an apology. Do this without ever creating a new Apartment in main.
Java code to implement the above functionality is :
AptType.java
public enum AptType {
STUDIO("Studio",520,480),ONE_BEDROOM("1 Bedroom",810,720),TWO_BEDROOM("2 Bedroom",1120,1030),LUXURY("Luxury",1720,1780);
public final String description;
public final int size;
public final int rent;
AptType(String description,int size,int rent) {
this.description=description;
this.size=size;
this.rent=rent;
}
}
Apartment.java
public class Apartment {
public AptType apartmentType;
boolean isRented=false;
private static final Apartment building[][];
public static final int FLOORS = 10;
public static final int APTSPERFLOOR = AptType.values().length;
static{
building= new Apartment[FLOORS][APTSPERFLOOR];
final AptType aptType[] = AptType.values();
for(int i=0;i<FLOORS;i++){
for(int j=0;j<APTSPERFLOOR;j++){
building[i][j]= new Apartment(); //creating new Apartment
building[i][j].apartmentType=aptType[j]; //setting the apartment type
}
}
}
static void rent(int floor, int num){
if(floor>=0 && floor<=9 && num>=0 && num <=3) { //checking if aprtment number is valid or not
Apartment a = building[floor][num];
if(a.isRented==false){ //if apartment is available to rent
building[floor][num].isRented=true; //aprtment is rented
System.out.println("You are renting a "+a.apartmentType.size+" square foot "+a.apartmentType.description+" for $"+a.apartmentType.rent+"/ month. Welcome to our building");
}else{
System.out.println("Sorry,that apartement is already taken");
}
}else{
System.out.println("You have selected invalid apartment");
}
}
public static void main(String[] args){
Apartment.rent(9, 3);
Apartment.rent(4, 1);
Apartment.rent(2, 2);
Apartment.rent(6, 4);
Apartment.rent(4, 1);
Apartment.rent(12, 3);
}
}
The output screenshot of the above program is :
If you have any queries regarding this answer, please reach out through the comment section.