In Java. Write a GUI contact list application. The program should allow you to i
ID: 3763248 • Letter: I
Question
In Java.
Write a GUI contact list application. The program should allow you to input names and phone numbers. You should also be able to input a name and have it display the previously entered phone number. The GUI should look something like the following, although you are welcome to format it in any way that works. This should be a GUI application with a JFrame. The program should contain two arrays of Strings. One array will contain a list of names and the other will contain the phone numbers. Even though we refer to a phone "number", it may be easiest to store the number as a string. We will assume that no more than 100 names will be in the phonebook. These two arrays should be class instance variables. You will also want an integer class instance variable (initialized to zero) that will contain the number of names that have been entered into the phonebook. When the user presses the Add button, the program should get the text in the two input files and put the strings into the next location in the two arrays. The count should be incremented. When the user presses the Find button, the program should get the text from the name input field and search for that name in the name array. If the name is found in the name array, the program should set the text of the phone number input field to the string in the phone number array with the same index. If the input name string is not found m the name array, the program should display an appropriate error message in the phone number input field. Implement the program as described above with two arrays, one for the names and one for the phone numbers. Instead of two arrays of Strings, create another class called Contact with two class instance String variables to hold the name and phone number. You can make the two data fields public for easy access, hi the GUI program use an array of Contact objects instead of two arrays of Strings. When the user presses the Add button, create a new object of the Contact class and put the new object in the array of Contact objects. When the Find button is pressed, search the array for a Contact object whose name field matches the name entered.Explanation / Answer
GUI Program
Contact.Java
import java.util.*;
import java.io.*;
public class Contact implements Serializable
{
public static final long serialVersionUID = 42L;
public String name, number;
Contact()
{
name = "No name";
number = "No number";
}
Contact (String theName, String theNumber)
{
this.name = theName;
this.number = theNumber;
}
public void setName(String aName)
{
this.name = aName;
}
public void setNumber(String aNumber)
{
this.number =aNumber;
}
public String getName()
{
return name;
}
public String getNumber()
{
return number;
}
public String toString()
{
return name + ": " + number;
}
public boolean equals(Contact other)
{
if (name.equals(other.getName()) && number.equals(other.getNumber()))
{
return(true);
}
else
{
return(false);
}
}
}
Contact Collection Class
import java.io.*;
import java.util.*;
class Contact_data
{
public static ArrayList<Contact> contactList;
public static void write()
{
try
{
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("contactList.dat"));
for (int i = 0; i < contactList.size(); i++)
{
out.writeObject(contactList.get(i));
}
out.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
public static void read()
{
contactList = new ArrayList<Contact>();
try
{
ObjectInputStream in = new ObjectInputStream(new FileInputStream("contactList.dat"));
Contact temp;
while (in.available()!=0)
{
temp = (Contact)in.readObject();
contactList.add(temp);
}
in.close();
}
catch(FileNotFoundException e)
{
e.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}
catch(ClassNotFoundException e)
{
e.printStackTrace();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.