2016-02-26 45 views
-3

我創建了一些基於一些在線教程的會計程序,但該程序似乎不起作用。我對Ruby相當陌生,但我會採取任何幫助和建議,我可以得到!在此先感謝所有!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 
+2

你似乎在你的問題中缺少一個問題。 – Amadan

回答

0
  1. 在你的存款方式,有一個額外的結束,這可能是問題,如果它顯示了你意想不到的問題。

2.Also in rails elseif is elsif

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

更換存款操作,因爲這除去端。

+0

這個方法的真正問題是OP使用'else if'而不是'elsif'。 –

+0

是的好趕上@Jordan – Sravan

+0

改變了所有其他的如果是elsif,擺脫了額外的結束,但它似乎仍然不工作我收到語法錯誤,它似乎是尋找另一端或可能我有另外一個地方?感謝所有幫助到目前爲止的傢伙! –