Write a java class encapsulating the concept of a Home, assuming that it has the
ID: 3573576 • Letter: W
Question
Write a java class encapsulating the concept of a Home, assuming that it has the following attributes: the number of rooms, the square footage, and whether it has a basement. Write a client program that creates five home objects, writes them to a file as objects, then reads them from a file as objects, outputs a description of the object using the toString() method (which the Home class should override), and outputs the number of Home objects. When reading the objects, you should assume that you do not know the number of objects in the file.
Explanation / Answer
Home.java
public class Home {
private int numberOfRooms;
private double areaInSquarefeet;
private boolean hasBasement;
public Home(int numberOfRooms, double areaInSquarefeet, boolean hasBasement) {
this.numberOfRooms = numberOfRooms;
this.areaInSquarefeet = areaInSquarefeet;
this.hasBasement = hasBasement;
}
public int getNumberOfRooms() {
return numberOfRooms;
}
public void setNumberOfRooms(int numberOfRooms) {
this.numberOfRooms = numberOfRooms;
}
public double getAreaInSquarefeet() {
return areaInSquarefeet;
}
public void setAreaInSquarefeet(double areaInSquarefeet) {
this.areaInSquarefeet = areaInSquarefeet;
}
public boolean isHasBasement() {
return hasBasement;
}
public void setHasBasement(boolean hasBasement) {
this.hasBasement = hasBasement;
}
@Override
public String toString()
{
String description = "Number of Rooms : "+ numberOfRooms + " SquareFootage : "+areaInSquarefeet + " HasBasement :"+hasBasement;
return description;
}
}
ClientHome.java
// Uses Gson external library to convert object to json string and vice-versa
// Download and gson-2.2.jar and add to project build path
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.List;
import java.util.ArrayList;
import java.util.Scanner;
import com.google.gson.Gson;
public class ClientHome {
public static void main(String[] args)
{
Home home1 = new Home(2,1500,true);
Home home2 = new Home(3,2000,true);
Home home3 = new Home(5,3000,false);
Home home4 = new Home(1,4500,true);
Home home5 = new Home(6,5500,false);
List<Home> homes = new ArrayList<Home>();
homes.add(home1);
homes.add(home2);
homes.add(home3);
homes.add(home4);
homes.add(home5);
HomeObjectsList listofHomes = new HomeObjectsList();
listofHomes.setHomes(homes);
Gson gson = new Gson();
String jsonString = gson.toJson(listofHomes);
final String fileName = "outputfile.txt";
writeToFile(fileName,jsonString);
String jsonStringRead = readFile(fileName);
HomeObjectsList listOfHomesRead;
listOfHomesRead = gson.fromJson(jsonStringRead, HomeObjectsList.class);
System.out.println("Number of Home Objects = "+listOfHomesRead.getHomes().size());
for(Home home : listOfHomesRead.getHomes())
{
System.out.println(home.toString());
}
}
private static void writeToFile(String fileName,String jsonString)
{
boolean fileOpened = true;
try {
PrintWriter toFile = new PrintWriter(fileName);
if(fileOpened){
toFile.println(jsonString);
toFile.close();
}
}
catch (FileNotFoundException e) {
fileOpened = false;
}
}
private static String readFile(String fileName)
{
StringBuilder builder = new StringBuilder();
try {
Scanner fileData = new Scanner(new File(fileName));
while(fileData.hasNextLine()){
String line = fileData.nextLine();
line = line.trim();
builder.append(line);
if("".equals(line)){
continue;
} // end if
} // end while
fileData.close(); // close file
} // end try
catch (FileNotFoundException e) {
// Error message
} // end catch
return builder.toString();
}
}
HomeObjectsList.java
import java.util.List;
public class HomeObjectsList
{
List<Home> homes;
public List<Home> getHomes() {
return homes;
}
public void setHomes(List<Home> homes) {
this.homes = homes;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.