File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes sorting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "sorting" Watch "sorting" New topic
Author

sorting

Dallas Costain
Greenhorn

Joined: Nov 06, 2003
Posts: 12
here is my program so far:
import java.io.*;
import java.util.*;

public class Emacs
{
BufferedReader kb;
Vector houses;
Vector agents;
int[] Price_list;
//you may add more variables
public Emacs() throws Exception
{
kb = new BufferedReader(new InputStreamReader(System.in));
houses = new Vector();
agents = new Vector();
Price_list = new int[50];
//add your code for initializing all instance variables
}

// this method should
//1) call the readInUsers() method
//2) provide the menu loop
//3) call the needed methods depending on user choice
public void start() throws Exception
{
//read in list of houses and agents
readInHousesAndAgents();
//display menu of choices
int id = menu();
while (id != 5)
{
if ((id < 5) && (id >0))
{
if (id == 1)
{
addHouseMenu();
}
else if (id == 2)
{
DisplayHouses();
}
else if (id == 3)
{
displayAgentMenu();
}
else if (id == 4)
{
updateSoldHouses();
}
}
else
{
System.out.println("Invalid choice");
}
id = menu();
}
System.out.println("Thanks for using the system, goodbye");
System.exit(0);
}

//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 addHouseMenu() throws Exception
{
System.out.print("Enter the address of the house: ");
String address = kb.readLine();
System.out.print("Enter the asking price of the house: ");
String pricex = kb.readLine();
int price = Integer.parseInt(pricex);
House temp = new House(address, price);
houses.add(temp);
System.out.print("House added");
start();
}
//The user will be asked in what order to display the Houses (by price or address) and whether s/he wants to see all the houses, just the sold ones, or just the unsold ones. First you must sort the houses according to the given criteria and the display the address of the house, whether or not it has been sold, the asking price if it hasn't been sold and the selling price if it has been sold.
//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
{
System.out.println("How would you like to display the houses? ");
System.out.println("1. All houses ordered by asking price");
System.out.println("2. All houses ordered by address");
System.out.println("3. Sold houses ordered by selling price");
System.out.println("4. Sold houses ordered by address");
System.out.println("5. Unsold houses ordered by asking price");
System.out.println("6. Unsold houses ordered by address");
System.out.print("Please select one of the options: ");
int pick = Integer.parseInt(kb.readLine().trim());
return pick;
DisplayHouses();

}
//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
{
System.out.println("How would you like to display the agents?");
System.out.println("1. Ordered by name");
System.out.println("2. Ordered by value of sales");
System.out.println("3. Ordered by value of purchases");
System.out.print("Please select one of the options: ");
int selection = Integer.parseInt(kb.readLine().trim());
return selection;
}

//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 readInHousesAndAgents() throws Exception
{
System.out.print("Enter the filename in which the houses and agents are 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=new String();
//read a line from the file
lineRead=file.readLine();
//while text can be read from the file, keep reading
while (lineRead!=null)
{
//add your code
StringTokenizer st = new StringTokenizer(lineRead,":");
String type = st.nextToken();
if(type.equals("Agent")
{
String name = st.nextToken();
Agent temp = new Agent(name);
agents.add(temp);
}
else // case house
{
String address = st.nextToken();
String aP = st.nextToken();
int askingPrice = Integer.parseInt(aP);
House temp = new House(address, askingPrice);
houses.add(temp);

}

}
file.close();
}

//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=new String();
//read a line from the file
lineRead=file.readLine();
//while text can be read from the file, keep reading
while (lineRead!=null)
{
//add your code here

}
file.close();
}

//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 DisplayHouses()
{
int id = displayHousesMenu();
while (id != 6)
{
if ((id < 6) && (id >0))
{
if (id == 1)
{
price_list();
}
else if (id == 2)
{
selectionSort2();*******
}
else if (id == 3)
{
displayAgentMenu();
}
else if (id == 4)
{
updateSoldHouses();
}
}
else
{
System.out.println("Invalid choice");
}
id = menu();
}

public void Sort_int(int[] Price_list)
{
int minPos;
for (int index = 0; index < Price_list.length-1; index++)
{
minPos = index;
for (int look = index+1; look < Price_list.length; look++)
{
if (Price_list[look] < Price_list[minPos])
minPos = look;
}
if (minPos > index)
swap(Price_list, minPos, index);
}
}
public void swap(int[] Price_list, int pos1, int pos2)
{
int temp = numbers[pos1];
Price_list[pos1] = Price_list[pos2];
Price_list[pos2] = temp;
}

}
public void Sort_string(String[] askingPrice)
{
}
public void price_list()
{
int index = 0;
for (int i=0; i< house.size(); i++)
{
House x=(House)house.elementAt(i);
int price = x.getaskingPrice();
Price_list[index] = price;
index++;
}
Sort_int(Price_list);
for (int j = 0; j< index; j++)
{
for (int z = 0; z < house.size(); z++)
{
House y =(House)house.elementAt(z);
int price = y.getaskingPrice();
if (Price_list[j]==price)
{
System.out.println(y.toString());
}
}
}
}
public static void selectionSort1()
{
}
public static void selectionSort2()
{
int temp;
for (int scan = index+1; scan < numbers.length-1; index++)
{
address = index;
for (int scan = index+1; scan < numbers.length; scan++)
if (houses.elementAt(scan) < houses.elementAt(address))
minPos = look;
// Swap the values
temp = houses.elementAt(addy);
houses.elementAt(addy) = houses.elementAt(index);
houses.elementAt(index) = temp;
}
}

public Agent getAgent(String name) throws Exception
{
}
public House getHouse(String address)
{
}
//There will be other methods needed to do sorting etc.

public static void main(String[] args) throws Exception
{
Emacs e = new Emacs();
e.start();
}
}


I think i got the sorting of my prices down, but i have no idea how i can sort my addresses. my list looks like this:
House:123 Elm Street:125000
House:24 Bear Crescent:140000
Agent:John Smith
Agent :Arthur Dent
Agent:Ford Prefect
House:666 Beelzebob Road:166600
House:42 Adams Drive: 242000
House:555 11th Street: 180000
House: 898 Brown Cres:240000
House:23 Pink Road:129900
House:15 Charles Avenue:127000
House :251 Semaphore Boulevard:130000
Agent:Mary Lamb
Agent:Jane Muffet
Agent eter Piper
House:123 Moose Road:150000
Agent:Monica Gellar
House:225 Fox Avenue: 360000
House:789 Deer Lane:188000

any help?
chi Lin
Ranch Hand

Joined: Aug 24, 2001
Posts: 348
Dallas,
I think you can use the Collections.sort(List,Comparator) from java.util
package for sorting.
Collections.sort(houses, new HouseAddComp());
with a comprator defined to sort the address
example for the comparator would like

HTH


not so smart guy still curious to learn new stuff every now and then
Dallas Costain
Greenhorn

Joined: Nov 06, 2003
Posts: 12
Thank you, i'll give it a try.
if there's anyone who is willing to help me with this i could send you the assignment.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: sorting