Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- file = File.new("Accounts.txt", "a") #load information on startup, and create the file
- class Account # Making a class for the whole code to call it up later in the code
- def new_account # The user method
- prompt = "> " #creating a class for prompt here, since I use it multiple times
- puts"Insert Username"
- print prompt
- @username = gets.chomp.upcase #A global variabel which i can use outside the class
- if File.read("Accounts.txt").include?(@username) #so you don't register the same username, twice
- puts"This username is already taken. Returning you to the main menu."
- puts "_____________________________________________" #Making the code more visuable pretty and more orginasied
- else #If the name is not stored the method contiues
- puts"Insert Password"
- print prompt
- @password = gets.chomp
- if File.read("Accounts.txt").include?(@password)#the same as before, so you dont register the same number twice.
- puts
- puts "This Password is allready in use. Returning you to the main menu."
- puts "_____________________________________________"
- else
- puts"What is your full name?"
- print prompt
- @name = gets.chomp
- puts
- puts"Thanks for the input."
- puts "_____________________________________________"
- puts"Is this information correct? " #Giving the user all the information back to check for mistakes, etc.
- puts
- puts ("Username: #{@username} Password: #{@password} Name: #{@name}") #Loading up the information the user has given and shows it in the code
- puts "_____________________________________________"
- puts "Yes or No?" #Gives the user a choice to see if the information is correct
- print prompt
- 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
- case user_input #Keeps the information
- when "YES" #When the user have written yes it will save the input in the textfile
- file = File.new("Accounts.txt", "a")
- file.puts("Username: #{@username}, Password: #{@password}, Name: #{@name}") #puts the information into the textfile, separeted by commas
- file.close
- puts
- puts "The information has now been stored in the Account.txt file."
- puts "_____________________________________________"
- break # make sure to break so you don't ask again
- when "NO" #When no the info will not be saved and you will be returing to the main menu
- puts
- puts "The information has not been stored. Returning you to the main menu."
- puts "_____________________________________________"
- break
- 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
- puts "Please either write 'Yes' or 'No'"
- print prompt # print the prompt, so the user knows to re-enter input
- end
- end
- end
- end
- end
- def search_account(search) #Making a search account method
- keyword = File.readlines('Accounts.txt') #converting all the lines into indexes in an Array
- 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.
- if File.read("Accounts.txt").include?(search) #An if statement that will print the results if the textfile matches the keyword
- puts "_____________________________________________"
- puts ("Search results including the word/username/name " + search + ":") #
- puts
- puts matches
- puts "_____________________________________________"
- else #If not it will give the user feedback that its not there
- puts "_____________________________________________"
- puts
- puts ("Sorry, we couldnt find #{search} in the textfile.")
- puts "_____________________________________________"
- end
- end
- def all_Accounts
- puts
- puts "__________Users registered____________"
- puts
- f = File.new("Accounts.txt","r") # reads the textfie and shows what the textfile contais on the consol
- while !(f.eof?)
- line = f.gets()
- puts line
- end
- end
- def delete_Account
- puts("What user do you want to delete?")
- print("> ")
- keyword = gets.chomp.upcase # variable that ask for user input and making it upcase. Easier to find it in textfile
- txt_file = File.readlines('Accounts.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("Accounts.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
- puts matches
- puts "_____________________________________________"
- puts "Yes or No?"
- print "> "
- while user_input = gets.chomp.upcase # loop while getting user input
- case user_input # A case that keeps the loop with while
- when "YES"
- no_matches = txt_file.reject { |name| name[/#{keyword}/] } #variable do not saves the input
- File.open('Accounts.txt','w+'){|out| out.puts no_matches}
- puts
- puts"User has been deleted. Returning you to the main menu."
- break # make sure to break so you don't ask again
- when "NO"
- puts
- puts "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
- puts "_____________________________________________"
- else #If not it will give the user feedback that its not there
- puts "_____________________________________________"
- puts
- puts ("Sorry, we couldnt find #{keyword} in the textfile.")
- puts "_____________________________________________"
- end
- end
- end
- end
- account = Account.new
- require 'io/console' # So you dont need to press enter
- select = 0
- prompt = "> "
- puts
- puts
- puts "Welcome to the quiz"
- puts "_____________________________________________"
- loop do (select != 7)
- puts
- 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.")
- puts "_____________________________________________"
- select = STDIN.getch.to_i
- if(select == 1)
- account.new_account
- elsif(select == 2)
- 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
- print prompt
- account.search_account(gets.chomp.upcase)
- #elsif calling up all methods if u press the keywords
- elsif(select == 3)
- account.all_Accounts
- puts "_____________________________________________"
- elsif(select == 4)
- account.delete_Account
- elsif(select == 5)
- puts
- puts "The application will now exit."
- puts "_____________________________________________"
- break
- else
- puts"Invalid input. Please try again."
- puts "_____________________________________________"
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement