/
Main.py
108 lines (80 loc) · 2.78 KB
/
Main.py
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
# Author: Ashley Johnson
# Program: Simple Sqlite Chainsaw Jugglers Program
''' This main module executes action methods based on
user input.
it imports the database module to send the user interaction
data to the database for CRUD processing.
For user interaction ease, I borrowed Clara's menu module and
modified it to reflect the flow and use of this program. as well
as the ask question and message methods, because they are a brilliant idea
Hope you don't mind. '''
from menu import Menu
import chain_db as db
from chain_db import JugglerError
import ui
# declare a menu loop terminator
QUIT = 'Q'
def main():
menu = create_menu()
while True:
choice = ui.display_menu_get_choice(menu)
action = menu.get_action(choice)
action()
if choice == QUIT:
break
# menu creation to add menu options for user interaction
def create_menu():
menu = Menu()
menu.add_option('1', 'Add New Juggler', add_new_juggler)
menu.add_option('2', 'Show All Jugglers', show_table)
menu.add_option('3', 'Search by Juggler Name', search_by_name)
menu.add_option('4', 'Update Catches for Juggler', update_juggler_catches)
menu.add_option('5', 'Delete a Juggler', delete_by_name)
menu.add_option(QUIT, 'Quit', quit_program)
return menu
# add juggler method
# calls the interface to gather the information from the user, and package in a Juggler object
# then passes it to the database to add the new record
def add_new_juggler():
new_record = ui.get_info()
try:
db.add_record(new_record)
ui.message('Record Added!')
except JugglerError as e:
ui.message(e)
# calls the database to return all juggler records
# p passes to the interface to display
def show_table():
display_records = db.get_jugglers()
ui.show_jugglers(display_records)
# interface requests the name from the user
# passes the name to the database
# passes the matches back to the interface for dislay
def search_by_name():
search = ui.name_search()
matches = db.search_records(search)
ui.show_jugglers(matches)
# gets the Juggler's name from the interface
# gets the number of catches from the interface
# passes to the database for updating
def update_juggler_catches():
catcher_name = ui.name_search()
catches = ui.get_catches()
try:
db.update_catches(catcher_name, catches)
ui.message('Catches Updated')
except JugglerError as e:
ui.message(e)
# get juggler name from interface
# passes to the database for deleting
def delete_by_name():
juggler_name = ui.name_search()
try:
db.delete_juggler(juggler_name)
ui.message('Juggler Deleted')
except JugglerError as e:
ui.message(e)
def quit_program():
ui.message('Good bye!')
if __name__ == '__main__':
main()