By GokiSoft.com|
15:26 26/07/2023|
Java Advanced
[Share Code] Tìm hiểu Singleton Pattern & Localization Java - C2209I
#DataManager.java
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.gokisoft.java2.lesson08;
import java.util.ArrayList;
import java.util.List;
/***
* DataManager -> Chi can tao 1 object tu Class Object DataManager
* Neu tao nhieu hon 1 -> Sai nghiep vu du an.
* Lam sao -> Chi cho phep tao duy nhat 1 object tu DataManager
*/
public class DataManager {
List<Main.Student> studentList;
static DataManager instance = null;
private DataManager() {
studentList = new ArrayList<>();
}
public synchronized static DataManager getInstance() {
if(instance == null) {
instance = new DataManager();
}
return instance;
}
public List<Main.Student> getStudentList() {
return studentList;
}
public void setStudentList(List<Main.Student> studentList) {
this.studentList = studentList;
}
}
#globals.properties
# Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
# Click nbfs://nbhost/SystemFileSystem/Templates/Other/properties.properties to edit this template
test_option1 = 1. Input student's information
test_option2 = 2. Display student's information
test_option3 = 3. Search
test_option4 = 4. Sort by name
test_option5 = 5. Exit
test_option6 = Choose:
test_continue = Continue...
#globals_vi_VN.properties
# Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
# Click nbfs://nbhost/SystemFileSystem/Templates/Other/properties.properties to edit this template
test_option1 = 1. Nhap thong tin sinh vien
test_option2 = 2. Hien thi thong tin sinh vien
test_option3 = 3. Tim kiem
test_option4 = 4. Sap xep
test_option5 = 5. Thoat
test_option6 = Chon:
test_continue = Ti\u1ebfp t\u1ee5c...
#Main.java
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.gokisoft.java2.lesson08;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author teacher
*/
public class Main {
static class Student {
String fullname;
String email;
public Student() {
}
public Student(String fullname, String email) {
this.fullname = fullname;
this.email = email;
}
public String getFullname() {
return fullname;
}
public void setFullname(String fullname) {
this.fullname = fullname;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "fullname=" + fullname + ", email=" + email;
}
}
public static void main(String[] args) {
Student std = new Student();
Student std2 = new Student();
System.out.println(std);
System.out.println(std2);
// DataManager dm1 = new DataManager();
// DataManager dm2 = new DataManager();
DataManager dm1 = DataManager.getInstance();
dm1.getStudentList().add(new Student());
DataManager dm2 = DataManager.getInstance();
dm2.getStudentList().add(new Student());
System.out.println(dm1.getStudentList().size());
System.out.println(dm2.getStudentList().size());
}
}
#Test.java
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.gokisoft.java2.lesson08;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.Scanner;
/**
*
* @author teacher
*/
public class Test {
public static void main(String[] args) {
Locale locale = new Locale("vi", "VN");
ResourceBundle bundle = ResourceBundle.getBundle("resources/globals", locale);
Scanner scan = new Scanner(System.in);
int choose;
do {
showMenu();
choose = Integer.parseInt(scan.nextLine());
System.out.println(bundle.getString("test_continue"));
} while (choose != 5);
}
static void showMenu() {
Locale locale = new Locale("vi", "VN");
ResourceBundle bundle = ResourceBundle.getBundle("resources/globals", locale);
System.out.println(bundle.getString("test_option1"));
System.out.println(bundle.getString("test_option2"));
System.out.println(bundle.getString("test_option3"));
System.out.println(bundle.getString("test_option4"));
System.out.println(bundle.getString("test_option5"));
System.out.println(bundle.getString("test_option6"));
}
}
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)