Skip to content

Instantly share code, notes, and snippets.

@saintmedusa
Last active February 7, 2020 17:28
Show Gist options
  • Save saintmedusa/9757caf910d7c46a26b7aece88326caf to your computer and use it in GitHub Desktop.
Save saintmedusa/9757caf910d7c46a26b7aece88326caf to your computer and use it in GitHub Desktop.
input_choice = nil
student_data = Array.new
file_data = nil
# Introduce Program to user, select manual or file mode, and
# initiate the array of full_names accordingly.
puts "Welcome to Ada's Student Account Generator."
puts "Would you like to enter student names manually or by file?
Enter 'M' for manually or 'F' for file:"
input_choice = gets.chomp.upcase()
if input_choice == "F"
puts "What is the name of your text file, extension included?
Note: make sure your text file has no trailing empty lines."
# Open data file with open.
file_name = gets.chomp
file_data = File.open(file_name)
student_data = Array.new(File.foreach(file_name).count)
end
if input_choice == "M"
puts "How many names will you enter?"
student_data = Array.new(gets.chomp.to_i)
end
# Times loop to get student names (if entering manually), create
# ID numbers, generate student emails, and print full account info
(student_data.length).times do |i|
# initialize hash for individual account
acc_hash = Hash.new
# finalize full_names
if input_choice == "M"
puts "enter student name:"
acc_hash[:full_name] = gets.chomp.upcase()
elsif input_choice == "F"
acc_hash[:full_name] = file_data.readline().chomp.upcase()
end
# generate ID #
acc_hash[:id] = rand(111111..999999)
# generate email
fnl_names = acc_hash[:full_name].split(" ")
f_initial = fnl_names[0][0,1]
if fnl_names.length > 2
f_initial << fnl_names[1][0,1]
end
acc_hash[:email] = f_initial + fnl_names.last + acc_hash[:id].to_s[3,5] + "@adadevelopersacademy.org"
# assign to student_data array before moving on to next student
student_data[i] = acc_hash
end
# Print all students' generated account info
(student_data.length).times do |i|
puts student_data[i][:full_name] + ": ID - " + student_data[i][:id].to_s + ", email - " + student_data[i][:email] + "\n"
end
@CheezItMan
Copy link

Account Generator - Using Hashes

Requirement Comments
Well formatted code πŸ‘ Good indentation and meaningful variable names. I also like the comments breaking up each section.
Creates an array of hashes with keys for names, ID numbers and email addresses πŸ‘
Reads in Student names πŸ‘
Generates random ID numbers πŸ‘
Generates proper student email addresses using the (first initial) + (last name) + (last 3 digits of the ID number)@adadevelopersacademy.org πŸ‘
Prints out student accounts πŸ‘
Optional Prevents duplicates NA
Optional Handles small last 3 digits of an ID number πŸ‘
Optional Handles first names with spaces (initials MJ for "Mary Jane") πŸ‘ , for 3-word names
Optional Reads input from a file πŸ‘ , nice work

Summary

Great work, you hit all the learning goals here. I hope your 1st week at Ada is going well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment