Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java Programming: \"Create a class Realestate.java with the following fields: lo

ID: 3583913 • Letter: J

Question

Java Programming: "Create a class Realestate.java with the following fields: location, price, and description. Create a class RealestateFinder.java that reads in real estate data from a file. Then provide the user with the following options: Sort per Price, Sort per Location, and Exit. Use ArrayList and Arrays.sort to perform the sorts and display the data to the user."

I've created the Realestate class where the user inputs the information and it writes to a csv file. I've also created the RealestateFinder class that pulls that csv file into an arraylist. Now I'm having trouble trying to figure out how to create the portion that allows the user to sort the arraylist by location or price or exit. I've copied the code for both classes below.

import java.nio.file.*;
import java.io.*;
import static java.nio.file.StandardOpenOption.*;
import java.util.Scanner;

public class Realestate
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
Path file = Paths.get("RealestateList.txt");
String s = "";
String delimiter = ",";
int id;
String location;
double price;
String description;
final int QUIT = 999;
try
{
OutputStream output = new BufferedOutputStream(Files.newOutputStream(file, CREATE));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(output));
System.out.print("Enter address ID >> ");
id = input.nextInt();
while(id != QUIT)
{
System.out.print("Enter address location >> ");
input.nextLine();
location = input.nextLine();
System.out.print("Enter address description >> ");
description = input.nextLine();   
System.out.print("Enter address price >> ");
price = input.nextDouble();
s = id + delimiter + location + delimiter + description + delimiter + price;
writer.write(s, 0, s.length());
writer.newLine();
System.out.print("Enter next address ID or " + QUIT + " to quit >> ");
id = input.nextInt();
}
writer.close();
}
catch(Exception e)
{
System.out.println("Message: " + e);
}
}   
}

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.*;

public class RealestateFinder
{

public static void main(String[] args)
{
BufferedReader propertiesBuffer = null;
  
try
{
String propertiesLine;
propertiesBuffer = new BufferedReader(new FileReader("RealestateList.txt"));

//Read file in java line by line
while ((propertiesLine = propertiesBuffer.readLine()) != null)
{
System.out.println(RealestateFinder2(propertiesLine) + " ");
}
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
if(propertiesBuffer != null) propertiesBuffer.close();
}
catch (IOException propertiesException)
{
propertiesException.printStackTrace();
}
}
}

// Convert to ArrayList
public static ArrayList<String> RealestateFinder2(String propertyCSV)
{
ArrayList<String> propertyResult = new ArrayList<String>();
  
if (propertyCSV != null)
{
String[] splitData = propertyCSV.split("\s*,\s*");
for (int i = 0; i < splitData.length; i++)
{
if (!(splitData[i] == null) || !(splitData[i].length() == 0))
{
propertyResult.add(splitData[i].trim());
}
}
}
return propertyResult;
}   

}

Explanation / Answer

To be able to use ArrayList and Arrays.sort :

1. Create a class with 4 variables for id , Location , Description and Price .

public class CityPrice implements Comparable<CityPrice>

{

static int sortFlag ; // To decide which sort to use location or price

String id ;

String location ;

String description ;

double price ;

CityPrice( int i , String l , String d, double p)

{

id =i ;

location = l ;

description = d;

price = p ;

}

}

2. Create as ArrayList of the above class type

ArrayList<CityPrice > cp = new ArrayList<CityPrice >();

Taking a loop of 4 from the split string , creating new objects of the above type and adding elements into the above ArrayList , cp .

To be able to use Arrays.sort convert the ArrayList into array of type CityPrice

Arrays.sort(cpa) ; // Using Arrays.sort

And print the values as desired .

3. Make a menu similar to the one created to take inputs 1 sort per price , 2 sort per location , 3 for exit

an if else ladder or switch case can check for user input ( this can be put in an infinite while loop )

for exit option 3 juct use a break statement to terminate the infinite loop .

So for sort by price you can set the sortFlag of class CityPrice to 1 and call Arrays.sort();

And for sort by location you can set the sortFlag of class CityPrice to any other int value and call Arrays.sort();

System.out.print("Enter address location >> ");
input.nextLine();   // I think this is not needed
location = input.nextLine();  

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote