package body Bookstore.Carts is

   --------------------------
   -- Create_Shopping_Cart --
   --------------------------
5
   function Create_Shopping_Cart return Shopping_Cart is
      Cart : Shopping_Cart; -- default values
   begin
      return Cart;
10   end Create_Shopping_Cart;

   ---------
   -- Add --
   ---------
15
   procedure Add
     (Cart : in out Shopping_Cart;
      ID   : in     Books.Id_Str;
      Book : in     Books.Book)
20   is
      use type Item_Maps.Cursor;
      Position : Item_Maps.Cursor := Cart.Items.Find (ID);
   begin
      if Position = Item_Maps.No_Element then
25         Cart.Items.Insert (ID, Cart_Items.Create_Shopping_Cart_Item (Book));
      else
         declare
            procedure Increment
              (ID      : Books.Id_Str;
30               Element : in out Cart_Items.Shopping_Cart_Item)
            is
            begin
               Element.Increment_Quantity;
            end Increment;
35         begin
            Cart.Items.Update_Element (Position, Increment'Access);
         end;
      end if;

40      Cart.Number_Of_Items := Cart.Number_Of_Items + 1;   -- java code forget
                                                --  to increment
   end Add;

   ------------
45   -- Remove --
   ------------

   procedure Remove (Cart : in out Shopping_Cart;
                     ID   : in     Books.Id_Str) is
50      use type Item_Maps.Cursor;
      use type Cart_Items.Quantity;

      Position : Item_Maps.Cursor := Cart.Items.Find (ID);
   begin
55      if Position = Item_Maps.No_Element then
         raise Constraint_Error; -- java code doesn't raise an
                                 --  error : is it right ?
      elsif Item_Maps.Element (Position).Get_Quantity > 1 then
         declare
60            procedure Decrement
              (ID      : Books.Id_Str;
               Element : in out Cart_Items.Shopping_Cart_Item)
            is
            begin
65               Element.Decrement_Quantity;
            end Decrement;
         begin
            Cart.Items.Update_Element (Position, Decrement'Access);
         end;
70      else -- quantity = 1
         Cart.Items.Delete (ID);
      end if;

      Cart.Number_Of_Items := Cart.Number_Of_Items - 1;
75   end Remove;

   -----------
   -- Clear --
   -----------
80
   procedure Clear (Cart : in out Shopping_Cart) is
   begin
      Cart.Items.Clear;
      Cart.Number_Of_Items := 0;
85   end Clear;

   -------------------------
   -- Get_Number_Of_Items --
   -------------------------
90
   function Get_Number_Of_Items (Cart : in Shopping_Cart) return Cart_Size is
   begin
      return Cart.Number_Of_Items;
   end Get_Number_Of_Items;
95
   ---------------
   -- Get_Total --
   ---------------

100   function Get_Total (Cart : in Shopping_Cart) return Books.Value is
      use Item_Maps;
      use type Books.Value;

      Amount : Books.Value := 0.0;
105
      procedure Update_Amount (Position : in Cursor) is
         Item : Cart_Items.Shopping_Cart_Item := Element (Position);
      begin
         Amount := Amount
110           + Books.Value (Item.Get_Quantity)
           * Item.Get_Item.Get_Price;
      end Update_Amount;

   begin
115      Cart.Items.Iterate (Update_Amount'Access);
      return Amount;
   end Get_Total;

   ---------------
120   -- Get_Items --
   ---------------

   function Get_Items (Cart : in Shopping_Cart) return Cart_Items.Lists.List is
      use Cart_Items;
125
      Shop_Item_List : Lists.List := Lists.Empty_List;

      procedure Update_List (Position : in Hashed_Maps.Cursor) is
      begin
130         Shop_Item_List.Append (Hashed_Maps.Element (Position));
      end Update_List;
   begin
      Cart.Items.Iterate (Update_List'Access);
      return Shop_Item_List;
135   end Get_Items;

end Bookstore.Carts;
top