import java.awt.event.*;
import java.applet.Applet;
import java.awt.*;
public class addbook extends Applet implements ActionListener
{
TextField nameField = new TextField(30),
phoneField = new TextField(30),
addField = new TextField(30);
static int numPersons = 0;
Button previousButton = new Button("Prev"),
nextButton = new Button("Next"),
print = new Button("Print"),
addName = new Button("Add"),
clearFields = new Button("Clear");
Contact people[];
int currentPerson;
int totalPeople = 0;
public void init()
{
people = new Contact[10];
add(new Label("Name: "));
add(nameField);
add(new Label("Phone: "));
add(phoneField);
add(new Label("Address:"));
add(addField);
previousButton.addActionListener(this);
add(previousButton);
nextButton.addActionListener(this);
add(nextButton);
print.addActionListener(this);
add(print);
addName.addActionListener(this);
add(addName);
clearFields.addActionListener(this);
add(clearFields);
currentPerson = 0;
//displayPerson(currentPerson);
}
// Handle button presses
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == previousButton)
{
currentPerson = currentPerson + 1;
if (currentPerson > (totalPeople - 1)) currentPerson = 0;
displayPerson(currentPerson);
}
else if (e.getSource() == nextButton)
{
currentPerson = currentPerson - 1;
if (currentPerson < 0) // && currentPerson <= totalPeople)
currentPerson = totalPeople;
displayPerson(currentPerson);
}
else if (e.getSource() == addName)
{
if (totalPeople < 10)
{
people[totalPeople] = new Contact(nameField.getText(), phoneField.getText() , addField.getText());
totalPeople += 1;
nameField.setText("");
phoneField.setText("");
addField.setText("");
}
}
else if (e.getSource() == print)
{
for (int i = 0; i < totalPeople ; i++)
System.out.println(people[i].toString());
}
else if (e.getSource() == clearFields)
{
nameField.setText("");
phoneField.setText("");
addField.setText("");
}
}
// Display selected person's address
public void displayPerson(int i)
{
nameField.setText(people[i].name);
phoneField.setText(people[i].phone);
addField.setText(people[i].code);
}
class Contact
{
String name,
phone,
code;
public Contact(String theName, String thePhone, String theCode)
{
name = theName;
phone = thePhone;
code = theCode;
numPersons = numPersons + 1;
}
public String toString()
{
String description;
description = name + ", " + phone + ", " + code;
return description;
}
}
}