Skip to content

Commit c6bb565

Browse files
author
Danny_Lai
committed
Initial comment
0 parents  commit c6bb565

File tree

15 files changed

+591
-0
lines changed

15 files changed

+591
-0
lines changed

.bundle/config

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
---
2+
BUNDLE_BIN: "bin"

.rspec

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
--color
2+
--require spec_helper

.vscode/launch.json

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "Debug Local File",
9+
"type": "Ruby",
10+
"request": "launch",
11+
"cwd": "${workspaceRoot}",
12+
"program": "${workspaceRoot}/main.rb"
13+
},
14+
{
15+
"name": "Listen for rdebug-ide",
16+
"type": "Ruby",
17+
"request": "attach",
18+
"cwd": "${workspaceRoot}",
19+
"remoteHost": "127.0.0.1",
20+
"remotePort": "1234",
21+
"remoteWorkspaceRoot": "${workspaceRoot}"
22+
},
23+
{
24+
"name": "Rails server",
25+
"type": "Ruby",
26+
"request": "launch",
27+
"cwd": "${workspaceRoot}",
28+
"program": "${workspaceRoot}/bin/rails",
29+
"args": [
30+
"server"
31+
]
32+
},
33+
{
34+
"name": "RSpec - all",
35+
"type": "Ruby",
36+
"request": "launch",
37+
"cwd": "${workspaceRoot}",
38+
"program": "${workspaceRoot}/bin/rspec",
39+
"args": [
40+
"-I",
41+
"${workspaceRoot}"
42+
]
43+
},
44+
{
45+
"name": "RSpec - active spec file only",
46+
"type": "Ruby",
47+
"request": "launch",
48+
"cwd": "${workspaceRoot}",
49+
"program": "${workspaceRoot}/bin/rspec",
50+
"args": [
51+
"-I",
52+
"${workspaceRoot}",
53+
"${file}"
54+
]
55+
},
56+
{
57+
"name": "Cucumber",
58+
"type": "Ruby",
59+
"request": "launch",
60+
"cwd": "${workspaceRoot}",
61+
"program": "${workspaceRoot}/bin/cucumber"
62+
}
63+
]
64+
}

.vscode/settings.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{
2+
}

Gemfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
source "https://rubygems.org"
2+
3+
gem 'rspec'

account.rb

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
require './enums.rb'
2+
require './transaction.rb'
3+
require 'thread'
4+
5+
class Account
6+
include OperationResult
7+
8+
def initialize(name, balance = 0)
9+
@name = name
10+
@balance = balance
11+
@trans_history = []
12+
@mutex = Mutex.new
13+
end
14+
15+
public
16+
def get_balance
17+
@balance
18+
end
19+
20+
def desposit(trans)
21+
@mutex.synchronize do
22+
if trans.amount <= 0
23+
puts "Input amount '#{trans.amount}' invalid"
24+
return INVALIDINPUT
25+
end
26+
27+
@balance += trans.amount
28+
puts "Account: #{@name}, desposit: #{trans.amount}, balance: #{@balance}"
29+
# add to history
30+
@trans_history.push(trans)
31+
SUCCEEDED
32+
end
33+
34+
end
35+
36+
def withdraw(trans)
37+
@mutex.synchronize do
38+
if @balance >= trans.amount
39+
@balance -= trans.amount
40+
puts "Account: #{@name}, withdraw: #{trans.amount}, balance: #{@balance}"
41+
# add to history
42+
@trans_history.push(trans)
43+
return SUCCEEDED
44+
end
45+
46+
puts "Account: #{@name}, withdraw failed: #{trans.amount}, balance: #{@balance} not enough"
47+
FAILED
48+
end
49+
end
50+
51+
def show_balance_info
52+
puts "Account:#{@name}, balance: #{@balance}"
53+
return SUCCEEDED
54+
end
55+
56+
def show_history(count)
57+
if !(count.is_a? Integer) || count < 1
58+
puts "Input '#{count}' invalid"
59+
return INVALIDINPUT
60+
end
61+
62+
if !@trans_history.any?
63+
puts 'History was empty'
64+
return HISTORYEMPTY
65+
end
66+
67+
puts 'History:'
68+
puts '-----------------------------------------'
69+
@trans_history[0..count - 1].each_with_index do
70+
|t, i| puts "[#{i + 1}] Date: #{t.date}, amount: #{t.amount}, op-code: #{t.code}, memo: #{t.memo}"
71+
72+
end
73+
puts '-----------------------------------------'
74+
return SUCCEEDED
75+
end
76+
77+
end

