Ruby - 会计程序问题

时间:2016-02-26 04:39:09

标签: ruby account

我根据一些在线教程创建了一个小会计程序,但该程序似乎不起作用。我对Ruby很新,但我会接受任何帮助和建议!在此先感谢所有人!

class Account
  attr_reader :name, :check_account, :save_account

  def initialize(name, check_account, save_account)
    @name = name
    @check_account = check_account
    @save_account = save_account
  end

  def display_balance(pin_number)
    puts "Enter pin please\n"
    input = gets.chomp

    if input == pin
      main_menu
    else 
      pin_error
    end
  end

  def main_menu
    puts "Welcome back #{name}!\n
    What would you like to do?\n 
      Display balance (1)\n
      Make a withdraw (2)\n
      Make a deposit  (3)\n
      Quit            (4)\n"

    input = gets.chomp

    case option
      when 1 
        balance
      when 2 
        withdraw 
      when 3 
        deposit 
      else 
        exit 
    end 
  end


  def balance 
    puts "Which account do you wish to access? Checking or Savings?\n"
    input = gets.chomp

    if input =~ /checking/i 
      puts "Checking Balance: $#{check_account}."
    else if input =~ /savings/i 
      puts "Savings Balance: $#{save_account}."
    else
      main_menu
    end
    end
  end

  def withdraw(pin_number, amount)
    puts "Enter your PIN please\n"
    input - gets.chomp

    case withdraw
      when check_account
        @check_account -= amount
        puts "You withdrew $#{amount} and now have $#{check_account} in your Checking."

      when save_account
        @save_account -= amount
        puts "You withdrew $#{amount} and now have $#{save_account} in your Savings."
      else 
        pin_error
    end
  end

  def deposit
    puts "What account do you want to deposit into? Checking or Savings?"
    input = gets.chomp

    if input =~ /checking/i 
      @check_account += amount
      puts "You deposited $#{amount} adding up to a total of $#{check_account} in your Checking."
    else if input =~ /savings/i 
      @save_account += amount
      puts "You deposited $#{amount} adding up to a total of $#{save_account} in your Savings."
    else
      main_menu
    end
    end
  end

  private

  def pin
    @pin =1234
  end

  def pin_error
    "Access denied: incorrect PIN."
  end

  def amount_error
    "Access denied: incorrect amount."
  end
end

my_account = Account.new("Andrew", 100_000, 250_000)
my_account.display

1 个答案:

答案 0 :(得分:0)

  1. 在您的存款方式中,还有一个额外的结果,如果它为您显示意外问题,则可能是问题。
  2. 2.另外在导轨elseifelsif

    def deposit
      puts "What account do you want to deposit into? Checking or Savings?"
      input = gets.chomp
      if input =~ /checking/i 
        @check_account += amount
        puts "You deposited $#{amount} adding up to a total of $#{check_account} in your Checking."
      elsif input =~ /savings/i 
        @save_account += amount
        puts "You deposited $#{amount} adding up to a total of $#{save_account} in your Savings."
      else
         main_menu
      end
     end

    将存款操作替换为此移除端。