-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNameSurfer.java
More file actions
80 lines (69 loc) · 2.13 KB
/
Copy pathNameSurfer.java
File metadata and controls
80 lines (69 loc) · 2.13 KB
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
/*
* 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 {
public static void main(String[] args) {
new NameSurfer().start(args);
}
/* Method: init() */
/**
* This method has the responsibility for reading in the data base
* and initializing the interactors at the top of the window.
*/
public NameSurfer() {
JLabel name = new JLabel("Name: ");
add(name, NORTH);
textField = new JTextField(20);
textField.addActionListener(this);
textField.setActionCommand("Graph");
add(textField, NORTH);
JButton graphButton = new JButton("Graph");
add(graphButton, NORTH);
JButton deleteButton = new JButton("Delete");
add(deleteButton, NORTH);
JButton clearButton = new JButton("Clear");
add(clearButton, NORTH);
addActionListeners();
graph = new NameSurferGraph();
add(graph);
dataBase = new NameSurferDataBase(NAMES_DATA_FILE);
}
/* 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) {
// You fill this in //
String cmd = e.getActionCommand();
if (cmd.equals("Graph")) {
//println(textField.getText());
String inputName = textField.getText().toLowerCase();
NameSurferEntry nameData = dataBase.findEntry(inputName);
if(nameData!=null){
graph.addEntry(nameData);
}
graph.update();
} else if (cmd.equals("Clear")) {
graph.clear();
} else if (cmd.equals("Delete")) {
String inputName = textField.getText().toLowerCase();
NameSurferEntry nameData = dataBase.findEntry(inputName);
if(nameData!=null){
graph.deleteEntry(nameData);
}
graph.update();
}
}
private JTextField textField;
private NameSurferDataBase dataBase;
//private static final String FILE_NAME = "names-data.txt";
private NameSurferGraph graph;
}