Answer to Question #198002 in Python for MongoMEME

Question #198002

Online Cake Ordering System: 1) Add a new order 2) Retrieve an order 3) Deliver an order 4) Print summary report 5) Exit system 

Add a new order

  • require auto generated ID
  • Expected delivery Date and time
  • Name of the cake 
  • Weight 
  • Price
  • Status

Retrieve an order

The system shall retrieve and remove the order (with the nearest delivery data and time) from the binary heap data structure and update the status as “in progress” and move the order into a queue data structure.

Deliver an order

The user will retrieve a work in progress order from queue based on first in first out (FIFO) concept and move the order to a stack. Status of the order will be updated as “delivered”.

View summary report

  • Outstanding order list: Display a list of outstanding order information
  • Work In progress: Display a list of work in progress order information.
  • Top and latest order delivered: Display the n latest order delivered.

Note: No built-in classes of the data structure (heap, queue and stack) are allowed. 


1
Expert's answer
2021-05-24T16:52:52-0400
import time

main_menu=('Add a new order', 'Retrieve an order', 'Deliver an order', 'Print summary report', 'Exit system')

class Bin_heap:
	def __init__(self):
		self.heap_list = [[0]]
		self.current_size = 0

	def perc_up(self,i):
		while i // 2 > 0:
			if self.heap_list[i][0] < self.heap_list[i // 2][0]:
				tmp = self.heap_list[i // 2]
				self.heap_list[i // 2] = self.heap_list[i]
				self.heap_list[i] = tmp
			i = i // 2
	def insert(self,k):
		self.heap_list.append(k)
		self.current_size = self.current_size + 1
		self.perc_up(self.current_size)
	def perc_down(self,i):
		while (i * 2) <= self.current_size:
			mc = self.min_child(i)
			if self.heap_list[i][0] > self.heap_list[mc][0]:
				tmp = self.heap_list[i]
				self.heap_list[i] = self.heap_list[mc]
				self.heap_list[mc] = tmp
			i = mc
	def min_child(self,i):
		if i * 2 + 1 > self.current_size:
			return i * 2
		else:
			if self.heap_list[i*2][0] < self.heap_list[i*2+1][0]:
				return i * 2
			else:
				return i * 2 + 1
	def del_min(self):
		retval = self.heap_list[1]
		self.heap_list[1] = self.heap_list[self.current_size]
		self.current_size = self.current_size - 1
		self.heap_list.pop()
		self.perc_down(1)
		return retval
	def build_heap(self,alist):
		i = len(alist) // 2
		self.current_size = len(alist)
		self.heap_list = [0] + alist[:]
		while (i > 0):
			self.perc_down(i)
			i = i - 1
	def print_all(self):
		return self.heap_list

def new_id():
	time.sleep(0.0001)
	s = str(time.time())
	new_s = ''
	for el in s:
		if el.isdigit():
			new_s += el
	return int(new_s)

def add_order():
	info = []
	info.append(new_id())
	info.append(input('Delivery date and time: '))
	info.append(input('Name of the cake: '))
	info.append(input('Weight of the cake: '))
	info.append(input('Price of the cake: '))
	info.append('New')
	heap_data.insert(info)
	print('Order added ')

heap_data = Bin_heap()
while True:
	for i in range(len(main_menu)):
		print('{}) {}'.format(i+1, main_menu[i]))
	try:
		com = int(input('goto '))
	except ValueError:
		print('incorrect input')
		continue
	if com == 5:
		break
	elif com == 1:
		add_order()

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS