Help me Level Up


#1



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


I'm trying to verify log-in credentials. But not sure how to return the instance of the class. So that after someone logs in, they can list their files. To do that though I need to know the instance naming? ie "my_computer" Also, and this is annoying, I can't get my variables to return correctly.

Lots of code here, apologies. Look for the Comments.

exit=false
logged_in=false
user = " "

class Computer
    def initialize(username, password)
        @username=username
        @password=password
        @files={test:1, test2:2} #instance variable
        @@users[username] = password  #class variable
    end
    @@users={}
    def create(filename)
        time=Time.now
        @files[filename]=time 
        puts "#{filename.downcase} created by #{@username} at #{time}"
    end
    
    def Computer.get_users
        return @@users
    end
    
    def delete(filename)
        if @files.keys.includes(filename)
            @files.delete(filename)
        end
        puts @files
    end
    
    def listfiles
        filename=@files.each {|k,v| puts "#{k}"}
        puts filename
    end    
end

# ------Okay Here is the Method I use to verify Log-In ---------
def login_method
    x=false
    pword="test"
    until x do
        puts "enter your USERNAME:"
        user=gets.chomp
        h=Computer.get_users 
        if h.has_key?(user) 
            until h[user]==pword || pword=="exit"
            puts "enter Password or EXIT"
            pword=gets.chomp
            end
            if h[user]==pword
                logged_in=true
                puts "Log in success!"
            else 
                puts "okay out of here."
                user==" "
            end
            x=true
        else
            puts "#{user} is not in the system. Try Again (Y/N)"
            again=gets.chomp
            if again.downcase=="y"
                x=false 
            else
                x=true
            end
            user=nil
        end
    end
    return logged_in, user #----These are the variables not returning correctly
end

def create_user
    puts "alrighty lets do this."
end

my_computer=Computer.new("Catch","Twenty2")
your_computer=Computer.new("SomeName","somepassword")
# my_computer.create("DOCUMENT.txt")
# puts Computer.get_users
# puts my_computer.listfiles
 
    until exit do
        puts "LOGIN or CREATE a new user? (EXIT to end)"
        login=gets.chomp
        if login.downcase=="login"
            login_method
            puts "Welcome #{user}" if logged_in   #---This is where I can't get user to return or logged in to be true.
            puts "Maybe create a user first? eh?" unless logged_in
        elsif login.downcase=="create"
            create_user
        elsif login.downcase=="exit"
            puts "laters."
            exit = true
        else
            puts "I can do this all day you know..."
        end
    end


#2

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.