-
Notifications
You must be signed in to change notification settings - Fork 0
/
VotingEligiblity.java
70 lines (57 loc) · 2.21 KB
/
VotingEligiblity.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
import java.awt.*;
import java.awt.event.*;
import javax.swing.plaf.basic.BasicScrollBarUI;
public class VotingEligiblity extends Frame implements ActionListener{
private Label namelabel, agelabel, genderlabel;
private TextField nameTextField, ageTextField;
private CheckboxGroup genderCheckboxGroup;
private Button checkbutton;
public VotingEligiblity(){
setTitle("Eligible for voting");
setSize(500,500);
setResizable(true);
setLayout(new FlowLayout(){
} );
namelabel = new Label("Name:");
add(namelabel);
nameTextField = new TextField(20);
add(nameTextField);
genderlabel = new Label("Gender:");
add(genderlabel);
genderCheckboxGroup = new CheckboxGroup();
Checkbox maleCheckbox = new Checkbox("Male",genderCheckboxGroup,false);
Checkbox femaleCheckbox = new Checkbox("Female", genderCheckboxGroup, false);
add(maleCheckbox);
add(femaleCheckbox);
agelabel = new Label("Age:");
add(agelabel);
ageTextField = new TextField(5 );
add(ageTextField);
checkbutton = new Button("Check Eligibility");
checkbutton.addActionListener(this);
add(checkbutton);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent event){
dispose();
}
});
setVisible(true);
}
public void actionPerformed(ActionEvent event){
String name = nameTextField.getText();
String gender = genderCheckboxGroup.getSelectedCheckbox().getLabel();
int age = Integer.parseInt(ageTextField.getText());
String salutations = (gender.equals("Male"))? "Mr." : "Ms.";
if (age >= 18) {
System.out.println("Valid!");
System.out.println("Name: " + salutations + " " + name);
System.out.println("Gender: " + gender);
System.out.println("Age: " + age);
} else {
System.out.println("Not eligible to vote!");
}
}
public static void main(String[] args) {
VotingEligiblity eligibilityChecker = new VotingEligiblity();
}
}