In: Computer Science
Model the following in JAVA PROGRAMMING :
In a software engineering project we are developing software for a Real estate agency. The Real Estate Agency has these types of properties: Shops, Apartments and Villas. All properties have: Property Owner, Address, Property Purpose (Sale/Rent), Area (in m^2) and price.
Model these in an inheritance hierarchy considering that:
1. Shops have these fields in addition to properties: Floor (int), Street view (boolean).
2. Apartments have these feilds in addition to properties: Floor, Bedrooms(int), Bathroom (int) and balcony (true/false)
3. Villas have these fields in addition to properties: Count of floors, With/Without parking area (boolean), pool (boolean)
Develop these methods:
1. Two overloaded constructors for all properties, one with just Property Owner and Address and the other with all the fields.
2. toString() method for each class
Write a class to test the program:
1. Create two instances with all the attributes for each type of property by using the appropriate constructors.
2. Put all the created properties in one of data structures of Java that you have learned
3. Calculate and print the total value of all properties
4. Search and print information about the lowest price apartment with 2 Bedrooms.
import java.util.ArrayList;
class Property {
String owner;
String address;
String purpose;
double area;
int price;
};
class Shop extends Property {
int floor;
boolean streetView;
// Constructors
Shop(String own, String adr) {
this.owner = own;
this.address = adr;
}
// Owner, address, pupose, area, cost, floor, streetView
Shop(String own, String adr, String pur, double ar, int cost, int fl, boolean sv) {
this.owner = own;
this.address = adr;
this.purpose = pur;
this.area = ar;
this.price = cost;
this.floor = fl;
this.streetView = sv;
}
// toString() method
public String toString() {
return owner+" "+address+" "+purpose+" "+area+" "+price+" "+floor+" "+streetView;
}
};
class Apartment extends Property {
int floor;
int bedrooms;
int bathrooms;
boolean balcony;
// Constructors
Apartment(String own, String adr) {
this.owner = own;
this.address = adr;
}
// Owner, address, pupose, area, cost, floor, bedrooms, bathrooms, balcony
Apartment(String own, String adr, String pur, double ar, int cost, int fl, int broom, int bath, boolean bal) {
this.owner = own;
this.address = adr;
this.purpose = pur;
this.area = ar;
this.price = cost;
this.floor = fl;
this.bedrooms = broom;
this.bathrooms = bath;
this.balcony = bal;
}
// toString() method
public String toString() {
return owner+" "+address+" "+purpose+" "+area+" "+price+" "+floor+" "+bedrooms+" "+bathrooms+" "+balcony;
}
};
class Villa extends Property {
int floorCount;
boolean parking;
boolean pool;
// Constructors
Villa(String own, String adr) {
this.owner = own;
this.address = adr;
}
// Owner, address, pupose, area, cost, floors, parking, pool
Villa(String own, String adr, String pur, double ar, int cost, int fl, boolean park, boolean pl) {
this.owner = own;
this.address = adr;
this.purpose = pur;
this.area = ar;
this.price = cost;
this.floorCount = fl;
this.parking = park;
this.pool = pl;
}
// toString() method
public String toString() {
return owner+" "+address+" "+purpose+" "+area+" "+price+" "+floorCount+" "+parking+" "+pool;
}
}
public class testp {
public static void main(String[] args) {
// Creating two instances of each class
Shop shop1 = new Shop("Newton", "131 France", "Sell", 2500.32, 100000, 1, false);
Shop shop2 = new Shop("You", "Where", "Rent", 3005.2, 25000, 2, true);
Apartment apartment1 = new Apartment("Euler", "54H Madrid", "Rent", 546, 5200, 3, 2, 1, true);
Apartment apartment2 = new Apartment("Aljabr", "548 Alexandria", "Sell", 250, 1200, 1, 1, 1, false);
Villa villa1 = new Villa("C.V. Raman", "259/78 Chennai", "Sell", 450, 450000, 4, true, true);
Villa villa2 = new Villa("Ramanujan", "54A Madurai", "Rent", 500, 320000, 3, true, false);
// Storing these properties in ArrayList data structure
ArrayList<Property> list = new ArrayList<Property>();
list.add(shop1);
list.add(shop2);
list.add(apartment1);
list.add(apartment2);
list.add(villa1);
list.add(villa2);
// total value of all properties
long totalValue = 0;
for(int i = 0; i < list.size(); i++) {
totalValue += list.get(i).price;
}
// printing totalvalue
System.out.println("Total value of all properties: "+totalValue);
// Creating arraylist of type apartments
ArrayList<Apartment> apartments = new ArrayList<Apartment>();
apartments.add(apartment1);
apartments.add(apartment1);
// Searching and printing lowest valued two bedroom apartment
Apartment lowest = null;
for(int i = 0; i < apartments.size(); i++) {
// if it has two bedrooms
if(apartments.get(i).bedrooms == 2) {
// it it's first apartment with two bedrooms
if(lowest == null) {
lowest = apartments.get(i);
}
else {
if(lowest.price > apartments.get(i).price) {
lowest = apartments.get(i);
}
}
}
}
System.out.println("Lowest priced 2 bedroom apartment is: ");
System.out.println(lowest.toString());
}
}
OUTPUT