Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/53.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 我试图制作一个代码,在用户创建一个用户后,给用户一个个人号码_Ruby On Rails_Ruby - Fatal编程技术网

Ruby on rails 我试图制作一个代码,在用户创建一个用户后,给用户一个个人号码

Ruby on rails 我试图制作一个代码,在用户创建一个用户后,给用户一个个人号码,ruby-on-rails,ruby,Ruby On Rails,Ruby,这是我的ruby代码。当您运行它并按1时,它会询问您的姓名和出生日期。我想在用户输入完姓名和出生日期后给他一个个人号码。此外,我会很高兴搜索用户号码,以找到他们在文件中。希望有人能帮忙 我写了一封信:“如果我需要帮助,这里有帮助。”。代码运行良好,但我不知道如何编码我的问题 file = File.new("Capgemini.txt", "a") #load information on startup, and create the file class Customer

这是我的ruby代码。当您运行它并按1时,它会询问您的姓名和出生日期。我想在用户输入完姓名和出生日期后给他一个个人号码。此外,我会很高兴搜索用户号码,以找到他们在文件中。希望有人能帮忙

我写了一封信:“如果我需要帮助,这里有帮助。”。代码运行良好,但我不知道如何编码我的问题

file = File.new("Capgemini.txt", "a") #load information on startup, and create the file

        class Customer # Making a class for the whole code to call it up later in the code

            def new_custom

    er # The costumer method 


                prompt = "> " #creating a class for prompt here, since I use it multiple times
                puts"Full name of the person?"
                print prompt
                @name = gets.chomp.upcase #A global variabel which i can use outside the class

                if File.read("Capgemini.txt").include?(@name) #so you don't register the same name, twice
                    puts"This name is already stored. Returning you to the main menu."
                    puts "_____________________________________________"
                    else
                #puts"What is your employee number?"
                #print prompt
                #@number = gets.chomp #Global
                puts"Date of birth? (DD/MM/YYYY)"
                print prompt
                @birth = gets.chomp #Global 

                puts"Thanks for the input."
                 puts "_____________________________________________"

                puts"Is this information correct? " #Giving the user all the information back to check for mistakes, etc.

                puts ("Name: #{@name} Number: #{@number} Date of birth: #{@birth}")
                puts "_____________________________________________"

                puts "Yes or No?"
                print prompt

                while user_input = gets.chomp.upcase #loop while getting user input
                    case user_input
                    when "YES"
                    file = File.new("Capgemini.txt", "a")
                    file.puts("#{@name}, Number: #{@number}, Date of birth: #{@birth}") #puts the information into the textfile, separeted by commas
                    file.close 

                    #NEED HELP HERE

                    number = File.readlines('Capgemini.txt')


                    puts "_____________________________________________"
                    puts
                    puts "Your employee number: "
                    puts "_____________________________________________"


                    #NEED HELP OVER HERE^





                    puts
                    puts "The information has now been stored in the Capgemini.txt file."
                    puts "_____________________________________________"
                    break # make sure to break so you don't ask again
                    when "NO"
                    puts "The information has not been stored. Returning you to the main menu."
                    puts "_____________________________________________"
                    break # and again
                    else
                    puts "Please either write 'Yes' or 'No'"
                    print prompt # print the prompt, so the user knows to re-enter input
                    end
                    end
                end
        end

        def search_customer(search) 

                keyword = File.readlines('Capgemini.txt')  #converting all the lines into indexes in an Array



                matches = keyword.select { |name| name[/#{search}/] } #



                if File.read("Capgemini.txt").include?(search) #An if statement that will print the results if the textfile matches the keyword
                    puts "_____________________________________________"            
                    puts ("Search results including the word/number/birth " + search + ":")
                    puts "_____________________________________________"

                    puts matches
                    puts "_____________________________________________"
                    else #If not it will give the user feedback that its not there
                    puts "_____________________________________________"
                    puts ("Sorry, we couldnt find #{search} in the textfile.") 
                    puts "_____________________________________________"
                end
        end

        def all_customers

            f = File.new("Capgemini.txt","r")
            while !(f.eof?)
                line = f.gets()
                puts line
    end

        end

        def delete_customer

                puts("What customer do you want to delete?")
                print("> ")
                keyword = gets.chomp.upcase



                txt_file = File.readlines('Capgemini.txt')
                matches = txt_file.select { |name| name[/#{keyword}/] }
                search_results = matches.length




                if search_results > 1
                    puts "_____________________________________________"    
                    puts "The name you entered gave these outputs:"
                    puts ""
                    puts matches
                    puts ""
                    puts "Please specify the name better, as we only allow one person to be deleted at the time. \nReturning you to the main menu."
                    puts "_____________________________________________"    

                    else

                if File.read("Capgemini.txt").include?(keyword) #An if statement that will print the results if the textfile matches the person
                    puts "_____________________________________________"            
                    puts ("Is this the person you want to delete?")
                    puts matches

                    puts "_____________________________________________"

                    puts "Yes or No?"
                print "> "

                while user_input = gets.chomp.upcase # loop while getting user input
                    case user_input
                    when "YES"

                    no_matches = txt_file.reject { |name| name[/#{keyword}/] }

                    File.open('Capgemini.txt','w+'){|out| out.puts no_matches}
                    puts"User has been deleted. Returning you to the main menu."


                    puts "_____________________________________________"
                    break # make sure to break so you don't ask again
                    when "NO"
                    puts "User will not be deleted. Returning you to the main menu."
                    puts "_____________________________________________"
                    break # and again
                    else
                    puts "Please either write 'Yes' or 'No'"
                    print "> " # print the prompt, so the user knows to re-enter input
                    end
                    end


                    puts "_____________________________________________"
                    else #If not it will give the user feedback that its not there
                    puts "_____________________________________________"
                    puts ("Sorry, we couldnt find #{keyword} in the textfile.") 
                    puts "_____________________________________________"
                end
                end

            end

    end

    customer = Customer.new 
    require 'io/console'
    select = 0
    prompt = "> "
    puts
    puts
    puts "Welcome to Capgemini Sogeti Denmark"
    puts "_____________________________________________"

    loop do (select !=  7)
      puts
        puts("Press 1 to register a new user.\nPress 2 to search for a employee or keyword within the textfile.\nPress 3 to show all customers.\nPress 4 to delete a customer.\nPress 5 to exit.")
        puts "_____________________________________________"
        select  =   STDIN.getch.to_i

        if(select == 1)
            customer.new_customer

        elsif(select == 2)
        puts("What customer/keyword do you want to search for?") #You can search for a keyword, like forexample 'Manzur' which will prompt you back with every user names Manzur
        print prompt
        customer.search_customer(gets.chomp.upcase)


        elsif(select == 3)

        customer.all_customers
        puts "_____________________________________________"




        elsif(select == 4)

        customer.delete_customer


        elsif(select == 5)
        puts 
        puts "The application will now exit."
        puts "_____________________________________________"

        break

        else
        puts"Invalid input. Please try again."
        puts "_____________________________________________"

        end

    end

我不确定您希望如何生成这些数字,但类似的方法可能会奏效

这将始终为您提供文本文件中的最大数字,并向其中添加1

请记住,对于大量员工来说,这种方法会很慢

if File.exist?('Capgemini.txt')
    number = File.readlines('Capgemini.txt')
    @number = 1
    number.each do |x|
      customer = x.split(',')
      customer_number = customer[1].gsub('Number: ', '').to_i
      if customer_number >= @number
        @number = customer_number + 1
      end
    end
else
  @number = 1
end
输出:

BOB ROSS, Number: 1, Date of birth: 07/07/2007  
WILL SMITH, Number: 2, Date of birth: 08/08/2008  
JIM BOB, Number: 3, Date of birth: 09/09/2009  
您也可以使用类似的方法搜索数组:

number = File.readlines('Capgemini.txt')
number.each do |x|
  customer = x.split(',')
  customer_name = customer[0]
  customer_number = customer[1].gsub('Number: ', '').to_i
  customer_bday = customer[2].gsub('Date of birth: ', '')

  if customer_name == some_variable
    puts x
  end
end

关于这段代码(你的和我的)有很多话要说。使用
-w
选项运行Ruby以显示未使用的变量,它指出可能的错误,如:

$ ruby -w t_op.rb
t_op.rb:180: warning: mismatched indentations at 'end' with 'class' at 3
t_op.rb:191: warning: possibly useless use of != in void context
t_op.rb:1: warning: assigned but unused variable - file
有很多
File.read
,我用一个
IO.readlines
代替了它们,它创建了一个行数组(对于不是千兆字节大的文件可以)。它还允许存储用户号码

在Ruby中,有很多方法可以做同样的事情

require 'io/console'

class Customer
    attr_reader :file

    def initialize(p_file_name)
        @file_name   = p_file_name
        refresh
        @next_number = @numbers.max + 1
    end

    def refresh
        @lines   = IO.readlines(@file_name) # load information on startup
        @numbers = []
        @names   = @lines.collect do | line |
            # the line has the format : <name>, Number: <number>, Date of birth: <birth>
            idxn = line.index(', Number')
            idxd = line.index(', Date')
            @numbers << line[idxn + 10...idxd].to_i
            line[0...idxn]
        end
    end

    def new_customer
        prompt = "> " # creating a local variable for prompt, since I use it multiple times
        puts 'Full name of the person ?'
        print prompt
        name = gets.chomp.upcase

        if @names.include?(name) #so you don't register the same name, twice
        then
            puts_underlined 'This name is already stored. Returning you to the main menu.'
        else
            puts 'Date of birth? (DD/MM/YYYY)'
            print prompt
            birth = gets.chomp # TODO check validity
            puts_underlined 'Thanks for the input.'

            puts 'Is this information correct ?' # Giving the user all the information back to check for mistakes, etc.
            puts_underlined "Name: #{name} Number: #{@next_number} Date of birth: #{birth}"
            puts 'Y(es) or N(o)'
            print prompt

            while user_input = gets.chomp.upcase #loop while getting user input
                case user_input[0]
                when 'Y'
                    line = "#{name}, Number: #{@next_number}, Date of birth: #{birth}"
                    @next_number +=1
                    @lines << line
                    File.open(@file_name, 'a') do | file | # open the file for append, ensure it will be closed by the end of the block
                        file.puts line # puts the information into the textfile, separeted by commas
                    end
                    puts
                    puts_underlined "The information has now been stored in the #{@file_name} file."
                    break # make sure to break so you don't ask again
                when 'N'
                    puts_underlined 'The information has not been stored. Returning you to the main menu.'
                    break # and again
                else
                    puts 'Please either write Y(es) or N(o)'
                    print prompt # print the prompt, so the user knows to re-enter input
                end
            end
        end
    end # new_customer

    def search_customer(search) 
        matches = @lines.grep(/#{search}/)

        unless matches.empty? # An if statement that will print the results if the textfile matches the keyword
            puts_underlined()
            puts_underlined "Search results including the word/number/birth #{search} :"
            puts matches
            puts_underlined()
        else # If not it will give the user feedback that it's not there
            puts_underlined()
            puts_underlined "Sorry, we couldnt find #{search} in the text file."
        end
    end

    def search_customer_number(search) 
        index = @numbers.index(search.to_i)

        if index
        then # found, print the user
            puts_underlined()
            puts_underlined "This is the user number #{search} :"
            puts @lines[index]
            puts_underlined()
        else # not found, it will give the user feedback that it's not there
            puts_underlined()
            puts_underlined "Sorry, we couldnt find the user #{search}."
        end
    end

    def all_customers
        puts @lines
    end

    def delete_customer
        puts 'Which customer do you want to delete ?'
        print '> '
        keyword = gets.chomp.upcase
        matches = @lines.grep(/#{keyword}/)

        case matches.size
        when 0 # not found, give the user feedback that it's not there
            puts_underlined()
            puts_underlined "Sorry, we couldnt find #{keyword} in the textfile."
        when 1 # print the results if the textfile matches the person
            puts_underlined()
            puts 'Is this the person you want to delete ?'
            puts_underlined matches
            puts 'Yes or No?'
            print '> '

            while user_input = gets.chomp.upcase # loop while getting user input
                case user_input
                when 'YES'
                    no_matches = @lines.reject { | line | line[/#{keyword}/] }
                    File.open(@file_name, 'w+') { | out | out.puts no_matches }
                    refresh
                    puts_underlined 'User has been deleted. Returning you to the main menu.'
                    break # make sure to break so you don't ask again
                when 'NO'
                    puts_underlined 'User will not be deleted. Returning you to the main menu.'
                    break # and again
                else
                    puts "Please either write 'Yes' or 'No'"
                    print '> ' # print the prompt, so the user knows to re-enter input
                end
            end
        else
            puts_underlined()
            puts 'The name you entered gave these outputs:'
            puts
            puts matches
            puts
            puts_underlined "Please specify the name better, as we only allow one person to be deleted at the time. \nReturning you to the main menu."
        end
    end # delete_customer
end # class Customer

def puts_underlined(p_text = nil)
    puts p_text if p_text
    puts '_____________________________________________'
end

file_name = 'Capgemini.txt'
customer  = Customer.new(file_name)
prompt    = "> "
puts
puts_underlined 'Welcome to Capgemini Sogeti Denmark'

loop do
    puts
    puts_underlined "Press 1 to register a new user.\nPress 2 to search for a employee.\nPress 3 to search for a keyword within the textfile.\nPress 4 to show all customers.\nPress 5 to delete a customer.\nPress 6 to exit."
    select = STDIN.getch.to_i

    case select
    when 1
        customer.new_customer
    when 2
        puts 'Which customer number do you want to search for ?'
        print prompt
        customer.search_customer_number(gets.chomp.upcase)
    when 3
        puts 'What keyword do you want to search for ?' # You can search for a keyword, like for example 'Manzur' which will prompt you back with every user names Manzur
        print prompt
        customer.search_customer(gets.chomp.upcase)
    when 4
        customer.all_customers
        puts_underlined()
    when 5
        customer.delete_customer
    when 6
        puts 
        puts_underlined 'The application will now exit.'
        break
    else
        puts_underlined 'Invalid input. Please try again.'
    end
end
需要“io/控制台”
类客户
属性读取器:文件
def初始化(p_文件名)
@文件名=文件名
刷新
@next_number=@numbers.max+1
结束
def刷新
@lines=IO.readlines(@file_name)#启动时加载信息
@数字=[]
@name=@lines.collect do | line|
#行的格式为:,编号:,出生日期:
idxn=line.index(',Number')
idxd=line.index('日期')
@数字
当用户输入=get.chomp.upcase时,在获取用户输入时循环
案例用户输入
什么时候“是”
无匹配项=@lines.reject{| line | line[/#{keyword}/]}
open(@File_name,'w+'){| out | out.put无匹配项}
刷新
已删除带下划线的用户。返回主菜单。”
休息——一定要休息,这样你就不会再问了
什么时候“不”
将不会删除带下划线的用户。返回主菜单。”
断断续续
其他的
写上“请写‘是’或‘否’”
打印“>”#打印提示,以便用户知道如何重新输入
结束
结束
其他的
放上下划线()
将“输入的名称赋予这些输出:”
放
放火柴
放
加下划线“请更好地指定姓名,因为我们一次只允许删除一个人。\n请转到主菜单。”
结束
结束#删除#客户
末级客户
def将_加下划线(p_text=nil)
如果p_文本,则放置p_文本
放入“\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
结束
文件名='Capgemini.txt'
customer=customer.new(文件名)
prompt=“>”
放
加下划线“欢迎来到丹麦凯捷酒店”
环道
放
加下划线“按1注册新用户。\n按2搜索员工。\n按3搜索文本文件中的关键字。\n按4显示所有客户。\n按5删除客户。\n按6退出。”
选择=STDIN.getch.to_i
案例选择
当1
顾客,新顾客
当2
输入“您要搜索哪个客户号码?”
打印提示
customer.search\u customer\u number(get.chomp.upcase)
当3
输入“您想搜索什么关键字?”#您可以搜索关键字,例如“Manzur”,它会提示您返回每个用户名Manzur
打印提示
customer.search\u customer(get.chomp.upcase)
当4
顾客,所有的顾客
放上下划线()
5岁时
customer.delete_客户
当6
放
在“应用程序现在将退出”下加下划线
打破
其他的
在“无效输入”下加下划线。请再试一次
结束
结束

Small note:标签传达语言。没有必要在标题中重复这一点。它在任何形状或形式上都不像Rails。这只是一堆常规Ruby代码。