Ruby 重写ATM程序以需要两个文件

Ruby 重写ATM程序以需要两个文件,ruby,Ruby,我创建了一个ATM程序,并想重写它的某些方面,以需要两个文件,一个包含帐户上的名称和货币信息,另一个包含pin码信息这是工作ATM的当前代码: class Account attr_reader :name, :checking_account, :savings_account, :transfer def initialize(name, checking_account, savings_account) @name = name @checking_account

我创建了一个ATM程序,并想重写它的某些方面,以需要两个文件,一个包含帐户上的名称和货币信息,另一个包含pin码信息这是工作ATM的当前代码:

class Account
  attr_reader :name, :checking_account, :savings_account, :transfer
  def initialize(name, checking_account, savings_account)
    @name = name
    @checking_account = checking_account
    @savings_account = savings_account
    @transfer = transfer
  end


  def pin
      @pin = '1234'
  end


  def display
    puts "Welcome back #{name} enter your PIN:"
    input = gets.chomp
    if input == pin
      main_menu
    else
      bad_pin
  end
end


def main_menu
  puts """
  Welcome back #{name}!
  Follow the instructions as follows:
  To display Balance press '1'
  To make a Withdrawl press '2'
  To make a Deposit press '3'
  To Transfer funds press '4'
  To exit the system press '5'
  """
  input = gets.chomp
  case input
  when '1'
    balance
  when '2'
    withdrawl
  when '3'
    deposit
  when '4'
    transfer
  else
    exit
  end
end


def balance
  puts "Which balance? Checking or Savings?"
  input = gets.chomp
  if input =~ /checking/i
    puts "Your balance for your Checking Account is: $#{checking_account}."
    main_menu
  elsif input =~ /savings/i
    puts "Your balance for your Savings Account is: $#{savings_account}."
    main_menu
  else
    main_menu
  end
end


def withdrawl
  puts "Please press '1' to make a withdrawl from checking. Press '2' to make a withdrawl from Savings. Press '3' to go back to the menu."
  input = gets.chomp
  case input
  when '1'
    puts "How much would you like to withdrawl?"
    input = gets.chomp!
    @checking_account -= input.to_f
    puts "You have withdrawn $#{input}; you now have $#{checking_account} in your checking. Thank you for using this ATM you will be redirected to the main menu."
    main_menu
  when '2'
    puts "How much would you like to withdraw?"
    input = gets.chomp!
    @savings_account -= input.to_f
    puts "You have withdrawn ${input}; you now have $#{savings_account} in your savings. Thank you for using this ATM you will be redirected to the main menu"
    main_menu
  else
    main_menu
  end
end


def deposit
  puts "Which account would you like to deposit into: Checkings(1), or Savings(2)?"
  input = gets.chomp
  if input == '1'
    puts "Enter amount to deposit: "
    amount = gets.chomp!
    @checking_account += amount.to_f
    puts "You have made a depost of $#{amount} leaving you with $#{checking_account}. Thank you for using this ATM. You will be redirected to the main menu."
    main_menu
  elsif input == '2'
    puts "Enter amount to deposit: "
    amount = gets.chomp!
    @savings_account += amount.to_f
    puts "You have made a deposit of $#{amount} leaving you with $#{savings_account}. Thank you for using this ATM. You will be redirected to the main menu."
    main_menu
  else
    main_menu
  end
end


def transfer 
  puts "Would you like to transfer funds from Checking(1) or Savings(2)?"
  input = gets.chomp
  if input == '1'
    puts "Enter amount to transfer from Checking to Savings: Current balance #{checking_account}."
    amount = gets.chomp
    @checking_account -= amount.to_f
    @savings_account += amount.to_f
    puts "You have successfully transfered #{amount} from your Checking account new balance: #{checking_account}. Savings balance: #{savings_account}. You will be redirected to the main menu."
    main_menu
  elsif input == '2'
    puts "Enter amount to transfer from Savings to Checking: Current balance #{savings_account}."
    amount = gets.chomp
    @savings_account -= amount.to_f
    @checking_account += amount.to_f
    puts "You have succesfully transfered #{amount} from your Savings account new balance: #{savings_account}. Checking account balance: #{checking_account}. You will be redirected to the main menu."
    main_menu
  else
    puts "Invalid entry"
    exit
  end
end


def bad_pin
  puts "Access Denied: incorrect PIN"
    exit
  end
end

my_account = Account.new("Thomas", 500_000, 750_000)
my_account.display
我要做的是使用两个单独的文件,names.text和pins.text,一个保存帐户名和accounts informationnames.txt,另一个保存accountpins.txt的pin号

名称.文本,即:

my_account = Account.new("Thomas", 500_000, 750_000)
my_account.display

my_account2 = Account.new("Jake", 50_000, 50_000)    
my_account.display

my_account3 = Account.new("Anothername", 10, 50)
my_account.display
带有pin numberspins.txt的文件,我想应该是这样的,如果不是,请告诉我:

my_account @pin = 1234
my_account2 @pin = 2345
my_account3 @pin = 3456
因此,我的问题是:

为了从文件中获取信息,除了明显的pin方法之外,我需要在哪里重写这段代码。 我需要使用require还是require\u relative?
您如何将帐户与PIN关联?除非您计划使用索引,否则2个单独的文件将需要更多的标识符。由于这显然只是一个概念性的问题,安全性不是一个问题,而且您没有使用数据库,因此我建议您使用YAML而不是文本文件,因为它可以轻松地加载、解析和更新。或者可能会对您有所帮助。您将帐户与pin关联是什么意思?我会调查YAML谢谢。我已经调查过了,我更希望从文件中得到它。我真的需要练习这样做。好的,那么回到我最初的问题,你打算如何将帐户与Pin关联它将基于文件的行,因为根据你当前的示例,帐户文件是不需要的,因为你正在以任何方式将帐户信息存储在Pin文件中这就是我试图弄明白的,我希望能够根据给定的pin调用不同的帐户,但我也希望pin远离帐户信息存储。我从我的示例中猜测,这就是我存储PIN信息的方式,但我不知道。。