bin/bundle

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
3+
4+
#
5+
# This file was generated by Bundler.
6+
#
7+
# The application 'bundle' is installed as part of a gem, and
8+
# this file is here to facilitate running it.
9+
#
10+
11+
require "rubygems"
12+
13+
m = Module.new do
14+
module_function
15+
16+
def invoked_as_script?
17+
File.expand_path($0) == File.expand_path(__FILE__)
18+
end
19+
20+
def env_var_version
21+
ENV["BUNDLER_VERSION"]
22+
end
23+
24+
def cli_arg_version
25+
return unless invoked_as_script? # don't want to hijack other binstubs
26+
return unless "update".start_with?(ARGV.first || " ") # must be running `bundle update`
27+
bundler_version = nil
28+
update_index = nil
29+
ARGV.each_with_index do |a, i|
30+
if update_index && update_index.succ == i && a =~ Gem::Version::ANCHORED_VERSION_PATTERN
31+
bundler_version = a
32+
end
33+
next unless a =~ /\A--bundler(?:[= ](#{Gem::Version::VERSION_PATTERN}))?\z/
34+
bundler_version = $1 || ">= 0.a"
35+
update_index = i
36+
end
37+
bundler_version
38+
end
39+
40+
def gemfile
41+
gemfile = ENV["BUNDLE_GEMFILE"]
42+
return gemfile if gemfile && !gemfile.empty?
43+
44+
File.expand_path("../../Gemfile", __FILE__)
45+
end
46+
47+
def lockfile
48+
lockfile =
49+
case File.basename(gemfile)
50+
when "gems.rb" then gemfile.sub(/\.rb$/, gemfile)
51+
else "#{gemfile}.lock"
52+
end
53+
File.expand_path(lockfile)
54+
end
55+
56+
def lockfile_version
57+
return unless File.file?(lockfile)
58+
lockfile_contents = File.read(lockfile)
59+
return unless lockfile_contents =~ /\n\nBUNDLED WITH\n\s{2,}(#{Gem::Version::VERSION_PATTERN})\n/
60+
Regexp.last_match(1)
61+
end
62+
63+
def bundler_version
64+
@bundler_version ||= begin
65+
env_var_version || cli_arg_version ||
66+
lockfile_version || "#{Gem::Requirement.default}.a"
67+
end
68+
end
69+
70+
def load_bundler!
71+
ENV["BUNDLE_GEMFILE"] ||= gemfile
72+
73+
# must dup string for RG < 1.8 compatibility
74+
activate_bundler(bundler_version.dup)
75+
end
76+
77+
def activate_bundler(bundler_version)
78+
if Gem::Version.correct?(bundler_version) && Gem::Version.new(bundler_version).release < Gem::Version.new("2.0")
79+
bundler_version = "< 2"
80+
end
81+
gem_error = activation_error_handling do
82+
gem "bundler", bundler_version
83+
end
84+
return if gem_error.nil?
85+
require_error = activation_error_handling do
86+
require "bundler/version"
87+
end
88+
return if require_error.nil? && Gem::Requirement.new(bundler_version).satisfied_by?(Gem::Version.new(Bundler::VERSION))
89+
warn "Activating bundler (#{bundler_version}) failed:\n#{gem_error.message}\n\nTo install the version of bundler this project requires, run `gem install bundler -v '#{bundler_version}'`"
90+
exit 42
91+
end
92+
93+
def activation_error_handling
94+
yield
95+
nil
96+
rescue StandardError, LoadError => e
97+
e
98+
end
99+
end
100+
101+
m.load_bundler!
102+
103+
if m.invoked_as_script?
104+
load Gem.bin_path("bundler", "bundle")
105+
end

bin/htmldiff

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
3+
4+
#
5+
# This file was generated by Bundler.
6+
#
7+
# The application 'htmldiff' is installed as part of a gem, and
8+
# this file is here to facilitate running it.
9+
#
10+
11+
require "pathname"
12+
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
13+
Pathname.new(__FILE__).realpath)
14+
15+
bundle_binstub = File.expand_path("../bundle", __FILE__)
16+
17+
if File.file?(bundle_binstub)
18+
if File.read(bundle_binstub, 150) =~ /This file was generated by Bundler/
19+
load(bundle_binstub)
20+
else
21+
abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
22+
Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
23+
end
24+
end
25+
26+
require "rubygems"
27+
require "bundler/setup"
28+
29+
load Gem.bin_path("diff-lcs", "htmldiff")

bin/ldiff

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
3+
4+
#
5+
# This file was generated by Bundler.
6+
#
7+
# The application 'ldiff' is installed as part of a gem, and
8+
# this file is here to facilitate running it.
9+
#
10+
11+
require "pathname"
12+
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
13+
Pathname.new(__FILE__).realpath)
14+
15+
bundle_binstub = File.expand_path("../bundle", __FILE__)
16+
17+
if File.file?(bundle_binstub)
18+
if File.read(bundle_binstub, 150) =~ /This file was generated by Bundler/
19+
load(bundle_binstub)
20+
else
21+
abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
22+
Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
23+
end
24+
end
25+
26+
require "rubygems"
27+
require "bundler/setup"
28+
29+
load Gem.bin_path("diff-lcs", "ldiff")

bin/rspec

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
3+
4+
#
5+
# This file was generated by Bundler.
6+
#
7+
# The application 'rspec' is installed as part of a gem, and
8+
# this file is here to facilitate running it.
9+
#
10+
11+
require "pathname"
12+
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
13+
Pathname.new(__FILE__).realpath)
14+
15+
bundle_binstub = File.expand_path("../bundle", __FILE__)
16+
17+
if File.file?(bundle_binstub)
18+
if File.read(bundle_binstub, 150) =~ /This file was generated by Bundler/
19+
load(bundle_binstub)
20+
else
21+
abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
22+
Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
23+
end
24+
end
25+
26+
require "rubygems"
27+
require "bundler/setup"
28+
29+
load Gem.bin_path("rspec-core", "rspec")

0 commit comments

Comments
 (0)