You are on page 1of 5

In 

[10]:
import os, json, platform

class StudentInformationSystem:

def __init__(self):

self.students_data = self.upload_students()

self.menu = {

'C': ['Create Student', self.create_student],

'R': ['Read Student', self.read_student],

'U': ['Update Student', self.update_student],

'D': ['Delete Student', self.delete_student],

'E': ['System Exit', self.system_exit]

while True:

os.system('clear' if platform.system()=='Linux' else 'cls')

ch = self.show_menu()

self.menu[ch][1]()

if ch=='E':

break

def create_student(self):

print('\nModule: Create Student\n')

student = self.input_student()

student['grades'] = []

self.students_data.append(student)

self.save_students()

def read_student(self):

print('\nModule: Read Student\n')

self.list_students()

idxno = self.dialog('Choose... [c]-Cancel, [number]-Student Index Number:', ['c'] + \

[ str(i) for i in range(len(self.students_data)) ] )

if idxno != 'c':

self.student_information(int(idxno))

def update_student(self):

print('\nModule: Update Student\n')

self.list_students()

choice = self.dialog('Choose ... [c]-Cancel, [s]-Student, [g]-Grade : ', ['s','g','c'])

if choice != 'c':

idxno = self.dialog('Choose... [c]-Cancel, [number]-Student Index Number:', ['c'] + \

[ str(i) for i in range(len(self.students_data)) ] )

if idxno != 'c':

nidxno = int(idxno)

if choice == 's':

student = self.input_student()

for k,v in student.items():

if v != '':
self.students_data[nidxno][k] = v

elif choice == 'g':

while True:

grade = self.input_grade()

do_append = True

for i,v in enumerate(self.students_data[nidxno]['grades']):

if grade['subject'] == v['subject']:

self.students_data[nidxno]['grades'][i]['grade']=grade['grade']

do_append = False

break

if do_append:

self.students_data[nidxno]['grades'].append(grade)

answer = self.dialog('Input another grade? (y/n)', ['y', 'n'])

if answer == 'n':

break

self.save_students()

def delete_student(self):

print('\nModule: Delete Student\n')

self.list_students()

choice = self.dialog('Choose ... [c]-Cancel, [s]-Student, [g]-Grade : ', ['s','g','c'])

if choice != 'c':

idxno = self.dialog('Choose... [c]-Cancel, [number]-Student Index Number:', ['c'] + \

[ str(i) for i in range(len(self.students_data)) ] )

if idxno != 'c':

nidxno = int(idxno)

if choice == 's':

answer =self.dialog('Delete this student? (y/n)', ['y', 'n'])

if answer == 'y':

del self.students_data[nidxno]

self.save_students()

elif choice == 'g':

self.list_grades(nidxno)

grno = self.dialog('Choose... [c]-Cancel, [number]-Grade Index Number:', ['c'] + \

[ str(i) for i in range(len(self.students_data[nidxno]['grades'])) ] )

if grno != 'c':

answer =self.dialog('Delete this grade? (y/n)', ['y', 'n'])

if answer == 'y':

del self.students_data[nidxno]['grades'][int(grno)]

self.save_students()

def show_menu(self):

while True:

print('\nStudent Information System\n')

for k,v in self.menu.items():

print(f'{k}-{v[0]}')

choice = input('\nEnter your choice:').upper()

if choice in self.menu.keys():

return choice

def upload_students(self):

while True:

try:

data = open('students_data.dta').read()

return json.loads(data)

except:

open('students_data.dta', 'w').write('[]')

def save_students(self):

open('students_data.dta', 'w').write(json.dumps(self.students_data))

print('Saved students ...')

def input_student(self):

student = {

'idno': input('Enter ID Number:'),

'fname': input('Enter First Name:'),

'mname': input('Enter Middle Name:'),

'lname': input('Enter Last Name:'),

return student

def input_grade(self):

sgrade = {
'subject': input('Enter Subject Code:'),

'grade': input('Enter Subject Grade:'),

return sgrade

def list_students(self):

print('List of Students')

for i,stud in enumerate(self.students_data):


sdata = ', '.join([f'{k}={v}' for k,v in list(stud.items())[:-1] ])

print(f'[{i}]-{sdata}')

def dialog(self, prompt, choices):

while True:

choice = input(prompt)

if choice in choices:

return choice

def student_information(self, nidxno):

sdata = self.students_data[nidxno]

print('\nStudent Information:')

print(f"ID No.: {sdata['idno']}")

print(f"Student Name: {sdata['lname']}, {sdata['fname']} {sdata['mname']}")

self.list_grades(nidxno)

self.dialog('Press ENTER key...', [''])

def list_grades(self, nidxno):

print('List of Grades:')

for i,v in enumerate(self.students_data[nidxno]['grades']):

print(f"[{i}]-[{v['subject']}, {v['grade']}]")

def system_exit(self):

print('\nSystem Exit\n')

if __name__ == '__main__':

sis = StudentInformationSystem()

Student Information System

C-Create Student

R-Read Student

U-Update Student

D-Delete Student

E-System Exit

System Exit

In [11]:
sis

<__main__.StudentInformationSystem at 0x7fef880d52b0>
Out[11]:

In [12]:
sis.students_data

[{'idno': '45-67890',

Out[12]:
'fname': 'maria',

'mname': 'cortez',

'lname': 'rizal',

'grades': [{'subject': 'DCIT50', 'grade': '2.0'}]},

{'idno': '222-33333',

'fname': 'jose',

'mname': 'protacio',

'lname': 'mercado',

'grades': []},

{'idno': '00-11111',

'fname': 'nestor',

'mname': 'f',

'lname': 'bueno',

'grades': []}]

In [ ]:

You might also like