Skip to content

Instantly share code, notes, and snippets.

@darthdeus
Forked from anonymous/gist:1400321
Created November 28, 2011 13:07
Show Gist options
  • Save darthdeus/1400342 to your computer and use it in GitHub Desktop.
Save darthdeus/1400342 to your computer and use it in GitHub Desktop.
product_data = [
"PE316,Zvukotesna vlozka SPIRE pro redukci hluku,104,51",
"RI080,FAN COOLER PASIVNI,491,88",
"QE091,Nabijecka PENTAGRAM Pocket USB,608,30",
"QU286,RAMECEK ViPower VYMENNY PLAST ATA,572,95",
"PU441,RAZER LYCOSA Gaming Keyboard,160,56",
"QE170,ICON,136,47",
"SE125,UTP OCHRANA KONEKTORU,310,17",
"TI390,PCI Express,415,56",
"TO120,PRINTSERVER Digitus Fast Ethernet USB,674,38",
"QO071,230V Belkin Powerline Turbo Networking adapter,796,81",
"QO350,WIFI ANTENA smerova parabolicka,585,88",
"QE042,WIFI ANTENA smerova,398,81",
"RU090,TP-LINK router TL-R,646,92",
"SE447,ADSL WELL PC,155,-1000", # CHYBNY STAV SKLADU
"PI287,VoIP Perfect GW,-999,23", #CHYBNA CENA
"QA255,STOJANEK POD MONITOR cerny se zasuvkou,277,90",
"SE433,Brasna ultracase pro Notebook 15,425,80",
"SA309,BATOH pro Notebook BacPac Easy DICOTA,511,52",
"XX449,LOGO SAFE obal na Notebook neopren pro,328,80", #CHYBNY KOD
"TI185,PORTE Blue Laser samolepici folie pro NTB,383,56",
"TO280,PORTE Yellow Cosmo samolepici folie pro NTB,506,97",
"RI443,Evolve sportovni hodinky SPORT,682,17",
"PO040,PCMCIA SERIOVY PORT STLabs,584,10",
"TU116,Notebook HP EliteBook 8730w,142,11",
"QO408,Notebook ASUS Lamborghini VX5-6X002J,23441,12",
"RA222,Notebook LENOVO THINKPAD W700,34587,98",
"RI407,Notebook TOSHIBA Qosmio X300-15G,35890,25",
"PI422,Notebook SONY VAIO Z41XD/B/ C2D P9700,45691,75",
"PE217,Notebook APPLE MacBook Pro,55165,25",
"QI168,Notebook UMAX VisionBook,18230,56",
"SO999,Notebook HP EliteBook 8730w,20741,98" #CHYBNY KOD
]
order_data = [
"1,PE316,-100", #CHYBNY POCET VYROBKU
"2,RI080,40",
"3,QE091,45",
"4,QU286,62",
"5,PU441,8",
"6,QE170,40",
"7,SE125,34",
"8,TI390,52",
"9,TO120,2",
"10,QO071,15",
"11,QO350,32",
"12,QE042,38",
"13,RU090,54",
"14,SE447,52",
"15,PI287,70",
"16,QA255,54",
"17,SE433,20",
"18,SA309,66",
"19,XX449,57", #CHYBNY KOD
"20,TI185,54",
"21,TO280,69",
"22,RI443,13",
"23,PO040,55",
"24,TU116,67",
"25,QO408,7",
"26,RA222,32",
"27,RI407,18",
"28,PI422,35",
"29,PE217,31",
"30,QI168,1",
"31,SO999,54", #CHYBNY KOD
"32,PA001,54" #NEEXISTUJICI VYROBEK
]
class Product
attr_reader :code, :name, :price, :instore, :sold
# validuje data, vyhazuje vyjimky v pripade chyby
def initialize(code, name, price, instore)
@code = code
@name = name
@price = price
@instore = instore
@sold = 0
begin
if not code =~ /^[PQRST][AEIOU]([0][0-9][1-9]|[0][1-9][0-9]|[1-3][0-9][0-9]|[4][0-4][1-9]|[4][5][0])$/
raise "#{@code}: Wrong product code"
end
if (price <= 0)
raise "#{@code}: Wrong product price"
end
if (instore < 0)
raise "#{@code}: Wrong instore"
end
rescue Exception => e
puts "#{e}"
end
# DOPLNTE KONTROLU SPRAVNOSTI DAT
# V PRIPADE CHYBY VYHODTE VYJIMKU
end
# Kontroluje mnozstvi na sklade, pokud je dostatecne mnozstvi skladem,
# tak nastavi atribut @sold na pozadovane mnozstvi.
# V opacnem pripade vyhazuje vyjimku.
def buy(amount)
if amount <= @instore
@sold += amount
@instore -= amount
else
raise "Not enough items in store"
end
end
end
class Order
attr_reader :number, :code, :amount
# validuje data, vyhazuje vyjimky v pripade chyby
def initialize(number, code, amount)
@number = number
@code = code
@amount = amount
# DOPLNTE KONTROLU SPRAVNOSTI DAT
# V PRIPADE CHYBY VYHODTE VYJIMKU
if not code =~ /^[PQRST][AEIOU]([0][0-9][1-9]|[0][1-9][0-9]|[1-3][0-9][0-9]|[4][0-4][1-9]|[4][5][0])$/
raise "#{@code}: Wrong product code in order"
end
if (amount < 0)
raise "#{@code}: Wrong amount to buy"
end
end
end
class Store
attr_reader :products, :orders, :profit_notebooks, :profit_other
# Nacte data z textovych radku, osetruje pripadne vyjimky
def initialize(product_data, order_data)
@products = []
@orders = []
product_data.each do |prod|
data = prod.split(",")
begin
product_object = Product.new(data[0],data[1],data[2].to_i,data[3].to_i)
@products << product_object
rescue Exception => e
puts "#{e}"
end
end
order_data.each do |order|
data = order.split(",")
begin
order_object = Order.new(data[0].to_i,data[1],data[2].to_i)
@orders << order_object
rescue Exception => e
puts "#{e}"
end
end
end
# Projde objednavky - pokusi se najit vyrobek metodou find_product
# a nasledne koupit pomoci metody buy
# Osetruje vyjimky v pripade nenalezeni vyrobku nebo kdyz neni skladem
def process_orders
@orders.each do |order|
begin
produkt = find_product(order.code)
produkt.buy(order.amount)
rescue Exception => e
puts "Canceling order #{order.number} - #{order.code}: #{e}"
end
end
end
# Spocita zisk a ulozi do promennych @profit_notebooks a @profit_other
# Za notebook se povazuje kazdy produkt, jehoz nazev zacina na "Notebook"
def calculate_profit
@profit_notebooks = 0
@profit_other = 0
@products.each do |product|
profit = product.price * product.sold
p profit
if product.name =~ /^Notebook/
@profit_notebooks += profit
else
@profit_other += profit
end
end
puts "Profit on notebooks #{@profit_notebooks}"
puts "Profit on other items #{@profit_other}"
end
private
# Najde a vrati produkt podle kodu, nebo vyhodi vyjimku
def find_product(code)
if @products.find { |u|u.code == code }
return @products.find { |u|u.code == code }
else
raise "Product not found"
end
end
end
store = Store.new(product_data, order_data)
store.process_orders
store.calculate_profit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment