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

后端 未结 2 1804
轻奢々
轻奢々 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
    
    0 讨论(0)
  • 2021-01-17 06:35

    There is a lot to say about this code (yours and mine). Run Ruby with the -w option to display unused variables, it points to possible mistakes like :

    $ 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
    

    There are a lot of File.read, I have replaced them by an IO.readlines which creates an array of lines (OK for files which are not Gigabytes big). It also allows to store user numbers.

    As always in Ruby, there are many ways to do the same thing.

    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
    
    0 讨论(0)
提交回复
热议问题