Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import time
- import random
- # Set up constants dictating the upper limits of the warehouse and overruling company
- WAREHOUSE_VALUE_LIMIT = 2000000000
- COMPANY_TOTAL_LIMIT = 8000000000
- # 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
- def bubbleSort(sort_list):
- temp = list(sort_list)
- length = len(sort_list) - 1
- sorted = False
- start = time.time()
- while not sorted:
- sorted = True
- for i in range(length):
- if int(temp[i].value) > int(temp[i + 1].value):
- sorted = False
- temp[i], temp[i + 1] = temp[i + 1], temp[i]
- end = time.time()
- print('Bubble sorting took ' + str((end - start) * 1000) + 'ms')
- return temp
- # Class of item to be used in the warehouse array item_list
- class Item():
- def __init__(self, item_num, description, value):
- self.item_num = item_num
- self.description = description
- self.value = value
- # Class Warehouse to hold the array of Items and read in initial data from the file
- class Warehouse():
- def __init__(self, file, name):
- self.name = name
- noOfDigitsInValueWHB = []
- self.item_list = []
- self.total_value = 0
- line_count = 0
- value = 0
- f = open(file, "r")
- line = f.readline()
- while line:
- if line_count == 0:
- line_count = 1
- continue
- else:
- line_values = line.split(',')
- value_list = line_values[len(line_values) - 1].split('\r')
- if file == "DADSA Assignment 2018-19 Warehouse B.csv":
- line = line.replace('\"', '')
- l_count = 0
- for i in range(len(line_values)):
- l_count = l_count + 1
- noOfDigitsInValueWHB.append(l_count - 2)
- if noOfDigitsInValueWHB[line_count - 1] == 2:
- value_list = line_values[2] + line_values[3]
- elif noOfDigitsInValueWHB[line_count - 1] == 3:
- value_list = line_values[2] + line_values[3] + line_values[4]
- value_list = value_list.replace('\r', '')
- value_list = value_list.replace('\n', '')
- value_list = value_list.replace('\"', '')
- value = "".join(value_list)
- else:
- value = value_list[0]
- self.item_list.append(Item(line_values[0], line_values[1], value))
- self.total_value = self.total_value + (int(value))
- line_count = line_count + 1
- f.close()
- def updateTotalValue(self):
- self.total_value = 0
- for i in range(len(self.item_list) - 1):
- self.total_value = self.total_value + int(self.item_list[i].value)
- # Prints a list of items in the warehouse
- def printList(self):
- print("Contents of " + self.name + ": ")
- for i in range(len(self.item_list) - 1):
- 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 + "."))
- print("\n")
- # return the index of an item that the user has selected
- def selectItem(self):
- self.printList()
- return input("Choose an item from " + self.name + ": ") - 1
- # Class company to create and hold warehouses
- class Company():
- warehouseA = Warehouse("DADSA Assignment 2018-19 Warehouse A.csv", "Warehouse A")
- warehouseB = Warehouse("DADSA Assignment 2018-19 Warehouse B.csv", "Warehouse B")
- warehouseC = Warehouse("DADSA Assignment 2018-19 Warehouse C.csv", "Warehouse C")
- warehouseD = Warehouse("DADSA Assignment 2018-19 Warehouse D.csv", "Warehouse D")
- warehouseA.item_list = bubbleSort(warehouseA.item_list)
- warehouseB.item_list = bubbleSort(warehouseB.item_list)
- warehouseC.item_list = bubbleSort(warehouseC.item_list)
- warehouseD.item_list = bubbleSort(warehouseD.item_list)
- overall_total = warehouseA.total_value + warehouseB.total_value + warehouseC.total_value + warehouseD.total_value
- # Prints a list of all items in all warehouses
- def printList(self):
- self.warehouseA.printList()
- print("\n")
- self.warehouseB.printList()
- print("\n")
- self.warehouseC.printList()
- print("\n")
- self.warehouseD.printList()
- print("\n")
- def binarySearch(warehouse, value):
- lowest_value = 0
- highest_value = len(warehouse.item_list) - 1
- found = False
- while lowest_value <= highest_value and not found:
- midpoint = (lowest_value + highest_value) // 2
- if warehouse.item_list[midpoint].value == value:
- found = True
- else:
- if value < warehouse.item_list[midpoint].value:
- highest_value = midpoint - 1
- else:
- lowest_value = midpoint + 1
- return midpoint + 1
- def addItemToWarehouse(warehouse, item):
- remaining_value_of_warehouse = WAREHOUSE_VALUE_LIMIT - warehouse.total_value
- remove_index = 0
- moved = False
- if int(item.value) < remaining_value_of_warehouse:
- moved = True
- warehouse.item_list.append(item)
- print("Item has been moved into warehouse")
- else:
- remove_index = binarySearch(warehouse, item.value)
- while remove_index < (len(warehouse.item_list) - 1) and moved == False:
- if remaining_value_of_warehouse + int(warehouse.item_list[remove_index].value) > int(item.value):
- print("There is no room to add that item in " + warehouse.name + ".")
- print("You can move this item to another warehouse: ")
- print("Item Number: " + warehouse.item_list[remove_index].item_num)
- print("Description: " + warehouse.item_list[remove_index].description)
- print("Value: " + warehouse.item_list[remove_index].value)
- choice = input("Would you like to move this item to another warehouse? Y/N")
- if choice == 'y':
- moved = True
- elif choice == 'n':
- moved = False
- else:
- print("Invalid Option! Returning to main menu...")
- menu()
- else:
- print("Assessing item: " + warehouse.item_list[remove_index].description)
- remove_index = binarySearch(warehouse, warehouse.item_list[remove_index].value)
- if moved == False:
- print("The item cannot be placed in " + warehouse.name)
- warehouse.item_list = bubbleSort(warehouse.item_list)
- warehouse.updateTotalValue()
- print("Total Value: " + str(warehouse.total_value))
- def removeItemFromWarehouse(warehouse, item):
- found = False
- for i in range(len(warehouse.item_list) - 1):
- if item.item_num == warehouse.item_list[i].item_num:
- found = True
- if found == True:
- warehouse.item_list.remove(item)
- print("Item has been removed from warehouse")
- else:
- print("Item is not in warehouse")
- def menu():
- print("Options: ")
- print("1. Add an item to a warehouse")
- print("2. Remove an item from a warehouse")
- print("3. Sort a warehouse by value")
- print("4. View the contents of a particular warehouse")
- print("5. View the contents of all warehouses")
- print("6. Quit")
- choice = input("Choose an option: ")
- if choice == 1:
- print("Options - Choose warehouse: ")
- print("1. Warehouse A")
- print("2. Warehouse B")
- print("3. Warehouse C")
- print("4. Warehouse D")
- choice = input("Choose a warehouse to add an item to: ")
- if choice == 1:
- warehouse = Company.warehouseA
- elif choice == 2:
- warehouse = Company.warehouseB
- elif choice == 3:
- warehouse = Company.warehouseC
- elif choice == 4:
- warehouse = Company.warehouseD
- else:
- print("Invalid option! Returning to main menu...")
- menu()
- print("Options - Add Item Info: ")
- item_num = random.randint(11111, 99999)
- description = input("Enter the description of the item: ")
- value = input("Enter the value of the item: ")
- item = Item(item_num, description, value)
- addItemToWarehouse(warehouse, warehouse.item_list[item])
- elif choice == 2:
- print("Options - Choose warehouse: ")
- print("1. Warehouse A")
- print("2. Warehouse B")
- print("3. Warehouse C")
- print("4. Warehouse D")
- choice = input("Choose a warehouse to remove an item from: ")
- if choice == 1:
- warehouse = Company.warehouseA
- elif choice == 2:
- warehouse = Company.warehouseB
- elif choice == 3:
- warehouse = Company.warehouseC
- elif choice == 4:
- warehouse = Company.warehouseD
- else:
- print("Invalid option! Returning to main menu...")
- menu()
- print("Options - Choose item: ")
- item = warehouse.selectItem()
- removeItemFromWarehouse(warehouse, item)
- elif choice == 3:
- print("Options - Choose warehouse: ")
- print("1. Warehouse A")
- print("2. Warehouse B")
- print("3. Warehouse C")
- print("4. Warehouse D")
- choice = input("Choose a warehouse to sort by value: ")
- if choice == 1:
- warehouse = Company.warehouseA
- elif choice == 2:
- warehouse = Company.warehouseB
- elif choice == 3:
- warehouse = Company.warehouseC
- elif choice == 4:
- warehouse = Company.warehouseD
- else:
- print("Invalid option! Returning to main menu...")
- menu()
- warehouse.item_list = bubbleSort(warehouse.item_list)
- elif choice == 4:
- print("Options - Choose warehouse: ")
- print("1. Warehouse A")
- print("2. Warehouse B")
- print("3. Warehouse C")
- print("4. Warehouse D")
- choice = input("Choose a warehouse to view its contents: ")
- if choice == 1:
- warehouse = Company.warehouseA
- elif choice == 2:
- warehouse = Company.warehouseB
- elif choice == 3:
- warehouse = Company.warehouseC
- elif choice == 4:
- warehouse = Company.warehouseD
- else:
- print("Invalid option! Returning to main menu...")
- menu()
- warehouse.printList()
- elif choice == 5:
- company.printList()
- elif choice == 6:
- quit()
- else:
- print("Invalid Option! Returning to main menu...")
- menu()
- company = Company()
- while True:
- menu()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement