Advertisement
Guest User

Untitled

a guest
Dec 8th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 8.00 KB | None | 0 0
  1. file = File.new("Accounts.txt", "a") #load information on startup, and create the file
  2.  
  3. class Account # Making a class for the whole code to call it up later in the code
  4.  
  5.   def new_account # The user method
  6.  
  7.  
  8.     prompt = "> " #creating a class for prompt here, since I use it multiple times
  9.     puts"Insert Username"
  10.     print prompt
  11.     @username = gets.chomp.upcase #A global variabel which i can use outside the class
  12.  
  13.     if File.read("Accounts.txt").include?(@username) #so you don't register the same username, twice
  14.       puts"This username is already taken. Returning you to the main menu."
  15.       puts "_____________________________________________" #Making the code more visuable pretty and more orginasied
  16.     else #If the name is not stored the method contiues
  17.       puts"Insert Password"
  18.       print prompt
  19.       @password = gets.chomp
  20.  
  21.       if File.read("Accounts.txt").include?(@password)#the same as before, so you dont register the same number twice.
  22.  
  23.         puts
  24.         puts "This Password is allready in use. Returning you to the main menu."
  25.         puts "_____________________________________________"
  26.       else
  27.         puts"What is your full name?"
  28.         print prompt
  29.         @name = gets.chomp
  30.         puts
  31.         puts"Thanks for the input."
  32.         puts "_____________________________________________"
  33.  
  34.         puts"Is this information correct? " #Giving the user all the information back to check for mistakes, etc.
  35.         puts
  36.         puts ("Username: #{@username} Password: #{@password} Name: #{@name}") #Loading up the information the user has given and shows it in the code
  37.         puts "_____________________________________________"
  38.  
  39.         puts "Yes or No?" #Gives the user a choice to see if the information is correct
  40.         print prompt
  41.  
  42.         while user_input = gets.chomp.upcase #loop while getting user input, making the input upcase so it is easier to find it in the textfile
  43.           case user_input #Keeps the information
  44.             when "YES" #When the user have written yes it will save the input in the textfile
  45.               file = File.new("Accounts.txt", "a")
  46.               file.puts("Username: #{@username}, Password: #{@password}, Name: #{@name}") #puts the information into the textfile, separeted by commas
  47.               file.close
  48.               puts
  49.               puts "The information has now been stored in the Account.txt file."
  50.               puts "_____________________________________________"
  51.               break # make sure to break so you don't ask again
  52.             when "NO" #When no the info will not be saved and you will be returing to the main menu
  53.               puts
  54.               puts "The information has not been stored. Returning you to the main menu."
  55.               puts "_____________________________________________"
  56.               break
  57.             else #If the user writes something else than 'yes' or 'no' the code will ask for the correct input and will loop untill it has
  58.               puts "Please either write 'Yes' or 'No'"
  59.               print prompt # print the prompt, so the user knows to re-enter input
  60.           end
  61.         end
  62.       end
  63.     end
  64.   end
  65.  
  66.   def search_account(search) #Making a search account method
  67.  
  68.     keyword = File.readlines('Accounts.txt')  #converting all the lines into indexes in an Array
  69.  
  70.  
  71.     matches = keyword.select { |name| name[/#{search}/] } # Making a variable that ask for user imput .select returns the array containing all elements of search for which the given block returns a true value.
  72.  
  73.  
  74.  
  75.     if File.read("Accounts.txt").include?(search) #An if statement that will print the results if the textfile matches the keyword
  76.       puts "_____________________________________________"
  77.       puts ("Search results including the word/username/name " + search + ":") #
  78.       puts
  79.       puts matches
  80.       puts "_____________________________________________"
  81.     else #If not it will give the user feedback that its not there
  82.       puts "_____________________________________________"
  83.       puts
  84.       puts ("Sorry, we couldnt find #{search} in the textfile.")
  85.       puts "_____________________________________________"
  86.     end
  87.   end
  88.  
  89.   def all_Accounts
  90.     puts
  91.     puts "__________Users registered____________"
  92.     puts
  93.     f = File.new("Accounts.txt","r") # reads the textfie and shows what the textfile contais on the consol
  94.     while !(f.eof?)
  95.       line = f.gets()
  96.       puts line
  97.     end
  98.  
  99.   end
  100.  
  101.   def delete_Account
  102.  
  103.     puts("What user do you want to delete?")
  104.     print("> ")
  105.     keyword = gets.chomp.upcase # variable that ask for user input and making it upcase. Easier to find it in textfile
  106.  
  107.  
  108.  
  109.     txt_file = File.readlines('Accounts.txt')
  110.     matches = txt_file.select { |name| name[/#{keyword}/] }
  111.     search_results = matches.length
  112.  
  113.  
  114.  
  115.  
  116.     if search_results > 1
  117.       puts "_____________________________________________"
  118.       puts "The name you entered gave these outputs:"
  119.       puts ""
  120.       puts matches
  121.       puts ""
  122.       puts "Please specify the name better, as we only allow one person to be deleted at the time. \nReturning you to the main menu."
  123.       puts "_____________________________________________"
  124.  
  125.     else
  126.  
  127.       if File.read("Accounts.txt").include?(keyword) #An if statement that will print the results if the textfile matches the person
  128.         puts "_____________________________________________"
  129.         puts ("Is this the person you want to delete?")
  130.         puts
  131.         puts matches
  132.  
  133.         puts "_____________________________________________"
  134.  
  135.         puts "Yes or No?"
  136.         print "> "
  137.  
  138.         while user_input = gets.chomp.upcase # loop while getting user input
  139.           case user_input # A case that keeps the loop with while
  140.             when "YES"
  141.  
  142.               no_matches = txt_file.reject { |name| name[/#{keyword}/] } #variable do not saves the input
  143.  
  144.               File.open('Accounts.txt','w+'){|out| out.puts no_matches}
  145.               puts
  146.               puts"User has been deleted. Returning you to the main menu."
  147.               break # make sure to break so you don't ask again
  148.             when "NO"
  149.               puts
  150.               puts "User will not be deleted. Returning you to the main menu."
  151.               break # and again
  152.             else
  153.               puts "Please either write 'Yes' or 'No'"
  154.               print "> " # print the prompt, so the user knows to re-enter input
  155.           end
  156.         end
  157.         puts "_____________________________________________"
  158.       else #If not it will give the user feedback that its not there
  159.         puts "_____________________________________________"
  160.         puts
  161.         puts ("Sorry, we couldnt find #{keyword} in the textfile.")
  162.         puts "_____________________________________________"
  163.       end
  164.     end
  165.   end
  166. end
  167.  
  168. account = Account.new
  169. require 'io/console' # So you dont need to press enter
  170. select = 0
  171. prompt = "> "
  172. puts
  173. puts
  174. puts "Welcome to the quiz"
  175. puts "_____________________________________________"
  176.  
  177. loop do (select !=  7)
  178. puts
  179. puts("Press 1 to register a new user.\nPress 2 to search for a user or keyword within the textfile.\nPress 3 to show all users.\nPress 4 to delete a user.\nPress 5 to exit.")
  180. puts "_____________________________________________"
  181. select  =   STDIN.getch.to_i
  182.  
  183. if(select == 1)
  184.   account.new_account
  185.  
  186. elsif(select == 2)
  187.   puts("What user/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
  188.   print prompt
  189.   account.search_account(gets.chomp.upcase)
  190.  
  191.   #elsif calling up all methods if u press the keywords
  192.  
  193.  
  194. elsif(select == 3)
  195.  
  196.   account.all_Accounts
  197.   puts "_____________________________________________"
  198.  
  199.  
  200.  
  201.  
  202. elsif(select == 4)
  203.  
  204.   account.delete_Account
  205.  
  206.  
  207. elsif(select == 5)
  208.   puts
  209.   puts "The application will now exit."
  210.   puts "_____________________________________________"
  211.  
  212.   break
  213.  
  214. else
  215.   puts"Invalid input. Please try again."
  216.   puts "_____________________________________________"
  217.  
  218. end
  219.  
  220. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement