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

? Class Diagram Tutorial

A Class Diagram is a UML static structure diagram that illustrates the structure of a system through classes, attributes, methods, and their relationships. It serves to visualize system blueprints, aid in object-oriented design, and assist in code generation and database modeling. The document includes examples of class diagrams for an e-commerce system, detailing classes like Customer, Order, and Product, along with their attributes and relationships.

Uploaded by

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

? Class Diagram Tutorial

A Class Diagram is a UML static structure diagram that illustrates the structure of a system through classes, attributes, methods, and their relationships. It serves to visualize system blueprints, aid in object-oriented design, and assist in code generation and database modeling. The document includes examples of class diagrams for an e-commerce system, detailing classes like Customer, Order, and Product, along with their attributes and relationships.

Uploaded by

bai598164
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Class Diagram Tutorial (UML)

What is a Class Diagram?


A Class Diagram is a type of static structure diagram in UML that describes the structure of a
system by showing:

• Classes
• Attributes (data members)
• Methods (functions or operations)
• Relationships between classes

Purpose of a Class Diagram


• To visualize the blueprint of a system.
• To show object-oriented design clearly.
• Helps in code generation and reverse engineering.
• Useful in database modeling.

Components of a Class Diagram


Component Description
Class Represents a blueprint of objects.
Attributes The data/fields a class holds.
Methods The behaviors/functions of the class.
Relationships How classes are connected (association, inheritance, etc.).

Class Box Notation


+------------------------+
| Class Name |
+------------------------+
| - attribute1: Type |
| - attribute2: Type |
+------------------------+
| + method1(): ReturnType|
| + method2(arg): void |
+------------------------+
Symbols:

• + Public
• - Private
• # Protected

Example Class: Student


+----------------------+
| Student |
+----------------------+
| - name: String |
| - age: int |
+----------------------+
| + enroll(): void |
| + getDetails(): String |
+----------------------+

Relationships in Class Diagrams


Relationship Symbol/Notation Description
Association Line Basic connection
Inheritance (Generalization) Line with triangle One class is a subclass of another
Aggregation Line with white diamond "Has-a" relationship (weak)
Composition Line with black diamond "Owns-a" relationship (strong)
Dependency Dotted line with arrow Uses temporarily

Example: E-Commerce System


Classes:
• Customer
• Order
• Product

Relationships:

• A Customer places an Order


• An Order contains multiple Products
Text Representation
Customer
---------
- name: String
- email: String
+ placeOrder()

Order
---------
- orderId: int
- date: Date
+ addProduct()

Product
---------
- productId: int
- name: String
+ getPrice()

Relationships:

• Customer → Order: Association


• Order → Product: Composition (Order owns Product details at that time)

Class Diagram

• .
Classes in the Diagram
1. Customer

• Attributes:
o name: String
o email: String
• This class represents the users who place orders on the system.

2. Order

• Attributes:
o orderId: int
o date: Date
• Represents a customer's purchase order.
• An order is made by a customer and may include multiple products.

3. Product

• Attributes:
o productID: int
o name: String
• Represents a product that can be included in one or more orders.

Relationships
Association & Multiplicity

• Customer → Order
There is a line connecting Customer to Order with a diamond and a *:
o This shows a one-to-many relationship.
o One customer can place many orders.
o The diamond indicates aggregation (a customer "has" orders, but orders can exist
independently).
• Order → Product
o There's also a line from Order to Product.
o Suggests that an order contains multiple products.
o This could also be an aggregation (or even composition, if products are
considered part of an order).

Summary in Simple Terms


• A Customer can place many Orders.
• An Order contains many Products.
• The diagram models the real-world structure of an online store's database.

Code
Product.java
public class Product {

private int productID;

private String name;

public Product(int productID, String name) {

this.productID = productID;

this.name = name;

// Getters and Setters

public int getProductID() {

return productID;

public void setProductID(int productID) {

this.productID = productID;

public String getName() {

return name;

public void setName(String name) {

this.name = name;

}
Order.java

import java.util.Date;

import java.util.List;

public class Order {

private int orderId;

private Date date;

private List<Product> products;

public Order(int orderId, Date date, List<Product> products) {

this.orderId = orderId;

this.date = date;

this.products = products;

// Getters and Setters

public int getOrderId() {

return orderId;

public void setOrderId(int orderId) {

this.orderId = orderId;

public Date getDate() {

return date;

public void setDate(Date date) {


this.date = date;

public List<Product> getProducts() {

return products;

public void setProducts(List<Product> products) {

this.products = products;

public void addProduct(Product product) {

this.products.add(product);

Customer.java

java
CopyEdit
import java.util.List;

public class Customer {


private String name;
private String email;
private List<Order> orders;

public Customer(String name, String email, List<Order> orders) {


this.name = name;
this.email = email;
this.orders = orders;
}

// Getters and Setters


public String getName() {
return name;
}

public void setName(String name) {


this.name = name;
}

public String getEmail() {


return email;
}

public void setEmail(String email) {


this.email = email;
}

public List<Order> getOrders() {


return orders;
}

public void setOrders(List<Order> orders) {


this.orders = orders;
}

public void placeOrder(Order order) {


this.orders.add(order);
}
}

🛠 Optional: Main.java to test the setup


java
CopyEdit
import java.util.*;

public class Main {


public static void main(String[] args) {
Product p1 = new Product(101, "Laptop");
Product p2 = new Product(102, "Mouse");

List<Product> productList = new ArrayList<>();


productList.add(p1);
productList.add(p2);

Order order = new Order(1, new Date(), productList);

List<Order> orderList = new ArrayList<>();


orderList.add(order);

Customer customer = new Customer("Alice", "[email protected]",


orderList);

System.out.println("Customer: " + customer.getName());


System.out.println("Orders: " + customer.getOrders().size());
}
}

You might also like