Guest User

Untitled

a guest
Apr 19th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ada 5.25 KB | None | 0 0
  1. -- Book_Inventory.adb
  2. -- Written by James Halvorsen (W00974148)
  3.  
  4. with Ada.Text_IO; use Ada.Text_IO;
  5. with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
  6. with Ada.Command_Line; use Ada.Command_Line;
  7.  
  8. -- I was thinking about using a linked list to store my inventory
  9. -- With the Queue_Generic to store back orders as required
  10. -- But then I realized you never said we couldn't use Ada's prefedined types.
  11. -- So here's to laziness. Because with the time I could have used making my own
  12. -- Storage system, I could have used it doing much more productive things, like
  13. -- Writing this wonderfully useless comment.
  14. with Ada.Containers.Vectors; use Ada.Containers;
  15. with Queue_Generic;
  16.  
  17. procedure book_inventory is
  18.  
  19.     subtype ISBN_Type is String(1 .. 10);
  20.     type Dollars is delta 0.01 digits 6 range 0.0 .. 9999.0;
  21.     type Book is record
  22.         ISBN : ISBN_Type;
  23.         Price : Dollars;
  24.         Amount : Natural;
  25.     end record;
  26.     type Back_Order is record
  27.         Customer_Number : Natural;
  28.         ISBN : ISBN_Type;
  29.         Amount : Natural;
  30.     end record;
  31.  
  32.     package BOQ is new Queue_Generic (Back_Order); -- Back order queue
  33.     package Book_Storage is new Vectors (Positive, Book);
  34.     package Dollar_IO is new Decimal_IO (Dollars); use Dollar_IO;
  35.    
  36.     Back_Order_Queue : BOQ.Queue_Type;
  37.     Inventory_File : File_Type;
  38.     Actions_File : File_Type;
  39.     Wrong_Args : exception;
  40.     Inventory : Book_Storage.Vector;
  41.     Revenue : Dollars := 0.00;
  42.    
  43.     -- Reads the input file and fills Inventory with it
  44.     procedure Inventory_From_File (Initial_Inventory : in File_Type) is
  45.         Temp_Book : Book;
  46.     begin
  47.         while not End_Of_File (Initial_Inventory) loop
  48.             -- Read fields for a Book Type         
  49.             Get(Initial_Inventory, Temp_Book.ISBN);
  50.             Get(Initial_Inventory, Temp_Book.Price);
  51.             Get(Initial_Inventory, Temp_Book.Amount);
  52.            
  53.             -- Attach to a vector and reset fields
  54.             Inventory.Append(Temp_Book);
  55.             Temp_Book.ISBN := "0000000000";
  56.             Temp_Book.Price := 0.00;
  57.             Temp_Book.Amount := 0;
  58.  
  59.         end loop;
  60.     end Inventory_From_File;
  61.    
  62.     -- Handles STOCK and ORDER requests
  63.     procedure Execute_Orders (Order_File : in File_Type) is
  64.         Order_Type : String (1 .. 5);
  65.         Ignore_Space : Character;
  66.         ISBN : ISBN_Type;
  67.         Num_Purchased : Natural;
  68.         Customer_Number : Natural;
  69.  
  70.         Temp_Back_Order : Back_Order;
  71.         Temp_Book : Book;
  72.         Book_Found : Boolean := false;
  73.         Book_Index : Positive;
  74.        
  75.     begin
  76.         while not End_Of_File (Order_File) loop
  77.             -- Read fields for the order
  78.             Get(Order_File, Order_Type);
  79.             Get(Order_File, Ignore_Space);
  80.             Get(Order_File, ISBN);
  81.             Get(Order_File, Num_Purchased);
  82.            
  83.             if (Order_Type = "ORDER") then
  84.                 Get(Order_File, Customer_Number);
  85.             end if;
  86.  
  87.             if (Order_Type = "STOCK") then
  88.                 -- Check if back orders exist
  89.                 -- If back orders exist
  90.                     -- Fill out as many as possible with
  91.                     -- The new stock
  92.                 -- If no back orders exist
  93.                     -- Add book to inventory
  94.                     -- Be sure to check if similar books
  95.                     -- Exist, or if books need to be pushed
  96.                     -- Into the vector
  97.                 null;
  98.             elsif (Order_Type = "ORDER") then
  99.                 -- Check to see if books are available
  100.                 for I in Inventory.First_Index .. Inventory.Last_Index loop
  101.                     if Inventory.Element(Index => I).ISBN = ISBN then
  102.                         Book_Found := true;
  103.                         Book_Index := I;
  104.                     end if;
  105.                 end loop;
  106.                 if Book_Found then
  107.                     -- All books required are in inventory
  108.                     if Inventory.Element(Index => Book_Index).Amount >= Num_Purchased then
  109.                         Put("Order for customer ");
  110.                         Put(Item => Customer_Number, Width => 0);
  111.                         Put(" For "); Put(Item => Num_Purchased, Width => 0);
  112.                         Put(" Copies of book "); Put(ISBN);
  113.                         new_line;
  114.                        
  115.                         Temp_Book := Inventory.Element(Index => Book_Index);
  116.                         Revenue := Revenue + (Temp_Book.Price * Num_Purchased);
  117.                         Temp_Book.Amount := Temp_Book.Amount - Num_Purchased;
  118.                         Book_Storage.Replace_Element(Inventory, Book_Index, Temp_Book);
  119.                     -- None in stock               
  120.                     elsif Inventory.Element(Index => Book_Index).Amount = 0 then
  121.                         Temp_Back_Order.Customer_Number := Customer_Number;
  122.                         Temp_Back_Order.ISBN := ISBN;
  123.                         Temp_Back_Order.Amount := Num_Purchased;
  124.                         BOQ.Enqueue(Back_Order_Queue, Temp_Back_Order);
  125.                     -- Some in stock, not sufficient for all of order
  126.                     else
  127.                        
  128.                     end if;
  129.                 else
  130.                     Temp_Back_Order.Customer_Number := Customer_Number;
  131.                     Temp_Back_Order.ISBN := ISBN;
  132.                     Temp_Back_Order.Amount := Num_Purchased;
  133.                     BOQ.Enqueue(Back_Order_Queue, Temp_Back_Order);
  134.                 end if;
  135.             else
  136.                 null;
  137.                 -- Throw an exception, nothing other than
  138.                 -- Stock or order should exist.
  139.             end if;
  140.             skip_line(Order_File);
  141.         end loop;
  142.     end Execute_Orders;
  143.  
  144. -- Main Procedure, handles command line arguments then passes them off
  145. -- To Inventory_From_File and Execute_Orders
  146. begin
  147.     if Argument_Count /= 2 then
  148.         raise Wrong_Args;
  149.     end if;
  150.  
  151.     Open( File => Inventory_File, Mode => In_File, Name => Argument(1) );
  152.     Inventory_From_File(Inventory_File);
  153.     Close(Inventory_File);
  154.    
  155.     Open( File => Actions_File, Mode => In_File, Name => Argument(2) );
  156.     Execute_Orders(Actions_File);
  157.     Close(Actions_File);
  158. exception
  159.     when Wrong_Args =>
  160.         Put("Usage: book_inventory InventoryFile TransactionsFile");
  161.         new_line;
  162.     when Name_Error =>
  163.         Put("One or more files inputted does not exist");
  164.         new_line;
  165.  
  166. end book_inventory;
Add Comment
Please, Sign In to add comment