0% found this document useful (0 votes)
2 views

Software Engineering mvc junit

Uploaded by

Zawahir
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Software Engineering mvc junit

Uploaded by

Zawahir
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Software Engineering

(mvc)
Name: Taha Dar
Roll No: Fa23-Bce-095

MVC in Online Shopping Cart


In an online shopping cart, the MVC (Model-View-Controller)
architectural pattern organizes the application into three
components to separate concerns and enhance maintainability.
Here's how MVC operates in this context

MVC in Online Shopping Cart


In an online shopping cart, the MVC (Model-View-Controller)
architectural pattern organizes the application into three
components to separate concerns and enhance maintainability.
Here's how MVC operates in this context;
1. Model
 Definition: The Model represents the application's data,
logic, and rules.
 Role in Online Shopping Cart:
o Manages the data for items in the cart (e.g., name,
price, quantity).
o Handles business logic such as adding items,
calculating totals, or managing inventory.
o Stores data persistently (e.g., in memory or a
database).
 Examples:
o Adding an item to the cart.
o Calculating the total cost of items.
o Validating stock availability.

2. View
 Definition: The View is responsible for displaying the data
and user interface.
 Role in Online Shopping Cart:
o Presents cart data to the user, such as item lists, prices,
and total cost.
o Provides an interface for user interactions like adding or
removing items.
o Reflects real-time updates from the Model.
 Examples:
o Displaying a table of items in the cart.
o Showing the total price and checkout button.
o Error messages like "Item out of stock."

3. Controller
 Definition: The Controller acts as a mediator
between the Model and the View, handling user input
and updating data or UI accordingly.
 Role in Online Shopping Cart:
o Processes user actions such as adding or
removing items.
o Communicates with the Model to update data.
o Requests the View to refresh with updated
information.

 Examples:
o Handling a button click to add an item to the
cart.
o Removing an item when the user clicks
"Remove."
o Directing the View to display an error if an action
fails.
FLOW CHART OF MVC IN ONLINE SHOPPING CART
Start -> User Action (e.g., Add Item to Cart)
|
v
Controller Receives Input
|
v
Updates the Model (e.g., Add Item to Data Store)
|
v
Model Notifies Changes
|
v
View Fetches Updated Data from Model
|
v
Updated Cart Displayed to User
|
v
End
#include <iostream>

#include <vector>

#include <string>

#include <iomanip>

#include <algorithm>

// Model: Represents the data and business logic

class CartItem {

public:

std::string name;

double price;

int quantity;

CartItem(std::string itemName, double itemPrice, int itemQuantity)

: name(itemName), price(itemPrice), quantity(itemQuantity) {}

};

class ShoppingCartModel {

private:

std::vector<CartItem> cartItems;

public:

void addItem(const std::string& name, double price, int quantity) {

for (auto& item : cartItems) {

if (item.name == name) {

item.quantity += quantity; // Increment quantity if the item already exists

return;

cartItems.emplace_back(name, price, quantity);


}

void removeItem(const std::string& name) {

cartItems.erase(std::remove_if(cartItems.begin(), cartItems.end(),

[&](const CartItem& item) { return item.name == name; }),

cartItems.end());

double calculateTotal() const {

double total = 0;

for (const auto& item : cartItems) {

total += item.price * item.quantity;

return total;

const std::vector<CartItem>& getItems() const {

return cartItems;

};

// View: Displays the data and UI

class ShoppingCartView {

public:

void displayCart(const std::vector<CartItem>& items, double total) {

std::cout << "----------------- Shopping Cart -----------------\n";

std::cout << std::left << std::setw(20) << "Item Name"

<< std::setw(10) << "Price"

<< std::setw(10) << "Quantity\n";

for (const auto& item : items) {

std::cout << std::left << std::setw(20) << item.name


<< std::setw(10) << item.price

<< std::setw(10) << item.quantity << "\n";

std::cout << "-------------------------------------------------\n";

std::cout << "Total: $" << total << "\n";

std::cout << "-------------------------------------------------\n";

void displayMessage(const std::string& message) {

std::cout << message << "\n";

};

// Controller: Handles input and updates the Model and View

class ShoppingCartController {

private:

ShoppingCartModel model;

ShoppingCartView view;

public:

void addItem(const std::string& name, double price, int quantity) {

model.addItem(name, price, quantity);

view.displayMessage("Item added to cart!");

updateView();

void removeItem(const std::string& name) {

model.removeItem(name);

view.displayMessage("Item removed from cart!");

updateView();

}
void updateView() {

double total = model.calculateTotal();

view.displayCart(model.getItems(), total);

};

// Main: Simulates user interactions

int main() {

ShoppingCartController controller;

controller.addItem("Laptop", 1200.50, 1);

controller.addItem("Headphones", 200.75, 2);

controller.addItem("Mouse", 25.99, 1);

controller.removeItem("Headphones");

controller.addItem("Keyboard", 99.99, 1);

return 0;

You might also like