我已经创建了一个ATM程序,并希望重写它的某些方面需要两个文件,一个包含帐户上的名称和货币信息,另一个包含引脚号信息,这里是工作的当前代码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
一个保存帐户名和帐户信息(names.txt),另一个保存帐户的密码( pins.txt)。
names.text I.E。:
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
带有引脚号(pins.txt)的文件我认为看起来像这样(如果不是请赐教):
my_account @pin = 1234
my_account2 @pin = 2345
my_account3 @pin = 3456
所以我的问题是:
pin
方法之外,我还需要在哪里重写此代码才能从文件中获取信息。require
或require_relative
?