Last active
February 7, 2020 17:28
-
-
Save saintmedusa/9757caf910d7c46a26b7aece88326caf to your computer and use it in GitHub Desktop.
This file contains 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
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Account Generator - Using Hashes
Optional
Prevents duplicatesOptional
Handles small last 3 digits of an ID numberOptional
Handles first names with spaces (initials MJ for "Mary Jane")Optional
Reads input from a fileSummary
Great work, you hit all the learning goals here. I hope your 1st week at Ada is going well.