In: Computer Science
I need the output of the code like this in java
First we create a new building
and display the result:
This building has no apartments.
Press enter to continue.........................
Now we add some apartments to the
building
and display the result:
This building has the following apartments:
Unit 1 3 Bedroom
Rent $450 per month
Currently unavailable
Unit 2 2 Bedroom
Rent $400 per month
Currently available
Unit 3 4 Bedroom
Rent $1000 per month
Currently unavailable
Unit 4 1 Bedroom
Rent $500 per month
Currently unavailable
Unit 5 5 Bedroom
Rent $10000 per month
Currently available
Press enter to continue.........................
Now we try to add another apartment to the
building
Cannot add another apartment the building is
full.
Now we create a new building and add some
apartments
then we collect the rent from all the occupied
apartments
The total rent collected was: $2500
Finally we use the getUnits method to get a copy of the
3rd unit
from the building and change its rent to $10then we
display
the building again to show that the apartment has not
changed.
This building has the following apartments:
Unit 1 3 Bedroom
Rent $450 per month
Currently unavailable
Unit 2 2 Bedroom
Rent $400 per month
Currently available
Unit 3 4 Bedroom
Rent $1000 per month
Currently unavailable
Unit 4 1 Bedroom
Rent $500 per month
Currently unavailable
Unit 5 5 Bedroom
Rent $10000 per month
Currently available
Unit 6 3 Bedroom
Rent $550 per month
Currently unavailable
Press enter to end the program...................
and now I have to make a new class Building.java to show output like this given above and given below is the main java file
package lab06; import properties.Building; import properties.Apartment; import util1228.Utilities; //Tests the class Building public class Lab06Driver { public static void main(String[] args) { //testing constructor and toString System.out.println("\nFirst we create a new building"); Building b=new Building(5); System.out.println("and display the result:\n"); System.out.printf("%s\n\n", b); Utilities.pause("Press enter to continue........................."); //testing addUnit System.out.println("\nNow we add some apartments to the building"); b.addUnit(new Apartment(1,3,450,true)); b.addUnit(new Apartment(2,2,400,false)); b.addUnit(new Apartment(3,4,1000,true)); b.addUnit(new Apartment(4,1,500,true)); b.addUnit(new Apartment(5,5,10000,false)); System.out.println("and display the result:\n"); System.out.printf("%s\n", b); Utilities.pause("Press enter to continue........................."); System.out.println("\nNow we try to add another " + "apartment to the building"); b.addUnit(new Apartment(6,3,550,true)); //testing collectRent System.out.println("\nNow we create a new building " + "and add some apartments"); b=new Building(10); b.addUnit(new Apartment(1,3,450,true)); b.addUnit(new Apartment(2,2,400,false)); b.addUnit(new Apartment(3,4,1000,true)); b.addUnit(new Apartment(4,1,500,true)); b.addUnit(new Apartment(5,5,10000,false)); b.addUnit(new Apartment(6,3,550,true)); System.out.println("then we collect the rent from " + "all the occupied apartments"); System.out.println("The total rent collected was: $"+b.collectRent()); //testing getUnits System.out.print("\nFinally we use the getUnits method to get a copy " + "of the 3rd unit \nfrom the building and change its rent to " + "$10"); b.getUnits()[2].setRent(10); System.out.println("then we display \nthe building again to show that " + "the apartment has not changed.\n"); System.out.printf("%s\n", b); Utilities.pause("Press enter to end the program..................."); } }
Lab06Driver .java
----------------------------
package com.faisal.com.laraib;
public class Lab06Driver {
public static void main(String[] args) {
//testing constructor and toString
System.out.println("\nFirst we create a new building");
Building b=new Building(5);
System.out.println("and display the result:\n");
// System.out.printf("%s\n\n", b.displayApartment());
b.displayApartment();
Utilities.pause("Press enter to
continue.........................");
//testing addUnit
System.out.println("\nNow we add some apartments to the
building");
b.addUnit(new Apartment(1,3,450,true));
b.addUnit(new Apartment(2,2,400,false));
b.addUnit(new Apartment(3,4,1000,true));
b.addUnit(new Apartment(4,1,500,true));
b.addUnit(new Apartment(5,5,10000,false));
System.out.println("and display the result:\n");
// System.out.printf("%s\n", b);
b.displayApartment();
Utilities.pause("Press enter to
continue.........................");
System.out.println("\nNow we try to add another "
+ "apartment to the building");
b.addUnit(new Apartment(6,3,550,true));
//testing collectRent
System.out.println("\nNow we create a new building "
+ "and add some apartments");
b=new Building(10);
b.addUnit(new Apartment(1,3,450,true));
b.addUnit(new Apartment(2,2,400,false));
b.addUnit(new Apartment(3,4,1000,true));
b.addUnit(new Apartment(4,1,500,true));
b.addUnit(new Apartment(5,5,10000,false));
b.addUnit(new Apartment(6,3,550,true));
System.out.println("then we collect the rent from "
+ "all the occupied apartments");
System.out.println("The total rent collected was:
$"+b.collectRent());
// b.displayApartment();
//testing getUnits
System.out.print("\nFinally we use the getUnits method to get a
copy "
+ "of the 3rd unit \nfrom the building and change its rent to
"
+ "$10");
b.getUnits(3,10);
System.out.println("then we display \nthe building again to show
that "
+ "the apartment has not changed.\n");
// System.out.printf("%s\n", b);
b.displayApartment();
Utilities.pause("Press enter to end the
program...................");
}
}
-------------------------------
Building.java
-----------------------------
package com.faisal.com.laraib;
import java.util.ArrayList;
public class Building {
ArrayList<Apartment> apartmentsList=null;
int numberApartment;
public Building(int n) {
numberApartment=n;
apartmentsList=new
ArrayList<Apartment>(n);
}
public void addUnit(Apartment apartment) {
if(numberApartment>=apartmentsList.size()+1) {
apartmentsList.add(apartment);
}
else {
System.out.println("Cannot add another apartment the building is
full.");
}
}
public int collectRent() {
int totalRent=0;
for(Apartment
apartment:apartmentsList) {
if(apartment.isAvailabel()) {
totalRent=totalRent+apartment.getRent();
}
}
return totalRent;
}
public void getUnits(int unit, int newRent) {
for(Apartment
apartment:apartmentsList) {
if(unit==apartment.getFloorNumber()) {
apartment.setRent(newRent);
}
}
}
public void displayApartment() {
if(apartmentsList.size()==0)
{
System.out.println("This building has no apartments.");
}else {
for(Apartment
apartment:apartmentsList) {
System.out.println("Unit
"+apartment.getFloorNumber()+" "+apartment.getFlatNumber()+"
Bedroom");
System.out.println("Rent
$"+apartment.getRent()+" per month");
if(apartment.isAvailabel()) {
System.out.println("Currently
unavailable");
}
else {
System.out.println("Currently
available");
}
System.out.println();
}
}
}
public int getNumberApartment() {
return numberApartment;
}
public void setNumberApartment(int numberApartment)
{
this.numberApartment =
numberApartment;
}
}
------------------------
Apartment.java
package com.faisal.com.laraib;
public class Apartment {
int floorNumber;
int flatNumber;
int rent;
boolean availabel;
public Apartment(int floorNumber, int flatNumber, int
rent, boolean availabel) {
super();
this.floorNumber =
floorNumber;
this.flatNumber = flatNumber;
this.rent = rent;
this.availabel = availabel;
}
public int getFloorNumber() {
return floorNumber;
}
public void setFloorNumber(int floorNumber) {
this.floorNumber =
floorNumber;
}
public int getFlatNumber() {
return flatNumber;
}
public void setFlatNumber(int flatNumber) {
this.flatNumber = flatNumber;
}
public int getRent() {
return rent;
}
public void setRent(int rent) {
this.rent = rent;
}
public boolean isAvailabel() {
return availabel;
}
public void setAvailabel(boolean availabel) {
this.availabel = availabel;
}
}
---------------------------
Utilities.java
-------------------------
package com.faisal.com.laraib;
public class Utilities {
public static void pause(String msg) {
System.out.println(msg);
}
}
------------------------------
SAMPLE OUTPUT:
First we create a new building
and display the result:
This building has no apartments.
Press enter to continue.........................
Now we add some apartments to the building
and display the result:
Unit 1 3 Bedroom
Rent $450 per month
Currently unavailable
Unit 2 2 Bedroom
Rent $400 per month
Currently available
Unit 3 4 Bedroom
Rent $1000 per month
Currently unavailable
Unit 4 1 Bedroom
Rent $500 per month
Currently unavailable
Unit 5 5 Bedroom
Rent $10000 per month
Currently available
Press enter to continue.........................
Now we try to add another apartment to the building
Cannot add another apartment the building is full.
Now we create a new building and add some apartments
then we collect the rent from all the occupied apartments
The total rent collected was: $2500
Finally we use the getUnits method to get a copy of the 3rd
unit
from the building and change its rent to $10then we display
the building again to show that the apartment has not changed.
Unit 1 3 Bedroom
Rent $450 per month
Currently unavailable
Unit 2 2 Bedroom
Rent $400 per month
Currently available
Unit 3 4 Bedroom
Rent $10 per month
Currently unavailable
Unit 4 1 Bedroom
Rent $500 per month
Currently unavailable
Unit 5 5 Bedroom
Rent $10000 per month
Currently available
Unit 6 3 Bedroom
Rent $550 per month
Currently unavailable
Press enter to end the program...................
-----------------------------------------------------------------
SUMMARY:
I have provided the solution as per your requirement, i hope
you're satisfied with the way i have approached. please dont
hesitate to give me a Like if you like it, i appreciate your like.
If you have any queries you can shoot them any time in comments
section. I will be glad to help you.
Thank you :)