-
Notifications
You must be signed in to change notification settings - Fork 0
/
Clothing_Allowance_App_Code
213 lines (177 loc) · 8.7 KB
/
Clothing_Allowance_App_Code
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
##*********************************************** IMPORTS ***************************************************
from tkinter import *
from tkinter import ttk
import os
##********************************************** CLASSES ****************************************************
#Create the Account class
class Account:
#The Account class stores the details of each account and has methods to deposit and withdraw money from their account
def __init__(self, name, balance):
self.name = name
self.balance = float(balance)
account_list.append(self)
#Deposit method adds money to balance
def deposit(self, amount):
#Doesn't accept values of less than a cent
if amount >= 0.01:
self.balance += amount
return True
else:
return False
#Withdraw method subtracts money from balance
def withdraw(self, amount):
if amount >= 0.01 and amount <= self.balance:
self.balance -= amount
return True
else:
return False
##****************************************** FUNCTIONS AND SETUP *****************************************
#Create a function to read data from the file
def get_data():
#Checks whether the file exists and if so reads the file and separtes it to find the accounts and their balance
if os.path.exists("clothing_allowance_app_file.txt"):
if os.path.isfile("clothing_allowance_app_file.txt"):
account_file = open("clothing_allowance_app_file.txt","r")
line_list = account_file.readlines()
for line in line_list:
account_data = line.strip().split(",")
Account(*account_data)
account_file.close()
#If the file doesn't exist a new file is created and set at the initial amounts set by the parents for the clothing allowance app
else:
print("File is not present new account file is being set up.")
account_file = open("clothing_allowance_app_file.txt","w")
account_file.write("Nikau's Account,300.0\nHana's Account,300.0\nTia's Account,300.0")
account_file.close()
get_data()
#Create a function to get account names list
def create_name_list():
name_list = []
for account in account_list:
name_list.append(account.name)
return name_list
#Checks if balance of the account is above $50, if so then bonus message pops up else message to encourage saving money will pop up
def bonus_check(self):
if self.balance >= 50.00:
bonus_message.set(" You're on track to recieve you're allocated bonus.\nMake sure to keep at least $50.00 in your account till the end of the year to recieve the bonus! :)")
else:
bonus_message.set(" You're not on track to receive you're allocated bonus.\nTry save up at least $50.00 in your account till then end of the year to recieve the bonus! :)")
#Create a function that will update the balance.
def update_balance():
balance_string = ""
account_file = open("clothing_allowance_app_file.txt", "w")
#Append each accounts balance to the specific account name
for account in account_list:
balance_string += "{}: ${:.2f} ".format(account.name, account.balance)
account_file.write("{},{}\n".format(account.name, account.balance))
account_details.set(balance_string)
account_file.close()
#Create the deposit function
def deposit_money(account):
#Provides appropriate error and feedback messages regarding transactions
if account.deposit(amount.get()):
action_feedback.set("Your transaction has been completed. Total of ${:.2f} has been deposited into {}".format(amount.get(), account.name))
else:
action_feedback.set("Please enter a positive number/a number larger or equal to 0.01")
#Create the withdraw function
def withdraw_money(account):
#Provides appropriate error and feedback messages regarding transactions
if account.withdraw(amount.get()):
action_feedback.set("Your transaction has been completed. Total of ${:.2f} has been withdrawn from {}".format(amount.get(), account.name))
else:
action_feedback.set("Sorry, you either do not have enough money in {} or have written an invalid amount".format(account.name))
#Create the manage action function
def manage_action():
try:
for account in account_list:
if chosen_account.get() == account.name:
if chosen_action.get() == "Deposit":
deposit_money(account)
else:
withdraw_money(account)
bonus_check(account)
#Update the GUI
update_balance()
amount.set("")
#Add an exception for text input
except:
action_feedback.set("Please enter a positive number (a number larger or equal to 0.01). Special characters and letters will not be accepted!")
#Set up Lists
account_list = []
#Create instances of the class
get_data()
account_names = create_name_list()
##******************************************** GUI CODE ***************************************************
root = Tk()
root.title("Clothing Allowance App")
#Create the top frame
top_frame = ttk.LabelFrame(root, text="Account Details")
top_frame.grid(row=0, column=0, padx=10, pady=10, sticky="NSEW")
#Create and set the message text variable
message_text = StringVar()
message_text.set("Welcome! You can deposit or withdraw money from your account and check whether you will be allocated a bonus at the end of this year \n(if you have $50.00 or more in your account left in your account).")
#Create and pack the message label
message_label = ttk.Label(top_frame, textvariable=message_text, wraplength=1000)
message_label.grid(row=0, column=0, columnspan=2, padx=10, pady=10)
#Create and set the account details variable
account_details = StringVar()
#Create the details label and pack it into the GUI
details_label = ttk.Label(top_frame, textvariable=account_details)
details_label.grid(row=2, column=0, columnspan=2, padx=10, pady=10)
#Create the bottom frame
bottom_frame = ttk.LabelFrame(root)
bottom_frame.grid(row=1, column=0, padx=10, pady=10, sticky="NSEW")
#Create a label for the account combobox
account_label = ttk.Label(bottom_frame, text="Account: ")
account_label.grid(row=3, column=0, padx=10, pady=3)
#Set up a variable and option list for the account Combobox
chosen_account = StringVar()
chosen_account.set(account_names[0])
#Create a Combobox to select the account
account_box = ttk.Combobox(bottom_frame, textvariable=chosen_account, state="readonly")
account_box['values'] = account_names
account_box.grid(row=3, column=1, padx=10, pady=3, sticky="WE")
#Create a label for the action Combobox
action_label = ttk.Label(bottom_frame, text="Action:")
action_label.grid(row=4, column=0)
#Set up a variable and option list for the action Combobox
action_list = ["Deposit", "Withdraw"]
chosen_action = StringVar()
chosen_action.set(action_list[0])
#Create the Combobox to select the action
action_box = ttk.Combobox(bottom_frame, textvariable=chosen_action, state="readonly")
action_box['values'] = action_list
action_box.grid(row=4, column=1, padx=10, pady=3, sticky="WE")
#Create a label for the amount field and pack it into the GUI
amount_label = ttk.Label(bottom_frame, text="Amount:")
amount_label.grid(row=5, column=0, padx=10, pady=3)
#Create a variable to store the amount
amount = DoubleVar()
amount.set("")
#Create an entry to type in amount
amount_entry = ttk.Entry(bottom_frame, textvariable=amount)
amount_entry.grid(row=5, column=1, padx=10, pady=3, sticky="WE")
#Create a submit button
submit_button = ttk.Button(bottom_frame, text="Submit", command=manage_action)
submit_button.grid(row=6, column=0, columnspan=2, padx=10, pady=10)
root.bind('<Return>', lambda event:manage_action())
#Create an action feedback label
action_feedback = StringVar()
action_feedback_label = ttk.Label(bottom_frame, textvariable=action_feedback)
action_feedback_label.grid(row=7, column=0, columnspan=2)
#Create an allocated bonus message when accounts have more than $50 left in it
bonus_message = StringVar()
#Create a savings message when accounts have less than $50 left in it
savings_message = StringVar()
#Create and pack the bonus label
bonus_label = ttk.Label(bottom_frame, textvariable=bonus_message, wraplength=1000)
bonus_label.grid(row=10, column=0, columnspan=4, padx=100, pady=10)
##**********************************************************************************************************
#Run the mainloop
update_balance()
root.mainloop()
##**********************************************************************************************************
#Inputs from file to copy for trials
#Nikau's Account, 300
#Hana's Account, 300
#Tia's Account, 300