Docsity

Java Code Implementation: Classes and Design Patterns, Essays (high school) of Technology

Partial preview of the text

Download Java Code Implementation: Classes and Design Patterns and more Essays (high school) Technology in PDF only on Docsity!

BTEC FPT INTERNATIONAL COLLEGE

INFORMATION TECHNOLOGY

HIGHER NATIONAL DIPLOMA IN COMPUTING

FINAL REPORT OF ASSIGNMENT 2

UNIT: ADVANCED PROGRAMMING

STUDENT : TRAN XUAN TU

CLASS : PBIT

STUDENT ID : BDAF

SUPERVISOR : NGUYEN PHUONG TAM

DaNang, December 2021

ASSIGNMENT 2 FRONT SHEET

Qualification BTEC Level 5 HND Diploma in Computing

Unit number and title Unit: Advanced Programming

Submission date 04/12/2021 Date received (1st sub- mission) 04/12/

Re-submission date Date received (2nd submission)

Student name Tran Xuan Tu Student ID BDAF 190018

Class PBIT15101 Assessor name Nguyen Phuong Tam

Student declaration I certify that the assignment submission is entirely my own work and I fully understand the consequences of plagiarism. I understand that making a false declaration is a form of malpractice. Student’s signature: Tu

Grading grid

P3 P4 M3 M4 D3 D

ii

Table 1 Members Name ID Email

Nguyen Van Hieu BDAF190023 [email protected]

Tran Quoc Phuc BDAF190012 [email protected]

Nguyen Thi My Duyen BDGF190005 [email protected]

Tran Xuan Tu BDAF 190018 tutxbdaf1 90018 @fpt.edu.vn

Nguyen Dinh Nhat BDAF 190009 nhatndbdaf1 90009 @fpt.edu.vn

Table 2: List of works of each member Members Tasks

Nguyen Thi My Duyen Create Customer, CustomerList class,

Nguyen Dinh Nhat Employees, EmployeesList class

Tran Xuan Tu Create Product, ProductList class

Nguyen Van Hieu Class diagram for the system Scenario , present result of code,

Tran Quoc Phuc create MainShoes, BuyShoes class

iii

TABLE OF CONTENT

TABLE OF CONTENT ....................................................................................... iii

v

LIST OF TABLES AND FIGURES

Table 1 Members ................................................................................................ii

  • CHAPTER 1 IMPLEMENT CODE APPLYING DESIGN PATTERNS SCERIANO LIST OF TABLES AND FIGURES v
    • 1.1 Scenario
    • 1.2 Class diagram for the system
    • 1.3 Java Code implements
      • 1.3.1 Customer class
      • 1.3.2 Employees class
      • 1.3.3 Products class
      • 1.3.4 CustomersList class
      • 1.3.5 EmployeesList class..........................................................................
      • 1.3.6 ProductsList class
      • 1.3.7 BuyShoes class
      • 1.3.8 MainShoes class.............................................................................
    • 1.4 Result of code
      • 1.4.1 Add Product
      • 1.4.2 Show Product
      • 1.4.3 Purchase
  • PATTERNS CHAPTER 2 INVESTIGATE SCENARIOS WITH RESPECT TO DESIGN - 2.1 Range of design patterns with relevant examples.................................... - 2.1.1 Creational patterns.......................................................................... - 2.1.2 Structural patterns........................................................................... - 2.1.3 Behavioral Patterns
    • CONCLUSION
  • Table 3 Code of Customer class Table 2: List of works of each member .................................................................ii
  • Table 4 Code of Employees class
  • Table 5 Code of Products class...........................................................................
  • Table 6 Code of CustomersList class
  • Table 7 Code of EmployeesList class
  • Table 8 Code of ProductsList class
  • Table 9 Code of BuyShoes class.........................................................................
  • Table 10 Code of MainShoes class
  • Figure 7 Class Diagram
  • Figure 8 Add product
  • Figure 9 Show Product
  • Figure 10 Purchase
  • Figure 11 Example UML of Singleton design pattern
  • Figure 12 Example UML of Factory design Pattern
  • Figure 13 Example UML of Adapter design Pattern
  • Figure 14 Example UML of Iterator design Pattern

Perfomed Student: Tran Xuan Tu 1

CHAPTER 1 IMPLEMENT CODE APPLYING DESIGN PATTERNS

SCERIANO

1.1 Scenario

Based on the scenario presented in section 2.2.1 in ASM1, the problem has the following functions:

Admin:  Product management  Order management  Category management  Account management  Login/Logout Customer:  Register  View product  Search for shoes  Shopping cart management  Payment  Login/Logout

1.2 Class diagram for the system

After analyzing and choosing a design pattern, we chose the Singleton Design Pattern for this scenario. The content is presented in detail in the figure below:

Perfomed Student: Tran Xuan Tu 3

The Customers class declares the properties of customers such as: customerId of customers, customerName, phone, address and list of products. The Customers class has methods like: Constructor Customers(), equals(), display() and displayProducts() methods.

Products class The Products class declares the properties of products such as: productsId of products, productsName, productsPrice, productsBrand and list of Customers. The Products class has methods like: Constructor Products(), equals(), display() and displayCustomers() methods.

Employees class The Employees class declares the attributes of employees such as: employeesId of employees , employeesName, employeesPhone, employeesEmail, employeesAddress and list products. The Employees class has methods such as: Constructor Employees(), equals(), display() and displayProducts() methods.

CustomersList class The Customers List class has methods such as: Constructor method CustomersList(), display() and displayProducts().

EmployeesList class EmployeeList class has methods like: Constructor method EmployeesList(), display() and displayProducts().

ProductsList class Products List class has methods such as: Constructor method ProductsList(), display() and displayCustomers().

