-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShowStudent.py
More file actions
156 lines (137 loc) · 5.61 KB
/
ShowStudent.py
File metadata and controls
156 lines (137 loc) · 5.61 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
151
152
153
154
155
156
import json
class ShowStudent:
# The costructor method
def __init__(self, json_file):
self.json_file = json_file
self.students = self.load_students()
self.check_mark = "\u2705"
self.envelope = "\u2709"
self.cross_mark = "\u274C"
self.telephone = "\u260E"
# Method that loads the students from the file
def load_students(self):
try:
with open('ProjectJSONs/student.json', 'r') as f:
data = json.load(f)
students = data.get('students', [])
if not students:
print("\n ⚠️ Warning: No students found in JSON file")
return students
except json.JSONDecodeError as e:
print("\n ⚠️ Error: Invalid JSON format in file: {str(e)}")
return []
except Exception as e:
print(f"\n ⚠️ Error reading file! Please Make Sure you have created a student already!: {str(e)}")
return []
# The menu to give the user search options
def search_by_menu(self):
while True:
print("\n=== Student Management System ===")
print("1. Show All Students")
print("2. Search by Name")
print("3. Search by ID")
print("4. Search by Major")
print("Enter to Exit")
choice = input("\nEnter your choice (1-4): ")
if choice == '1':
self.display_all_students()
elif choice == '2':
self.search_by_name()
elif choice == '3':
self.search_by_id()
elif choice == '4':
self.filter_by_major()
elif choice == '':
print("Goodbye!")
break
else:
print("\n❌Invalid choice. Please try again.")
# Method to print out students individually
def display_student(self, student):
if student:
print(f"{student['id']:<12}"
f"{student['name']:<15}"
f"{student['age']:<5}"
f"{student['gender']:<8}"
f"{student['major']:<8}"
f"{student['gpa']:<7}"
f"{student['phone']:<15}")
# 1. Show all students
# Method to display all of the students
def display_all_students(self):
print("=" * 60)
print(f"{'ID':<12}{'Name':<15}{'Age':<5}{'Gender':<8}{'Major':<8}{'GPA':<7}{'Phone':<15}")
print("=" * 60)
if not self.students:
print("\n❌ No students found.")
return
for student in self.students:
print(f"{student['id']:<12}"
f"{student['name']:<15}"
f"{student['age']:<5}"
f"{student['gender']:<8}"
f"{student['major']:<8}"
f"{student['gpa']:<7}"
f"{student['phone']:<15}")
print("=" * 60)
# 2. Search by Name
# Method that allows the user to search the database by name
def search_by_name(self):
name = input("\nEnter student name to search: ").strip().upper()
found = False
print("=" * 60 )
print(f"{'ID':<12}{'Name':<15}{'Age':<5}{'Gender':<8}{'Major':<8}{'GPA':<7}{'Phone':<15}")
print("=" * 60)
for student in self.students:
if student['name'].upper().find(name) != -1:
self.display_student(student)
found = True
if not found:
print(f"\n❌ No students found with name containing '{name}'")
# 3. Search by ID
# Method that allows the user to search by the id
def search_by_id(self):
student_id = input("\nEnter student ID: ").strip()
found = False
print("=" * 60)
print(f"{'ID':<12}{'Name':<15}{'Age':<5}{'Gender':<8}{'Major':<8}{'GPA':<7}{'Phone':<15}")
print("=" * 60)
for student in self.students:
if student['id'] == student_id:
self.display_student(student)
found = True
break
if not found:
print(f"\n❌ No student found with ID: {student_id}")
# 4. Search by major
# Method that allows the user to earch by major
def filter_by_major(self):
print("\nAvailable majors:")
majors = set(student['major'] for student in self.students)
for major in majors:
print(f"* {major}")
while True:
major = input("\nEnter the major to filter by (or press Enter to see all students): ").strip().upper()
print(f"\n=== {major} Students ===")
print("=" * 60)
print(f"{'ID':<12}{'Name':<15}{'Age':<5}{'Gender':<8}{'Major':<8}{'GPA':<7}{'Phone':<15}")
print("=" * 60)
if not major:
self.display_all_students()
break
elif major.upper() in {m.upper() for m in majors}:
self.display_filtered_students(major)
break
else:
print("\n❌ Invalid major. Please try again.")
# Method that filters the students by their major
def display_filtered_students(self, major=None):
filtered_students = self.students
if major:
filtered_students = [student for student in self.students
if student['major'].upper() == major.upper()]
if not filtered_students:
print(f"\n❌ No students found" + (f" with major {major}." if major else "."))
return
for student in filtered_students:
self.display_student(student)