-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcost.py
126 lines (106 loc) · 4.97 KB
/
cost.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import datetime
from pathlib import Path
import os.path
import os
FILE_DIR = os.getcwd() + "/files/expense_dir"
USER_DIR = os.getcwd() + "/files/user"
class CostFunction():
def current_balanace(self):
with open(FILE_DIR+"/total_balance.txt") as file:
balance = file.read()
return balance
def say_hello(self):
current_date = datetime.datetime.now()
local_date_time = current_date.strftime("%c")
user = "User"
if Path(USER_DIR + "/user.txt").is_file():
with open(USER_DIR + "/user.txt", "r") as user_flle:
user = user_flle.readline().strip("\n")
print("Welcome Mr. {0}".format(user))
print(local_date_time)
print("Current balance: {:.2f}".format(float(self.current_balanace())))
def command(self, cmd):
cmd = cmd.split()
main_cmd = cmd[0]
if main_cmd == 'expense':
# amounts = cmd[1].strip()
amounts = cmd[1].split(",")
try:
amounts = [float(x) for x in amounts]
current_date = datetime.datetime.now()
local_date_time = current_date.strftime("%c")
total_amount = sum(amounts)
if Path(FILE_DIR + "/daily_expenses.txt").is_file() and Path(FILE_DIR + "/total_balance.txt").is_file():
with open(FILE_DIR + "/daily_expenses.txt", "a") as daily_expense:
daily_expense.write(local_date_time + " -----> " + str(total_amount) + "\n")
daily_expense.close()
with open(FILE_DIR + "/total_balance.txt", "r+") as total_expense:
balance = total_expense.read()
balance = float(balance)
balance -= total_amount
if balance < 0.0:
print("You have crossed balance!! Please add")
total_expense.seek(0)
total_expense.write(str(balance))
total_expense.truncate()
total_expense.close()
except:
print("Wrong input. type help to see commands")
return True
elif main_cmd == "balance":
if Path(FILE_DIR + "/total_balance.txt").is_file():
with open(FILE_DIR + "/total_balance.txt", "r") as total_balance:
balance = total_balance.read()
balance = float(balance.strip("\n"))
print("%.2f" % balance)
if balance < 0.0:
print("Your balance is negative!! Please add")
total_balance.close()
return True
elif main_cmd == "add":
if Path(FILE_DIR + "/total_balance.txt").is_file():
with open(FILE_DIR + "/total_balance.txt", "r+") as total_balance:
balance = total_balance.read()
balance = float(balance)
amounts = cmd[1].replace(",","")
amounts = float(amounts)
balance += amounts
total_balance.seek(0)
total_balance.write(str(balance))
total_balance.truncate()
total_balance.close()
print("Balance Updated! Current balance: {0}".format(balance))
return True
elif main_cmd == "delete":
if Path(FILE_DIR + "/total_balance.txt").is_file():
with open(FILE_DIR + "/total_balance.txt", "r+") as total_balance:
total_balance.write(str(0))
total_balance.truncate()
total_balance.close()
print("Balance deleted!")
return True
elif main_cmd == 'help':
with open(os.getcwd() + "/commands/command.txt", "r") as cmd_file:
for command in cmd_file:
print(command.strip("\n"))
cmd_file.close()
return True
elif main_cmd == "history":
if Path(FILE_DIR + "/daily_expenses.txt").is_file():
with open(FILE_DIR + "/daily_expenses.txt", "r") as daily_expenses:
# for expense in daily_expenses:
# print(expense.strip("\n"))
first_line = daily_expenses.readline().strip("\n")
if first_line == None or len(first_line)==0:
print("No history found!")
else:
print(first_line)
for expense in daily_expenses:
print(expense.strip("\n"))
daily_expenses.close()
return True
elif main_cmd == 'stop':
return False
else:
print("Command not found! Type help to get commands")
return True