Question: First, we'll need to be able to represent a menu. Create the file menu.py and write the following classes in that file. The MenuItem class
First, we'll need to be able to represent a menu. Create the file menu.py and write the following classes in that file.
The MenuItem class is a simple class that represents a single item on a menu. It has two attributes: namea str and price a float The MenuItem constructor takes those two arguments name and price in that order.
The Menu class stores a list of MenuItems, and has some member functions to make working with menus easier:
init takes no arguments, and creates an empty menu.
addname price takes two arguments: a name and a price. If an item with that name is not already present in the menu, it adds a new MenuItem for the new item to the end of the menu. If an item with that name already exists, it updates the item's price.
findname takes one argument: a name. It tries to find a MenuItem in the menu with that name. If it exists, it returns that MenuItem. Otherwise, it returns None.
print takes no arguments, and prints the menu as shown below.
TODAY'S MENU: Burger $ Double Burger $ Cheeseburger $ Double Cheeseburger $ Fountain Drink $
The first line has a newline immediately after the colon. Then, for each item on the menu in the order it was added it prints:
Two spaces.
Twentyfour characters for the item name, aligned left.
Two spaces.
Eight characters for the price, aligned right.
A newline.
The Order
The next step is to be able to represent what a user orders off of a menu. Make a file called order.py and in this file, write the following classes.
The OrderItem class is similar to the MenuItem class. It's a simple helper class for storing information about part of an order. It has two attributes: menuitem a MenuItem and quantity an int Its constructor takes those two arguments in that order; if quantity isn't specified, it defaults to one.
The Order class stores a list of OrderItems, and, like a Menu, has member functions that handle common Order operations:
init takes no arguments, and creates an empty order.
addmenuitem quantity takes one or two arguments: a MenuItem and maybe an int. If the order doesn't contain any of that menu item yet, it adds a new OrderItem to the end of the order. If the order does contain that menu item, it adjusts the existing quantity. If quantity isn't specified, it defaults to one.
totalprice takes no arguments, and returns the total price of the order as a float.
totalquantity takes no arguments, and returns the number of menu items in the order as an int an order item with a quantity of three counts as three menu items
print takes no arguments, and prints the order as shown below.
YOUR ORDER: Qty Price Total Cheeseburger $ $ Fountain Drink $ $ TOTAL $
For each order item in the order, it prints:
Two spaces.
Twenty four characters for the name, aligned left.
Two spaces.
Three characters for the quantity, aligned right.
Two spaces.
Eight characters for the price, aligned right.
Two spaces.
Eight characters for the line total, aligned right.
A newline.
Now that you have your menu and order types, you can use them to make a program that lets people order off a menu. Write it in a file called main.py You'll want to import the Menu and Order classes that you wrote earlier; that code will be doing most of the work. See the Notes section for more on importing.
Your program should take exactly one command line argument. This argument is the path to a TSV file the format is described below When the program starts, it should load the menu described in this file, print it followed by a newline and then ask the user to order:
dent@bistromath$ python main.py datacheesetsv TODAY'S MENU: Cheese $ What would you like to order?
It should then wait for the user to enter an order. This will be any number of lines of user input; each line will be the name of one menu item. Each line adds one menu item to the user's order. A user who wants a higher quantity of some item will have to enter its name multiple times.
When your program reaches the end of its user input, it should print a newline, then print the user's order, then exit.
dent@bistromath$ python main.py dataexampletsv TODAY'S MENU: Burger $ Double Burger $ Cheeseburger $ Double Cheeseburger $ Fountain Drink $ What would you like to order? Cheeseburger Cheeseburger Burger Fountain Drink Cheeseburger Fountain Drink YOUR ORDER: Qty Price Total Cheeseburger $ $ Burger $ $ Fountain Drink $ $ TOTAL $
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
