By GokiSoft.com|
19:14 03/04/2023|
Java Advanced
[Source Code] Bài tập luyện thi Java2 - C2206L
Tóm tắt nội dung:
- Kiến thức OOP -> Bắt buộc
- Collections
- ArrayList
- HashMap
- List
- Exception
- Nhập dữ liệu -> Ko dùng Scanner
- File
- FileInputStream / FileOutputStream
- ObjectInputStream / ObjectOutputStream -> Serialization
- FileReader / FileWriter (BufferedReader / BufferedWriter)
- Thread
- Sync 2 Thread
- CSDL
- CRUD
- Java Swing
- Lambda -> Code bình thường -> Click warning -> Generate
- Đa ngôn ngữ
=========================================================================================
#Opportunity.java
/*
* 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 com.gokisoft.bt3192;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author teacher
*/
public class Opportunity implements Serializable{
String id;
String jobTitle;
float expectedSalary;
List<String> skills;
List<String> education;
public Opportunity() {
skills = new ArrayList<>();
education = new ArrayList<>();
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getJobTitle() {
return jobTitle;
}
public void setJobTitle(String jobTitle) {
this.jobTitle = jobTitle;
}
public float getExpectedSalary() {
return expectedSalary;
}
public void setExpectedSalary(float expectedSalary) {
this.expectedSalary = expectedSalary;
}
public List<String> getSkills() {
return skills;
}
public void setSkills(List<String> skills) {
this.skills = skills;
}
public List<String> getEducation() {
return education;
}
public void setEducation(List<String> education) {
this.education = education;
}
@Override
public String toString() {
return "id=" + id + ", jobTitle=" + jobTitle + ", expectedSalary=" + expectedSalary;
}
public void input() {
try {
InputStreamReader reader = new InputStreamReader(System.in);
BufferedReader bufferedReader = new BufferedReader(reader);
System.out.println("Nhap ID: ");
id = bufferedReader.readLine();
do {
System.out.println("Nhap tieu de cong viec: ");
jobTitle = bufferedReader.readLine();
} while (jobTitle.length() < 10);
do {
System.out.println("Nhap muc luong mong muon: ");
expectedSalary = Integer.parseInt(bufferedReader.readLine());
} while (expectedSalary <= 20);
int n;
do {
System.out.println("Nhap so ky nang: ");
n = Integer.parseInt(bufferedReader.readLine());
} while (n < 2);
for (int i = 0; i < n; i++) {
System.out.println("Nhap ky nang: ");
String skill = bufferedReader.readLine();
skills.add(skill);
}
do {
System.out.println("Nhap so truong: ");
n = Integer.parseInt(bufferedReader.readLine());
} while (n < 1);
for (int i = 0; i < n; i++) {
System.out.println("Nhap truong: ");
String edu = bufferedReader.readLine();
education.add(edu);
}
} catch (IOException ex) {
Logger.getLogger(Opportunity.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void display() {
System.out.println(toString());
System.out.println("Danh sach ky nang: ");
for (String skill : skills) {
System.out.println(skill);
}
System.out.println("Danh sach truong: ");
for (String edu : education) {
System.out.println(edu);
}
}
}
#Test.java
/*
* 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 com.gokisoft.bt3192;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author teacher
*/
public class Test {
static List<Opportunity> dataList;
public static void main(String[] args) {
dataList = new ArrayList<>();
// for (int i = 0; i < 5; i++) {
// Opportunity op = new Opportunity();
// op.input();
//
// dataList.add(op);
// }
//
// saveFile();
readFile();
System.out.println("Danh sach cong viec: ");
for (Opportunity op : dataList) {
op.display();
}
search();
}
static void search() {
try {
InputStreamReader reader = new InputStreamReader(System.in);
BufferedReader bufferedReader = new BufferedReader(reader);
System.out.println("Nhap tieu de cong viec can tim: ");
String jobTitle = bufferedReader.readLine();
boolean isFind = false;
for (Opportunity op : dataList) {
if(op.getJobTitle().equalsIgnoreCase(jobTitle)) {
if(!isFind) {
System.out.println("Danh sach cong viec tim thay: ");
}
op.display();
isFind = true;
}
}
if(!isFind) {
System.out.println("NOT FOUND");
}
} catch (IOException ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
}
}
static void readFile() {
FileInputStream fis = null;
ObjectInputStream ois = null;
try {
fis = new FileInputStream("data_file.dat");
ois = new ObjectInputStream(fis);
dataList = (List<Opportunity>) ois.readObject();
} catch (FileNotFoundException ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
} catch (ClassNotFoundException ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if(ois != null) {
try {
ois.close();
} catch (IOException ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
}
}
if(fis != null) {
try {
fis.close();
} catch (IOException ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
static void saveFile() {
FileOutputStream fos = null;
ObjectOutputStream oos = null;
try {
fos = new FileOutputStream("data_file.dat");
oos = new ObjectOutputStream(fos);
oos.writeObject(dataList);
} catch (FileNotFoundException ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if(oos != null) {
try {
oos.close();
} catch (IOException ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
}
}
if(fos != null) {
try {
fos.close();
} catch (IOException ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
}
Thread
#Main.java
/*
* 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 com.gokisoft.bt3192;
/**
*
* @author teacher
*/
public class Main {
public static void main(String[] args) {
SharedData sharedData = new SharedData();
Thread1 t1 = new Thread1(sharedData);
Thread2 t2 = new Thread2(sharedData);
t1.start();
t2.start();
}
}
#SharedData.java
/*
* 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 com.gokisoft.bt3192;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.Set;
/**
*
* @author teacher
*/
public class SharedData {
Map<String, String> dataMap;
String language;
public SharedData() {
dataMap = new HashMap<>();
dataMap.put("Monday", "Thu 2");
dataMap.put("Tuesday", "Thu 3");
dataMap.put("Wednesday", "Thu 4");
dataMap.put("Thursday", "Thu 5");
dataMap.put("Friday", "Thu 6");
dataMap.put("Saturday", "Thu 7");
dataMap.put("Sunday", "Chu Nhat");
}
public String getEnglishDay() {
Random random = new Random();
int rad = random.nextInt(6);
Set<String> sets = dataMap.keySet();
language = (String) sets.toArray()[rad];
return language;
}
public String getVietnameseDay() {
return dataMap.get(language);
}
}
#Thread1.java
/*
* 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 com.gokisoft.bt3192;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author teacher
*/
public class Thread1 extends Thread{
SharedData sharedData;
public Thread1(SharedData sharedData) {
this.sharedData = sharedData;
}
@Override
public void run() {
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
}
synchronized(sharedData) {
//for (int i = 0; i < 10; i++) {
for (;;) {
String en = sharedData.getEnglishDay();
System.out.println("T1 > " + en);
sharedData.notifyAll();
try {
sharedData.wait();
} catch (InterruptedException ex) {
Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
}
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
// System.out.println("T1 stop");
// synchronized(sharedData) {
// sharedData.notifyAll();
// }
}
}
#Thread2.java
/*
* 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 com.gokisoft.bt3192;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author teacher
*/
public class Thread2 extends Thread {
SharedData sharedData;
public Thread2(SharedData sharedData) {
this.sharedData = sharedData;
}
@Override
public void run() {
synchronized (sharedData) {
//for (int i = 0; i < 10; i++) {
for (;;) {
sharedData.notifyAll();
try {
sharedData.wait();
} catch (InterruptedException ex) {
Logger.getLogger(Thread2.class.getName()).log(Level.SEVERE, null, ex);
}
String vn = sharedData.getVietnameseDay();
System.out.println("T2 > " + vn);
}
}
// System.out.println("T2 stop");
// synchronized(sharedData) {
// sharedData.notifyAll();
// }
}
}
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)