cash_register 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9903c93d23b99d9e29386368753bec17026c5956
4
- data.tar.gz: 04a4bedb120c2eb8f2575c7e528dc37ffb732412
3
+ metadata.gz: c4074732f9cf3422faad31d4e81e62448670e1bc
4
+ data.tar.gz: 843e5d59a862ff02401ee747bc6492f0de7a5a96
5
5
  SHA512:
6
- metadata.gz: 4e2e8f0cee10be2c4eed68f3524b55926d70fed5999ce007934a355d0955055fd7702ce3e3b45919c059ee8a6edaf45098d9cd19283582f6332e6981e284848a
7
- data.tar.gz: 3b29e11e52b9ad3c181eff69e19d1ec17d86175a6bfc9bfb8b8dc829bc719db4fbf1c1c5fdb2bf42350c03fb72ded45f1235e50cc04e8fac24bda1ba5c624959
6
+ metadata.gz: 44bdbf55fac56939ec6a8fced720f6ca83f8ba7a9db374d972b1b22ec9bbc1224084a19f4eaa900a88469b9b122deba18b8f432516207a021da5f5dee4f312f8
7
+ data.tar.gz: 97cb9a01f84869a0c05d9818fa1fee482b9f8b94b70cc5140e5cdd32cdf8c8a0e3e3d1abb6c303d6734f2a8cad59298b4e74ab0b0f6c763fabb4594e3c9a3289
data/lib/cash_register.rb CHANGED
@@ -1,17 +1,5 @@
1
1
  # encoding: UTF-8
2
- require 'json'
3
2
  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
3
  SHOP_NAME = '没钱赚商店'
16
4
 
17
5
  #价格单位为百分之一分
@@ -63,21 +51,57 @@ class CashRegister
63
51
  class << self
64
52
  #***<没钱赚商店>购物清单***
65
53
  #名称:可口可乐,数量:3瓶,单价:3.00(元),小计:6.00(元)
66
- #名称:羽毛球,数量:5个,单价:1.00(元),小计:4.00(元)
67
- #名称:苹果,数量:2斤,单价:5.50(元),小计:11.00(元)
54
+ #名称:羽毛球,数量:6个,单价:1.00(元),小计:4.00(元)
55
+ #名称:苹果,数量:2斤,单价:5.50(元),小计:10.45(元),节省0.55(元)
68
56
  #----------------------
69
57
  #买二赠一商品:
70
58
  #名称:可口可乐,数量:1瓶
71
59
  #名称:羽毛球,数量:1个
72
60
  #----------------------
73
- #总计:21.00(元)
74
- #节省:4.00(元)
61
+ #总计:20.45(元)
62
+ #节省:4.55(元)
75
63
  #**********************
76
- def output_bill
77
- data_to_print = calculate(@input_array)
64
+ def output_bill(input_array = [])
65
+ data_to_print = calculate(input_array)
78
66
  p data_to_print
67
+ output = "***<#{SHOP_NAME}>购物清单***\n"
68
+ two4one_data = []
69
+ total_price = 0
70
+ total_saving = 0
71
+ data_to_print.each do |data|
72
+ output += "名称:#{data[:name]},数量:#{data[:count]}#{data[:unit]},单价:#{get_price data[:price]}(元),"
73
+ if data[:saving] && !data[:free_num]
74
+ output += "小计:#{get_price data[:billing]}(元),节省:#{get_price data[:saving]}(元)\n"
75
+ else
76
+ output += "小计:#{get_price data[:billing]}(元)\n"
77
+ end
78
+ if data[:free_num]
79
+ two4one_data << data
80
+ end
81
+ total_price += data[:billing]
82
+ total_saving += data[:saving].to_i
83
+ end
84
+ p total_saving
85
+ if !two4one_data.empty?
86
+ output += '-'*22 + "\n"
87
+ output += "买二赠一商品:\n"
88
+ end
89
+ two4one_data.each do |data|
90
+ output += "名称:#{data[:name]},数量:#{data[:free_num]}#{data[:unit]}\n"
91
+ end
92
+ output += '-'*22 + "\n"
93
+ output += "总计:#{get_price total_price}(元)\n"
94
+ if total_saving > 0
95
+ output += "节省:#{get_price total_saving}(元)\n"
96
+ end
97
+ output += "*"*22
98
+ puts output
99
+ output
79
100
  end
