9 Ekim 2013 Çarşamba

Odev Yapiyorum "Harry Potter Kata" adim adim

Tekrar Merhaba

Simdi kurs sirasinda verilen odevlerden "Harry Potter Kata"yi yapacagim. Senaryo su sekilde

Harry Potter Kata 
To try and encourage more sales of the 5 different Harry Potter books they sell, a bookshop has decided to offer discounts of multiple­ book purchases.

One copy of any of the five books costs 8 EUR.

If, however, you buy two different books, you get a 5% discount on those two books.

If you buy 3 different books, you get a 10% discount.

If you buy 4 different books, you get a 20% discount.

If you go the whole hog, and buy all 5, you get a huge 25% discount.

Note that if you buy, say, four books, of which 3 are different titles, you get a 10% discount on the 3 that form part of a set, but the fourth book still costs 8 EUR.

Your mission is to write a piece of code to calculate the price of any conceivable shopping basket (containing only Harry Potter books), giving as big a discount as possible.

For example, how much does this basket of books cost?

2 copies of the first book 2 copies of the second book 2 copies of the third book 1 copy of the fourth book 1 copy of the fifth book

Answer: 51.20 EUR
Once github'da repository olusturarak basliyorum. Daha yeteri kadar tecrubem olmadigi icin gorsel araclari kullanmak daha kolay geliyor.

Repository olusturuyoruz
Altta isaretlemis oldugum README dosyasi olusturma secenegi sayesinde projenin tanimini yazacagim bir dosyayi otomatik olarak olusmasini sagladim.

Sonraki ekranda basta yazdigim tanimi icine yazarak proje hakkinda on bilgi olusturdum.

Bu repository'nin adresi bana birazdan lazim olacak o yuzden repository sayfasina gecip harry_potter_kata repository'sini seciyor ve url'ini kopyaliyorum. (https kismi dahil) yani simdi elimde
https://github.com/Aliiybar/harry_potter_kata
gibi bir url var ve clippoard'da bekliyor.

command prompt'a gecip projeyi koyacagim klasoru olusturuyor ve icine giriyorum.

mkdir harry_potter_kata
cd harry_potter_kata

simdi git ile baglantisini kuracagim
git init 
git remote add harry_potter_kata https://github.com/Aliiybar/harry_potter_kata
git pull https://github.com/Aliiybar/harry_potter_kata

simdi program dosyalarimi olusturabilirim
touch hpk.rb
touch hpk_spec.rb

her iki komutla bos birer metin dosyasi olusturdum 
hpk_spec.rb dosyasini editor ile acip yazmaya basliyorum 


hpk_spek.rb
require './hpk.rb'

describe Hpk do
describe 'bookSet' do
it 'Returns with empty set array' do
hpk = Hpk.new
expect(hpk.bookset).to eq([0, 0, 0, 0, 0])
end
end
end


hpk.rb
class Hpk
bookset = []
def initialize ()
@bookset = []
5.times do |book_nr|
@bookset << 0
end
end

def bookset
return @bookset
end
end

simdi ilk testimize haziriz.
terminal penceresinde (command prompt) asagidaki komutu yaziyoruz.
rspec hpk_spec.rb
cevap olarak
Finished in 0.00051 seconds 1 example, 0 failures

veya bir hata varsa hata ile donuyor.

Simdi gelistirelim (renklerle ugrasamadim bu sefer)


hpk_spek.rb
require './hpk.rb'

describe Hpk do  
describe 'bookSet' do
it 'Returns with empty set array' do
hpk = Hpk.new
expect(hpk.bookset).to eq([0, 0, 0, 0, 0])
end
end

describe 'calculate' do
it 'Returns total amount' do
hpk = Hpk.new
expect(hpk.calculate_books_in_cart([2,2,1,0,0])).to eq(36.8)
end
end


describe 'calculate' do
it 'Returns total amount' do
hpk = Hpk.new
expect(hpk.calculate_books_in_cart([2,2,2,1,1])).to eq(51.2)
end
end
end


hpk.rb
class Hpk
BookPrice = 8

bookset = []
def initialize ()
@bookset = []
5.times do |book_nr|
@bookset << 0
end
end

def bookset
return @bookset
end

def calculate_books_in_cart(cart_arr)
looper = true
grand_total = 0.0
while looper
nr_of_books = 0 
5.times do |book_nr|
if cart_arr[book_nr] > 0 
nr_of_books += 1 
cart_arr[book_nr] = cart_arr[book_nr] - 1
end
end

case nr_of_books
when 5
grand_total += ((BookPrice * 5) * 0.75)
when 4
grand_total += ((BookPrice * 4) * 0.80)
when 3
grand_total += ((BookPrice * 3) * 0.90)
when 2
grand_total += ((BookPrice * 2) * 0.95)
when 1 
grand_total +=  BookPrice  
when 0 
looper = false
end

end
return grand_total
end

end


test yaptigimizda gorevden farkli bir sonuc elde ettik ama yine de verilen gorevi kismen yerine getirdik. o halde bu haliyle github'a gonderelim
git add .
git commit
git push harry_potter_kata
git commit'ten sonra commit mesajini yazmayi unutmuyoruz. Bu versiyonun neler icerdigini anlatan bir mesaj ileride faydali olacaktir.

bu odevdeki kodlara benim git adresimden ulasabilirsiniz.


https://github.com/Aliiybar


Hiç yorum yok:

Yorum Gönder