Answer to Question #197757 in Python for Cassandra

Question #197757

The system shall allow the technician to perform the following operations until the option 5 (exit system) is selected: 1) Add a new order 2) Retrieve an order 3) Deliver an order 4) Print summary report 5) Exit system

Add a new order

The user will add a new cake order request into the system. The system shall store the following cake information into a binary heap. The bakery shop will always prepare an order with nearest delivery date/time. 1. Order ID: Auto generated unique ID to each new job created. 2. Expected delivery Date and time: Delivery date and time 3. Name of the cake: Name based on the flavour of the cake. For example, cheese cake, black forest and etc. 4. Weight: Weight of the cake (0.5kg, 1kg, 2kg) 5. Price: Price of the cake. 6. Status: Status of the order, shall set to “new” when a new order is created.

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


1
Expert's answer
2021-05-24T07:42:20-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