80
101
 
102
+ def get_price(price)
103
+ format('%.2f', price.to_f/10000)
104
+ end
81
105
 
82
106
  def calculate(deals = [])
83
107
  out_data = []
@@ -95,7 +119,6 @@ class CashRegister
95
119
  end
96
120
  deals.each do |deal, count|
97
121
  deal_type = get_deal_type(deal)
98
- p deal_type
99
122
  out_data << calculate_item(deal, count, deal_type)
100
123
  end
101
124
  out_data
@@ -104,10 +127,12 @@ class CashRegister
104
127
  def calculate_item(deal_id = '', count = 0, deal_type = 0)
105
128
  result = {}
106
129
  deal_info = DEAL_INFOS[deal_id.to_sym]
130
+ result[:count] = count
107
131
  case deal_type
108
132
  when DEAL_TWO_FOR_ONE
109
- result[:free_num] = count/2
110
- result[:billing] = (count - count/2)*deal_info[:price]
133
+ result[:free_num] = count/3
134
+ result[:billing] = (count - count/3)*deal_info[:price]
135
+ result[:saving] = (count/3)*deal_info[:price]
111
136
  when DEAL_DISCOUNT
112
137
  result[:saving] = (count*deal_info[:price]*0.05).to_i
113
138
  result[:billing] = count*deal_info[:price] - result[:saving]
@@ -0,0 +1,48 @@
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
+ before(:example) do
9
+ @two4one_deals = [
10
+ 'ITEM000001',
11
+ 'ITEM000001',
12
+ 'ITEM000001',
13
+ 'ITEM000001',
14
+ 'ITEM000001',
15
+ ]
16
+ @two4one_deals2 = [
17
+ 'ITEM000001',
18
+ 'ITEM000001',
19
+ 'ITEM000001',
20
+ 'ITEM000001',
21
+ 'ITEM000001',
22
+ 'ITEM000001',
23
+ 'ITEM000001',
24
+ ]
25
+ @discount_deals = [
26
+ 'ITEM000003-2'
27
+ ]
28
+ end
29
+
30
+ context 'input with only two-for-one deals' do
31
+ it "one free" do
32
+ bill = CashRegister.output_bill(@two4one_deals)
33
+ expect(bill).to include('名称:可口可乐,数量:1瓶')
34
+ end
35
+
36
+ it "more than one" do
37
+ bill = CashRegister.output_bill(@two4one_deals2)
38
+ expect(bill).to include('名称:可口可乐,数量:2瓶')
39
+ end
40
+ end
41
+
42
+ context 'input with only discount deals' do
43
+ it 'discount deals' do
44
+ bill = CashRegister.output_bill(@discount_deals)
45
+ expect(bill).to include('名称:苹果,数量:2斤,单价:5.50(元),小计:10.45(元),节省:0.55(元)')
46
+ end
47
+ end
48
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,14 +1,96 @@
1
- ENV["RAILS_ENV"] ||= 'test'
2
- require 'rubygems'
3
- require 'bundler/setup'
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # The generated `.rspec` file contains `--require spec_helper` which will cause
4
+ # this file to always be loaded, without a need to explicitly require it in any
5
+ # files.
6
+ #
7
+ # Given that it is always loaded, you are encouraged to keep this file as
8
+ # light-weight as possible. Requiring heavyweight dependencies from this file
9
+ # will add to the boot time of your test suite on EVERY test run, even for an
10
+ # individual file that may not need all of that loaded. Instead, consider making
11
+ # a separate helper file that requires the additional dependencies and performs
12
+ # the additional setup, and require it from the spec files that actually need
13
+ # it.
14
+ #
15
+ # The `.rspec` file also contains a few flags that are not defaults but that
16
+ # users commonly want.
17
+ #
18
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
19
+ RSpec.configure do |config|
20
+ # rspec-expectations config goes here. You can use an alternate
21
+ # assertion/expectation library such as wrong or the stdlib/minitest
22
+ # assertions if you prefer.
23
+ config.expect_with :rspec do |expectations|
24
+ # This option will default to `true` in RSpec 4. It makes the `description`
25
+ # and `failure_message` of custom matchers include text for helper methods
26
+ # defined using `chain`, e.g.:
27
+ # be_bigger_than(2).and_smaller_than(4).description
28
+ # # => "be bigger than 2 and smaller than 4"
29
+ # ...rather than:
30
+ # # => "be bigger than 2"
31
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
32
+ end
4
33
 
