-
-
Save ovargas27/852247 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
require './user.rb' | |
def mainmenu | |
puts 'select an option please | |
1.- new user | |
2.- delete user | |
3.- consult user | |
4.- exit | |
type a number: ' | |
option=gets.chomp | |
case option | |
when '1' then create_new_user | |
when '2' then delete_user | |
when '3' then consult_user | |
when '4' then Process.exit | |
end | |
end | |
def create_new_user | |
user = User.new | |
p 'name' | |
user.name = gets.chomp | |
p'age' | |
user.age = gets.chomp | |
p'phone' | |
user.phone = gets.chomp | |
p'address' | |
user.address=gets.chomp | |
user.save | |
mainmenu | |
end | |
def delete_user | |
puts 'please type the name of the user' | |
name = gets.chomp | |
user = User.find_by_name name | |
user.delete | |
mainmenu | |
end | |
def consult_user | |
puts 'please type the name of the user' | |
name = gets.chomp | |
user = User.find_by_name name | |
if user | |
puts ">> User: #{user}" | |
else | |
puts">> User #{name} doesn't exist" | |
end | |
mainmenu | |
end | |
mainmenu |
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
eduardo | 23 | 3124567 | sanisidro 23 | |
---|---|---|---|---|
arturo | 32 | 3245678 | la calua 78 | |
jorge | 45 | 2345678 | javier mina 4 | |
josue | 5000000 | 310000000 | jerusalem 111 |
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
class User | |
attr_accessor :name, :age, :phone, :address | |
def self.filepath | |
'data.csv' | |
end | |
def filepath | |
User.filepath | |
end | |
def self.new(args=nil) | |
user = super | |
unless args.nil? or args.empty? | |
if args.is_a? Hash | |
user.name, user.age, user.phone, user.address = args[:name], args[:age], args[:phone], args[:adrress] | |
elsif args.is_a? Array | |
user.name, user.age, user.phone, user.address = args[0], args[1], args[2], args[3] | |
end | |
end | |
user | |
end | |
def save | |
File.open(filepath,'a') do |file| | |
file.puts("#{self.name}, #{self.age}, #{self.phone}, #{self.address}") | |
end | |
end | |
def self.find_by_name(name) | |
File.open(filepath,'r') do |file| | |
user_line = file.select{|line| line.start_with? name }.first | |
user_array = user_line.chop.split(',') if user_line | |
User.new( user_array ) if user_array | |
end | |
end | |
def delete | |
file_content =[] | |
File.open(filepath,'r') do |file| | |
file_content = file.select{|line| !line.start_with?(@name) }.collect{|line| line.chop } | |
end | |
File.open(filepath,'w') do |file| | |
file_content.each{|line| file.puts line } | |
end | |
end | |
def to_s | |
"#{self.name}, #{self.age}, #{self.phone}, #{self.address}" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment