Write a program to keep your friends telephone numbers. You should be able to; Add a new name and number (Look up a number given a name) Save the data to an object file when you terminate the programme Restore the saved data when the program is rerun
the part i am confused about is in the brackets above
class CarMain { public static void main (String[] args) throws IOException, ClassNotFoundException { List< Friend> Friends = new ArrayList< Friend>();
Friends.add(new Friend("blah","blah" 1234567));
Friends.add(new Friend("blah","blah" 1234567));
Friends.add(new Friend("blah","blah" 1234567));
Friends.add(new Friend("blah","blah" 1234567));
Friends.add(new Friend("blah","blah" 1234567));
for (int i = 0; i<Friends.size(); i++)
System.out.println(Friends.get(i).toString() );
System.out.println("Friends set up");
ObjectOutputStream out = null;
try {
out = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream("FriendFile.txt")));
out.writeObject(Friends);
}
finally {
out.close();
}
ObjectInputStream in = null;
try {
in = new ObjectInputStream(new
BufferedInputStream(new FileInputStream("FriendFile.txt")));
List<Friend> newFriends = (List<Friend>) in.readObject();
for (int i = 0; i<newFriends.size(); i++)
System.out.println(newFriends.get(i).toString() );
}
finally {
in.close();
}
}//end main }//end class CarMain
//import java.io.*;
class Friend implements Serializable { private String fname, lname; private int number;
public Friend(String fna, String lna, int num) {
lname= lna;
fname = fna;
number=num;
}
public String getFname() {
return fname;
}
public String getLname () {
return lname;
}
public int getNumber() {
return number;
}
public String toString() {
String out = "Last Name: " + fname + "\t First Name: " + lname + "\t Telephone number: "+ number ;
return out;
}
}