0% found this document useful (0 votes)
6 views20 pages

Bai 2500 Loc

The document contains Java code for a customer management system, including classes for data input, validation, customer entities, and a manager for customer operations. It features methods for retrieving and validating user input, managing customer details, and handling exceptions. The main program provides a menu for user interaction, allowing operations such as registering and updating customer information.

Uploaded by

toilakiet01
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)
6 views20 pages

Bai 2500 Loc

The document contains Java code for a customer management system, including classes for data input, validation, customer entities, and a manager for customer operations. It features methods for retrieving and validating user input, managing customer details, and handling exceptions. The main program provides a menu for user interaction, allowing operations such as registering and updating customer information.

Uploaded by

toilakiet01
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
You are on page 1/ 20

package Utilities;

import java.time.LocalDate;

import java.time.format.DateTimeFormatter;

import java.util.Scanner;

/**

* @author SwordLake

*/

public class DataInput {

// ------------------------------------------------------------------------

public static int getIntegerNumber(String displayMessage) throws


Exception {

int number = 0;

System.out.print(displayMessage);

number = getIntegerNumber();

return number;

// ------------------------------------------------------------------------

public static int getIntegerNumber() throws Exception {

int number = 0;

String strInput;

strInput = getString();

// if (!strInput.matches("\\d{1,10}")) {
if (!DataValidation.checkStringWithFormat(strInput, "\\d{1,10}")) {

throw new Exception("Data invalid.");

} else {

number = Integer.parseInt(strInput);

return number;

// ------------------------------------------------------------------------

public static double getDoubleNumber(String displayMessage) throws


Exception {

double number = 0;

String strInput = getString(displayMessage);

if (DataValidation.checkStringEmpty(strInput)) {

if (!DataValidation.checkStringWithFormat(strInput, "[0-9]+[.]?[0-
9]+")) {

throw new Exception("Data invalid.");

} else {

number = Double.parseDouble(strInput);

return number;

// ------------------------------------------------------------------------
public static String getString(String displayMessage) {

String strInput;

System.out.print(displayMessage);

strInput = getString();

return strInput;

// ------------------------------------------------------------------------

public static String getString() {

String strInput;

Scanner sc = new Scanner(System.in);

strInput = sc.nextLine();

return strInput;

// ------------------------------------------------------------------------

public static LocalDate getDate(String displayMessage) throws Exception {

String strInput;

LocalDate date = null;

strInput = getString(displayMessage);

if (DataValidation.checkStringEmpty(strInput)) {

try {

date = LocalDate.parse(strInput,
DateTimeFormatter.ofPattern("dd/MM/yyyy"));

} catch (Exception ex) {

throw new Exception("Date invalid.");


}

return date;

// To do here...

package Utilities;

/**

* @author Swordlake

*/

public final class DataValidation {

// ------------------------------------------------------------------------

public static boolean checkNumberInMinMax(int number, int min, int max)


{

boolean result = true;

if (number < min || number > max) {

result = false;

return result;

}
// ------------------------------------------------------------------------

public static boolean checkStringEmpty(String value) {

boolean result = true;

if (value.isEmpty()) {

result = false;

return result;

// ------------------------------------------------------------------------

public static boolean checkStringLengthInRange(String value, int min, int


max) {

boolean result = true;

int length;

if (checkStringEmpty(value)) {

result = false;

} else {

length = value.length();

if (length < min || length > max) {

result = false;

return result;

// ------------------------------------------------------------------------
public static boolean checkStringWithFormat(String value, String pattern)
{

boolean result = false;

if (value.matches(pattern)) {

result = true;

return result;

/*

* To change this license header, choose License Headers in Project


Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package Core.Entities;

import DataObject.CustomerDAO;

import Utilities.DataValidation;

/**

*
* @author toila

*/

public class Customer extends User {

private String Phone;

private String Email;

public Customer(String CusCode, String name, String Phone, String Email)


throws Exception {

super(CusCode,name);

setCusCode(CusCode);

setName(name);

setPhone(Phone);

setEmail(Email);

//--------------------------------------------------------

// kiểm tra

public void setCusCode (String Cuscode) throws Exception{

if (!DataValidation.checkStringWithFormat(Cuscode.toUpperCase(), "C\\
d{4}" )

&& !
DataValidation.checkStringWithFormat(Cuscode.toUpperCase(), "G\\d{4}" )

&& !
DataValidation.checkStringWithFormat(Cuscode.toUpperCase(), "K\\d{4}" ) )
{

throw new Exception("Id invalid. The correct format: Cxxxx or Gxxxx


or Kxxxx, with x is digit.");

}
this.CusCode = Cuscode;

//----------------------------------------------------

public void setPhone(String Phone) throws Exception{

if (Phone.length() != 10) {

throw new Exception("Phone must be 10 digit");

if(Phone.charAt(0) != '0'){

throw new Exception("First digit in Phone must be 0");

if (!checkPhone(Phone)){

throw new Exception("Your Phone must be digit");

this.Phone = Phone;

public static boolean checkPhone(String Phone) throws Exception{

for (int i = 0; i < Phone.length(); i++) {

if (!Character.isDigit(Phone.charAt(i))){

return false;

return true;

//----------------------------------------------------------------

public boolean checkEmail(String Email){


if(Email == null ) return false;

int aindex = Email.indexOf('@');

int dotindex = Email.indexOf('.');

return aindex > 0 && aindex + 1 < dotindex && dotindex <
Email.length() - 1;

public void setEmail(String Email) throws Exception{

if(!checkEmail(Email)) throw new Exception("Error Email !!!");

this.Email = Email;

//----------------------------------------------------------------

public String getCusCode() {

return CusCode;

public String getName() {

return name;

public String getPhone() {

return Phone;

public String getEmail() {

return Email;
}

//-------------------------------------------------------------------

package Core.Entities;

import Utilities.DataValidation;

public abstract class User {

// Các thuộc tính của lớp User

protected String CusCode;

protected String name;

// Hàm khởi tạo mặc định

public User() {

CusCode = "U0000";

name = "New User";

// Hàm khởi tạo có tham số

public User(String CusCode, String name) {

this.CusCode = CusCode;

this.name = name;
}

// Getter và setter cho thuộc tính id

public String getId() {

return CusCode.toUpperCase(); // Trả về id sau khi chuyển thành chữ


hoa

public void setId(String id) throws Exception {

// Không có kiểm tra giá trị hợp lệ cho id

this.CusCode = CusCode;

// Getter và setter cho thuộc tính name

public String getName() {

return toTitleCase(name); // Trả về name sau khi chuyển thành dạng


viết hoa chữ cái đầu

public void setName(String name) throws Exception {

if (DataValidation.checkStringWithFormat(name, "[A-Za-z\\s] {2,25}")) {

throw new Exception("Name must be from 2 to 25 characters.");

this.name = toTitleCase(name);

// Hàm chuyển đổi chuỗi thành dạng viết hoa chữ cái đầu
public String toTitleCase(String value) {

// ... (Mã thực hiện chuyển đổi)

String s = "";

value = value.trim().replaceAll("\\s+"," ").toLowerCase();

String [] words = value.split(" ");

for (String word : words) {

char[] arr = word.toCharArray();

arr[0] = Character.toUpperCase(arr[0]);

s = s + new String(arr) + " ";

return s.trim();

// Override phương thức toString() để hiển thị thông tin của đối tượng

@Override

public String toString() {

return String.format("%s, %s", CusCode, name);

/*

* To change this license header, choose License Headers in Project


Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package ManagerObject;
import Core.Entities.Customer;

import Core.Interfaces.ICustomerDAO;

import DataObject.CustomerDAO;

import Utilities.DataInput;

import java.util.List;

/**

* @author toila

*/

public class ManagerCustomers {

CustomerDAO customerDAO ;

public ManagerCustomers (CustomerDAO customerDAO ) {

this.customerDAO = customerDAO;

public void processMenuCustomers () {

boolean stop = true;

try {

do {

System.out.println("******Employee Management******");

System.out.print("1.Register customers\n"

+ "2.Update customer informatio\n"

+ "3.Search for customer information by name\n"

+ "4.Display feast menus\n"

+ "5.Place a feast menus.\n"

+ "6.Update order information\n"


+ "7.Save data to file"

+ "\n8.Display Customer or Order lists"

+ "\n9.Quit");

int choice = DataInput.getIntegerNumber();

switch(choice){

case 1 : {

registerCustomers();

break;

case 2 : {

updateCustomer();

break;

case 3 : {

searchCustomer();

break;

case 4 : {

displayFeastMenus();

break;

case 5 : {

placeAFeastMenus();

break;

case 6: {

updateOrder();
break;

case 7 : {

saveDataToFile();

break;

case 8 : {

displayCustomerOrOrder();

break;

case 9 : {

stop = false;

break;

default:

System.out.println("Wrong choice");

} while (true);

catch (Exception e) {

System.out.println("Error Choice");

private Customer inputCustomer () throws Exception{

//String CusCode, String name, String Phone, String Email


String cusCode = DataInput.getString("Enter the Customer Code");

String name = DataInput.getString("Enter the name customer");

String Phone = DataInput.getString("Enter Phone ");

String Email = DataInput.getString("Enter Email ");

return new Customer(cusCode, name, Phone, Email);

private void registerCustomers() {

try {

Customer newcus = inputCustomer();

if(customerDAO.getCustomerByCode(newcus.getCusCode()) != null){

System.out.println("Customer has existed");

return;

customerDAO.addCustomer(newcus);

System.out.println("Add success");

} catch (Exception e) {

System.out.println("Error" + e.getMessage());

private void updateCustomer() {

throw new UnsupportedOperationException("Not supported yet."); //To


change body of generated methods, choose Tools | Templates.

private void searchCustomer() {


throw new UnsupportedOperationException("Not supported yet."); //To
change body of generated methods, choose Tools | Templates.

private void displayFeastMenus() {

throw new UnsupportedOperationException("Not supported yet."); //To


change body of generated methods, choose Tools | Templates.

private void placeAFeastMenus() {

throw new UnsupportedOperationException("Not supported yet."); //To


change body of generated methods, choose Tools | Templates.

private void updateOrder() {

throw new UnsupportedOperationException("Not supported yet."); //To


change body of generated methods, choose Tools | Templates.

private void saveDataToFile() {

throw new UnsupportedOperationException("Not supported yet."); //To


change body of generated methods, choose Tools | Templates.

private void displayCustomerOrOrder() {

throw new UnsupportedOperationException("Not supported yet."); //To


change body of generated methods, choose Tools | Templates.

}
}
/*

* To change this license header, choose License Headers in Project


Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package Presentation;

import Core.Interfaces.ICustomerDAO;

import DataObject.CustomerDAO;

import ManagerObject.ManagerCustomers;

/**

* @author toila

*/

public class Menu {

public static void manageCustomer(CustomerDAO service){

ManagerCustomers managerCustomers = new


ManagerCustomers(service);

managerCustomers.processMenuCustomers();

}
/*
* To change this license header, choose License Headers in Project
Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package Presentation;

import Core.Entities.*;

import static Core.Entities.Customer.checkPhone;

import Core.Interfaces.ICustomerDAO;

import DataObject.CustomerDAO;

import ManagerObject.ManagerCustomers;

import Utilities.DataInput;

import Utilities.DataValidation;

import java.util.Scanner;

/**

* @author toila

*/

public class Program {

public static void main(String[] args) throws Exception {

String customerDataFile = "Customers.txt";

for(;;){

System.out.println("*****************Main Menu*************");

System.out.print("1.Employee Management|2.Customer Management|


3.Exit|Select:");

int choice = DataInput.getIntegerNumber();

switch(choice){
case 1 :{

CustomerDAO customerDAO = new


CustomerDAO(customerDataFile);

Menu.manageCustomer(customerDAO);

case 2 :

case 3 :

break;

default:

System.out.println("Wrong");

You might also like