//These are some of the standard "imports" we'll be using, explained in class import java.awt.*; import java.util.*; import java.awt.event.*; import javax.swing.*; import java.applet.*; class Contact { // List of properties of a contact public String name; public String email; public float beardLength; } class AddressBook { // Create the internal list (like an array) of contacts ArrayList< Contact > contacts = new ArrayList< Contact >(); // Method to add a new contact to the list public void add(Contact newPerson) { contacts.add(newPerson); } // Method to retrieve the number of contacts currently in the address book public int numContacts() { return contacts.size(); } // Method to retrieve a specific contact given its index public Contact getContact(int index) { return contacts.get(index); } } public class TestAddressBook extends Applet implements ActionListener { JTextArea output; JTextField name, email, beard; JButton button; AddressBook contacts; public void init() { setLayout(new BorderLayout()); // Create the text-area and button we'll use name = new JTextField(8); email = new JTextField(8); beard = new JTextField(8); output = new JTextArea(); button = new JButton("Add Contact"); // Next we create a "panel" that we add button to JPanel p = new JPanel(); p.setLayout(new FlowLayout()); p.add(new JLabel("name: ")); p.add(name); p.add(new JLabel("email: ")); p.add(email); p.add(new JLabel("beard size: ")); p.add(beard); p.add(button); // Finally, to finish building the UI, we simply add the text and the panel // (which contains the button) to the applet add("Center", new JScrollPane(output)); add("South", p); // Lastly, we instruct the applet to "listen" for the button to be pressed button.addActionListener(this); // Create the address book object contacts = new AddressBook(); } public void actionPerformed(ActionEvent evt) { // create new contact based on the inputs Contact newPerson = new Contact(); newPerson.name = name.getText(); newPerson.email = email.getText(); newPerson.beardLength = Float.parseFloat(beard.getText()); // add new person to the address book contacts.add(newPerson); // now list the contacts one at a time output.setText("Current List of Contacts:\n"); for (int index = 0; index < contacts.numContacts(); index++) { output.append(" " + index + ". "); output.append(contacts.getContact(index).name + ", "); output.append(contacts.getContact(index).email + ", "); output.append(contacts.getContact(index).beardLength + "\n"); } } }