Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 11.01 KB | None | 0 0
  1. import time
  2. import random
  3.  
  4. # Set up constants dictating the upper limits of the warehouse and overruling company
  5. WAREHOUSE_VALUE_LIMIT = 2000000000
  6. COMPANY_TOTAL_LIMIT = 8000000000
  7.  
  8. # Funtion that bubble sorts a given warehouse's item_list in order of value, the biggest value at the end of the array, the smallest at the beginning
  9. def bubbleSort(sort_list):
  10.  
  11.     temp = list(sort_list)
  12.     length = len(sort_list) - 1
  13.     sorted = False
  14.  
  15.     start = time.time()
  16.  
  17.     while not sorted:
  18.         sorted = True
  19.         for i in range(length):
  20.             if int(temp[i].value) > int(temp[i + 1].value):
  21.                 sorted = False
  22.                 temp[i], temp[i + 1] = temp[i + 1], temp[i]
  23.  
  24.     end = time.time()
  25.     print('Bubble sorting took ' + str((end - start) * 1000) + 'ms')
  26.  
  27.     return temp
  28.  
  29. # Class of item to be used in the warehouse array item_list
  30. class Item():
  31.     def __init__(self, item_num, description, value):
  32.         self.item_num = item_num
  33.         self.description = description
  34.         self.value = value
  35.  
  36. # Class Warehouse to hold the array of Items and read in initial data from the file
  37. class Warehouse():
  38.     def __init__(self, file, name):
  39.         self.name = name
  40.         noOfDigitsInValueWHB = []
  41.         self.item_list = []
  42.         self.total_value = 0
  43.         line_count = 0
  44.         value = 0
  45.         f = open(file, "r")
  46.         line = f.readline()
  47.         while line:
  48.             if line_count == 0:
  49.                 line_count = 1
  50.                 continue
  51.             else:
  52.                 line_values = line.split(',')
  53.                 value_list = line_values[len(line_values) - 1].split('\r')
  54.  
  55.                 if file == "DADSA Assignment 2018-19 Warehouse B.csv":
  56.                     line = line.replace('\"', '')
  57.                     l_count = 0
  58.                     for i in range(len(line_values)):
  59.                         l_count = l_count + 1
  60.                     noOfDigitsInValueWHB.append(l_count - 2)
  61.  
  62.                     if noOfDigitsInValueWHB[line_count - 1] == 2:
  63.                         value_list = line_values[2] + line_values[3]
  64.                     elif noOfDigitsInValueWHB[line_count - 1] == 3:
  65.                         value_list = line_values[2] + line_values[3] + line_values[4]
  66.  
  67.                     value_list = value_list.replace('\r', '')
  68.                     value_list = value_list.replace('\n', '')
  69.                     value_list = value_list.replace('\"', '')
  70.  
  71.                     value = "".join(value_list)
  72.                 else:
  73.                     value = value_list[0]
  74.  
  75.                 self.item_list.append(Item(line_values[0], line_values[1], value))
  76.                 self.total_value = self.total_value + (int(value))
  77.                 line_count = line_count + 1
  78.         f.close()
  79.  
  80.     def updateTotalValue(self):
  81.         self.total_value = 0
  82.         for i in range(len(self.item_list) - 1):
  83.             self.total_value = self.total_value + int(self.item_list[i].value)
  84.  
  85.     # Prints a list of items in the warehouse
  86.     def printList(self):
  87.         print("Contents of " + self.name + ": ")
  88.         for i in range(len(self.item_list) - 1):
  89.             print(str(i + 1) + ". Item Number: " + str(self.item_list[i].item_num) + ". Description: " + self.item_list[i].description + ". Value: " + str(self.item_list[i].value + "."))
  90.         print("\n")
  91.  
  92.     # return the index of an item that the user has selected
  93.     def selectItem(self):
  94.         self.printList()
  95.         return input("Choose an item from " + self.name + ": ") - 1
  96.  
  97. # Class company to create and hold warehouses
  98. class Company():
  99.     warehouseA = Warehouse("DADSA Assignment 2018-19 Warehouse A.csv", "Warehouse A")
  100.     warehouseB = Warehouse("DADSA Assignment 2018-19 Warehouse B.csv", "Warehouse B")
  101.     warehouseC = Warehouse("DADSA Assignment 2018-19 Warehouse C.csv", "Warehouse C")
  102.     warehouseD = Warehouse("DADSA Assignment 2018-19 Warehouse D.csv", "Warehouse D")
  103.     warehouseA.item_list = bubbleSort(warehouseA.item_list)
  104.     warehouseB.item_list = bubbleSort(warehouseB.item_list)
  105.     warehouseC.item_list = bubbleSort(warehouseC.item_list)
  106.     warehouseD.item_list = bubbleSort(warehouseD.item_list)
  107.     overall_total = warehouseA.total_value + warehouseB.total_value + warehouseC.total_value + warehouseD.total_value
  108.  
  109.     # Prints a list of all items in all warehouses
  110.     def printList(self):
  111.         self.warehouseA.printList()
  112.         print("\n")
  113.         self.warehouseB.printList()
  114.         print("\n")
  115.         self.warehouseC.printList()
  116.         print("\n")
  117.         self.warehouseD.printList()
  118.         print("\n")
  119.  
  120. def binarySearch(warehouse, value):
  121.     lowest_value = 0
  122.     highest_value = len(warehouse.item_list) - 1
  123.     found = False
  124.  
  125.     while lowest_value <= highest_value and not found:
  126.         midpoint = (lowest_value + highest_value) // 2
  127.         if warehouse.item_list[midpoint].value == value:
  128.             found = True
  129.         else:
  130.             if value < warehouse.item_list[midpoint].value:
  131.                 highest_value = midpoint - 1
  132.             else:
  133.                 lowest_value = midpoint + 1
  134.  
  135.     return midpoint + 1
  136.  
  137. def addItemToWarehouse(warehouse, item):
  138.     remaining_value_of_warehouse = WAREHOUSE_VALUE_LIMIT - warehouse.total_value
  139.     remove_index = 0
  140.     moved = False
  141.  
  142.     if int(item.value) < remaining_value_of_warehouse:
  143.         moved = True
  144.         warehouse.item_list.append(item)
  145.         print("Item has been moved into warehouse")
  146.     else:
  147.         remove_index = binarySearch(warehouse, item.value)
  148.         while remove_index < (len(warehouse.item_list) - 1) and moved == False:
  149.             if remaining_value_of_warehouse + int(warehouse.item_list[remove_index].value) > int(item.value):
  150.                     print("There is no room to add that item in " + warehouse.name + ".")
  151.                     print("You can move this item to another warehouse: ")
  152.                     print("Item Number: " + warehouse.item_list[remove_index].item_num)
  153.                     print("Description: " + warehouse.item_list[remove_index].description)
  154.                     print("Value: " + warehouse.item_list[remove_index].value)
  155.                     choice = input("Would you like to move this item to another warehouse? Y/N")
  156.                     if choice == 'y':
  157.                         moved = True
  158.                     elif choice == 'n':
  159.                         moved = False
  160.                     else:
  161.                         print("Invalid Option! Returning to main menu...")
  162.                         menu()
  163.             else:
  164.                 print("Assessing item: " + warehouse.item_list[remove_index].description)
  165.                 remove_index = binarySearch(warehouse, warehouse.item_list[remove_index].value)
  166.     if moved == False:
  167.         print("The item cannot be placed in " + warehouse.name)
  168.  
  169.     warehouse.item_list = bubbleSort(warehouse.item_list)
  170.     warehouse.updateTotalValue()
  171.     print("Total Value: " + str(warehouse.total_value))
  172.  
  173. def removeItemFromWarehouse(warehouse, item):
  174.     found = False
  175.  
  176.     for i in range(len(warehouse.item_list) - 1):
  177.         if item.item_num == warehouse.item_list[i].item_num:
  178.             found = True
  179.     if found == True:
  180.         warehouse.item_list.remove(item)
  181.         print("Item has been removed from warehouse")
  182.     else:
  183.         print("Item is not in warehouse")
  184.  
  185. def menu():
  186.     print("Options: ")
  187.     print("1.   Add an item to a warehouse")
  188.     print("2.   Remove an item from a warehouse")
  189.     print("3.   Sort a warehouse by value")
  190.     print("4.   View the contents of a particular warehouse")
  191.     print("5.   View the contents of all warehouses")
  192.     print("6.   Quit")
  193.     choice = input("Choose an option: ")
  194.     if choice == 1:
  195.         print("Options - Choose warehouse: ")
  196.         print("1.   Warehouse A")
  197.         print("2.   Warehouse B")
  198.         print("3.   Warehouse C")
  199.         print("4.   Warehouse D")
  200.         choice = input("Choose a warehouse to add an item to: ")
  201.         if choice == 1:
  202.             warehouse = Company.warehouseA
  203.         elif choice == 2:
  204.             warehouse = Company.warehouseB
  205.         elif choice == 3:
  206.             warehouse = Company.warehouseC
  207.         elif choice == 4:
  208.             warehouse = Company.warehouseD
  209.         else:
  210.             print("Invalid option! Returning to main menu...")
  211.             menu()
  212.  
  213.         print("Options - Add Item Info: ")
  214.         item_num = random.randint(11111, 99999)
  215.         description = input("Enter the description of the item: ")
  216.         value = input("Enter the value of the item: ")
  217.         item = Item(item_num, description, value)
  218.         addItemToWarehouse(warehouse, warehouse.item_list[item])
  219.  
  220.     elif choice == 2:
  221.         print("Options - Choose warehouse: ")
  222.         print("1.   Warehouse A")
  223.         print("2.   Warehouse B")
  224.         print("3.   Warehouse C")
  225.         print("4.   Warehouse D")
  226.         choice = input("Choose a warehouse to remove an item from: ")
  227.         if choice == 1:
  228.             warehouse = Company.warehouseA
  229.         elif choice == 2:
  230.             warehouse = Company.warehouseB
  231.         elif choice == 3:
  232.             warehouse = Company.warehouseC
  233.         elif choice == 4:
  234.             warehouse = Company.warehouseD
  235.         else:
  236.             print("Invalid option! Returning to main menu...")
  237.             menu()
  238.  
  239.         print("Options - Choose item: ")
  240.         item = warehouse.selectItem()
  241.         removeItemFromWarehouse(warehouse, item)
  242.  
  243.     elif choice == 3:
  244.         print("Options - Choose warehouse: ")
  245.         print("1.   Warehouse A")
  246.         print("2.   Warehouse B")
  247.         print("3.   Warehouse C")
  248.         print("4.   Warehouse D")
  249.         choice = input("Choose a warehouse to sort by value: ")
  250.         if choice == 1:
  251.             warehouse = Company.warehouseA
  252.         elif choice == 2:
  253.             warehouse = Company.warehouseB
  254.         elif choice == 3:
  255.             warehouse = Company.warehouseC
  256.         elif choice == 4:
  257.             warehouse = Company.warehouseD
  258.         else:
  259.             print("Invalid option! Returning to main menu...")
  260.             menu()
  261.  
  262.         warehouse.item_list = bubbleSort(warehouse.item_list)
  263.  
  264.     elif choice == 4:
  265.         print("Options - Choose warehouse: ")
  266.         print("1.   Warehouse A")
  267.         print("2.   Warehouse B")
  268.         print("3.   Warehouse C")
  269.         print("4.   Warehouse D")
  270.         choice = input("Choose a warehouse to view its contents: ")
  271.         if choice == 1:
  272.             warehouse = Company.warehouseA
  273.         elif choice == 2:
  274.             warehouse = Company.warehouseB
  275.         elif choice == 3:
  276.             warehouse = Company.warehouseC
  277.         elif choice == 4:
  278.             warehouse = Company.warehouseD
  279.         else:
  280.             print("Invalid option! Returning to main menu...")
  281.             menu()
  282.  
  283.         warehouse.printList()
  284.  
  285.     elif choice == 5:
  286.         company.printList()
  287.  
  288.     elif choice == 6:
  289.         quit()
  290.  
  291.     else:
  292.         print("Invalid Option! Returning to main menu...")
  293.         menu()
  294.  
  295.  
  296. company = Company()
  297.  
  298. while True:
  299.    menu()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement