Hi guys I need help creating an unordered list of objects.I am supposed to create a file of name and phone number pairs, such that each name is on a separate line and each phone number is on a separate line. Then I am to edit the Phonetest and phone file to
-im supposed to create phone list and print
-then print the Ordered List of phone numbers.
Here is the class Phone:
public class Phone implements Comparable<Phone> {
private String name;
private String phone;
public Phone(){
name = "";
phone = "";
}
public Phone(String name, String phone){
this.name = name;
this.phone = phone;
}
public String getName(){
return name;
}
public String getPhone(){
return phone;
}
public void setName(String name){
this.name = name;
}
public void setPhone(String phone){
this.phone = phone;
}
public String toString(){
return (name + " " + phone);
}
public boolean equals(Phone other)
{
return (name == other.name)&&(phone == other.phone);
}
@Override
public int compareTo(Phone other) {
int compare = other.getPhone().compareTo(name);
if (compare != 0)
return compare;
else
{
if (other.getName().compareTo(name) < 0)
return - 1;
if (other.getName().compareTo(name) > 0)
return 1;
}
return 0;
}
}
Here is the class Phonetest:
public class PhoneTest {
public static void main(String[] args) throws Exception {
// get the filename from the user
BufferedReader keyboard = new BufferedReader
(new InputStreamReader(System.in),1);
System.out.println("Enter name of the input file: ");
String fileName= keyboard.readLine();
// create object that controls file reading and opens the file
InStringFile reader = new InStringFile(fileName);
System.out.println("\nReading from file " + fileName + "\n");
// your code to create (empty) ordered list here
ArrayOrderedList<Phone> list = new ArrayOrderedList<Phone>();
// read data from file two lines at a time (name and phone number)
String name, phone;
do {
name = (reader.read());
phone = (reader.read());
// your code to add the entry to your ordered list here
Phone phone2 = new Phone(name,phone);
list.add(phone2);
} while (!reader.endOfFile());
System.out.println("Here is my phone book:");
// your code to print the ordered list here
System.out.println(list);
// close file
reader.close();
System.out.println("\nFile " + fileName + " is closed.");
}
}
Here us my File:
So how can I change the print portion so I am getting the list in alphabitically order top down?
So I my file looks like this
Claire hfjg
555-222-1111
Bob abc
222-900-1818
Don cbcd
122-564-9653
I get this when I input my code:
Don cbcd 122-564-9653
Claire hfjg 555-222-1111
Bob abc 222-900-1818
How can I get the list in alhpabetical order so it looks more like this:
Bob abc 222-900-1818
Claire hfjg 555-222-1111
Don cbcd 122-564-9653