diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 000000000..26755594c Binary files /dev/null and b/.DS_Store differ diff --git a/.gitignore b/.gitignore index 5e1422c9c..c0ac3dc53 100644 --- a/.gitignore +++ b/.gitignore @@ -48,3 +48,4 @@ build-iPhoneSimulator/ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this: .rvmrc +coverage diff --git a/design-activity.md b/design-activity.md new file mode 100644 index 000000000..f583702fa --- /dev/null +++ b/design-activity.md @@ -0,0 +1,59 @@ +# What classes does each implementation include? Are the lists the same? +Each implementation include: CartEntry, ShoppingCart and Order. +The lists are the same. + +# Write down a sentence to describe each class. + CartEntry: each time when the user add new items into the Cart , taking care of that addition + + ShoppingCart: holding a list of the cart entries , represents all the items user has added to the Shopping Cart. + + Order: taking care of the order. + +# How do the classes relate to each other? It might be helpful to draw a diagram on a whiteboard or piece of paper. + + ShoppingCart contains a list of CartEntry instances. + Order is generated based on the current items holding in the ShoppingCart. + +# What data does each class store? How (if at all) does this differ between the two implementations? + CartEntry class: stores unit_price and quantity of the entry item. + ShoppingCart class: stores a list of the CartEntry. + Order class: stores sales tax rate, a new instance of the SHoppingCart class + + two implementations has no difference. + +# What methods does each class have? How (if at all) does this differ between the two implementations? + + CartEntry: A has read, write methods on instance variable @unit_price and @quantity. B has a price method to calculate the price of each CartEntry. + + ShoppingCart: A has a read write method on instance variable @entries. B has a price method on ShoppingCart to calculate the total price of the ShoppingCart + + Order : A has a method to calculate the total price of the order. B is the same as A. + +# Consider the Order#total_price method. In each implementation: +#Is logic to compute the price delegated to "lower level" classes like ShoppingCart and CartEntry, or is it retained in Order? + + B : The logic to compute the price delegated to the lower level classes. + A : it is retained all in order. + +# Does total_price directly manipulate the instance variables of other classes? + + B: does not directly manipulate the instance variable s of other classes. + A: need to access the price and quantity of CartEntry class through ShoppingCart class . + +# If we decide items are cheaper if bought in bulk, how would this change the code? Which implementation is easier to modify? + we need to add a conditional statement when calculating the price of that entry, the price should be discounted based on the quantity. + B is easier for implementation. + since in B, order only need to know the price of ShoppingCart, don't need to know how many CartEntries that instance of ShoppingCart holds, and the price & quantity of each CartEntries. The change will only need to be made to the CartEntry class, ShoppingCart class and Order class do not need to know what happened, and no change needed to these two classes. + while in A, order need to know what happened in other two classes. order need to know how many instances of CartEntry are contained in the ShoppingCart instance, the entry unit_price and entry quantity. + Order need to access the unit_price and quantity of CartEntry class through the SHoppingCart class to decide if a bulk bought has happened, and to calculate a new total price. + +# Which implementation better adheres to the single responsibility principle? + B is better + +# Bonus question once you've read Metz ch. 3: Which implementation is more loosely coupled? + B is loosely coupled. + +# What changes need to make to the design of the hotel class? + changed the Reservation.new method, to remove the reservation id from the parameters. Instead, generate the reservation id by the program. + + the block_admin class is a child class of admin class, which made the design very complicated, will keep the block class, but instead of make it inherit from admin class, will make it totally independent. diff --git a/lib/admin.rb b/lib/admin.rb new file mode 100644 index 000000000..301989ed6 --- /dev/null +++ b/lib/admin.rb @@ -0,0 +1,184 @@ +require 'pry' +require 'date' +require_relative 'reservation' +require_relative 'room' +require_relative 'block_admin' + +class Admin + attr_reader :reservations, :rooms, :room_unbooked_dates, :room_blocks, :start_date, :end_date + + def initialize(room_id_list, start_date, end_date) + check_input_dates(start_date, end_date) + @rooms = [] + @reservations = [] + @room_unbooked_dates = [] + @room_blocks =[] + @start_date = start_date + @end_date = end_date + + room_id_list.each do |i| + room_new = create_room(i) + @rooms << room_new + start_d = start_date + + while start_d < end_date + + @room_unbooked_dates << {room_n: room_new, unbooked_date: start_d} + start_d += 1 + end + end + end + + # find availabe rooms in a given period + def find_room_available(start_date, end_date) + + check_input_dates(start_date, end_date) + dates_available_rooms = [] + + @rooms.each do |room| + dates_needed = [] + start_d = start_date + + while start_d < end_date + dates_needed << {room_n: room, unbooked_date: start_d} + start_d += 1 + end + + if (@room_unbooked_dates & dates_needed) == dates_needed + dates_available_rooms << room + end + end + return dates_available_rooms + end + + # make new reservations + def make_reservation(customer_name, start_date, end_date) + + check_input_dates(start_date, end_date) + if start_date < @start_date || (end_date > @end_date) + raise ArgumentError, "can only book reservations between #{@start_date} and #{@end_date}" + end + + rooms_not_booked = find_room_available(start_date, end_date) + + if rooms_not_booked == [] + raise ArgumentError, "No room available at this time." + else + room = rooms_not_booked.first + end + + result = create_reservation(customer_name, room, start_date, end_date) + @reservations << result + start_d = start_date + + while start_d < end_date + @room_unbooked_dates.reject! {|item| item == {room_n: room, unbooked_date: start_d}} + start_d += 1 + end + + return result + end + + # create room blocks + def create_room_block(name_of_block, room_collection, start_date, end_date, discount_rate) + if discount_rate < 0 || discount_rate > 1 + raise ArgumentError + end + + if room_collection.nil? || room_collection == [] || room_collection.length > 5 + raise ArgumentError, "room list can not be nil, empty array or more than 5 in the list" + end + + if start_date.class != Date || end_date.class != Date + raise ArgumentError, "start_date and end_Date should be Date objects" + end + if start_date >= end_date + raise ArgumentError, "invlid dates entered, start_date should be ealier than end_date" + end + + if start_date < @start_date || end_date > @end_date + raise ArgumentError, "start_date and end_date of room block must be in the working period" + end + + available_rooms = find_room_available(start_date, end_date) + room_entered = room_collection.map {|item| find_room(item)} + + if (available_rooms & room_entered) != room_entered + raise ArgumentError, "rooms are not available in given period" + else + start_d = start_date + + date_list = [] + while start_d < end_date + date_list << start_d + start_d += 1 + end + + room_date_list = [] + date_list.each do |item| + room_entered.each do |r| + room_date_list << {room_n: r, unbooked_date: item} + end + end + + @room_unbooked_dates = @room_unbooked_dates - room_date_list + end + + new_block = create_block_admin(name_of_block,room_collection, start_date, end_date, discount_rate) + room_blocks << new_block + return new_block + + end + + # input a string of date, to return the list of the reservations on that date + def list_reservations(date_selected) + return @reservations.select {|reserve| reserve.dates_booked.include? date_selected} + + end + + # find room obj by id + def find_room(id) + raise ArgumentError, "ID cannot be blank, less than zero or more than 20. (got #{id})" if id.nil? || id <= 0 || id > 20 + return @rooms.find { |room| room.room_num == id } + end + + # find reservation obj by id + def find_reservation(id) + + return @reservations.find {|reserve| reserve.id == id } + + end + + # calculate reservation cost by reservation id + def calculate_cost(reservation_id) + + return find_reservation(reservation_id).reserve_cost + + end + + private + # check inputed start_date and end_date to make sure they are valid + def check_input_dates(start_date, end_date) + if start_date.class != Date || end_date.class != Date + raise ArgumentError, "start_date and end_Date should be Date objects" + end + if start_date >= end_date + raise ArgumentError, "invlid dates entered, start_date should be ealier than end_date" + end + end + + # create new instance of reservation object + def create_reservation(customer_name, room, start_date, end_date) + return Reservation.new(customer_name, room, start_date, end_date) + end + + # create new room object + def create_room(id) + return Room.new(id) + end + + # create new room_block admin object + def create_block_admin(name, room_id_list, start_date, end_date, discount_rate) + return Block.new(name, room_id_list, start_date, end_date, discount_rate) + end +end diff --git a/lib/block_admin.rb b/lib/block_admin.rb new file mode 100644 index 000000000..a2e4f5ddf --- /dev/null +++ b/lib/block_admin.rb @@ -0,0 +1,60 @@ +require 'pry' +require 'date' +require_relative 'reservation' +require_relative 'room' + + +class Block + attr_reader :discount, :block_name, :rooms, :reservations, :start_date, :end_date, :rooms_available + + def initialize(name, room_id_list, start_date, end_date, discounted_rate) + @discount = discounted_rate + @block_name = name + @rooms = [] + @reservations = [] + @start_date = start_date + @end_date = end_date + @rooms_available = [] + + room_id_list.each do |i| + room_new = create_room(i) + @rooms << room_new + @rooms_available << room_new + end + end + + def calculate_cost(reservation_id) + return find_reservation(reservation_id).reserve_cost * (1 - @discount) + end + + def make_reservation(customer_name) + + if @rooms_available == [] + raise ArgumentError, "No room available at this time." + else + room = @rooms_available.pop + end + + result = create_reservation(customer_name, room, @start_date, @end_date) + @reservations << result + + return result + end + + + private + + def create_reservation(customer_name, room, start_date, end_date) + return Reservation.new(customer_name, room, start_date, end_date) + end + + def create_room(id) + return Room.new(id) + end + + def find_reservation(id) + + return @reservations.find {|reserve| reserve.id == id } + end + +end diff --git a/lib/reservation.rb b/lib/reservation.rb new file mode 100644 index 000000000..b582f4b2d --- /dev/null +++ b/lib/reservation.rb @@ -0,0 +1,37 @@ +require 'pry' +require 'date' + +class Reservation + attr_reader :id, :customer_name, :room, :start_date, :end_date + + def initialize (customer_name,room,start_date, end_date) + if start_date >= end_date || start_date < Date.today + raise ArgumentError + else + @id = sprintf("%20.10f", Time.now.to_f).delete('.').to_i.to_s(36) + @customer_name = customer_name + @room = room + @start_date = start_date + @end_date = end_date + + end + end + + # list all the dates been covered in the reservation + def dates_booked + result = [] + date_enter = @start_date + while date_enter < @end_date + result << date_enter + date_enter += 1 + end + return result + end + + # calculate the cost of the reservation + def reserve_cost + return (@end_date - @start_date) * @room.rate + end + + +end diff --git a/lib/room.rb b/lib/room.rb new file mode 100644 index 000000000..ab73894e4 --- /dev/null +++ b/lib/room.rb @@ -0,0 +1,21 @@ +require 'pry' +require 'date' +require_relative 'reservation' + + +class Room + attr_reader :room_num, :rate + + def initialize(room_num) + if room_num.to_i > 20 || room_num.to_i < 1 + raise ArgumentError + else + @room_num = room_num + @rate = 200 + + + end + end + + +end diff --git a/refactor.txt b/refactor.txt new file mode 100644 index 000000000..a54483bda --- /dev/null +++ b/refactor.txt @@ -0,0 +1,5 @@ +refactor plan for hotel project + +#1. in hotel project, I have used an array of the available room & date combinations as the instant variable in the admin class, which need to occupy lot of the space. Each time, when a reservation made, a list of the room_date combination need to be removed from the the available list, which might need a long processing time to accomplish that. To improve, I plan to change the that design . I will use the list of reservations to replace the list of the available room & date combinations. in that case, although the logic to check if a room is available on a specific period is more complicated than my original design, it might save the space and the time needed to processing. + +#2 the block_admin class is a child class of admin class, which made the design very complicated, will keep the block class, but instead of make it inherit from admin class, will make it a totally independent. diff --git a/spec/admin_spec.rb b/spec/admin_spec.rb new file mode 100644 index 000000000..25f1d6abd --- /dev/null +++ b/spec/admin_spec.rb @@ -0,0 +1,271 @@ +require 'pry' +require_relative 'spec_helper' + +describe "Admin class" do + before do + list = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20] + @admin_1 = Admin.new(list, Date.today, (Date.today + 365)) + end + + describe "Initializer" do + it "is an instance of Admin" do + expect(@admin_1).must_be_kind_of Admin + end + + it "establishes the base data structures when instantiated" do + expect(@admin_1.reservations).must_be_kind_of Array + expect(@admin_1.rooms).must_be_kind_of Array + expect(@admin_1.room_unbooked_dates).must_be_kind_of Array + expect(@admin_1.rooms.first).must_be_kind_of Room + expect(@admin_1.room_unbooked_dates.length).must_equal 7300 + expect(@admin_1.room_unbooked_dates.first[:unbooked_date]).must_equal Date.today + expect(@admin_1.room_blocks).must_equal [] + end + end + + + describe "make_reservation" do + before do + @start_date_1 = Date.new(2018,12,4) + @end_date_1 = Date.new(2018,12,6) + @res_1 = @admin_1.make_reservation("Mike Murry",@start_date_1, @end_date_1) + @count_1 = @admin_1.reservations.count + @start_date_2 = Date.new(2018,12,5) + @end_date_2 = Date.new(2018,12,7) + @res_2 = @admin_1.make_reservation("Julie Smith",@start_date_2, @end_date_2) + @count_2 = @admin_1.reservations.count + + 20.times do |i| + @admin_1.make_reservation("Lily Xia", Date.new(2018,10,25), Date.new(2018,10,27)) + end + end + + it "will add a reservation object to reservation array" do + expect(@admin_1.reservations).must_include @res_1 + expect(@count_2).must_equal @count_1 + 1 + end + + it "will return a reservation object " do + expect(@res_1).must_be_kind_of Reservation + expect(@res_2).must_be_kind_of Reservation + end + + it "will reduce the room_unbooked_dates array correctly" do + room_1 = @admin_1.find_room(1) + # room_2 = @admin_1.find_room(2) + result = false + selected = @admin_1.room_unbooked_dates.select {|a, b| a == room_1 && b == @start_date_1} + if selected == [] + result = true + end + + result_2 = false + selected_2 = @admin_1.room_unbooked_dates.select {|a, b| a == room_1 && b == @end_date_1} + if selected_2 == [] + result = true + end + expect(result).must_equal true + expect(result_2).must_equal false + expect(@admin_1.room_unbooked_dates.length).must_equal 7256 + + end + + it "raise ArgumentError if start_date is not ealier than end_date" do + start_date_3 = Date.new(2018,12,5) + end_date_3 = Date.new(2018,12,3) + expect{@admin_1.make_reservation("Jessica lee", start_date_3,end_date_3)}.must_raise ArgumentError + + end + + it "raise ArgumentError if start_date is ealier than today" do + start_date_3 = Date.new(2018,8,5) + end_date_3 = Date.new(2018,12,3) + expect{@admin_1.make_reservation("Jessca lee",start_date_3,end_date_3)}.must_raise ArgumentError + + end + + it "will raise ArgumentError when no room is available for trip" do + + expect{@admin_1.make_reservation("Jessica lee",Date.new(2018,10,25),Date.new(2018,10,26))}.must_raise ArgumentError + end + + it "will not create a reservation if no room is available" do + + expect(@admin_1.reservations.length).must_equal 22 + + end + + end + + describe "list_reservations on a specific date" do + before do + @start_date_1 = Date.new(2018,12,4) + @end_date_1 = Date.new(2018,12,6) + @res_1 = @admin_1.make_reservation("Mike Murry",@start_date_1, @end_date_1) + @count_1 = @admin_1.reservations.count + @start_date_2 = Date.new(2018,12,5) + @end_date_2 = Date.new(2018,12,7) + @res_2 = @admin_1.make_reservation("Julie Smith",@start_date_2, @end_date_2) + @count_2 = @admin_1.reservations.count + + end + + it "returns nil if no result being found" do + expect(@admin_1.list_reservations(Date.new(2018,12,10))).must_equal [] + end + + it "if result being found, must return an array of resrvations " do + expect(@admin_1.list_reservations(Date.new(2018,12,5))).must_include @res_1 + expect(@admin_1.list_reservations(Date.new(2018,12,5))).must_include @res_2 + end + + it "must return the right list of reservations " do + expect(@admin_1.list_reservations(Date.new(2018,12,5)).length).must_equal 2 + end + end + + describe "find_room method" do + + it "throws an argument error for a bad ID" do + expect{ @admin_1.find_room(0) }.must_raise ArgumentError + end + + it "finds a room instance" do + room_found = @admin_1.find_room(2) + expect(room_found).must_be_kind_of Room + end + end + + describe "find_reservation method" do + before do + @start_date_1 = Date.new(2018,12,4) + @end_date_1 = Date.new(2018,12,6) + @res_1 = @admin_1.make_reservation("Mike Murry",@start_date_1, @end_date_1) + @count_1 = @admin_1.reservations.count + @start_date_2 = Date.new(2018,12,5) + @end_date_2 = Date.new(2018,12,7) + @res_2 = @admin_1.make_reservation("Julie Smith",@start_date_2, @end_date_2) + @count_2 = @admin_1.reservations.count + + end + + + + it "finds a reservation instance" do + res_id = @res_1.id + + reserve_found = @admin_1.find_reservation(res_id) + # binding.pry + expect(reserve_found).must_be_kind_of Reservation + expect(reserve_found).must_equal @res_1 + end + end + + describe "calculate_cost" do + before do + @start_date_1 = Date.new(2018,12,4) + @end_date_1 = Date.new(2018,12,6) + @res_1 = @admin_1.make_reservation("Mike Murry",@start_date_1, @end_date_1) + @count_1 = @admin_1.reservations.count + @start_date_2 = Date.new(2018,12,5) + @end_date_2 = Date.new(2018,12,7) + @res_2 = @admin_1.make_reservation("Julie Smith",@start_date_2, @end_date_2) + @count_2 = @admin_1.reservations.count + + end + + it "returns the right number" do + res_id = @res_1.id + cost = @admin_1.calculate_cost(res_id) + expect(cost).must_equal 400 + end + end + + describe "find room availabe " do + before do + @start_date_1 = Date.new(2018,12,4) + @end_date_1 = Date.new(2018,12,6) + @res_1 = @admin_1.make_reservation("Mike Murry",@start_date_1, @end_date_1) + + @start_date_2 = Date.new(2018,12,5) + @end_date_2 = Date.new(2018,12,7) + @res_2 = @admin_1.make_reservation("Julie Smith",@start_date_2, @end_date_2) + + end + + it "returns an array of rooms" do + room_list_1 = @admin_1.find_room_available(Date.new(2018,12,4),Date.new(2018,12,5)) + + expect(room_list_1).must_be_kind_of Array + expect(room_list_1.first).must_be_kind_of Room + end + + it "returns the right number of rooms " do + room_book = @admin_1.find_room(3) + expect(@admin_1.find_room_available(Date.new(2018,12,4),Date.new(2018,12,5)).length).must_equal 19 + expect(@admin_1.find_room_available(Date.new(2018,12,4),Date.new(2018,12,5))).must_include room_book + + end + + it " will not include a room already booked on that day" do + room_first = @admin_1.find_room(1) + room_selected = @admin_1.find_room_available(Date.new(2018,12,4), Date.new(2018,12,5)) + + expect(room_selected.include? room_first).must_equal false + end + end + + describe "create_room_block" do + before do + @start_date_1 = Date.new(2018,12,4) + @end_date_1 = Date.new(2018,12,6) + @res_1 = @admin_1.make_reservation("Mike Murry",@start_date_1, @end_date_1) + @count_1 = @admin_1.reservations.count + @start_date_2 = Date.new(2018,12,5) + @end_date_2 = Date.new(2018,12,7) + @res_2 = @admin_1.make_reservation("Julie Smith",@start_date_2, @end_date_2) + @count_2 = @admin_1.reservations.count + + 20.times do |i| + @admin_1.make_reservation("Lily Xia", Date.new(2018,10,25), Date.new(2018,10,27)) + end + + @name_of_block = "tech_forum" + @list_3 = [10,11,12,13,14] + @list_6 = [10,11,12,13,14,15] + @start_3 = Date.new(2018,12,1) + @end_3 = Date.new(2018,12,11) + @discount = 0.1 + end + + it "returns a new object of Block " do + expect(@admin_1.create_room_block("block1",@list_3, @start_3, @end_3, @discount)).must_be_kind_of Block + end + + it "increase the length of room_blocks array be 1" do + @admin_1.create_room_block("block1",@list_3, @start_3, @end_3, @discount) + expect(@admin_1.room_blocks.length).must_equal 1 + end + + it "remove the related rooms from the room_unbooked_dates array in the specified date " do + @admin_1.create_room_block("block1",@list_3, @start_3, @end_3, @discount) + expect(@admin_1.room_unbooked_dates.length).must_equal 7206 + end + + it "raise ArgumentError if room_id_list has more than 5 elements" do + expect{@admin_1.create_room_block("block1",@list_6, @start_3, @end_3, @discount)}.must_raise ArgumentError + end + + it "raise ArgumentError if room specified was not available in that period" do + expect{@admin_1.create_room_block("block1",[1,2], Date.new(2018,10,25), Date.new(2018,10,27),@discount)}.must_raise ArgumentError + end + + it "raise ArgumentError if the period requested for set up room block is outside of the current working period )" do + expect{@admin_1.create_room_block("block1",[1,2], Date.new(2019,11,25), Date.new(2019,11,27),0.1)}.must_raise ArgumentError + end + + it "raise ArgumentError if the discount_rate is < 0 or > 1" do + expect{@admin_1.create_room_block("block1",[1,2], Date.new(2018,11,2), Date.new(2018,11,5),1.1)}.must_raise ArgumentError + end + end +end diff --git a/spec/block_admin_spec.rb b/spec/block_admin_spec.rb new file mode 100644 index 000000000..e8d8bc6bf --- /dev/null +++ b/spec/block_admin_spec.rb @@ -0,0 +1,39 @@ +require 'pry' +require_relative 'spec_helper' + +describe "Block Admin class" do + + before do + list = [6,7,8,9] + @admin_b = Block.new("block1",list, Date.new(2018,10,5), Date.new(2018,10,10),0.10) + + end + + describe "Initializer" do + it "is an instance of BlockAdmin" do + expect(@admin_b).must_be_kind_of Block + end + + it "establishes the base data structures when instantiated" do + expect(@admin_b.discount).must_equal 0.10 + end + end + + describe "make_reservation" do + it "will only allow to the period same as the block set up period" do + @admin_b.make_reservation("Mike lee") + expect(@admin_b.reservations.first.start_date).must_equal Date.new(2018,10,5) + expect(@admin_b.reservations.first.end_date).must_equal Date.new(2018,10,10) + end + end + + describe "calculate_cost" do + + it "returns a right number" do + @admin_b.make_reservation("Mike lee") + res_id = @admin_b.reservations.first.id + + expect(@admin_b.calculate_cost(res_id)).must_equal 900.0 + end + end +end diff --git a/spec/reservation_spec.rb b/spec/reservation_spec.rb new file mode 100644 index 000000000..9fb1ca87d --- /dev/null +++ b/spec/reservation_spec.rb @@ -0,0 +1,84 @@ +require_relative 'spec_helper' +require 'pry' + +describe "Reservation in Wave 1" do + before do + + @customer_name = "Mike Bogdon" + @start_date = Date.new(2018,10,2) + @room = Room.new(1) + end + describe "#initialize" do + it "Takes a reservation start_date, end_date " do + end_date = Date.new(2018,10,5) + rsv_1 = Reservation.new(@customer_name, @room, @start_date, end_date) + expect(rsv_1).must_respond_to :id + expect(rsv_1).must_respond_to :start_date + expect(rsv_1).must_respond_to :end_date + expect(rsv_1).must_respond_to :customer_name + expect(rsv_1).must_respond_to :room + + expect(rsv_1.start_date).must_equal @start_date + expect(rsv_1.end_date).must_equal end_date + expect(rsv_1.customer_name).must_equal @customer_name + expect(rsv_1.room).must_equal @room + end + + it "Raises an ArgumentError for invalid date input, end_date is not later than start_date" do + end_date = Date.new(2018,10,2) + expect { + Reservation.new(@customer_name, @room, @start_date, end_date) + }.must_raise ArgumentError + end + + it "Raises an ArgumentError for invalid date input, start_date is a day earlier than today" do + start_date_2= Date.new(2018,8,2) + end_date_2 = Date.new(2018,10,2) + expect { + Reservation.new(@customer_name,@room, start_date_2, end_date_2) + }.must_raise ArgumentError + end + end + + describe "#dates_booked" do + before do + @end_date = Date.new(2018,10,6) + @rsv_2 = Reservation.new(@customer_name,@room, @start_date, @end_date) + end + it "returns an array" do + expect(@rsv_2.dates_booked).must_be_kind_of Array + end + it "returns an array of date" do + expect(@rsv_2.dates_booked.first).must_be_kind_of Date + expect(@rsv_2.dates_booked.first).must_equal @start_date + end + + it "end_date should not be included into the array" do + expect(@rsv_2.dates_booked.include?@end_date).must_equal false + end + end + + describe "#reserve_cost" do + before do + @end_date = Date.new(2018,10,6) + @rsv_3 = Reservation.new(@customer_name,@room, @start_date, @end_date) + end + it "returns a number" do + expect(@rsv_3.reserve_cost).must_be_kind_of Numeric + end + it "do the calculation correctly" do + expect(@rsv_3.reserve_cost).must_equal 800 + end + + it "calculation correctly for a one day stay" do + id = 5 + customer_name = "Jezz Bogdon" + start_date = Date.new(2018,10,5) + end_date = Date.new(2018,10,6) + room = Room.new(4) + rsv_4 = Reservation.new(customer_name,room,start_date,end_date) + expect(rsv_4.reserve_cost).must_equal 200 + end + end + +end diff --git a/spec/room_spec.rb b/spec/room_spec.rb new file mode 100644 index 000000000..2202f27e7 --- /dev/null +++ b/spec/room_spec.rb @@ -0,0 +1,56 @@ +require_relative 'spec_helper' + + +describe "Room in Wave 1" do + describe "#initialize" do + it "Takes a room number" do + id = 1 + room_1 = Room.new(id) + + expect(room_1).must_respond_to :room_num + expect(room_1.room_num).must_equal id + expect(room_1.rate).must_equal 200 + # expect(room_1.reserve_dates).must_equal [] + + end + + it "Raises an ArgumentError for invalid room numbers" do + expect { + Room.new('a') + }.must_raise ArgumentError + expect { + Room.new(21) + }.must_raise ArgumentError + end + end + + # describe "# add_reserve_dates" do + # before do + # @room_1 = Room.new(3) + # @date1 = Date.new(2018,10,4) + # @date2 = Date.new(2018,10,5) + # @new_reserve_dates = [ @date1, @date2 ] + # @previous = @room_1.reserve_dates.length + # @room_1.add_reserve_dates(@new_reserve_dates) + # end + # + # it "increases the reserve_dates array length correctly" do + # expect(@room_1.reserve_dates.length).must_equal @previous + 2 + # end + # + # it "the updated reseve_dates array should include the newly added dates" do + # expect(@room_1.reserve_dates).must_include @date1 + # expect(@room_1.reserve_dates).must_include @date2 + # end + # + # it "throw ArgumentError if attemp to add a specific date to the list twice or more" do + # expect{ @room_1.add_reserve_dates([@date1]) }.must_raise ArgumentError + # end + # + # it "if not adding an array of dates, throw ArgumentError " do + # expect{ @room_1.add_reserve_dates("date") }.must_raise ArgumentError + # expect{ @room_1.add_reserve_dates(["date1","date2"]) }.must_raise ArgumentError + # end + # end + +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 4d1e3fdc8..518314421 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,8 +1,16 @@ +require 'simplecov' require 'minitest' +SimpleCov.start do + add_filter "/spec/" +end require 'minitest/autorun' require 'minitest/reporters' -# Add simplecov + + Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new -# Require_relative your lib files here! +require_relative '../lib/admin' +require_relative '../lib/reservation' +require_relative '../lib/room' +require_relative '../lib/block_admin'