-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rb
181 lines (130 loc) · 4.53 KB
/
main.rb
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
require './lib/user'
require './lib/query'
require './lib/spending'
# sizes for dates
DAY = 'day'
MONTH = 'month'
YEAR = 'year'
def print_spendings spendings
spendings.each do |spending|
print spending['date'].to_s + ' ' if spending['date'] != nil
print spending['category'].to_s + ' '
puts spending['price'].to_s + '$'
end
end
while 2 + 2 == 4
puts '1. Register'
puts '2. Login'
puts '3. Exit'
command_from_guest = gets.chomp
puts '============================'
if command_from_guest == '1'
print 'Username(one word): '
username = gets.chomp
if (!User.is_username_empty? username) && (!Query.new.is_username_taken? username)
print 'Password: '
password = gets.chomp
Query.new.register User.new username, password
puts 'Register was successful.'
else
if Query.new.is_username_taken? username
puts 'Username is already taken.'
else
puts 'Username requires at least one character!'
end
end
elsif command_from_guest == '2'
print 'Username: '
username = gets.chomp
print 'Password: '
password = gets.chomp
if Query.new.is_login_succed? username, password
while 2 - 2 == 0
puts '============================'
puts '1. Add spending'
puts '2. Get spending statistics'
puts '3. Get spending statistics by category'
puts '4. Get spending statistics by day'
puts '5. Get spending statistics by month'
puts '6. Get spending statistics by year'
puts '7. Delete all spendings'
puts '8. Delete account and all spendings'
puts '9. Logout'
command_from_user = gets.chomp
puts '============================'
if command_from_user == '1'
puts "Date: yyyy.mm.dd\n"\
"Category: food, housing etc\n"\
"Price: 18.87$\n"\
"For example: \'2022.08.28 food 15\'"
date_cat_price = gets.strip.downcase.split(/\ /)
if (Spending.is_date_correct? date_cat_price[0], DAY) && (Spending.is_price_correct? date_cat_price[2])
Query.new.add_spending Spending.new date_cat_price[0], date_cat_price[1], date_cat_price[2]
puts "Spending added."
else
puts "Input wrong format. Try 'yyyy.mm.dd category price'"
end
elsif command_from_user == '2'
spendings = Query.new.spending_statistics
print_spendings spendings
elsif command_from_user == '3'
print 'Enter category: '
category = gets.chomp.downcase
spendings = Query.new.spending_statistics_by_category category
print_spendings spendings
elsif command_from_user == '4'
print 'Enter yyyy.mm.dd: '
day = gets.chomp
if Spending.is_date_correct? day, DAY
spendings = Query.new.spending_statistics_by_day day
print_spendings spendings
else
puts 'Input wrong format'
end
elsif command_from_user == '5'
puts 'Enter yyyy.mm:'
month = gets.chomp
if Spending.is_date_correct? month, MONTH
spendings = Query.new.spending_statistics_by_month month
print_spendings spendings
else
puts 'Input wrong format'
end
elsif command_from_user == '6'
puts 'Enter yyyy:'
year = gets.chomp
if Spending.is_date_correct? year, YEAR
spendings = Query.new.spending_statistics_by_year year
print_spendings spendings
else
puts 'Input wrong format'
end
elsif command_from_user == '7'
print 'Are you sure to delete all spendings?(y/n): '
ans = gets.chomp.downcase
if ans == 'y' || ans == 'yes'
Query.new.delete_spendings
puts 'All spendings was deleted'
end
elsif command_from_user == '8'
print 'Are you sure to delete your account and all spendings?(y/n): '
ans = gets.chomp.downcase
if ans == 'y' || ans == 'yes'
Query.new.delete_account_and_spendings
puts 'Your account and all spendings were deleted. You are logout.'
break
end
elsif command_from_user == '9'
Query.new.logout
puts 'You are logout.'
break
end
end
else
puts 'Username or password incorrect!'
end
elsif command_from_guest == '3'
puts 'Goodbye...'
break
end
end