@anonymous/

ReliableUnevenHamadryas

Ruby

No description

fork
loading
Files
  • main.rb
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
class Transaction
  attr_accessor :amount, :type, :timestamp

  def initialize(amount, type)
    raise 'Invalid type' unless POSSIBLE_TYPES.include? type
    @amount = amount
    @type = type
    @timestamp = Time.now.getutc
  end

  def value
    return @amount if type == :deposit
    [email protected]
  end

  def to_s
    "#{@timestamp} - #{type} - #{amount} USD"
  end

  private
  POSSIBLE_TYPES = [:deposit, :withdraw]
end

class Account
  def initialize
    @transactions = []
  end

  def balance
    @transactions.inject(0) { |sum, transaction| sum + transaction.value }
  end

  def deposit(amount)
    add_transaction(amount, :deposit)
  end

  def withdraw(amount)
    add_transaction(amount, :withdraw)
  end

  def statement
    @transactions.map { |transaction| transaction.to_s }
  end

  private
  attr_accessor :transactions

  def add_transaction(amount, type)
    @transactions << Transaction.new(amount, type)
  end
end

account = Account.new

loop do
  puts "What you want to do?\n1- Deposit\n2- Withdraw\n3- Check balance\n4- Check statement\n5- Exit"
  option = gets.chomp
  case option
  when '1'
    puts "How many dollars do you want to deposit?"
    amount = gets.chomp.to_f
    account.deposit(amount)
  when '2'
    puts "How many dollars do you want to withdraw?"
    amount = gets.chomp.to_f
    account.withdraw(amount)
  when '3'
    puts "You have #{account.balance} USD."
  when '4'
    puts account.statement.join("\n")
  when '5'
    break
  else
    "Wrong option. Try again."
  end
end
ruby 2.5.5p157 (2019-03-15 revision 67260) [x86_64-linux]