BuyShoes class The BuyShoes class declares properties such as: List employees final, List customers final, input and buffer final. The BuyShoes class has the following methods: Constructor method BuyShoes(), getString(), showProduct(), addProduct(), Purchase(), cancelPurchase, runCustomer() and runAdmin().

MainShoes class The MainShoes class declares the function "public static void main (String[] args)" which is the most important function for the program to run.

Perfomed Student: Tran Xuan Tu 4

1.3 Java Code implements

1.3.1 Customer class

Table 3 Code of Customer class public class Customers { public String customerId; public String customerName; public String phone; public String address; public ProductsList products = new ProductsList(); public Customers() { } @Override public boolean equals(Object node) { return customerId.equals(((Customers) node).customerId); } public void display() { System.out.println("-> Customer ID: " + customerId + " - " + "Customer Name: " + customerName + " - " + "Phone: " + phone + " - " + "Address: " + address); } public void displayProducts() { if (!products.isEmpty()) { System.out.println("-> Customer: " + customerName + " purchased the following products: "); products.display(); } else {

Perfomed Student: Tran Xuan Tu 6

public void displayProducts() { System.out.println("-> Employee Name: " + employeesName); products.display(); } }

1.3.3 Products class

Table 5 Code of Products class public class Products { public String productId; public String productName; public String productPrice; public String productBrand; public CustomersList customers = new CustomersList(); public Products() { } @Override public boolean equals(Object node) { return productId.equals(((Products) node).productId); } public void display() { System.out.println("-> Product ID: " + productId + " - " + "Name: " + productName + " - " + "Price: " + productPrice + " USD" + " - " + "Brand: " + productBrand); } public void displayCustomers() { System.out.println("-> Product ID: " + productId + " - " + "Name: " + productName + " - " + "Price: " + productPrice + " USD" + " - " + "Brand: " + productBrand);

Perfomed Student: Tran Xuan Tu 7

customers.display(); } }

1.3.4 CustomersList class

Table 6 Code of CustomersList class

import java.util.LinkedList; public class CustomersList extends LinkedList{ public CustomersList() { super(); } public void display() { for (java.util.Iterator it = iterator(); it.hasNext();) ((Customers) it.next()).display(); } public void displayProducts() { for (java.util.Iterator it = iterator(); it.hasNext();) ((Customers) it.next()).displayProducts(); } }

1.3.5 EmployeesList class

Table 7 Code of EmployeesList class

import java.util.LinkedList; public class EmployeesList extends LinkedList { public EmployeesList() { super();

Perfomed Student: Tran Xuan Tu 9

public void displayCustomers() { Object[] products = toArray(); for (Object product : products) { ((Products) product).displayCustomers(); } } }

1.3.7 BuyShoes class

Table 9 Code of BuyShoes class

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; public class BuyShoes { private final EmployeesList[] employees = new EmployeesList[(int)('Z' + 1)]; private final CustomersList[] customers = new CustomersList[(int) ('Z' + 1)]; private String input; private final BufferedReader buffer = new BufferedReader(new In- putStreamReader(System.in));

public BuyShoes() { for (int i = 0; i <= (int) 'Z'; i++) { employees[i] = new EmployeesList(); customers[i] = new CustomersList();

Perfomed Student: Tran Xuan Tu 10

private String getString(String msg) { System.out.print(msg + " "); System.out.flush(); try { input = buffer.readLine(); } catch (IOException io) {} return input.substring(0, 1).toUpperCase() + input.substring(1); }

private void showProduct() { System.out.println("\nThe following products and employees are available:"); for (int i = (int) 'A'; i <= (int) 'Z'; i++) { if (!employees[i].isEmpty()) { employees[i].displayProducts(); } } }

private void addProduct() { Employees em = new Employees(); int emIndex; Products pr = new Products(); em.employeesId = getString("\nEnter employee’s ID:");

Perfomed Student: Tran Xuan Tu 12

Customers cu = new Customers(), cuRef; Employees em = new Employees(), emRef = new Employees(); Products pr = new Products(), prRef; int cuIndex, prIndex = -1, emIndex = -1; while (emIndex == -1) { em.employeesId = getString("\nEnter Employee’s ID:"); emIndex = employees[(int) em.employeesId.charAt(0)].indexOf(em); if (emIndex == -1) { System.out.println("Incorrect Employee's ID!!"); } } while (prIndex == -1) { pr.productId = getString("\nEnter the product's ID:"); emRef = (Employees) employees[(int) em.employeesId.charAt(0)].get(emIn- dex); prIndex = emRef.products.indexOf(pr); if (prIndex == -1) { System.out.println("Incorrect Product's ID!!"); } } prRef = (Products) emRef.products.get(prIndex); cu.customerId = getString("\nEnter customer’s ID:"); cuIndex = customers[(int) cu.customerId.charAt(0)].indexOf(cu); if (cuIndex == -1) { cu.customerName = getString("\nEnter customer’s name:"); cu.products.add(prRef);

Perfomed Student: Tran Xuan Tu 13

customers[(int) cu.customerId.charAt(0)].add(cu); prRef.customers.add(cu); } else { cuRef = (Customers) customers[(int) cu.customerId.charAt(0)].get(cuIndex); cuRef.products.add(prRef); prRef.customers.add(cuRef); } check_purchase = true; } } if (check_purchase == true) { System.out.println("-> Purchase Successfully!"); } }

private void cancelPurchase() { boolean check_cancel_purchase = false; Customers cu = new Customers(), cuRef; Products pr = new Products(), prRef; Employees em = new Employees(), emRef; int cuIndex = -1, prIndex = -1, emIndex = -1; for (int i = (int) 'A'; i <= (int) 'Z'; i++) { if (!employees[i].isEmpty()) { employees[i].displayProducts(); } }