I trying to make a code that gives the user a personal number after they have made an user

后端 未结 2 1828
轻奢々
轻奢々 2021-01-17 06:04

Here is my ruby code. When you run it and you press 1 it will ask you for name and birth of date. I want to give the user a personal number after he is finished typing name

2条回答
  •  生来不讨喜
    2021-01-17 06:34

    I am not sure how you want the numbers generated, but something like this could work.

    This will always give you the highest number from your text file, and add 1 to it.

    Keep in mind this method will be slow with a rather large amount of employees.

    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
    

    Output:

    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  
    

    You can also use a similar method for searching through an array:

    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
    

提交回复
热议问题