Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

GUI Lab Write a Java program which will use a graphic user interface to look up the phone number associated with a specific name and

GUI Lab

Write a Java program which will use a graphic user interface to look up the phone number associated with a specific name and vice versa. This program will be broken up into two parts, the back end and the front end. The back end component of this lab will consist of reading a list of names and numbers from a file and storing the information into Binary Search Trees. The front end component will consist of presenting the user with a graphical user interface to search the data.

To implement this program, create a Java class called Phonebook. The Phonebook class must subclass the JFrameclass. The Phonebookobject should have two instance variables: two TreeMapobjects named names and numbers. (A third instance variable, serialVersionUID may be automatically generated using Eclipse and is optional) When defining and initializing TreeMap objects we must specify a data type for the generic data types Key and Value. For both instance variables the Key and Value data types must be type String.

In the Phonebookclass, define a parameterized constructor which takes a single parameter of type File. The given text file will contain name and number information, where each line of the file contains the information for a single person and their corresponding phone number. The name and number will be separated by a , character. For example, a line of text would look like:

Leslie Cortez,1-515-718-4932

When the parameterized constructor is called, it should populate the names and numbers instance variables with these values. TreeMap objects store a key value pair. To add a new key value pair to a TreeMap object, use the putmethod, which takes two parameters one of type Key and one of type Value. (These are generic types and in for this lab, the data type for both Key and Value should be String) The names instance variable should use the name as the key and the number as the associated value. The numbers instance variable should use the number as the key and the name as the associated value.

The parameterized constructor should also configure the Phonebookframe. Phonebookshould havea height of 70 and a width of 670. The resizableproperty should be set to false and the default close operationshould be set to JFrame.EXIT_ON_CLOSE

The Phonebook frame should contain a single panel with four components: two JTextFields of length 15 and two JButtons with text Get Number and Get Name respectively. The first text field will be used to input and display names, the second text field will be used to input and display numbers. The resulting GUI should look like the following image.

Both buttons should implement Action Listeners. The Get Number button should have an action listener which:

Gets the Stringin the name text field Check if the name existsin the names TreeMap instance variable If it does not exist, set the number text fieldto NO NUMBER If it does exist, set the number text field to the number associated with the parsed name

The Get Name button should also have an action listener. The Get Name buttons action listener would have the same behavior as described above except it would get the String in the number text field, check if that number exists in the numbers TreeMap, and update the name text field. If the number was not found in the numbers TreeMap, then the name text field should be set to NO NAME

NOTE: The only method your program is required to have is a parameterized constructor. Although you can implement all of your programs behavior in that single method, I recommend creating helper methods to keep your program organized and readable.

When you have completed your implementation of both the Phonebook class, use the provided test cases to test your work. When complete, submit your Phonebook class, along with and any other classes you created to Blackboard.

Given Test Case

import static org.junit.Assert.*;

import java.awt.Component;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.PrintWriter;

import java.lang.reflect.Field;

import java.lang.reflect.Modifier;

import java.lang.reflect.ParameterizedType;

import java.util.TreeMap;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JTextField;

import org.junit.FixMethodOrder;

import org.junit.Rule;

import org.junit.Test;

import org.junit.rules.TemporaryFolder;

import org.junit.runners.MethodSorters;

@FixMethodOrder(MethodSorters.NAME_ASCENDING)

