Python Forum
Building a food delivery program
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Building a food delivery program
#1
Hello,

We have been assigned to do a project that I find quite hard, and I am a bit stuck.

There are 4 questions and I do not know how to do the last two.

The subject is the following :

Let’s build a Food Delivery program for a restaurant!
Here are the first user actions of our program:
As a user, I can add a new meal
As a user, I can list all the meals
As a user, I can add a new customer
As a user, I can list all the customers
WARNINGS
The software is designed for one restaurant only (e.g. we don’t need a Restaurant class).
The software is designed for the restaurant’s staff only, so no need to design a login interface for customers.
Hence,the first components of our software are:
Meals
Customers

1 - Meals
​1.1 - Meal class
Our restaurant sells meals, so we need a way to represent what a meal is.
Each meal has an id, a name and a price.

1.2 - Meal repository
Now that we have a class representing our meals, we need a repository to store them.
This repository is initialized with a txt file. It reads/writes the meals from the file and holds them as objects in a dictionary. The behavior we want for the repository is to:
Add a new meal
Get all the meals
Find a specific meal thanks to its id


​2 - Customers
​2.1 - Customer class
Our restaurant sells to customers, so we need a way to represent what a customer is.
Each customer has an id, a name and an address.


​2.2 - Customer repository
Now that we have a model representing our customers, we need a repository to store them.
This repository is initialized with a txtfile path. It reads/writes the customers from the file and holds them as objects in a dictionary. The behavior we want for the repository is to:
Add a new customer
Get all the customers
Find a specific customer thanks to its id


3 - Orders
Create an Order class to manage orders from customers
An order should have an id, a meal_id (which is the is for the meal ordered), and a customer_id (which is the id for the customer who placed the order), and a ‘delivered’ attribute (a boolean to record whether the order has been delivered or not).
Here are the user actions we want to implement:
I can add a new order
I can list all the undelivered orders
I can mark one order as delivered


​4 - Manage data
​Implement edit and destroy actions for meals and customers
In our app, a user can’t edit or destroy an existing meal or customer.
Implement these additional user actions:
As a user, I can edit an existing meal
As a user, I can destroy an existing meal
As a user, I can edit an existing customer
As a user, I can destroy an existing customer

Here is what I have done for question 2,

import csv

class Customer:  

    def __init__(self, id_customer, name, address):
        self.id_customer = id_customer
        self.name = name
        self.address = address


def load_csv():
  with open('customers.csv', newline='') as f:
    reader = csv.reader(f)
    data = list(reader)

    customers = []
    for customer in data:
        customers.append(Customer(customer[0],customer[1],customer[2])) 
    return customers

def add_customer(id_customer,name,address):
    customers.append(Customer(id_customer,name,address))

def get_customer_by_id(id_customer):
     for customer in customers:
        if(customer.id_customer == id_customer):
            return customer

def display_customers():
    for c in customers:
        print(c.id_customer,c.name,c.address)
    
customers = load_csv()

display_customers()
    
add_customer(4,"Test,","Bal")

display_customers()

print("Customer with id 4 is :", get_customer_by_id(4).name)
It would mean a lot if you could help me for questions 3 and 4, I do not really know how to do them.

Thanks a lot!
snippsat write Dec-06-2020, 02:42 PM:
Added code tag in your post,look at BBCode on how to use.
Reply
#2
(Dec-06-2020, 01:26 PM)ElenaPapagia Wrote: 3 - Orders
Create an Order class to manage orders from customers
An order should have an id, a meal_id (which is the is for the meal ordered), and a customer_id (which is the id for the customer who placed the order), and a ‘delivered’ attribute (a boolean to record whether the order has been delivered or not).
Here are the user actions we want to implement:
I can add a new order
I can list all the undelivered orders
I can mark one order as delivered

I think there are many ways to do this, but using pandas dataframe is likely to be the easiest since you have multiple "variables" to manipulate. You can also use nested dictionaries but I personally do not like them. Essentially your dataframe could look something like this:

order_id       customer_id     meal_id       Delivered
 1                1              1             False
 2                1              1             False
 3                1              2             True
 4                2              2             True
 5                2              3             False
 6                2              4             True
Once you generate this dataframe it should be trivial to implement all your tasks in Q3 and 4. You probably don't even need classes to do them, but if your assignment requires it, you can use methods in your Order class to set the relevant values in the dataframe. You can even use a separate (or even the same) dataframe to manipulate your customer details. Pandas has a convenient read_csv and to_csv methods that allows you to read and write to csv files quite easily.

You can read about Pandas Dataframe here: https://pandas.pydata.org/pandas-docs/st...Frame.html. It's a bit of reading to go through but I think it is the easiest solution to your problem.

By the way, I'm not sure if the code you supplied for Q2 will work.....

Good luck!
Reply
#3
Since it's not part of the assignment, I'm going to assume that Panda isn't allowed.


The Order class for question 3 is pretty straight forward if you go bit by bit. It says all the class is supposed to have so make a class with those characteristics eg:

:An order should have an id, a meal_id (which is the is for the meal ordered), and a customer_id (which is the id for the customer who placed the order), and a ‘delivered’ attribute (a boolean to record whether the order has been delivered or not)."

class Order():
    def __init__(self, id, meal_id, customer_id):
        self.id = id
        self.meal_id = meal_id
        self.customer_id = customer_id
        self.delivered = False
When you make new orders add them to a list. Then you can find a particular order by its id thus:
for order in orders_list:
    if order.id == whatever_you_are_looking_for:
        order.delivered = True #or any change you want
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Add food choice to menu extricate 4 2,728 Jun-13-2020, 07:44 AM
Last Post: Yoriz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020