Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

OLAII2 Project Due: April9 201s BACKGROUND: You have been hired by the ACME Supp

ID: 3703449 • Letter: O

Question

OLAII2 Project Due: April9 201s BACKGROUND: You have been hired by the ACME Supply Corporation to write a program to merge the inventory records of two newly acquired stores. Although the inventory records of these two stores are very similar, they need a programmer to conflate the two sets of files, remove any erroneous inventory records, and standardize the inventory measures. TASK: Write a program to merge the two inventory files. The format of the inventory files is described below. Each unique item of inventory has been assigned a unique SKU number. This SKU number is a self-checking number consisting of a base number and a modulus-10 check- digit suffix. Both files are sorted in SKU number order and the resulting merged file must be in proper order. Note that some inventory items have incorrect SKU numbers (i.e., the self-check fails on these numbers), these items should not be placed in the merged file but should be sent to a reject file, as described below. Note also that if the identical SKU item appears in both input files, then the item amounts should be suitably combined and reported as one inventory record COMPLICATIONS: The inventory measures are not uniformly consistent. Some inventory items have amounts expressed in imperial (aka english; aka U.S. Customary Measure) units. The company needs all inventory measures to be in metric units. Thus if an inventory amount is expressed as an imperial amount, it must be converted to the equivalent metric amount in the merged file. The units of measure (expressed as character codes) that are within the files are: Square Inches C Square Centimeters P Pounds (avoirdupois) F Feet Kilos M Heters E Each INPUTIOUTPUT: The format for both the input and the output files is: Positions 01-09 10-18 19-19 20-20 Description SKU Number (right-justified) Amount (xxxxxxx.x) Unit of Measure (see above) NOTE: The amount should have one decimal place. Round as necessary REJECT FILE: Any inventory records that have invalid inventory numbers are written to the reject file. Do not convert units of measure nor attempt to merge them; these records should look exactly like they did on input. IMPLEMENTATION NOTES: For the completed version of this program, use as your source file name "consolidate.py". A starter skeleton program, is available as Be sure the first function in your source file is main0 and list the remaining functions in hierarchical order (as discussed when we reviewed hierarchy charts). Do not use any global variables. The input files will be store1.dat and "store2.dat": you will need to make a (virtual) copy of these files in your directory. To make these virtual copies (i.e. symbolic links), use the commands S 1n -s SPUB/storel.dat storel.dat S1n -s SPUB/store2.dat store2.dat The merged output file must be named "merged.dat". The reject output file must be named reject.dat" INSPIRATION: Pattern your conflation logic after the example SPUB/conflate.py we developed in class.

Explanation / Answer

PROGRAM:

import os.path

# File name constants

FILE_NAME1 = "store1.dat"

FILE_NAME2 = "store2.dat"

MERGED_FILE = "merged.dat"

REJECT_FILE = "reject.dat"

def main():

# Open the input files

if not os.path.isfile(FILE_NAME1) or not os.path.isfile(FILE_NAME2):

return

file1 = open(FILE_NAME1,'r')

file2 = open(FILE_NAME2,'r')

merged = open(MERGED_FILE, 'w')

reject = open(REJECT_FILE, 'w')

# Conflate inventory items from the two input files.

eof1, sku1, amt1, uom1 = obtain_record(file1)

eof2, sku2, amt2, uom2 = obtain_record(file2)

while not eof1 or not eof2:

# ...remove and replace with working code

# Hint: Pattern your logic around that of $PUB/conflate.py

if eof2 or (not eof1 and sku1 < sku2):

# print(itemA)

if is_valid_sku(sku1):

amt1, uom1=convert_to_metric(amt1, uom1)

amt1=round(amt1,1)

string='%9s%9s %s'%(sku1,amt1,uom1)

merged.write(string+' ')

else:

string='%9s%9s %s'%(sku1,amt1,uom1)

reject.write(string+' ')

eof1, sku1, amt1, uom1 = obtain_record(file1)

elif eof1 or sku1 > sku2:

# print(itemB)

if is_valid_sku(sku2):

amt2, uom2=convert_to_metric(amt2, uom2)

amt2=round(amt2,1)

string='%9s%9s %s'%(sku2,amt2,uom2)

merged.write(string+' ')

else:

string='%9s%9s %s'%(sku2,amt2,uom2)

reject.write(string+' ')

eof2, sku2, amt2, uom2 = obtain_record(file2)

else: # itemA == itemB

# print(itemA)

if is_valid_sku(sku1):

amt1, uom1=convert_to_metric(amt1, uom1)

amt2, uom2=convert_to_metric(amt2, uom2)

amt1=round(amt1,1)

amt2=round(amt2,1)

string='%9s%9s %s'%(sku1,amt1+amt2,uom1)

merged.write(string+' ')

else:

string='%9s%9s %s'%(sku1,amt1,uom1)

reject.write(string+' ')

string='%9s%9s %s'%(sku2,amt2,uom2)

reject.write(string+' ')

eof1, sku1, amt1, uom1 = obtain_record(file1)

eof2, sku2, amt2, uom2 = obtain_record(file2)

# Close all files.

file1.close()

file2.close()

merged.close()

reject.close()

# Obtain an inventory item from specified file.

def obtain_record(file):

record = file.readline()

if record=='':

eof = True

return eof, 0, 0.0, ''

else:

eof = False

sku, amt, unit_of_measure = record.split()

return eof, int(sku), float(amt), unit_of_measure

# Determine validity of SKU number

def is_valid_sku(sku):

inventory_number = sku // 10

check_suffix = sku % 10

if check_digit(inventory_number)==check_suffix:

return True

else:

return False

# Calculate the Modulus-10 check-digit of the input parameter.

def check_digit(number):

crossfoot = 0

while number>0:

product = number%10 * 2

crossfoot += product//10 + product%10

number //= 10

crossfoot += number%10

number //= 10

return (10 - crossfoot%10) % 10

# If a record is in english units, convert to metric system

def convert_to_metric(amount, unit_of_measure):

if unit_of_measure=='I':

amount *= 6.4516 # Square Inches to Square Centimeters

unit_of_measure='C'

# ...needs to be finished. Some help on conversions:

# Pounds to Kilograms 0.45359237

# Feet to Meters 0.3048

elif unit_of_measure=='P':

amount *= 0.45359237 # Square Inches to Square Centimeters

unit_of_measure='K'

elif unit_of_measure=='F':

amount *= 0.3048 # Square Inches to Square Centimeters

unit_of_measure='M'

else:

return amount, unit_of_measure

return amount, unit_of_measure

# Invoke main

main()