0

I'm just starting out with OOP and am struggling to work out how to implement it. I can't work out how to create an instance of the class using the hash.new method. It's a basic program that takes input from a user and stores it in a hash. I was then planning on pushing the hash into an array to take advantage of the index. Below is the code

class Customer

    def initialize(id, name, address, phone, email, bank_balance)
        @id = id
        @name = name
        @address = address
        @phone = phone
        @email = email
        @bank_balance = bank_balance
    end
end

puts "Would you like to create an account?"
answer = gets.chomp

loop do

    new_hash = {}
    if answer.downcase.start_with?('y')

        puts "Enter your name"
        name = gets.chomp.downcase
        new_hash['name'] = name

        puts "Enter your address"
        address = gets.chomp.downcase
        new_hash['address'] = address

        puts "Enter your ph number"
        number = gets.chomp
        number = number.split.join
        new_hash['number'] = number

        puts "Enter your email"
        email = gets.chomp.downcase
        new_hash['email'] = email       

        puts "Enter your bank balance"
        bank_balance = gets.chomp
        bank_balance = "$" + '%.2f' % bank_balance
        new_hash['bank_balance'] = bank_balance

        customer << new_hash

        binding.pry
        puts "Thankyou, details successfully Added"

        break
    else
        break
    end
end
RuNpiXelruN
  • 1,850
  • 2
  • 17
  • 23
  • `customer << new_hash`: where is `customer` coming from? – Petr Gazarov Apr 07 '16 at 04:38
  • 1
    Your class is `Customer`: you need `customer = Customer.new(id, name, address, phone, email, bank_balance)`. – Amadan Apr 07 '16 at 04:41
  • Thanks for the reply @Amadan. I'm just confused how i'd implement that by the customer input? – RuNpiXelruN Apr 07 '16 at 04:53
  • 2
    This replaces your `new_hash`; delete every single line that contains it. You go to all the trouble to make a class, then use a hash to store your values. It's like living in a trailer on the lawn after having built a mansion. Your user input ends up in variables `name`, `address`... then just use those variables and pass it to the `Customer` constructor as shown in my previous comment. – Amadan Apr 07 '16 at 04:55
  • In addition, you could remove the arguments from `initialize` and set up some getters / setters instead (`attr_accessor :name, :address, :phone, ...`). This allows you to set the customer's attributes one by one, i.e. `customer = Customer.new` and later on `customer.name = gets.chomp.downcase` – Stefan Apr 07 '16 at 07:31

1 Answers1

0

you should remove everything inside initialize(...) and instead use attr_accessor :name, :address, :phone, :email, :bank_balance) write it above your initialize statement for more information on attr_accessor see this post What is attr_accessor in Ruby? also follow it up by reading up on attr_reader and attr_writer

you can then assign values to those initialized variables in the rest of your code. you will call your class by running Customer.new

you can get a little more snap out of your code by assigning your gets.chomp values to additional methods, then you can do something like

class Customer
  attr_accessor :name, :address, :phone
  def initialize
    @name = ''
    @address = ''
    @phone = ''
  end

  def name
    puts "Enter your name"
    name = gets.chomp.downcase
  end
  def address
   puts "Enter your address"
    address = gets.chomp.downcase
  end
  def phone
   puts 'enter your phone'
   phone = gets.chomp
   phone = phone.split.join
  end
  def all
    name
    address
    phone  
  end
end
customer = Customer.new
customer.all
Community
  • 1
  • 1
ADL
  • 185
  • 8
  • 19