-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestdates.py
More file actions
221 lines (210 loc) · 8.58 KB
/
testdates.py
File metadata and controls
221 lines (210 loc) · 8.58 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#Module name : testdates.py
#created by : Yew Fu Li, Tan Wen Zhe
#created date : 25/2/2023
#import : os, json, time, calendar, coursemaintain, generalcode
import os,json,coursemaintain as a,time,calendar,generalcode as g
data =g.readfile("tdates.json")
courses= g.readfile('courses.json')
def display_time(code):#display date,slot and number of questions
found=True
for cornm in courses['Courses']:
if cornm["COURSES"][:8]==code:
name=cornm.get('COURSES')[9:]
print('-'*50)
print("Time Slots available --->")
print('-'*50)
print('%s (%s):'%(code,name))
print('-'*50)
print("Date Slots Number of questions(Size)")
print('-'*50)
if not data.get(code):
print('No record found')
else:
for course in data[code]:
print('%-13s%02d %02d' % (course['Date'], int(course['Slots']), int(course['Number of question(size)'])))
def main():#input course code
loop3=True
while loop3:
os.system('cls')
a.display_courses()
coursecode=input('Course code <Q>uit:').upper()
if coursecode=='Q':
loop3=False
os.system('cls')
elif (len(coursecode)!=8 or coursecode[:2]!='FH') or\
coursecode[4:8].isdigit()==False or coursecode[:4].isalpha()==False:
print('Invalid course code entered.')
time.sleep(0.5)
else:
found=False
for course in courses['Courses']:
if course['COURSES'][:8]==coursecode:
found=True
main2(coursecode)
if not found:
print("Course hasn't added")
time.sleep(0.5)
def main2(coursecode):#menu
loop=True
while loop:
os.system('cls')
display_time(coursecode)
opt=input('<A>dd <U>pdate <D>elete <Q>uit').upper()
if opt=='A':
step=3
maintain(step,opt,coursecode)
elif opt in ['U','D']:
step=1
maintain(step,opt,coursecode)
elif opt=='Q':
loop=False
else:
print('invalid option entered')
time.sleep(0.5)
def maintain(step,opt,code):
coursedate=''
loop1=True
while loop1:
if step==1:#input date that will be removed/changed
os.system('cls')
display_time(code)
removedate=input('Enter date to (delete/change) the details (dd/mm/yyyy) <Q>uit:').upper()
if removedate=='Q':
step=99
os.system('cls')
elif (len(removedate)!=10 or removedate[:2].isdigit()==False or\
removedate[3:5].isdigit()==False or removedate[6:10].isdigit()==False\
or removedate[2]!='/' or removedate[5]!='/'):
print('Invalid date entered')
step=1
time.sleep(0.5)
elif not is_valid_date(removedate):
print('Invalid date entered')
step=1
time.sleep(0.5)
else:
found=False
for codes in data[code]:
if codes["Date"]==removedate:
slot=codes['Slots']
ques=codes['Number of question(size)']
found=True
if found:
step+=1
else:
print('The date that you want to delete/change is not found in file.')
time.sleep(0.5)
if step==2:#confirmation about date,number of slots and number of questions that will be appended/updated/removed and
#append date,number of slots and number of questions
os.system('cls')
display_time(code)
if opt in ['U','D'] and coursedate=='':
print('The details in date that you want to delete/change:%s(slots: %s)(number of questions: %s)'%(removedate,slot,ques))
confirm=input('Do you confirm to delete/change the details in date?(yes/no) <Q>uit').lower()
else:
print('The details in date that you want to append/update:%s(slots: %s)(number of questions: %s)'%(coursedate,courseslot,courseques))
confirm=input('Do you confirm to append/update the details in date?(yes/no) <Q>uit').lower()
if confirm=='q':
step=99
os.system('cls')
elif not confirm in ['yes','no']:
print('Invalid input entered.')
time.sleep(0.5)
elif confirm=='yes':
if opt=='A':
if not data.get(code):
data[code]=[date]
else:
data[code].append(date)
step+=5
elif opt=='U' and coursedate=='':
step+=1
else:
step=6
else:
if opt in ['U','D'] and coursedate=='':
step=1
else:
step=3
if step==3:#input date that will be appended/updated
os.system('cls')
display_time(code)
if opt=='U':
print('The details in date that you want to delete/change:%s(slots: %s)(number of questions: %s)'%(removedate,slot,ques))
coursedate=input('Enter date (dd/mm/yyyy) <Q>uit:').upper()
if coursedate=='Q':
step=99
os.system('cls')
elif (len(coursedate)!=10 or coursedate[:2].isdigit()==False or\
coursedate[3:5].isdigit()==False or coursedate[6:10].isdigit()==False\
or coursedate[2]!='/' or coursedate[5]!='/') :
print('Invalid date entered')
step=3
time.sleep(0.5)
elif not is_valid_date(coursedate):
print('Invalid date entered')
step=3
time.sleep(0.5)
else:
step+=1
if step==4:#input number of slots that will be appended/updated
os.system('cls')
display_time(code)
if opt=='U':
print('The details in date that you want to delete/change:%s(slots: %s)(number of questions: %s)'%(removedate,slot,ques))
print('Date: %s'%(coursedate))
courseslot=input('Enter number of slots <Q>uit:').upper()
if courseslot=='Q':
step=99
os.system('cls')
elif courseslot.isdigit()==False or len(courseslot)>2 or float(courseslot)==True:
print('Invalid number of slots entered.')
step=4
time.sleep(0.5)
else:
step+=1
if step==5:#input number of questions that will be appended/updated
os.system('cls')
display_time(code)
if opt=='U':
print('The details in date that you want to delete/change:%s(slots: %s)(number of questions: %s)'%(removedate,slot,ques))
print('Date: %s'%(coursedate))
print('Slots: %s'%(courseslot))
courseques=input('Enter number of questions <Q>uit:').upper()
if courseques=='Q':
step=99
os.system('cls')
elif courseques.isdigit()==False or len(courseques)>2 or float(courseques)==True:
print('Invalid number of questions entered.')
step=5
time.sleep(0.5)
else:
date={"Date":coursedate,"Slots":courseslot,"Number of question(size)":courseques}
step=2
if step==6:#remove date,number of slots and number of questions
for codes in data[code]:
if codes["Date"]==removedate:
idx=data[code].index(codes)
data[code].remove(codes)
if opt=='U':
data[code].insert(idx,date)
step+=1
if step==7:#write into json file
g.writefile(data,"tdates.json",4)
loop1=False
if step==99:#quit
os.system('cls')
loop1=False
# Validate a date string in 'DD/MM/YYYY' format
def is_valid_date(date_string):
status=True
day, month, year = map(int, date_string.split('/'))
# Check if the month is valid
if month < 1 or month > 12:
status=False
else:
# Check if the day is valid for the given month and year
days_in_month = calendar.monthrange(year, month)[1]
if day < 1 or day > days_in_month:
status=False
return status