0% found this document useful (0 votes)
14 views9 pages

Online Food Ordering System Code

The document outlines the implementation of an Online Food Ordering System using various design patterns in Java. It includes code for classes representing food items (Pizza, Burger, Salad), a factory for creating these items, a singleton order manager, and an observer for order status updates. The main class allows users to select food items and displays their details based on user input.

Uploaded by

rakeshraj5186
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views9 pages

Online Food Ordering System Code

The document outlines the implementation of an Online Food Ordering System using various design patterns in Java. It includes code for classes representing food items (Pizza, Burger, Salad), a factory for creating these items, a singleton order manager, and an observer for order status updates. The main class allows users to select food items and displays their details based on user input.

Uploaded by

rakeshraj5186
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Vaisha 112328 4H

li 83 1
Project 1

Aim:- Implementation of Online Food ordering system using at least 5


design pattern.

Code :-

[Link]

a Package

mypack;

Interface

FoodItem {

String getName();

Double getPrice();

[Link]

Package mypack;

Class Pizza implements FoodItem {

Private String name;

Private double price;

Public Pizza(String name, double

price) { [Link] = name;

[Link] = price;

@Override

Public String

getName() { Return

name;

@Override

Public double getPrice() {


Vaisha 112328 4H
li 83 1
Return price;

[Link]

Package mypack;

Class Burger implements FoodItem

{ Private String name;

Private double price;

Public Burger(String name, double price)

{ [Link] = name;

[Link] = price;

@Override

Public String

getName() { Return

name;

@Override

Public double

getPrice() { Return

price;

[Link]

Package mypack;

Class Salad implements FoodItem {


Vaisha 112328 4H
li 83 1
Private String

name; Private

double price;

Public Salad(String name, double

price) { [Link] = name;

[Link] = price;

@Override

Public String

getName() { Return

name;

@Override

Public double

getPrice() { Return

price;

5. [Link]

va Package mypack;

// Factory pattern to create food items

Class FoodItemFactory {

Public static FoodItem createFoodItem(String

type) { Switch ([Link]()) {

Case “pizza”:

Return new Pizza(“Margherita”,

8.99); Case “burger”:


Vaisha 112328 4H
li 83 1
Return new Burger(“Classic Burger”,

6.99); Case “salad”:

Return new Salad(“Caesar Salad”, 5.99);

Default: Return null;

[Link]

Package mypack;

// Singleton class for managing

orders Class OrderManager {

Private static OrderManager

instance; Private OrderManager()

{}

Public static OrderManager

getInstance() { If (instance ==

null) {

Instance = new OrderManager();

Return instance;

[Link]

Package mypack;

Interface OrderStatusObserver {
Vaisha 112328 4H
li 83 1
Void update(String status);

Class CustomerOrderStatusObserver implements OrderStatusObserver {


Private
String name;

Public CustomerOrderStatusObserver(String name)

{ [Link] = name;

@Override

Public void update(String status) {

[Link](name + “: Order status changed to “ + status);

[Link]

Package mypack;

Interface

FoodItemPrototype {

FoodItem clone();

G. [Link]

Package mypack;

Interface FoodItemIterator

{ Boolean hasNext();

FoodItem next();

10. [Link]

Package mypack;
Vaisha 112328 4H
li 83 1
Import [Link];

Class FoodItemListIterator implements

FoodItemIterator { Private List<FoodItem>

foodItems;

Private int position;

Public FoodItemListIterator(List<FoodItem>

foodItems) { [Link] = foodItems;

[Link] = 0;

@Override

Public boolean hasNext() {

Return position < [Link]();

@Override

Public FoodItem next() {

Return [Link](position++);

11. [Link]

Package mypack;

Import [Link].*;

Public class Main {

Public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);


Vaisha 112328 4H
li 83 1
[Link](“Select a food

item:”); [Link](“1. Pizza”);

[Link](“2. Burger”);

[Link](“3. Salad”); [Link](“Enter your

choice: “); Int choice = [Link]();

FoodItem foodItem =

null; Switch (choice) {

Case 1:

foodItem =

[Link](“Pizza”); break;

case 2:

foodItem =

[Link](“Burger”); break;

case 3:

foodItem =

[Link](“Salad”); break;

default:

[Link](“Invalid choice”);

Return;

OrderManager orderManager = [Link]();

OrderStatusObserver customerObserver = new


CustomerOrderStatusObserver(“John Doe”);

List<FoodItem> foodItems = new

ArrayList<>(); [Link](new

Pizza(“Margherita”, 8.99));

[Link](new Pizza(“Pepperoni”, 9.99));

[Link](new Pizza(“Vegetarian”, 7.99));


Vaisha 112328 4H
li 83 1
[Link](new Burger(“Classic Burger”,

6.99)); [Link](new

Burger(“Cheeseburger”, 7.99));
Vaisha 112328 4H
li 83 1
[Link](new Salad(“Caesar Salad”, 5.99)); for

(FoodItem item : foodItems) {

if ([Link]().equals([Link]())) {

[Link](“Name: “ + [Link]() + “, Price: “ +

[Link]()); Break;

Output :-

Common questions

Powered by AI

A developer might choose to implement the Iterator Design Pattern to provide a structured way to access elements of a collection of FoodItem objects sequentially without exposing the underlying representation. It simplifies navigation through a list by abstracting the iteration logic. The pattern enhances encapsulation and decoupled client implementation by using components like FoodItemIterator and FoodItemListIterator, helping clients iterate over items in different ways without modifying the collection .

Implementing design patterns in the development of the online food ordering system is significant because they provide proven, reusable solutions to common design problems, making the system more robust and maintainable. Patterns like Factory, Singleton, Observer, and Iterator offer structured ways to construct, manage, and interact with complex systems, promoting reusability, scalability, and reduced coupling. This results in a system that is easier to debug, extend, and adapt to future requirements, ensuring long-term reliability and flexibility .

The Factory Design Pattern in the food ordering system separates the food item creation logic from the client, encapsulating it within the FoodItemFactory class. This class determines which FoodItem object to create based on the provided type, such as 'pizza', 'burger', or 'salad'. This encapsulation allows for easy addition of new FoodItem types without altering the client code, promoting scalability and reducing coupling between the client and the product instantiation logic .

The implementation of a console interface in Main.java influences user interaction by providing a simple and direct way to interact with the system without the need for a graphical interface. It enables quick selection and ordering of food items through text-based menus and inputs. Users can enter their choices via keyboard input, quickly accessing item details and seeing real-time feedback. This method is efficient for developers when testing functionality but is limited in terms of usability and visual appeal compared to full-fledged GUI applications .

The design of the Base classes, such as FoodItem, employs abstraction by defining common operations through interfaces that must be implemented by concrete classes (e.g., Pizza, Burger, Salad). This ensures flexibility and scalability, as new types of food items can be introduced without modifying existing code. By providing a consistent interface, the system allows for interchangeable and extendable components, enabling developers to easily add more specialized functionalities within the subclasses, while maintaining standard usage protocols defined by the base .

The hardcoding of food items in the Factory Design Pattern potentially leads to decreased flexibility and increased maintenance overhead. When food items are hardcoded, each change requires code updates and redeployment, reducing agility in responding to changes in the menu or prices. Such tight coupling between the code and data also makes the system less dynamic, hindering scalability and flexibility. It can be remedied by implementing a database or configuration file to manage such data externally, thereby enhancing the adaptability and ease of updates .

Encapsulation is crucial as it restricts direct access to certain components of an object, only allowing modifications and retrieval via defined methods. In the food ordering system, encapsulation is illustrated by the use of private fields for properties such as 'name' and 'price' in FoodItems. This control is necessary to maintain data integrity and prevent unauthorized modifications. For instance, classes like Pizza, Burger, and Salad use getName() and getPrice() methods to provide controlled access to the values, ensuring that internal modifications adhere to the class's defined logic .

The Singleton Design Pattern is employed in the OrderManager class to ensure that only one instance of this class exists throughout the application. This pattern is achieved by having a private static instance of OrderManager and a private constructor. The method getInstance() checks whether the instance has already been created, and if not, it initializes the instance. This pattern is useful for managing order data in a consistent manner across different parts of the application, preventing conflicts and ensuring data integrity .

The Prototype Design Pattern facilitates the creation of object copies without modifying the code that uses them. For duplicating food items, this pattern allows new FoodItem instances to be created cheaply by cloning existing instances, thus saving resources by avoiding the overhead of instantiating objects from scratch. Through implementing a FoodItemPrototype interface, the system can seamlessly produce copies of existing items with different attributes, suitable for situations where various configurations of the same item (e.g., different toppings on a Pizza) are needed .

The Observer Design Pattern enhances the order status system by allowing various components (observers) to be updated automatically when there is a change in the subject (order status). In this system, the CustomerOrderStatusObserver class implements the OrderStatusObserver interface to react to status updates. This decouples the notification sender from the receivers, enabling different parts of the system to listen to and respond to order status changes without altering the subject class. This results in a more flexible and modular design that can easily be extended to include additional observers .

You might also like