Answer to Question #208183 in Python for kasiko

Question #208183

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

+ a new order

  • require auto generated ID
  • Expected delivery Date & time
  • Name of the cake (choco, frap, straw, blue)
  • Weight (0.5kg/1kg/1.5kg)
  • Price(vary for each)
  • Status

Retrieve order

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

Deliver 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 order will be updated as “delivered”.

View summary report

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

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


1
Expert's answer
2021-06-18T02:54:30-0400
from datetime import datetime

class Cake:

    _id_: int = 0


    def __init__(self, name, date, weight, price):
        """
        Construct a cake.
        """
        self.id: int = self._next_id_()
        self.state: str = 'new'  # initial state is always 'new'
        self.name: str = name
        self.date: datetime = datetime.strptime(date, '%Y-%m-%d %H:%M:%S')
        self.weight: float = weight
        self.price: int = price


    def _next_id_(self):
        """
        Generate a new unique id.
        """
        self.__class__._id_ = self.__class__._id_ + 1
        return self.__class__._id_


    # Comparisons of Cake objects with other.


    def __gt__(self, other):
        """
        Implementation of operator greater than.
        """
        return self.date > other.date


    def __lt__(self, other):
        """
        Implementation of operator less than.
        """
        return self.date < other.date


    def __str__(self):
        """
        Implementation of __str__ method.
        """
        return f"Cake: [id={self.id}, name='{self.name}', date='{self.date}', weight={self.weight}kg, price=${self.price}, status='{self.state}']"


    def set_state(self, state: str):
        """
        Setter of property state.
        """
        if state == 'new' or state == 'in progress' or state == 'delivered':
            self.state = state
        else:
            raise ValueError
class System:
    # List of services of the cake ordering system.
    menu: list = ['Add a new order',
                  'Retrieve an order',
                  'Deliver an order',
                  'Print summary report',
                  'Exit system']


    def __init__(self):
        """
        Construct a cake ordering system.
        """
        self.running: bool = True
        self.temporary: int = 1  # for generating unique id
        self.heap: Heap[Cake] = Heap()
        self.queue: Queue[Cake] = Queue()
        self.stack: Stack[Cake] = Stack()


    def print_menu(self):
        """
        Prints the main menu of the cake ordering system.
        """
        for i in range(len(self.menu)):
            print('({}) {}'.format(i + 1, self.menu[i]))


    def run(self):
        """
        Run the cake ordering system.
        """
        while self.running:  # Loops while the system running
            self.print_menu()  # Display the menu


            try:
                n: int = int(input())


                if n == 5:
                    self.running = False  # Terminate the system
                elif n == 1:
                    self.add_new()
                elif n == 2:
                    self.retrieve()
                elif n == 3:
                    self.deliver()
                elif n == 4:
                    self.print_report()
                else:
                    print('No choice allowed!\n')


            except ValueError:
                print('Invalid syntax!\n')


    def add_new(self):
        """
        Add a new order.
        """
        cake = Cake(input('Name of the cake [str]:'),
                    input('Date and time [datetime: yyyy-mm-dd hh:mm:ss]:'),
                    input('Weight [float]:'),
                    input('Price [int]:'))


        self.heap.insert(cake)
        # status 'new'
        print('Successfully added a new order.\n')


    def retrieve(self):
        """
        Retrieve an order.
        """
        cake = self.heap.remove()


        if cake is not None:
            cake.set_state('in progress')
            self.queue.push(cake)
            # status 'in progress'
            print('Successfully retrieved the order.\n')
        else:
            print('No any order with "new" status!\n')


    def deliver(self):
        """
        Deliver an order.
        """
        cake = self.queue.pop()


        if cake is not None:
            cake.set_state('delivered')
            self.stack.push(cake)
            # status 'delivered'
            print('Successfully delivered the order.\n')
        else:
            print('No any order with "in progress" status!\n')


    def print_report(self):
        """
        Print summary report.
        """
        report = ['Outstanding order list',
                  'Work in progress',
                  'Top n latest order delivered']


        for i in range(len(report)):
            print('({}) {}'.format(i + 1, report[i]))


        n: int = int(input())


        if n != 1 and n != 2 and n != 3:
            raise ValueError
        elif n == 1:
            self.outstanding_order()
        elif n == 2:
            self.in_progress()
        elif n == 3:
            self.top_latest()


    def outstanding_order(self):
        """
        Print a list of outstanding order information.
        """
        for item in self.heap.items:
            print(item)
        if self.heap.empty():
            print('No any order with "new" status!\n')


    def in_progress(self):
        """
        Print a list of work in progress order information.
        """
        for item in self.queue.items:
            print(item)
        if self.queue.empty():
            print('No any order with "in progress" status!\n')

    def top_latest(self):
        """
        Print the n latest order delivered.
        """
        n: int = int(input('n:'))
        for item in reversed(self.stack.items):
            if n != 0:
                print(item)
                n -= 1
        if self.stack.empty():
            print('No any order with "delivered" status!\n')

if __name__ == '__main__':
    # The enter point of the main program
    System().run()

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