5
- Dir["#{File.dirname(__FILE__)}/../lib/*.rb"].sort.each { |f| require f }
34
+ # rspec-mocks config goes here. You can use an alternate test double
35
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
36
+ config.mock_with :rspec do |mocks|
37
+ # Prevents you from mocking or stubbing a method that does not exist on
38
+ # a real object. This is generally recommended, and will default to
39
+ # `true` in RSpec 4.
40
+ mocks.verify_partial_doubles = true
41
+ end
6
42
 
7
- RSpec.describe CashRegister do
8
- it "sums the price" do
43
+ # The settings below are suggested to provide a good initial experience
44
+ # with RSpec, but feel free to customize to your heart's content.
45
+ =begin
46
+ # These two settings work together to allow you to limit a spec run
47
+ # to individual examples or groups you care about by tagging them with
48
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
49
+ # get run.
50
+ config.filter_run :focus
51
+ config.run_all_when_everything_filtered = true
9
52
 
10
- bill = CashRegister.output_bill
53
+ # Allows RSpec to persist some state between runs in order to support
54
+ # the `--only-failures` and `--next-failure` CLI options. We recommend
55
+ # you configure your source control system to ignore this file.
56
+ config.example_status_persistence_file_path = "spec/examples.txt"
11
57
 
12
- expect(bill.length).to eq(3)
13
- end
58
+ # Limits the available syntax to the non-monkey patched syntax that is
59
+ # recommended. For more details, see:
60
+ # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
61
+ # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
62
+ # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
63
+ config.disable_monkey_patching!
64
+
65
+ # This setting enables warnings. It's recommended, but in some cases may
66
+ # be too noisy due to issues in dependencies.
67
+ config.warnings = true
68
+
69
+ # Many RSpec users commonly either run the entire suite or an individual
70
+ # file, and it's useful to allow more verbose output when running an
71
+ # individual spec file.
72
+ if config.files_to_run.one?
73
+ # Use the documentation formatter for detailed output,
74
+ # unless a formatter has already been configured
75
+ # (e.g. via a command-line flag).
76
+ config.default_formatter = 'doc'
77
+ end
78
+
79
+ # Print the 10 slowest examples and example groups at the
80
+ # end of the spec run, to help surface which specs are running
81
+ # particularly slow.
82
+ config.profile_examples = 10
83
+
84
+ # Run specs in random order to surface order dependencies. If you find an
85
+ # order dependency and want to debug it, you can fix the order by providing
86
+ # the seed, which is printed after each run.
87
+ # --seed 1234
88
+ config.order = :random
89
+
90
+ # Seed global randomization in this process using the `--seed` CLI option.
91
+ # Setting this allows you to use `--seed` to deterministically reproduce
92
+ # test failures related to randomization by passing the same `--seed` value
93
+ # as the one that triggered the failure.
94
+ Kernel.srand config.seed
95
+ =end
14
96
  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.1
4
+ version: 0.0.2
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/cash_register_spec.rb
34
35
  - spec/spec_helper.rb
35
36
  homepage: https://github.com/wskongdesheng/homework-kongdesheng
36
37
  licenses:
@@ -58,3 +59,4 @@ specification_version: 4
58
59
  summary: cash register
59
60
  test_files:
60
61
  - spec/spec_helper.rb
62
+ - spec/cash_register_spec.rb