Here it is:
# Orders Aggregator
class BreadType
attr_accessor :type
def initialize(type)
@type = type
end
def eql?(other)
@type = other.type
end
def hash
@type.hash
end
def to_s
@type
end
end
class Sandwich
attr_accessor :name, :type
def initialize (name, type)
@name = name
@type = type
end
def eql?(other)
@name = other.name #&& @type = other.type # buggy and I cannot resolve, so I decided to comment
end
def hash
@name.hash ^ @type.hash
end
def to_s
@name.to_s << " : " << @type.to_s
end
end
balady = BreadType.new "Balady"
shamy = BreadType.new "Shamy"
# Talaat
talaat = []
talaat << Sandwich.new(:Ta3mya, balady) << Sandwich.new(:Batates_omlete, balady)
# Hewedy
hewedy = []
2.times {
hewedy << Sandwich.new(:Ta3mya, balady)
}
# Ahmad
ahmad = []
ahmad << Sandwich.new(:Swabe3_Salada, balady) << Sandwich.new(:Fol_Eskandrany, balady)
# Khalid
khalid = []
khalid << Sandwich.new(:Ta3mya_Eskandrany, balady)
# Alaa
alaa = []
alaa << Sandwich.new(:Ta3mya_Eskandrany, balady)
# Hatem
hatem = []
3.times {
hatem << Sandwich.new(:Ta3mya, shamy)
}
total = talaat + hewedy + ahmad + khalid + alaa + hatem
counts = Hash.new(0)
total.each { |name| counts[name] += 1 }
puts "Aggregation: "
counts.each { |k, v|
puts k.to_s << " ::" << v.to_s
}
Output:
Aggregation:
Batates_omlete : Balady ::1
Ta3mya : Shamy ::3
Swabe3_Salada : Balady ::1
Ta3mya : Balady ::3
Ta3mya_Eskandrany : Balady ::2
Fol_Eskandrany : Balady ::1
No comments:
Post a Comment