/
NameSurfer.java
82 lines (71 loc) · 2.4 KB
/
NameSurfer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
* File: NameSurfer.java
* ---------------------
* When it is finished, this program will implements the viewer for
* the baby-name database described in the assignment handout.
*/
import acm.program.*;
import java.awt.event.*;
import javax.swing.*;
public class NameSurfer extends Program implements NameSurferConstants {
/**
* private constants
*/
private static final String POSITION_OF_INTERACTORS = NORTH;
private static final int N_CHARS_NAMEFIELD = 20;
/**
* private instance variables
*/
private JTextField nameField;
private JButton graphButton;
private JButton clearButton;
private JButton deleteButton;
private NameSurferDataBase dataBase;
private NameSurferGraph graph;
/* Method: init() */
/**
* This method has the responsibility for reading in the data base
* and initializing the interactors at the top of the window.
*/
public void init() {
dataBase = new NameSurferDataBase(NAMES_DATA_FILE);
graph = new NameSurferGraph();
add(graph);
nameField = new JTextField(N_CHARS_NAMEFIELD);
nameField.addActionListener(this);
graphButton = new JButton("Graph");
deleteButton = new JButton("Delete");
clearButton = new JButton("Clear");
add(new JLabel("Name:"), POSITION_OF_INTERACTORS);
add(nameField, POSITION_OF_INTERACTORS);
add(graphButton, POSITION_OF_INTERACTORS);
add(deleteButton, POSITION_OF_INTERACTORS);
add(clearButton, POSITION_OF_INTERACTORS);
addActionListeners();
}
/* Method: actionPerformed(e) */
/**
* This class is responsible for detecting when the buttons are
* clicked, so you will have to define a method to respond to
* button actions.
*/
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if ((source == nameField) || (source == graphButton)) {
//println("Graph: \"" + nameField.getText() + "\"");
/*NameSurferEntry entry = new NameSurferEntry(nameField.getText());
println(entry.getName());
println(entry.getRank(0));
println(entry.getRank(NDECADES - 2));
println(entry.getRank(NDECADES));
println(entry.toString());*/
//println("Graph: \"" + dataBase.findEntry(nameField.getText()) + "\"");
graph.addEntry(dataBase.findEntry(nameField.getText()));
} else if (source == clearButton) {
//println("Clear");
graph.clear();
} else if (source == deleteButton) {
graph.removeEntry(dataBase.findEntry(nameField.getText()));
}
}
}