-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
111 lines (86 loc) · 2.56 KB
/
main.py
File metadata and controls
111 lines (86 loc) · 2.56 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
from src.HashList import HashList
from src.Student import Student
from datetime import datetime
file = "Data.txt"
list = HashList(53)
running = True
def readFile():
""" Read the file
Also save the content of the file into the
HashList and removing the latest added object and measure
the duration of this operation.
"""
lastStudent = None
with open(file) as f:
for l in f:
(firstName, lastName, id) = l.replace("\n", "").split(",")
student = Student(firstName, lastName, id)
lastStudent = student
list.add(student)
start = datetime.now().microsecond * 1000
list.delete(lastStudent.getLastName())
end = datetime.now().microsecond * 1000
t = end - start
print("Deleting latest added element took " + str(t) + "ms.")
def printMenu():
""" Print the main menu """
o = "\n\n[1 = Add student]\n[2 = Remove student]\n[3 = Modify student]\n[4 = Show students]\n[5 = End]\n"
print(o)
def userInput():
""" Get a user input for the options
If the users input cant be converted into a
Integer object, the user will get an error
message and is returning to the menu.
"""
try:
i = int(input("Select an option: "))
options(i)
except ValueError:
print("Invalid input.")
def options(i):
""" Match the users input to a function
:param i: Users selection.
"""
if i == 1:
add()
elif i == 2:
delete()
elif i == 3:
modify()
elif i == 4:
show()
elif i == 5:
global running
running = False
else:
print("Invalid input.")
def add():
""" Add a new Student
If the ID cant be casted into a Integer
object, the user will get an error message
and will return to the menu.
"""
firstName = input("First Name: ")
lastName = input("Last Name: ")
try:
id = int(input("ID: "))
list.add(Student(firstName, lastName, id))
except ValueError:
print("Invalid input for ID.")
def delete():
""" Delete an object by its last name """
list.delete(input("Last Name: "))
def modify():
""" Modify an object by its last name with a new last name """
list.modify(input("Old last name: "), input("New last name: "))
def show():
""" Prints the list """
print(list.toString())
startTime = datetime.now().microsecond * 1000
readFile()
endTime = datetime.now().microsecond * 1000
total = endTime - startTime
print("Reading file took " + str(total) + "ms.\n\n\n")
while (running):
printMenu()
userInput()