In: Computer Science
Create a Java application for Real Estates including Apartment and Homes. First, have a look at the class diagram that you will implement, and then read the explanation of these classes below.
|
|
|||||
|
Class RealEstate
There are two types of real estates: Apartment and Home.
Class Apartment
Class Home
Test Class |
|
Information about objects are presented in what follows.
Home Objects
ID |
hasGarage |
H1234 |
No |
H1235 |
Yes |
Information about objects are presented in what follows.
Apartment Objects
ID |
Surface |
A1234 |
125 |
A1235 |
205 |
A1236 |
200 |
Recall that: SortedSet <Double> treeSurface = new TreeSet<Double> ();
java code
import java.util.*;
interface RealEstate
{
public void setId(String s);
}
class Apartment implements RealEstate
{
String id;
double surface;
public Apartment(String id, double surface)
{
this.id = id;
this.surface = surface;
}
public String getId()
{
return this.id;
}
public void setId(String s)
{
this.id = s;
}
public double getSurface()
{
return this.surface;
}
public void setSurface(double s)
{
this.surface = s;
}
public String toString()
{
return getId()+" "+getSurface()+"\n";
}
}
class Home implements RealEstate
{
String id;
boolean hasGarage;
public Home(String id, boolean hasGarage)
{
this.id = id;
this.hasGarage= hasGarage;
}
public String getId()
{
return this.id;
}
public void setId(String s)
{
this.id = s;
}
public boolean getHasGarage()
{
return this.hasGarage;
}
public void setHasGarage(boolean hasGarage)
{
this.hasGarage = hasGarage;
}
public String toString()
{
if (getHasGarage()==true)
return getId() +" Yes\n";
else
return getId() + " No\n";
}
}
public class Main
{
public static void main(String[] args) {
//Test part
ArrayList<Home> homes = new
ArrayList<Home>();
homes.add(new
Home("H1234",false));
homes.add(new
Home("H1235",true));
System.out.println("Id
hasGarage");
System.out.println(homes.toString());
for (int i = 0; i <
homes.size(); i++)
{
if
(homes.get(i).getHasGarage()==false)
homes.remove(i);
}
System.out.println("Id
hasGarage");
System.out.println(homes.toString());
LinkedList<Apartment> apart =
new LinkedList<Apartment>();
apart.add(new
Apartment("A1234",125));
apart.add(new
Apartment("A1235",205));
apart.add(new
Apartment("A1236",200));
System.out.println("Id
Surface");
System.out.println(apart.toString());
SortedSet<Double> treeSurface
= new TreeSet<Double>();
for (int i = 0; i <
apart.size(); i++)
{
treeSurface.add(apart.get(i).getSurface());
}
System.out.println(treeSurface.first());
System.out.println(treeSurface.last());
}
}
Output
Id hasGarage
[H1234 No
, H1235 Yes
]
Id hasGarage
[H1235 Yes
]
Id Surface
[A1234 125.0
, A1235 205.0
, A1236 200.0
]
125.0
205.0