8. You did it! extra for experts?


#1

Anyone know how to tackle the extra exercises - What if you add a method that updates files? Or deletes them? What if you want to add additional parameters to initialize or create?


https://www.codecademy.com/en/courses/ruby-beginner-en-X5wcR/0/8?curriculum_id=5059f8619189a5000201fbcb



#2

I think to erase and update files you must write something like this inside Computer class
def erase (file)
file.delete(files)
end
def update (file)
file.update(files)
end

But im not 100% sure


#3

I was able to do it. The update method lets you change the file name and it goes to a change description method to change the description. The delete method deletes a file

Here's my code.

class Computer
    @@users = {}
    def initialize(username, password)
        @username = username
        @password = password
        @files = {}
        @@users[username] = password
    end
    def create(filename)
        time = Time.now
        @files[filename] = time.to_s + " this is a test I'm doing to make sure I'm actually changing the key and not just deleting a file and creating a file. It worked!"
        puts "#{filename} was create by #{@username} at #{time}" 
    end
    
    def update
      puts "What file would you like to update?"
      file_to_update = gets.chomp
      if @files.key?(file_to_update)
        puts "this is an existing filename. Type yes if you'd like to change its name. If not type something else like no"
        change_name = gets.chomp
        if change_name == "yes"
          puts "type in the new filename"
          new_name = gets.chomp.downcase
          @files[new_name] = @files.delete(file_to_update) #parentheses around file_to_update aren't necessary
          change_description(new_name)
        else
          puts "the name won't be changed."
          change_description(file_to_update)
        end
      else
        puts "This isn't an existing file."
      end
    end

    def delete(filename)
      if @files.keys.include?(filename)
        @files.delete(filename)
      end
      puts @files
    end
    
    def Computer.get_users
        @@users
    end

    private

    def change_description(filename)
      puts "Would you like to change the file description? If so type yes. Otherwise type something else like no."
      change_description = gets.chomp
      if change_description == "yes"
        puts "Type the new file description. The file time will remain at the end of the file."
        new_description = gets.chomp
        @files[filename] = "#{new_description} #{@files[filename]}"
      else
        "The description won't be changed."
      end
    end
end

my_computer = Computer.new("Matt","my_password")
my_computer.create("coit_moytes_songs.txt")
my_computer.create("e_book_ideas.txt")
my_computer.update
my_computer.delete("e_book_ideas.txt")`

#4

I used this code:

class Computer
@@users = {}

def initialize(username, password)
    @username = username
    @password = password
    @files = {}

    @@users[username] = password
end

def create(filename)
    time = Time.now
    @files[filename] = time
    
    puts "File #{filename} created successfully at #{time}!"
end

def update(filename)
    if @files.include?(filename)
        time = Time.now
        @files[filename] = time
        puts "File #{filename} updated successfully!"
    else
        puts "File #{filename} not found!"
    end 
end

def remove(filename)
    if @files.include?(filename)
        @files.delete(filename)
        puts "File #{filename} deleted successfully!"
    else
        puts "File #{filename} not found!"
    end 
end

def Computer.get_users
    return @@users
end

end

my_computer = Computer.new("Mameli", "123456")
my_computer.create("file1.txt")
my_computer.create("file2.txt")
my_computer.create("file3.txt")

Computer.get_users
my_computer

my_computer.remove("file2.txt")
my_computer.remove("file45.txt")
my_computer.update("file33.txt")
my_computer.update("file3.txt")

Computer.get_users
my_computer