Ruby rehber uygulaması kodları basit şekilde ruby programlama dilinde rehber uygulamaı yapma kodlarını sizlere paylaşıyorum umarım faydalı olur iyi kodlamalar dilerim.
require_relative 'Contact.rb'
require_relative 'ContactList.rb'
class StoreProgram
def main
list = ContactList.new
index = ""
puts "\nWelcome.\n"
while index != "0"
index = menu(index) # Open Main Menu
list = openSubMenu(index, list) # Open selected Menu
end
puts "\n==EXIT=="
end
def menu(inIndex)
puts "\n\nPlease enter index"
puts "\n 1 - Create New Contact"
puts "\n 2 - Delete Contact"
puts "\n 3 - Sort Contacts by Name"
puts "\n 4 - View List"
puts "\n 0 - Exit"
puts "Enter: "
inIndex = gets.chomp
inIndex # Return index
end
def openSubMenu(inIndex, inList)
case inIndex
when "1" # Create New Contact
inList = createNewContact(inList)
when "2" # Delete Contact
displayList(inList)
inList = removeContact(inList)
when "3" # Sort Contact by Name
# Display sorted list straight away after sort
# (without replacing the original list)
displayList(sortContact(inList))
when "4" # View List
displayList(inList)
when "0" # Exit
# Do nothing, exit loop
else
puts "\n[INVALID INPUT]\n"
end
inList # Return the entire List
end
def createNewContact(inList)
# This method handles new contact creation,
# asks for information, then pushes into the array
puts "\nPlease Enter New Contact Name: "
newContact = Contact.new(gets.chomp)
puts "\nAddress: "
newContact.address = gets.chomp
puts "\nLocation: "
newContact.location = gets.chomp
puts "\nEmail: "
newContact.email = gets.chomp
puts "\nTelephone: "
newContact.telephone = gets.chomp
puts "\nDate of Birth: "
newContact.dob = gets.chomp
puts "\nFavourite?: "
newContact.favourite = gets.chomp
inList.add(newContact) # Add into array
inList # Return the entire list
end
def displayList(inList)
# This function prints the entire array
index = 0
max = inList.counter # Get the highest position in the array
while index < max
puts " [#{index + 1}] - #{inList.contactList(index).name}"
index += 1
end
end
def removeContact(inList)
puts "\n\nPlease enter which contact to delete"
# Get the position of what user input
# Position will be -1 because array starts from 0
inList.remove(inList.contactList((gets.chomp.to_i) - 1))
inList # Return the entire list
end
def sortContact(inList)
sortedList = ContactList.new
# Custom sort (not something special)
sortedList.contactListWhole = inList.contactListWhole.sort { |x, y| x.name <=> y.name }
sortedList.counter = inList.counter
sortedList # Return the entire object
end
end
# Run the Program
runProgram = StoreProgram.new
runProgram.main
Ruby