[Case Study #1] Tạo ứng dụng Quản lý ShopX đơn giản bằng Java Console Application

Xin chào ban,

Trong serial hướng dẫn xây dựng ứng dụng quản lý Shop bán hàng bằng Java Console Application gồm nhiều phần này, chúng ta sẽ cùng nhau hoàn thiện một Case Study ShopX đơn giản áp dụng các kỹ thuật sau:

  • Lập trình hướng đối tượng.
  • Java Database Connectivity (JDBC) để thao tác với CSDL quan hệ MySQL.
  • Đọc ghi dữ liệu với Java I/O.
  • Data Access Object (DAO) Pattern.

Đầu ra ứng dụng Quản lý Shop bán hàng

Java Console Application ứng dụng CRUD quản lý shop bán hàng
Java Console Application ứng dụng CRUD quản lý shop bán hàng.

Các công cụ/tool cần thiết

  • IntelliJ IDE.
  • Gradle.
  • MySQL database server (Hệ quản trị cơ sở dữ liệu quan hệ).
  • MySQL Workbench (Một tool để quản lý CSDL).

Các bước thực hiện tạo ứng dụng ShopX

Bước 1: Thiết kế và xây dựng CSDL

Tên CSDL: “shopx_db” bao gồm các bảng và sơ đồ quan hệ giữa các bảng như sau:

Cơ sở dữ liệu ShopX
Cơ sở dữ liệu ShopX

Bước 2: Thiết kế các lớp Entities để tạo các đối tượng trong Java:

Các lớp Entities trong ShopX
Các lớp Entities trong ShopX

Thiết kế Class Product như sau:

package entities;

import java.io.Serializable;
import java.time.LocalDateTime;

public class Product implements Serializable {

private Integer id;
private String name;
private Double price;
private Short quantityInStock;
private String description;
private LocalDateTime lastUpdate;
private Category category;

public Product() {
}

public Product(String name, Double price, Short quantityInStock, String description, LocalDateTime lastUpdate, Category category) {
this.name = name;
this.price = price;
this.quantityInStock = quantityInStock;
this.description = description;
this.lastUpdate = lastUpdate;
this.category = category;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Double getPrice() {
return price;
}

public void setPrice(Double price) {
this.price = price;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public Short getQuantityInStock() {
return quantityInStock;
}

public void setQuantityInStock(Short quantityInStock) {
this.quantityInStock = quantityInStock;
}

public LocalDateTime getLastUpdate() {
return lastUpdate;
}

public void setLastUpdate(LocalDateTime lastUpdate) {
this.lastUpdate = lastUpdate;
}

public Category getCategory() {
return category;
}

public void setCategory(Category category) {
this.category = category;
}

@Override
public String toString() {
return "Product{" +
"id=" + id +
", name='" + name + '\'' +
", price=" + price +
", quantityInStock=" + quantityInStock +
", description='" + description + '\'' +
", lastUpdate=" + lastUpdate +
", category=" + category +
'}';
}
}

Nội dung code trong class Category như sau:

package entities;

public class Category {
private Integer id;
private String name;

public Category(String name) {
this.name = name;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

@Override
public String toString() {
return "Category{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}

Nội dung code trong class Customer:

package entities;

public class Customer {
private Integer id;
private String name;
private String email;
private String phone;
private String address;
private String city;

public Customer(String name, String email, String phone, String address, String city) {
this.name = name;
this.email = email;
this.phone = phone;
this.address = address;
this.city = city;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

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 String getPhone() {
return phone;
}

public void setPhone(String phone) {
this.phone = phone;
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

public String getCity() {
return city;
}

public void setCity(String city) {
this.city = city;
}

@Override
public String toString() {
return "Customer{" +
"id=" + id +
", name='" + name + '\'' +
", email='" + email + '\'' +
", phone='" + phone + '\'' +
", address='" + address + '\'' +
", city='" + city + '\'' +
'}';
}
}

Nội dung code trong class Order:

package entities;

import java.time.LocalDateTime;

public class Order {
private Integer id;
private Double amount;
private LocalDateTime dateCreated;
private Customer customer;
private Integer confirmationNumber;


public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public Double getAmount() {
return amount;
}

public void setAmount(Double amount) {
this.amount = amount;
}

public LocalDateTime getDateCreated() {
return dateCreated;
}

public void setDateCreated(LocalDateTime dateCreated) {
this.dateCreated = dateCreated;
}

public Customer getCustomer() {
return customer;
}

public void setCustomer(Customer customer) {
this.customer = customer;
}

public Integer getConfirmationNumber() {
return confirmationNumber;
}

public void setConfirmationNumber(Integer confirmationNumber) {
this.confirmationNumber = confirmationNumber;
}

@Override
public String toString() {
return "Order{" +
"id=" + id +
", amount=" + amount +
", dateCreated=" + dateCreated +
", customer=" + customer +
", confirmationNumber=" + confirmationNumber +
'}';
}
}

Bước 3: Thiết kế Menu chính cho ứng dụng:

System.out.println("========================================================="
+ "\n\t\tWelcome to the Shop X!"
+ "\n\t\tby Nguyen Ba Tuan Anh"
+ "\n=========================================================");
System.out.println("Please enter a choice and press <enter>: ");
System.out.println("\t1. List all product");
System.out.println("\t2. Create a new product");
System.out.println("\t3. View all customers");
System.out.println("\t4. View all orders");
System.out.println("\t5. Sign out");
System.out.println("\t6. Quit the application");
System.out.print("Enter you choice: ");
userOption = Integer.parseInt(scanner.nextLine());

Còn tiếp…

Mình là Nguyễn Bá Tuấn Anh. Một nhà phát triển web sống ở Hà Nội, Việt Nam

Leave a reply:

Your email address will not be published.

Site Footer

Sliding Sidebar

Facebook