public class PhonebookTest {

//Check instance variables

//Check superclass

//Check constructor

//Check frame size and default close operation

//Check Number of items in panel

//Two text fields and two buttons

//Check the size and contents of the buttons

//Check the size and contents of the textFields

//Test Name button no entry

//Test Number button no entry

//Test Name button invalid entry

//Test Number button invalid entry

//Test Name button valid entry

//Test Number button valid entry

//Test that the instance variables are populated when the object is

//constructed and not constantly or something

@Rule

public TemporaryFolder folder = new TemporaryFolder();

@Test

public void PhonebookInstanceVariablesTest() {

@SuppressWarnings("rawtypes")

Class c = Phonebook.class;

try {

c.getDeclaredField("names");

c.getDeclaredField("numbers");

boolean hasUID = false;

for(Field aField : c.getDeclaredFields()) {

if(aField.getType() == long.class) {

if(aField.getName().equals("serialVersionUID")) {

assertEquals("You must only have the instance variables specified in the lab manual. When looking at the number of instance variables we",3,c.getDeclaredFields().length);

hasUID = true;

}

}

}

if(hasUID == false) {

assertEquals("You must only have the instance variables specified in the lab manual. When looking at the number of instance variables we",2,c.getDeclaredFields().length);

}

assertEquals("You must make your instance variables private.",true,Modifier.isPrivate(c.getDeclaredField("names").getModifiers()));

assertEquals("You must make your instance variables private.",true,Modifier.isPrivate(c.getDeclaredField("numbers").getModifiers()));

assertEquals("Your instance variables must NOT be static.",false,Modifier.isStatic(c.getDeclaredField("names").getModifiers()));

assertEquals("Your instance variables must NOT be static.",false,Modifier.isStatic(c.getDeclaredField("numbers").getModifiers()));

assertEquals("You must make your names instance variable of type TreeMap.",(TreeMap.class),c.getDeclaredField("names").getType());

assertEquals("You must make your numbers instance variable of type TreeMap.",(TreeMap.class),c.getDeclaredField("numbers").getType());

assertEquals("You must make the Key generic type in the names instance variable of type String.",(String.class),((ParameterizedType) c.getDeclaredField("names").getGenericType()).getActualTypeArguments()[0]);

assertEquals("You must make the Value generic type in the names instance variable of type String.",(String.class),((ParameterizedType) c.getDeclaredField("names").getGenericType()).getActualTypeArguments()[1]);

assertEquals("You must make the Key generic type in the numbers instance variable of type String.",(String.class),((ParameterizedType) c.getDeclaredField("numbers").getGenericType()).getActualTypeArguments()[0]);

assertEquals("You must make the Value generic type in the numbers instance variable of type String.",(String.class),((ParameterizedType) c.getDeclaredField("numbers").getGenericType()).getActualTypeArguments()[1]);

}

catch(NoSuchFieldException e) {

fail("Could not find the " + e.getLocalizedMessage().toString() + " instance variable");

}

catch(Exception e) {

fail("Something weird went wrong");

}

}

@Test

public void PhonebookSuperClass() throws IOException {

Phonebook myPhonebook = new Phonebook(inputFile());

myPhonebook.setVisible(false);

assertEquals("Testing if the Phonebook Super Class is JFrame, we",true,(myPhonebook instanceof JFrame));

myPhonebook.dispose();

}

@Test

public void PhonebookParameterizedConstructor() throws IOException {

Phonebook testPhonebook = new Phonebook(inputFile());

@SuppressWarnings("rawtypes")

Class c = testPhonebook.getClass();

try {

Field phonebookNames = c.getDeclaredField("names");

phonebookNames.setAccessible(true);

@SuppressWarnings("unchecked")

TreeMap phonebookNamesValue = (TreeMap) phonebookNames.get(testPhonebook);

assertEquals("When checking the size of the names instance variable we",100,phonebookNamesValue.size());

Field phonebookNumbers = c.getDeclaredField("numbers");

phonebookNumbers.setAccessible(true);

@SuppressWarnings("unchecked")

TreeMap phonebookNumbersValue = (TreeMap) phonebookNumbers.get(testPhonebook);

assertEquals("When checking the size of the numbers instance variable we",100,phonebookNumbersValue.size());

assertEquals("When checking the height of the Phonebook frame, we",70.0,testPhonebook.getSize().getHeight(),.0001);

assertEquals("When checking the width of the Phonebook frame, we",670.0,testPhonebook.getSize().getWidth(),.0001);

assertEquals("When checking the default close operation of the Phonebook frame, we expected EXIT_ON_CLOSE",3.0,(testPhonebook.getDefaultCloseOperation()),.0001);

assertEquals("When checking the if the Phonebook frame was resizable, we",false,testPhonebook.isResizable());

} catch (Exception e) {

fail(e.toString());

}

}

@Test

public void PhonebookPanelItems() throws IOException {

Phonebook myPhonebook = new Phonebook(inputFile());

myPhonebook.setVisible(false);

Component[] allComponents = ((JPanel) myPhonebook.getContentPane().getComponents()[0]).getComponents();

assertEquals("You must only have two text fields and two buttons in your JPanel as specified in the lab manual. When looking at the number of components we",4,allComponents.length);

assertEquals("Expected the first component to be a JTextField",JTextField.class,allComponents[0].getClass());

assertEquals("Expected the second component to be a JTextField",JTextField.class,allComponents[1].getClass());

assertEquals("Expected the third component to be a JButton",JButton.class,allComponents[2].getClass());

assertEquals("Expected the fourth component to be a JButton",JButton.class,allComponents[3].getClass());

myPhonebook.dispose();

}

@Test

public void PhonebookJButtons() throws IOException {

Phonebook myPhonebook = new Phonebook(inputFile());

myPhonebook.setVisible(false);

Component[] allComponents = ((JPanel) myPhonebook.getContentPane().getComponents()[0]).getComponents();

assertEquals("When checking the text of the first button, we","Get Number",((JButton) allComponents[2]).getText());

assertEquals("When checking the text of the second button, we","Get Name",((JButton) allComponents[3]).getText());

myPhonebook.dispose();

}

@Test

public void PhonebookJTextFields() throws IOException {

Phonebook myPhonebook = new Phonebook(inputFile());

myPhonebook.setVisible(false);

Component[] allComponents = ((JPanel) myPhonebook.getContentPane().getComponents()[0]).getComponents();

assertEquals("When checking the size of the first text field, we",15,((JTextField) allComponents[0]).getColumns());

assertEquals("When checking the size of the second text field, we",15,((JTextField) allComponents[1]).getColumns());

myPhonebook.dispose();

}

@Test

public void PhonebookNameButtonNoEntry() throws IOException {

Phonebook myPhonebook = new Phonebook(inputFile());

Component[] allComponents = ((JPanel) myPhonebook.getContentPane().getComponents()[0]).getComponents();

JButton nameButton = (JButton) allComponents[3];

JTextField nameTextField = (JTextField) allComponents[0];

nameButton.doClick();

assertEquals("When checking the name text field after pressing the get name button with no number input, we","NO NAME",nameTextField.getText());

}

@Test

public void PhonebookNumberButtonNoEntry() throws IOException {

Phonebook myPhonebook = new Phonebook(inputFile());

Component[] allComponents = ((JPanel) myPhonebook.getContentPane().getComponents()[0]).getComponents();

JButton numberButton = (JButton) allComponents[2];

JTextField numberTextField = (JTextField) allComponents[1];

numberButton.doClick();

assertEquals("When checking the number text field after pressing the get number button with no name input, we","NO NUMBER",numberTextField.getText());

}

@Test

public void PhonebookNameButtonInvalidEntry() throws IOException {

Phonebook myPhonebook = new Phonebook(inputFile());

Component[] allComponents = ((JPanel) myPhonebook.getContentPane().getComponents()[0]).getComponents();

JButton nameButton = (JButton) allComponents[3];

JTextField nameTextField = (JTextField) allComponents[0];

JTextField numberTextField = (JTextField) allComponents[1];

numberTextField.setText("1-804-371-3000");

nameButton.doClick();

assertEquals("When checking the name text field after pressing the get name button with an invalid number input, we","NO NAME",nameTextField.getText());

}

@Test

public void PhonebookNumberButtonInvalidEntry() throws IOException {

Phonebook myPhonebook = new Phonebook(inputFile());

Component[] allComponents = ((JPanel) myPhonebook.getContentPane().getComponents()[0]).getComponents();

JButton numberButton = (JButton) allComponents[2];

JTextField numberTextField = (JTextField) allComponents[1];

JTextField nameTextField = (JTextField) allComponents[0];

nameTextField.setText("Zachary Whitten");

numberButton.doClick();

assertEquals("When checking the number text field after pressing the get number button with invalid name input, we","NO NUMBER",numberTextField.getText());

}

@Test

public void PhonebookNameButtonValidEntry() throws IOException {

Phonebook myPhonebook = new Phonebook(inputFile());

Component[] allComponents = ((JPanel) myPhonebook.getContentPane().getComponents()[0]).getComponents();

JButton nameButton = (JButton) allComponents[3];

JTextField nameTextField = (JTextField) allComponents[0];

JTextField numberTextField = (JTextField) allComponents[1];

numberTextField.setText("1-265-866-0567");

nameButton.doClick();

assertEquals("When checking the name text field after pressing the get name button with a valid number input (1-265-866-0567), we","Jermaine Reilly",nameTextField.getText());

numberTextField.setText("1-915-829-4671");

nameButton.doClick();

assertEquals("When checking the name text field after pressing the get name button with a valid number input (1-915-829-4671), we","Wyatt Chase",nameTextField.getText());

numberTextField.setText("1-705-934-8296");

nameButton.doClick();

assertEquals("When checking the name text field after pressing the get name button with a valid number input (1-705-934-8296), we","Xena Johnson",nameTextField.getText());

}

@Test

public void PhonebookNumberButtonValidEntry() throws IOException {

Phonebook myPhonebook = new Phonebook(inputFile());

Component[] allComponents = ((JPanel) myPhonebook.getContentPane().getComponents()[0]).getComponents();

JButton numberButton = (JButton) allComponents[2];

JTextField numberTextField = (JTextField) allComponents[1];

JTextField nameTextField = (JTextField) allComponents[0];

nameTextField.setText("Zeus Spears");

numberButton.doClick();

assertEquals("When checking the number text field after pressing the get number button with a value name input (Zeus Spears), we","1-793-379-3986",numberTextField.getText());

nameTextField.setText("John James");

numberButton.doClick();

assertEquals("When checking the number text field after pressing the get number button with a value name input (John James), we","1-184-447-8392",numberTextField.getText());

nameTextField.setText("Magee Kerr");

numberButton.doClick();

assertEquals("When checking the number text field after pressing the get number button with a value name input (Magee Kerr), we","1-138-454-1488",numberTextField.getText());

}

private File inputFile() throws IOException {

final String INPUT_FILENAME = "foo.txt";

//Create input and output files and populate input file

File inputFile = folder.newFile( INPUT_FILENAME );

writeFile(inputFile);

return inputFile;

}

private void writeFile(File inputFile) throws FileNotFoundException {

PrintWriter write = new PrintWriter( inputFile );

write.println( "Ishmael Steele,1-168-305-6879" );

write.println( "Magee Kerr,1-138-454-1488" );

write.println( "Darryl Newman,1-625-946-0850" );

write.println( "Tobias Emerson,1-443-352-5388" );

write.println( "Jade Casey,1-963-309-2284" );

write.println( "Jane Gonzalez,1-279-280-2889" );

write.println( "Katelyn Sears,1-100-155-6737" );

write.println( "Tamara David,1-251-819-8926" );

write.println( "Jermaine Reilly,1-265-866-0567" );

write.println( "Bianca Beach,1-959-460-5599" );

write.println( "Stuart Butler,1-589-489-8072" );

write.println( "Uta Douglas,1-524-652-9299" );

write.println( "Flavia Byers,1-728-897-3353" );

write.println( "Ainsley Keith,1-896-348-0629" );

write.println( "Xena Johnson,1-705-934-8296" );

write.println( "Leonard Bernard,1-790-464-1341" );

write.println( "Kennedy Flowers,1-749-326-2404" );

write.println( "Ginger Merrill,1-195-219-1754" );

write.println( "Eaton Walton,1-655-243-7614" );

write.println( "Zachery George,1-163-246-5637" );

write.println( "Austin Walsh,1-479-630-5902" );

write.println( "Raja Cruz,1-879-772-1395" );

write.println( "Baker Baker,1-155-373-0276" );

write.println( "Zeus Spears,1-793-379-3986" );

write.println( "Caesar Cantrell,1-973-178-2162" );

write.println( "Piper Carver,1-773-559-3645" );

write.println( "Imelda Oneill,1-670-562-6755" );

write.println( "Shaeleigh Harrell,1-319-908-7254" );

write.println( "Joshua Parsons,1-763-144-9926" );

write.println( "Channing Buck,1-872-888-9753" );

write.println( "Ursula Vargas,1-403-649-6648" );

write.println( "Ima Christian,1-141-418-1505" );

write.println( "Eric Cunningham,1-176-659-8394" );

write.println( "Alec Cline,1-557-454-6020" );

write.println( "Alice Hernandez,1-672-457-0031" );

write.println( "Georgia Poole,1-609-614-7062" );

write.println( "Hector Glenn,1-509-285-3492" );

write.println( "Selma Moody,1-269-183-7886" );

write.println( "Carl Hanson,1-713-778-4806" );

write.println( "Steven Sullivan,1-558-525-5563" );

write.println( "Kylie Orr,1-462-489-5406" );

write.println( "Leslie Cortez,1-515-718-4932" );

write.println( "Gabriel Burke,1-333-208-6806" );

write.println( "Kennedy Petersen,1-465-848-8401" );

write.println( "Venus Guzman,1-553-867-1578" );

write.println( "Drew Clayton,1-846-779-2978" );

write.println( "Melissa Douglas,1-958-508-1382" );

write.println( "Len Ramirez,1-734-978-1887" );

write.println( "Lisandra Allison,1-710-640-0215" );

write.println( "Ivy Evans,1-178-439-6129" );

write.println( "Perry Cohen,1-479-708-1776" );

write.println( "Phyllis Leon,1-370-865-7284" );

write.println( "Judith Heath,1-353-162-2917" );

write.println( "Dennis Osborne,1-579-523-3677" );

write.println( "Hermione Smith,1-448-253-0753" );

write.println( "Keaton Velazquez,1-742-686-3142" );

write.println( "Adena Gregory,1-632-684-1454" );

write.println( "Zia Swanson,1-722-364-5700" );

write.println( "Erin Aguilar,1-846-663-2746" );

write.println( "Ariel Miles,1-558-804-1708" );

write.println( "Bell Barton,1-238-313-7992" );

write.println( "Rose Bishop,1-861-144-4843" );

write.println( "Kevyn Sanchez,1-354-223-9876" );

write.println( "Meghan Serrano,1-272-249-1316" );

write.println( "Herrod Ayala,1-466-372-8685" );

write.println( "Alan Macdonald,1-844-264-7579" );

write.println( "George Keith,1-141-168-8728" );

write.println( "Sara Clements,1-774-993-4141" );

write.println( "Reece Gutierrez,1-121-711-5966" );

write.println( "Micah Jensen,1-728-155-6148" );

write.println( "Nissim Gonzalez,1-585-871-8268" );

write.println( "Azalia Mcgee,1-185-257-7477" );

write.println( "Allen Velazquez,1-534-427-1256" );

write.println( "Bethany Lowe,1-410-967-3808" );

write.println( "Wesley Terry,1-272-174-1357" );

write.println( "Belle Price,1-889-990-0494" );

write.println( "Emery Pickett,1-912-962-1201" );

write.println( "Richard Jacobson,1-942-721-9761" );

write.println( "Stella Mcintyre,1-617-758-5698" );

write.println( "Griffith Skinner,1-784-206-8616" );

write.println( "Benedict Shannon,1-733-599-8920" );

write.println( "Blythe Raymond,1-143-713-4030" );

write.println( "Jordan Preston,1-358-587-3208" );

write.println( "Rylee Hatfield,1-208-249-3704" );

write.println( "Chandler Jensen,1-532-977-9906" );

write.println( "Kaitlin Warren,1-800-346-6056" );

write.println( "John James,1-184-447-8392" );

write.println( "Tyler Ross,1-181-113-5382" );

write.println( "Grady Kemp,1-252-410-4800" );

write.println( "Malachi Mccoy,1-835-392-0258" );

write.println( "Laurel Mcdowell,1-187-152-9384" );

write.println( "Christian Buckner,1-432-157-0514" );

write.println( "Ila Le,1-979-166-4188" );

write.println( "Wyatt Chase,1-915-829-4671" );

write.println( "Damon Kidd,1-949-418-2678" );

write.println( "Aubrey Harrell,1-564-311-4088" );

write.println( "Julian Ferguson,1-247-747-8888" );

write.println( "Holmes Mendoza,1-551-960-4994" );

write.println( "Rudyard Green,1-308-350-2115" );

write.println( "Barbara Rosario,1-502-262-3889" );

write.close();

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Spatial Databases With Application To GIS

Authors: Philippe Rigaux, Michel Scholl, Agnès Voisard

1st Edition

1558605886, 978-1558605886

More Books

Students also viewed these Databases questions

Question

What is DDL?

Answered: 1 week ago