cash_register 0.0.0 → 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/cash_register.rb +129 -0
- data/spec/spec_helper.rb +14 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9903c93d23b99d9e29386368753bec17026c5956
|
4
|
+
data.tar.gz: 04a4bedb120c2eb8f2575c7e528dc37ffb732412
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4e2e8f0cee10be2c4eed68f3524b55926d70fed5999ce007934a355d0955055fd7702ce3e3b45919c059ee8a6edaf45098d9cd19283582f6332e6981e284848a
|
7
|
+
data.tar.gz: 3b29e11e52b9ad3c181eff69e19d1ec17d86175a6bfc9bfb8b8dc829bc719db4fbf1c1c5fdb2bf42350c03fb72ded45f1235e50cc04e8fac24bda1ba5c624959
|
data/lib/cash_register.rb
CHANGED
@@ -1,2 +1,131 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
require 'json'
|
1
3
|
class CashRegister
|
4
|
+
@input_array = [
|
5
|
+
'ITEM000001',
|
6
|
+
'ITEM000001',
|
7
|
+
'ITEM000001',
|
8
|
+
'ITEM000001',
|
9
|
+
'ITEM000001',
|
10
|
+
'ITEM000003-2',
|
11
|
+
'ITEM000005',
|
12
|
+
'ITEM000005',
|
13
|
+
'ITEM000005'
|
14
|
+
]
|
15
|
+
SHOP_NAME = '没钱赚商店'
|
16
|
+
|
17
|
+
#价格单位为百分之一分
|
18
|
+
DEAL_INFOS = {
|
19
|
+
'ITEM000001': {
|
20
|
+
name: '可口可乐',
|
21
|
+
price: 30000,
|
22
|
+
unit: '瓶'
|
23
|
+
},
|
24
|
+
'ITEM000002': {
|
25
|
+
name: '羽毛球',
|
26
|
+
price: 10000,
|
27
|
+
unit: '个'
|
28
|
+
},
|
29
|
+
'ITEM000003': {
|
30
|
+
name: '苹果',
|
31
|
+
price: 55000,
|
32
|
+
unit: '斤'
|
33
|
+
},
|
34
|
+
'ITEM000004': {
|
35
|
+
name: '牛奶',
|
36
|
+
price: 50000,
|
37
|
+
unit: '瓶'
|
38
|
+
},
|
39
|
+
'ITEM000005': {
|
40
|
+
name: '包子',
|
41
|
+
price: 20000,
|
42
|
+
unit: '个'
|
43
|
+
}
|
44
|
+
}
|
45
|
+
#普通商品
|
46
|
+
DEAL_NORMAL = 0
|
47
|
+
#买二赠一商品
|
48
|
+
DEAL_TWO_FOR_ONE = 1
|
49
|
+
#打折商品
|
50
|
+
DEAL_DISCOUNT = 2
|
51
|
+
|
52
|
+
ON_SALE = {
|
53
|
+
TWO_FOR_ONE: [
|
54
|
+
'ITEM000001',
|
55
|
+
'ITEM000002'
|
56
|
+
],
|
57
|
+
DISCOUNT: [
|
58
|
+
'ITEM000002',
|
59
|
+
'ITEM000003'
|
60
|
+
]
|
61
|
+
}
|
62
|
+
|
63
|
+
class << self
|
64
|
+
#***<没钱赚商店>购物清单***
|
65
|
+
#名称:可口可乐,数量:3瓶,单价:3.00(元),小计:6.00(元)
|
66
|
+
#名称:羽毛球,数量:5个,单价:1.00(元),小计:4.00(元)
|
67
|
+
#名称:苹果,数量:2斤,单价:5.50(元),小计:11.00(元)
|
68
|
+
#----------------------
|
69
|
+
#买二赠一商品:
|
70
|
+
#名称:可口可乐,数量:1瓶
|
71
|
+
#名称:羽毛球,数量:1个
|
72
|
+
#----------------------
|
73
|
+
#总计:21.00(元)
|
74
|
+
#节省:4.00(元)
|
75
|
+
#**********************
|
76
|
+
def output_bill
|
77
|
+
data_to_print = calculate(@input_array)
|
78
|
+
p data_to_print
|
79
|
+
end
|
80
|
+
|
81
|
+
|
82
|
+
def calculate(deals = [])
|
83
|
+
out_data = []
|
84
|
+
deals = deals.inject({}) do |mem, deal|
|
85
|
+
deal_id, deal_count = deal.split('-')
|
86
|
+
if mem[deal_id].nil?
|
87
|
+
mem[deal_id] = 0
|
88
|
+
end
|
89
|
+
if deal_count.nil?
|
90
|
+
mem[deal_id] += 1
|
91
|
+
else
|
92
|
+
mem[deal_id] += deal_count.to_i
|
93
|
+
end
|
94
|
+
mem
|
95
|
+
end
|
96
|
+
deals.each do |deal, count|
|
97
|
+
deal_type = get_deal_type(deal)
|
98
|
+
p deal_type
|
99
|
+
out_data << calculate_item(deal, count, deal_type)
|
100
|
+
end
|
101
|
+
out_data
|
102
|
+
end
|
103
|
+
|
104
|
+
def calculate_item(deal_id = '', count = 0, deal_type = 0)
|
105
|
+
result = {}
|
106
|
+
deal_info = DEAL_INFOS[deal_id.to_sym]
|
107
|
+
case deal_type
|
108
|
+
when DEAL_TWO_FOR_ONE
|
109
|
+
result[:free_num] = count/2
|
110
|
+
result[:billing] = (count - count/2)*deal_info[:price]
|
111
|
+
when DEAL_DISCOUNT
|
112
|
+
result[:saving] = (count*deal_info[:price]*0.05).to_i
|
113
|
+
result[:billing] = count*deal_info[:price] - result[:saving]
|
114
|
+
else
|
115
|
+
result[:billing] = count*deal_info[:price]
|
116
|
+
end
|
117
|
+
result.merge!(deal_info)
|
118
|
+
result
|
119
|
+
end
|
120
|
+
|
121
|
+
def get_deal_type(deal = '')
|
122
|
+
if ON_SALE[:TWO_FOR_ONE].include?(deal)
|
123
|
+
DEAL_TWO_FOR_ONE
|
124
|
+
elsif ON_SALE[:DISCOUNT].include?(deal)
|
125
|
+
DEAL_DISCOUNT
|
126
|
+
else
|
127
|
+
DEAL_NORMAL
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
2
131
|
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
ENV["RAILS_ENV"] ||= 'test'
|
2
|
+
require 'rubygems'
|
3
|
+
require 'bundler/setup'
|
4
|
+
|
5
|
+
Dir["#{File.dirname(__FILE__)}/../lib/*.rb"].sort.each { |f| require f }
|
6
|
+
|
7
|
+
RSpec.describe CashRegister do
|
8
|
+
it "sums the price" do
|
9
|
+
|
10
|
+
bill = CashRegister.output_bill
|
11
|
+
|
12
|
+
expect(bill.length).to eq(3)
|
13
|
+
end
|
14
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cash_register
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Clark King
|
@@ -31,6 +31,7 @@ extensions: []
|
|
31
31
|
extra_rdoc_files: []
|
32
32
|
files:
|
33
33
|
- lib/cash_register.rb
|
34
|
+
- spec/spec_helper.rb
|
34
35
|
homepage: https://github.com/wskongdesheng/homework-kongdesheng
|
35
36
|
licenses:
|
36
37
|
- MIT
|
@@ -55,4 +56,5 @@ rubygems_version: 2.4.5.1
|
|
55
56
|
signing_key:
|
56
57
|
specification_version: 4
|
57
58
|
summary: cash register
|
58
|
-
test_files:
|
59
|
+
test_files:
|
60
|
+
- spec/spec_helper.rb
|