-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
109 lines (102 loc) · 2.77 KB
/
app.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
require "json"
require_relative 'mullet'
response = ""
total_price = 0;
path = "./mullets.json"
mullets = []
data = []
if File.exist?(path)
data = JSON.parse(File.read(path))
end
data.each do |d|
mullets << mullet = Mullet.new(d)
total_price += mullet.price
end
loop{
system "clear"
puts response if !response.empty?
puts "1. 新增烏魚子"
puts "2. 刪除烏魚子"
puts "3. 總計"
puts "4. 儲存"
puts "5. 清空"
puts "e. 離開"
print "輸入選項:"
user_action = gets.chomp!
case user_action.upcase
when "1"
print "\t請輸入烏魚子幾兩 : "
weight = gets.chomp!.to_f
if weight==0
response = "輸入錯誤,請重新輸入。"
next
end
mullet = Mullet.new(weight)
print "\t新增烏魚子:\n\t\t#{mullet.weight}兩/脾\t價格:#{mullet.price}$\t確定?(y/n)"
confirm = gets.chomp!
if confirm.upcase == "Y"
mullets << mullet
total_price += mullet.price
response = "新增烏魚子:\t#{mullet.weight}兩/脾\t價格:#{mullet.price}$\t總共:#{total_price}$"
else
response = "取消"
end
when "2"
mullet_total = mullets.length
if mullet_total == 0
response = "目前沒有任何烏魚子記錄。"
next
end
print "\t請輸入想刪除第幾筆烏魚子 : "
mullet_no = gets.chomp!.to_i
if (1..mullet_total).include?(mullet_no)
print "\t刪除烏魚子:\n\t\t#{mullets[mullet_no-1].weight}兩/脾\t價格:#{mullets[mullet_no-1].price}$\t確定?(y/n)"
confirm = gets.chomp!
if confirm.upcase == "Y"
mullet = mullets.delete_at(mullet_no-1)
total_price -= mullet.price
response = "刪除烏魚子:\t#{mullet.weight}兩/脾\t價格:#{mullet.price}$\t總共:#{total_price}$"
else
response = "取消"
end
else
response = "沒有烏魚子#{mullet_no}。"
end
when "3"
response = "總計:\t#{total_price}$\t#{mullets.length}脾\n"
mullets.each.with_index do |m,idx|
response += "\t烏魚子 #{idx+1}\t#{m.weight}兩/脾\t價格:#{m.price}$\n"
end
response +="\n"
when "4"
print "\t將資料儲存於檔案#{File.path(path)}\t確定?(y/n)"
confirm = gets.chomp!
if confirm.upcase == "Y"
data = []
mullets.each do |m|
data << m.weight
end
File.open(path,"w") do |f|
f.write(JSON.pretty_generate(data))
end
response = "已經存檔"
else
response = "取消"
end
when "5"
print "\t刪除所有烏魚子\t確定?(y/n)"
confirm = gets.chomp!
if confirm.upcase == "Y"
mullets = []
total_price = 0
response = "已經刪除所有烏魚子"
else
response = "取消"
end
when "E"
puts response="再見"
break
else
response="沒有此選項"
end
}