Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Book_Inventory.adb
- -- Written by James Halvorsen (W00974148)
- with Ada.Text_IO; use Ada.Text_IO;
- with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
- with Ada.Command_Line; use Ada.Command_Line;
- -- I was thinking about using a linked list to store my inventory
- -- With the Queue_Generic to store back orders as required
- -- But then I realized you never said we couldn't use Ada's prefedined types.
- -- So here's to laziness. Because with the time I could have used making my own
- -- Storage system, I could have used it doing much more productive things, like
- -- Writing this wonderfully useless comment.
- with Ada.Containers.Vectors; use Ada.Containers;
- with Queue_Generic;
- procedure book_inventory is
- subtype ISBN_Type is String(1 .. 10);
- type Dollars is delta 0.01 digits 6 range 0.0 .. 9999.0;
- type Book is record
- ISBN : ISBN_Type;
- Price : Dollars;
- Amount : Natural;
- end record;
- type Back_Order is record
- Customer_Number : Natural;
- ISBN : ISBN_Type;
- Amount : Natural;
- end record;
- package BOQ is new Queue_Generic (Back_Order); -- Back order queue
- package Book_Storage is new Vectors (Positive, Book);
- package Dollar_IO is new Decimal_IO (Dollars); use Dollar_IO;
- Back_Order_Queue : BOQ.Queue_Type;
- Inventory_File : File_Type;
- Actions_File : File_Type;
- Wrong_Args : exception;
- Inventory : Book_Storage.Vector;
- Revenue : Dollars := 0.00;
- -- Reads the input file and fills Inventory with it
- procedure Inventory_From_File (Initial_Inventory : in File_Type) is
- Temp_Book : Book;
- begin
- while not End_Of_File (Initial_Inventory) loop
- -- Read fields for a Book Type
- Get(Initial_Inventory, Temp_Book.ISBN);
- Get(Initial_Inventory, Temp_Book.Price);
- Get(Initial_Inventory, Temp_Book.Amount);
- -- Attach to a vector and reset fields
- Inventory.Append(Temp_Book);
- Temp_Book.ISBN := "0000000000";
- Temp_Book.Price := 0.00;
- Temp_Book.Amount := 0;
- end loop;
- end Inventory_From_File;
- -- Handles STOCK and ORDER requests
- procedure Execute_Orders (Order_File : in File_Type) is
- Order_Type : String (1 .. 5);
- Ignore_Space : Character;
- ISBN : ISBN_Type;
- Num_Purchased : Natural;
- Customer_Number : Natural;
- Temp_Back_Order : Back_Order;
- Temp_Book : Book;
- Book_Found : Boolean := false;
- Book_Index : Positive;
- begin
- while not End_Of_File (Order_File) loop
- -- Read fields for the order
- Get(Order_File, Order_Type);
- Get(Order_File, Ignore_Space);
- Get(Order_File, ISBN);
- Get(Order_File, Num_Purchased);
- if (Order_Type = "ORDER") then
- Get(Order_File, Customer_Number);
- end if;
- if (Order_Type = "STOCK") then
- -- Check if back orders exist
- -- If back orders exist
- -- Fill out as many as possible with
- -- The new stock
- -- If no back orders exist
- -- Add book to inventory
- -- Be sure to check if similar books
- -- Exist, or if books need to be pushed
- -- Into the vector
- null;
- elsif (Order_Type = "ORDER") then
- -- Check to see if books are available
- for I in Inventory.First_Index .. Inventory.Last_Index loop
- if Inventory.Element(Index => I).ISBN = ISBN then
- Book_Found := true;
- Book_Index := I;
- end if;
- end loop;
- if Book_Found then
- -- All books required are in inventory
- if Inventory.Element(Index => Book_Index).Amount >= Num_Purchased then
- Put("Order for customer ");
- Put(Item => Customer_Number, Width => 0);
- Put(" For "); Put(Item => Num_Purchased, Width => 0);
- Put(" Copies of book "); Put(ISBN);
- new_line;
- Temp_Book := Inventory.Element(Index => Book_Index);
- Revenue := Revenue + (Temp_Book.Price * Num_Purchased);
- Temp_Book.Amount := Temp_Book.Amount - Num_Purchased;
- Book_Storage.Replace_Element(Inventory, Book_Index, Temp_Book);
- -- None in stock
- elsif Inventory.Element(Index => Book_Index).Amount = 0 then
- Temp_Back_Order.Customer_Number := Customer_Number;
- Temp_Back_Order.ISBN := ISBN;
- Temp_Back_Order.Amount := Num_Purchased;
- BOQ.Enqueue(Back_Order_Queue, Temp_Back_Order);
- -- Some in stock, not sufficient for all of order
- else
- end if;
- else
- Temp_Back_Order.Customer_Number := Customer_Number;
- Temp_Back_Order.ISBN := ISBN;
- Temp_Back_Order.Amount := Num_Purchased;
- BOQ.Enqueue(Back_Order_Queue, Temp_Back_Order);
- end if;
- else
- null;
- -- Throw an exception, nothing other than
- -- Stock or order should exist.
- end if;
- skip_line(Order_File);
- end loop;
- end Execute_Orders;
- -- Main Procedure, handles command line arguments then passes them off
- -- To Inventory_From_File and Execute_Orders
- begin
- if Argument_Count /= 2 then
- raise Wrong_Args;
- end if;
- Open( File => Inventory_File, Mode => In_File, Name => Argument(1) );
- Inventory_From_File(Inventory_File);
- Close(Inventory_File);
- Open( File => Actions_File, Mode => In_File, Name => Argument(2) );
- Execute_Orders(Actions_File);
- Close(Actions_File);
- exception
- when Wrong_Args =>
- Put("Usage: book_inventory InventoryFile TransactionsFile");
- new_line;
- when Name_Error =>
- Put("One or more files inputted does not exist");
- new_line;
- end book_inventory;
Add Comment
Please, Sign In to add comment