我的目标是能够为每种茶分配自己的 ID,比较茶之间的价格和重量,并在命令行中完成所有操作。这样做的聪明方法是什么?到目前为止,这是我的代码:
class Tea
def initialize(name, price, shipping, weight)
@name = name
@price = price
@shipping = shipping
@weight = weight
get_tea_details
@total_price = total_price
end
def get_tea_details
puts "Enter name: "
@name = gets.chomp
puts "Enter price: "
@price = gets.chomp.to_f
puts "Enter shipping cost: "
@shipping = gets.chomp.to_f
puts "Enter weight: "
@weight = gets.chomp.to_i
end
def total_price
@total_price = @price + @shipping
end
def price_difference
price_difference = t1.total_price - t2.total_price
print "#{price_difference}"
end
end
puts "Do you want to compare teas?: "
answer = gets.chomp
if answer == "yes"
t1 = Tea.new(@name, @price, @shipping, @weight)
t1 = Tea.new(@name, @price, @shipping, @weight)
end
price_difference