These are my object classes:
House:
class House
{
String address;
int sellPrice;
int askPrice;
boolean sold;
public House(String newAddress, int newAsk)
{
address = newAddress;
askPrice = newAsk;
}
public String getAddress()
{
return address;
}
public int getPrice()
{
return askPrice;
}
}
Agent:
class Agent
{
String name;
int totSales;
int totPurch;
public Agent(String newName)
{
name = newName;
}
}
and the main program (it models a real estate co. that tracks sales of agents and allows houses to be bought below asking price):
import java.io.*;
import java.util.*;
public class Emacs
{
BufferedReader kb = new BufferedReader(new InputStreamReader(System.in));
House house[] = new House[1000];
Agent agent[] = new Agent[1000]; //you may add more variables
int ctr = 0;
public Emacs() throws Exception
{
readInFile();
menu();//add your code for initializing all instance variables
}
//this method should
//1) prompt the user for the address and price of the house
//2) create a new House with the address and price listed
//3) place the house into the array of houses
public void addHouse() throws Exception
{
System.out.println("What is the address and price of the house you would like to add, seperate them with a ':'");
String bits[] = kb.readLine().split(",");
if(bits.length != 2)
{
System.out.println("invalid data entered");
}
else
{
int newAdd = Integer.parseInt(bits[1].trim());
house[ctr] = new House(bits[0].trim(),newAdd);
saveToFile(house[ctr]);
ctr++;
System.out.println("House added as:" + bits[0] + "," + bits[1]);
}
}
public void saveToFile(House house)
{
System.out.println("Enter the file name to save to");
try
{
String fyle = kb.readLine();
FileWriter fw = new FileWriter(fyle,true);
fw.write("House : "+house.address+":"+house.askPrice+"\r\n");
fw.close();
}
catch(Exception e){errorHandler();}
}
//this method should
//1) ask the user which houses to display and in what order
//2) sort the houses according to the criteria entered
//3) display the houses according to the criteria entered
public void displayHousesMenu() throws Exception
{
}
//this method does:
//1) prompts the user on the order in which to display the agents
//2) sorts the Agents by the choice given by choice
//3) display the list of Agents
public void displayAgentMenu() throws Exception
{
for (int i=0; i<ctr; i++)
{
System.out.println(house[i].toString());
}
menu();
}
//this method should
//1) prompt the user for the name of the file that contains the list of houses and agents
//2) read in the list of houses and agents
//3) if line starts with house create a House object and add to list of houses
//4) if line starts with agent create an Agent object and add to list of agents
public void readInFile() throws Exception
{
System.out.println("Enter file name you wish to use");
try
{
String fyle = kb.readLine();
File usersFile = new File(fyle);
if(usersFile.exists())
{
BufferedReader fr = new BufferedReader(new FileReader(fyle));
String line = "";
String bits[];
while ((line = fr.readLine()) != null)
{
bits = line.split(":");
if(bits[0].equals("House"))
{
int newAdd = Integer.parseInt(bits[2]);
house[ctr++] = new House(bits[1],newAdd);
}
if(bits[0].equals("Agent"))
{
agent[ctr++] = new Agent(bits[1]);
}
}
fr.close();
}
}
catch(Exception e){errorHandler();}
}
//this method should:
//1) prompt the user for the name of the file containing the list of houses sold
//2) for each house sold, mark the corresponding house object sold
// and add it to the list of sold houses in the selling Agent and purchased houses in the buying Agent
public void updateSoldHouses() throws Exception
{
System.out.print("Enter the filename in which the house sale information is stored: ");
// read the filename from the keyboard
String filename = kb.readLine().trim();
// initialize a BufferedReader to read from the file
BufferedReader file = new BufferedReader(new FileReader(filename));
String lineRead;
//read a line from the file
lineRead = file.readLine();
//while text can be read from the file, keep reading
}
//this method:
//displays the list of choices to the user and prompts for input
//returns the choice the user made
public int menu() throws Exception
{
System.out.println("1. Add new house for sale");
System.out.println("2. Display houses");
System.out.println("3. Display agents");
System.out.println("4. Update sold houses");
System.out.println("5. Exit");
System.out.println("Please select one of the options:>");
int choice = Integer.parseInt(kb.readLine().trim());
return choice;
}
//there could be some useful methods such as getAgent and getHouse
public Agent getAgent(String name) throws Exception
{
}
public House getHouse(String address)
{
}
//There will be other methods needed to do sorting etc.
//this method just shuts down the program if ever an invalid argument is typed in
public void errorHandler()
{
System.out.println("I/O error - terminating.");
goodBye();
}
//this thanks the user for using the program then closes it
public void goodBye()
{
System.out.println("\nThanks for using the Phone Book. \nGoodbye.");
System.exit(0);
}
public static void main(String[] args) throws Exception
{ Emacs e = new Emacs();
e.menu();
}
}
i just can't get any of my other methods working, sorry if this seems like an ambiguous question.