-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessManagementAWT.java
More file actions
150 lines (123 loc) · 3.97 KB
/
Copy pathMessManagementAWT.java
File metadata and controls
150 lines (123 loc) · 3.97 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
class Student {
String id;
String name;
int meals;
double bill;
static final double COST_PER_MEAL = 50.0;
Student(String id, String name) {
this.id = id;
this.name = name;
this.meals = 0;
this.bill = 0.0;
}
void addMeal() {
meals++;
bill += COST_PER_MEAL;
}
public String toString() {
return id + " | " + name + " | Meals: " + meals + " | Bill: ₹" + bill;
}
}
public class MessManagementAWT extends Frame implements ActionListener {
TextField idField, nameField;
TextArea outputArea;
Button addBtn, mealBtn, viewBtn, clearBtn;
ArrayList<Student> students = new ArrayList<>();
public MessManagementAWT() {
setTitle("Mess Management System");
setSize(500, 500);
setLayout(null);
setBackground(new Color(240, 248, 255));
Label idLabel = new Label("Student ID:");
idLabel.setBounds(50, 50, 80, 25);
add(idLabel);
idField = new TextField();
idField.setBounds(140, 50, 120, 25);
add(idField);
Label nameLabel = new Label("Name:");
nameLabel.setBounds(50, 90, 80, 25);
add(nameLabel);
nameField = new TextField();
nameField.setBounds(140, 90, 200, 25);
add(nameField);
addBtn = new Button("Register");
addBtn.setBounds(50, 130, 80, 30);
addBtn.addActionListener(this);
add(addBtn);
mealBtn = new Button("Mark Meal");
mealBtn.setBounds(140, 130, 90, 30);
mealBtn.addActionListener(this);
add(mealBtn);
viewBtn = new Button("View All");
viewBtn.setBounds(240, 130, 80, 30);
viewBtn.addActionListener(this);
add(viewBtn);
clearBtn = new Button("Clear");
clearBtn.setBounds(330, 130, 60, 30);
clearBtn.addActionListener(this);
add(clearBtn);
outputArea = new TextArea();
outputArea.setEditable(false);
outputArea.setBounds(50, 180, 390, 250);
add(outputArea);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
}
});
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String id = idField.getText().trim();
String name = nameField.getText().trim();
if (e.getSource() == addBtn) {
if (id.isEmpty() || name.isEmpty()) {
showMessage("Enter both ID and Name!");
return;
}
students.add(new Student(id, name));
showMessage("Student registered.");
clearFields();
} else if (e.getSource() == mealBtn) {
Student student = findStudent(id);
if (student == null) {
showMessage("Student not found.");
} else {
student.addMeal();
showMessage("Meal marked for " + student.name);
}
} else if (e.getSource() == viewBtn) {
if (students.isEmpty()) {
showMessage("No students found.");
} else {
StringBuilder sb = new StringBuilder("---- Students ----\n");
for (Student s : students) {
sb.append(s).append("\n");
}
outputArea.setText(sb.toString());
}
} else if (e.getSource() == clearBtn) {
clearFields();
outputArea.setText("");
}
}
void showMessage(String msg) {
outputArea.setText(msg);
}
void clearFields() {
idField.setText("");
nameField.setText("");
}
Student findStudent(String id) {
for (Student s : students) {
if (s.id.equalsIgnoreCase(id)) return s;
}
return null;
}
public static void main(String[] args) {
new MessManagementAWT();
}
}