-- -- This module is submitted as part of the assessment for -- COMP1100. -- Its completion is substanitally my own work. -- (signed) Joseph Curtis 3952239 -- class OPT_STOCK_CONTROL -- program to manage stock in a department store creation make feature stocklist : ARRAY[STORE_ITEM] -- the items stocked by the store make is local do !!stocklist.make(1, 0) exec_commands end -- make exec_commands is -- Read in and execute commands. local s : STRING command : CHARACTER do from command := 'm' until (command = 'q') or (command = 'Q') loop inspect command when 'm', 'M' then print_menu when 'l', 'L' then load_data when 'p', 'P' then print_stocklist when 'n', 'N' then new_item when 'i', 'I' then item_report when 's', 'S' then sell_item when 'r', 'R' then receive_delivery when 'v', 'V' then value_stock when 'a', 'A' then save_data else io.put_string("Invalid command ") io.put_character(command); io.put_new_line print_menu end -- inspect -- get next command io.put_string("%NCommand : ") io.skip_separators io.read_line; s := io.last_string if s.count > 0 then command := s.item(1) else command := 'm' end -- if io.put_new_line end -- loop end -- exec_commands print_menu is -- Print menu of available commands. do io.put_string("Menu of commands%N") io.put_string("--------------------------------------%N") io.put_string(" m: menu - print this menu%N") io.put_string(" l: load stocklist data from file%N") io.put_string(" a: save stocklist data to file%N") io.put_string(" p: print all stock info%N") io.put_string(" n: add new store item%N") io.put_string(" i: item - print info on a store item%N") io.put_string(" s: sell an item%N") io.put_string(" r: receive delivery of an item in stocklist%N") io.put_string(" v: report total wholsale value of stock%N") io.put_string(" q: quit%N") end -- print_menu load_data is -- Fill stocklist array with items read from a file. require stocklist /= void local infile : STD_FILE_READ -- input file to read from infilename : STRING -- filename of input file item : STORE_ITEM -- store item ic : INTEGER -- itemcode of item desc : STRING -- description of item wp : REAL -- wholesale price of item rp : REAL -- retail price of item qty : INTEGER -- quantity in stock of item do -- connect to file io.put_string("Name of file containing stock information : ") io.read_word infilename := clone(io.last_string) !!infile.connect_to(infilename) if not infile.is_connected then io.put_string("%NError in connecting to input file%N") else -- load stock data from infile from -- read first itemcode infile.read_integer until infile.end_of_input loop ic := infile.last_integer -- description infile.read_word; desc := clone(infile.last_string) -- wholesale price infile.read_real; wp := infile.last_real -- retail price infile.read_real; rp := infile.last_real -- quantity in stock infile.read_integer; qty := infile.last_integer -- create item and add to stock list !!item.make(ic, desc, wp, rp, qty) stocklist.add_last(item) -- read next itemcode infile.read_integer end -- loop end -- if end -- load_data save_data is -- Fill stocklist array with items read from a file. require stocklist /= void local outfile : STD_FILE_WRITE -- output file to write from outfilename : STRING -- filename of output file i : INTEGER do -- get file name from user io.put_string("Name of file to save stock information : ") io.read_word outfilename := clone(io.last_string) -- connect to file !!outfile.connect_to(outfilename) -- check to see if you can connect if not outfile.is_connected then io.put_string("%NError in connecting to input file%N") else -- write stock data to outfile -- loop for all stock items from i:=stocklist.lower until i>stocklist.upper loop -- save the data to the file in the correct format outfile.put_integer(stocklist.item(i).itemcode) outfile.put_new_line outfile.put_string(stocklist.item(i).description) outfile.put_new_line outfile.put_real(stocklist.item(i).wholesale_price) outfile.put_new_line outfile.put_real(stocklist.item(i).retail_price) outfile.put_new_line outfile.put_integer(stocklist.item(i).qty_in_stock) outfile.put_new_line i:=i+1 end -- loop end -- if end -- load_data print_stocklist is --This routine prints out stock data for all stock --in a list. local i:INTEGER do -- Print out the heading and the list titles. io.put_string("Stock list%N") io.put_string("==========") io.put_new_line io.put_string("Itemcode Description Wholesale ") io.put_string("Retail Quantity") io.put_new_line io.put_string("----------------------------------") io.put_string("-----------------") io.put_new_line -- Loop to print out all the data from i:=stocklist.lower until i>stocklist.upper loop stocklist.item(i).print_store_item -- print out data for an itemcode i:=i+1 end end -- print_stock new_item is -- This routine receives data from the user for a new stock -- item. It then creates the store item and adds it to the -- stocklist. local icode : INTEGER -- Itemcode desc : STRING -- Description wp : REAL -- Wholesale price rp : REAL -- Retail price go : BOOLEAN -- if the user wants to go back to the menu or not s: STRING -- temporary storage for user answer choice : CHARACTER -- the user's choice item : STORE_ITEM -- temporary store item do io.put_string(" Itemcode : ") -- get itemcode from user io.read_integer;icode:=io.last_integer io.put_string(" Description : ") -- Get description from user io.read_line;io.read_line;desc:=clone(io.last_string) from go:=false until -- loop again if the wholesale price is higher than -- the retail price go loop io.put_string(" Wholesale price : ") -- Get wholesale price io.read_real;wp:=io.last_real io.put_string(" Retail price : ") -- get retail price io.read_real;rp:=io.last_real -- if the retail price is less than the wholesale -- price then prompt the user to either continue or -- re-enter it. if (rp < wp) then io.put_new_line io.put_string("The retail price you entered ") io.put_string("is less than the wholesale price.%N") io.put_string("[C]ontinue as is or [R]e-enter prices: ") io.skip_separators io.read_line; s:=clone(io.last_string) -- make sure the choice is valid if s.count > 0 then choice := s.item(1) else choice := 'r' end -- if inspect choice when 'c', 'C' then go :=true when 'r', 'R' then go := false end else go := true end end -- Create the temporary item with the user imputs !!item.make(icode, desc, wp, rp, 0) -- Add the item to the stocklist stocklist.add_last(item) end -- new_item item_report is --This routine prints out information about a stock time, given --the itemcode. local ic:INTEGER -- itemcode index_item : INTEGER -- index of item valid : BOOLEAN -- valid itemcode quit : BOOLEAN -- user wants to go back to the menu s : STRING -- user's input choice : CHARACTER --user's choice do from valid := false until valid -- end loop if itemcode is valid loop io.put_string(" Itemcode : ") -- get itemcode from user io.read_integer;ic:=io.last_integer -- get index of item index_item:=index_of_item(ic) -- check to see if itemcode is valid if (index_item = stocklist.upper + 1) then valid := false io.put_new_line io.put_string("Warning! Invalid itemcode.") io.put_new_line; io.put_string("[B]ack to the menu or [R]e-enter itemcode : ") -- get user's input io.read_line io.read_line; s:=clone(io.last_string) -- check for valid input if s.count > 0 then choice := s.item(1) else choice := 'r' end -- if inspect choice when 'b', 'B' then quit :=true;valid:=true when 'r', 'R' then valid := false end io.put_new_line else valid:=true end end if (not quit) then -- Print out the correct data stocklist.item(index_item).print_store_item end end -- item_report sell_item is -- This routine prompts the user for an itemcode and an amount -- to sell. It uses the 'sell_stock' routine of the STOCK_ITEM -- class to remove the correct amount from the 'quantity' attribute. -- It then displays the itemcode, number sold, retail price and -- then the total price. local ic:INTEGER -- itemcode qs:INTEGER -- quantity sold price:REAL -- total price of goods (retail price * quantity) i:INTEGER --index of item valid : BOOLEAN -- valid itemcode quit: BOOLEAN -- user wants to quit back to the menu index_item : INTEGER -- index of item s:STRING -- temporary storage for user's answer choice:CHARACTER -- user's choice do from valid := false until valid -- loop again if invalid itemcode loop -- get user's input io.put_string(" Itemcode : ") io.read_integer;ic:=io.last_integer -- get index of item index_item:=index_of_item(ic) -- check to see if index is valid if (index_item = stocklist.upper + 1) then valid := false io.put_new_line -- prompt the user to either return to the menu or re-enter -- the itemcode io.put_string("Warning! Invalid itemcode.") io.put_new_line; io.put_string("[B]ack to the menu or [R]e-enter itemcode : ") io.read_line io.read_line; s:=clone(io.last_string) -- check for a valid choice if s.count > 0 then choice := s.item(1) else choice := 'r' end -- if inspect choice when 'b', 'B' then quit :=true;valid:=true when 'r', 'R' then valid := false end io.put_new_line else valid:=true end end if (not quit) then -- get the quantity sold io.put_string(" Quantity sold : ") io.read_integer;qs:=io.last_integer i:=index_item -- get index of item -- evaluate the total price price:=qs*stocklist.item(i).retail_price -- adjust the quantity in the stocklist stocklist.item(i).sell_stock(qs) -- print out the info to the screen io.put_new_line io.put_integer_format(ic, 8) io.put_integer_format(qs, 4) io.put_string(" @ ") stocklist.item(i).my_print_real_format( stocklist.item(i).retail_price, 4, 2) stocklist.item(i).my_print_real_format(price, 8, 2) io.put_new_line end end -- sell_item receive_delivery is --This routine prompts the user for an itemcode and a quantity. -- It uses index_of_item to get the index and then the add_stock -- routine in STORE_ITEM to update the quantity variable. local ic:INTEGER -- itemcode quantity:INTEGER -- quantity received i:INTEGER -- index of item valid : BOOLEAN -- valid itemcode quit : BOOLEAN -- user wants to quit to menu choice : STRING -- user's input do from valid := false until valid loop -- get input from user io.put_string(" Itemcode : ") io.read_integer;ic:=io.last_integer i:=index_of_item(ic) -- get index of item if (i = stocklist.upper+1) then valid:=false io.put_new_line -- prompt the user to either return to the menu or re-enter -- the itemcode io.put_string("Warning! Invalid itemcode.") io.put_new_line; io.put_string("[B]ack to the menu or [R]e-enter itemcode : ") io.read_line io.read_line; choice:=clone(io.last_string) -- check for a valid choice if choice.count > 0 then inspect choice.item(1) when 'b', 'B' then quit :=true;valid:=true when 'r', 'R' then valid := false end io.put_new_line else valid := false end -- if else valid:=true end end if (not quit) then io.put_string(" Quantity received : ") io.read_integer;quantity:=io.last_integer -- update quantity in stocklist stocklist.item(i).add_stock(quantity) end end -- receive_delivery value_stock is -- This routine evaluates to total wholesale value of the stock -- in the list. To get this the quantity is multiplied by the -- wholesale price for every item in the stocklist and this is added -- up to get the total. local value : REAL -- total value of stock i : INTEGER do from -- Loop for all items in the stocklist i:=stocklist.lower until i>stocklist.upper loop -- evaluate the wholasale value for an item and then add -- it to the total sum. value:=value+(stocklist.item(i).wholesale_price* stocklist.item(i).qty_in_stock) i:=i+1 end -- Print this information to the screen io.put_string("Total wholesale value of stock is $") stocklist.item(1).my_print_real_format(value,8,2) io.put_new_line end -- value_stock index_of_item(icode : INTEGER) : INTEGER is -- Return the index in the stocklist array for the store item with -- itemcode icode. If there is no item with itemcode icode, -- then return stocklist.upper + 1 local i : INTEGER do from i := stocklist.lower until i > stocklist.upper or else (stocklist.item(i).itemcode = icode) loop i := i + 1 end --loop Result := i end -- index_of_item end -- class OPT_STOCK_CONTROL