Last active
December 24, 2015 01:19
-
-
Save quackhouse/6722553 to your computer and use it in GitHub Desktop.
HashBook - Beginnings of a Facebook-like (but super basic) social network. Storing capabilities for username, name, password, etc. Search function and ability to store a message to a user's "wall". No options for display of message, while loops in progress. Flow is not circular (no way to get back to home menu).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def ask(question) | |
# Defines method for asking questions and receiving input. | |
puts question | |
gets.chomp! | |
end | |
def get_name | |
puts "What is your full name?" | |
full_name = gets.chomp! | |
end | |
def sign_up(get_name) | |
puts "Enter your username:" | |
username = gets.chomp.downcase! | |
puts "Enter your password (case sensitive):" | |
password = gets.chomp! | |
full_name_array = get_name.split(" ").to_a | |
puts "What are your favorite hobbies?" | |
hobbies = gets.chomp! | |
puts "What is your gender?" | |
gender = gets.chomp! | |
user_wall = [] | |
hash_name = { | |
username: username, | |
password: password, | |
first_name: full_name_array.first, | |
last_name: full_name_array.last, | |
hobbies: hobbies, | |
gender: gender, | |
user_wall: user_wall} | |
return hash_name | |
end | |
def log_in(database) | |
puts "Please enter your username:" | |
username = gets.chomp.downcase! | |
hash_key = username.to_s | |
if database[hash_key][:username] != username | |
puts "That username does not exist." | |
else | |
puts "Please enter your password (case sensitive):" | |
password = gets.chomp | |
if database[hash_key][:password] == password | |
puts "Welcome to HashBook" | |
menu_run = false | |
next_step_run = true | |
else | |
puts "Password incorrect. Back to main menu." | |
end | |
end | |
end | |
def search_for_hash(database) | |
loop_question = true | |
while loop_question == true | |
puts "Please enter the full name of the friend you'd like to search for." | |
friend_full_name = gets.chomp! | |
friend_full_name.downcase! | |
if friend_full_name.index(" ") == nil | |
puts "I'm sorry, I don't understand. I need more than one name to search." | |
else | |
friend_full_name_array = friend_full_name.split(" ").to_a | |
friend_first_name = friend_full_name_array.first | |
friend_last_name = friend_full_name_array.last | |
loop_question = false | |
end | |
end# | |
found_first_name = false | |
find_last_name = false | |
first_name_exists = false | |
found_last_name = false | |
while found_first_name == false | |
database.each_key do |key| | |
if database[key][:first_name] == friend_first_name | |
first_name_exists = true | |
end | |
end | |
if first_name_exists == true | |
puts "Searching..." | |
find_last_name = true | |
else | |
return nil | |
end | |
found_first_name = true | |
end | |
while find_last_name == true | |
database.each_key do |key| | |
if database[key][:last_name] == friend_last_name | |
friend_hash = database[key] | |
found_last_name = true | |
end | |
end | |
if found_last_name != true | |
return nil | |
end | |
find_last_name = false | |
end | |
end | |
def actual_search(database) | |
friend_hash = search_for_hash(database) | |
end | |
database = {} | |
menu_run = true | |
friend_menu = false | |
next_step_run = false | |
sign_in_or_quit = false | |
while menu_run == true | |
puts "If you would like to sign up, please type 'signup'" | |
puts "If you would like to log in, please type 'login'" | |
puts "If you would like to search for a friend, type 'search'" | |
puts "If you want to quit, type 'quit'" | |
home_page_q = ask("Please type your choice now:") | |
case home_page_q | |
when "signup" | |
full_name = get_name | |
new_hash = sign_up(full_name) | |
hash_key = new_hash[:username].to_s | |
database[hash_key] = new_hash | |
puts "Welcome, #{full_name}. Thanks for signing up." | |
menu_run = false | |
next_step_run = true | |
when "login" | |
log_in(database) | |
menu_run = false | |
next_step_run = true | |
when "search" | |
@friend_hash = actual_search(database) | |
if @friend_hash != nil | |
friend_menu = true | |
puts "#{friend_hash[:first_name]} #{friend_hash[:last_name]}\'s wall." | |
friend_menu = true | |
else | |
puts "Nobody with that name exists on HashBook" | |
end | |
when "quit" | |
menu_run = false | |
else | |
puts "Hey, that's not an option!" | |
end | |
end | |
if next_step_run == true | |
puts "What would you like to do now?" | |
puts "Type 'search' to search for a friend, 'signout' to sign out or 'quit' to quit." | |
next_move = gets.chomp | |
next_move.downcase! | |
if next_move == "signout" | |
puts "bye!" | |
sign_in_or_quit = true | |
elsif next_move == "search" | |
@friend_hash = actual_search(database) | |
if @friend_hash != nil | |
friend_menu = true | |
puts "Ok, found #{@friend_hash[:first_name]}\'s wall." | |
else | |
puts "I couldn't find that person. Shutting down!" | |
end | |
elsif next_move == "quit" | |
puts "bye!" | |
else | |
puts "what?" | |
end | |
end | |
if sign_in_or_quit == true | |
puts "Type 'signin' to sign back in, or 'quit' to quit." | |
user_choice = gets.chomp | |
user_choice.downcase! | |
if user_choice == "signin" | |
log_in(database) | |
elsif user_choice == "quit" | |
puts "bye!" | |
else | |
puts "I'm sorry, I don't understand." | |
end | |
end | |
if friend_menu == true | |
puts "Type 'write' to write on their wall, or 'quit' to quit!" | |
user_choice = gets.chomp | |
if user_choice == "quit" | |
puts "bye!" | |
elsif user_choice == 'write' | |
puts "Write your message, and then press enter!" | |
message = gets.chomp! | |
@friend_hash[:user_wall] << message | |
puts "Ok, we'll send that!" | |
else | |
puts "I don't understand that choice. BYE!" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment