By GokiSoft.com| 14:11 13/01/2023|
Java Advanced

Ôn tập Java 2. C2109I

#Employee.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 java2.overview.lesson01;

import java.io.Serializable;

/**
 *
 * @author diepvan
 */
public class Employee implements Serializable{
    int id;
    String name;
    String email;

    public Employee(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int 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;
    }

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

#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 java2.overview.lesson01;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author diepvan
 */
public class Main {
    static void bai1() {
        Scanner scan = new Scanner(System.in);
        
        //Cau 1: Tao mang
        List<Employee> dataList = new ArrayList<>();
        
        for (int i = 0; i < 3; i++) {
            Employee employee;
            System.out.println("Nhap ID: ");
            int id = Integer.parseInt(scan.nextLine());
            System.out.println("Nhap ten: ");
            String name = scan.nextLine();
            
            employee = new Employee(id, name);
            
            dataList.add(employee);
        }
        
        //Cau 2: Luu File
        FileOutputStream fos = null;
        ObjectOutputStream oos = null;
        
        try {
            fos = new FileOutputStream("employee.dat");
            oos = new ObjectOutputStream(fos);
            
            oos.writeObject(dataList);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if(fos != null) {
                try {
                    fos.close();
                } catch (IOException ex) {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            
            if(oos != null) {
                try {
                    oos.close();
                } catch (IOException ex) {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        
        //Cau 3:
        FileInputStream fis = null;
        ObjectInputStream ois = null;
        
        try {
            fis = new FileInputStream("employee.dat");
            ois = new ObjectInputStream(fis);
            
            dataList = (List<Employee>) ois.readObject();
            
            for (Employee employee : dataList) {
                System.out.println(employee);
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if(fis != null) {
                try {
                    fis.close();
                } catch (IOException ex) {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            if(ois != null) {
                try {
                    ois.close();
                } catch (IOException ex) {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        
        //Ket thuc bai 1:
    }
    
    public static void main(String[] args) {
//        bai1();
        //Chuyen sang bai 2:
        SharedData sharedData = new SharedData();
        Thread1 t1 = new Thread1(sharedData);
        Thread2 t2 = new Thread2(sharedData);
        t1.start();
        t2.start();
    }
}

#SharedData.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 java2.overview.lesson01;

import java.util.Random;

/**
 *
 * @author diepvan
 */
public class SharedData {
    String[] vnDays = new String[]{"Thu hai", "Thu ba", "Thu tu", "Thu nam", "Thu sau", "Thu bay", "Chu Nhat"};
    String[] enDays = new String[]{"Monday", "Tuesday", "Wednesday", "Thurday", "Friday", "Saturday", "Sunday"};
    int index;
    
    public void randomIndex() {
        Random rand = new Random();
        index = rand.nextInt(0, vnDays.length);
    }
    
    public String getVnDay() {
        return vnDays[index];
    }
    
    public String getEnDay() {
        return enDays[index];
    }
}

#Thread1.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 java2.overview.lesson01;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author diepvan
 */
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);
        }
        while (true) {            
            synchronized (sharedData) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException ex) {
                    Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
                }
                sharedData.randomIndex();
                System.out.println(sharedData.getVnDay());
                
                sharedData.notifyAll();
                try {
                    sharedData.wait();
                } catch (InterruptedException ex) {
                    Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
    
}

#Thread2.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 java2.overview.lesson01;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author diepvan
 */
public class Thread2 extends Thread {

    SharedData sharedData;

    public Thread2(SharedData sharedData) {
        this.sharedData = sharedData;
    }

    @Override
    public void run() {
        while (true) {
            synchronized (sharedData) {
                sharedData.notifyAll();
                try {
                    sharedData.wait();
                } catch (InterruptedException ex) {
                    Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
                }
                String en = sharedData.getEnDay();
                System.out.println(en);
            }
        }
    }

}

#Bai2.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 java2.overview.lesson02;

/**
 *
 * @author diepvan
 */
public class Bai2 {
    public static void main(String[] args) {
        SharedData sharedData = new SharedData();
        Thread1 t1 = new Thread1(sharedData);
        Thread2 t2 = new Thread2(sharedData);
        t1.start();
        t2.start();
    }
}

#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 java2.overview.lesson02;

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 diepvan
 */
public class Main {
    public static void main(String[] args) {
        //Cau 1:
        List<Opportunity> dataList = new ArrayList<>();
        
        for (int i = 0; i < 5; i++) {
            Opportunity obj = new Opportunity();
            obj.input();
            
            dataList.add(obj);
        }
        
        //Cau 2:
        FileOutputStream fos = null;
        ObjectOutputStream oos = null;
        
        try {
            fos = new FileOutputStream("data_file.bat");
            oos = new ObjectOutputStream(fos);
            
            oos.writeObject(dataList);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if(fos != null) {
                try {
                    fos.close();
                } catch (IOException ex) {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            if(oos != null) {
                try {
                    oos.close();
                } catch (IOException ex) {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        
        //Cau 3:
        FileInputStream fis = null;
        ObjectInputStream ois = null;
        
        try {
            fis = new FileInputStream("data_file.bat");
            ois = new ObjectInputStream(fis);
            
            dataList = (List<Opportunity>) ois.readObject();
            
            for (Opportunity opportunity : dataList) {
                opportunity.display();
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if(fis != null) {
                try {
                    fis.close();
                } catch (IOException ex) {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            
            if(ois != null) {
                try {
                    ois.close();
                } catch (IOException ex) {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        
        //Cau 4: Tim kiem
        InputStreamReader reader = new InputStreamReader(System.in);
        BufferedReader bufferedReader = new BufferedReader(reader);
            
        System.out.println("Nhap tieu de viec can tim: ");
        try {
            String s = bufferedReader.readLine();
            int count = 0;
            
            for (Opportunity opportunity : dataList) {
                if(opportunity.getJobTitle().equalsIgnoreCase(s)) {
                    opportunity.display();
                    count++;
                }
            }
            
            if(count == 0) {
                System.out.println("NOT_FOUND");
            }
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

#Opportunity.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 java2.overview.lesson02;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author diepvan
 */
public class Opportunity implements Serializable{
    String id;
    String jobTitle;
    float expectedSalary;
    List<String> skills;
    List<String> education;

    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;
    }

    public void display() {
        System.out.println(this);
        System.out.println("Danh sach ky nang: ");
        for (String skill : skills) {
            System.out.println(skill);
        }
        
        System.out.println("Danh sach truong hoc: ");
        for (String str : education) {
            System.out.println(str);
        }
    }
    
    @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 Job Title: ");
                jobTitle = bufferedReader.readLine();
            } while(jobTitle.length() < 10);
            
            do {
                System.out.println("Nhap muc luong mong muon: ");
                expectedSalary = Float.parseFloat(bufferedReader.readLine());
            } while(expectedSalary <= 10);
            
            System.out.println("Nhap so ky nang: ");
            int n;
            do {
                n = Integer.parseInt(bufferedReader.readLine());
            } while(n < 2);
            for (int i = 0; i < n; i++) {
                System.out.println("Ky nang: ");
                String str = bufferedReader.readLine();
                skills.add(str);
            }
            
            System.out.println("Nhap so truong hoc: ");
            do {
                n = Integer.parseInt(bufferedReader.readLine());
            } while(n < 1);
            for (int i = 0; i < n; i++) {
                System.out.println("Truong: ");
                String str = bufferedReader.readLine();
                education.add(str);
            }
        } catch (IOException ex) {
            Logger.getLogger(Opportunity.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }
}

#SharedData.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 java2.overview.lesson02;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 *
 * @author diepvan
 */
public class SharedData {
    Map<String, String> map;
    String key;
    
    public SharedData() {
        map = new HashMap<>();
        map.put("Monday", "Thu 2");
        map.put("Tuesday", "Thu 3");
        map.put("Wednesday", "Thu 4");
        map.put("Thurday", "Thu 5");
        map.put("Friday", "Thu 6");
        map.put("Saturday", "Thu 7");
        map.put("Sunday", "Chu nhat");
        
        key = "Monday";
    }
    
    public void random() {
        Random ran = new Random();
        int index = ran.nextInt(0, map.keySet().size());
        
        Object[] list = map.keySet().toArray();
        key = (String) list[index];
    }
    
    public String getEN() {
        return key;
    }
    
    public String getVN() {
        return map.get(key);
    }
}

#Thread1.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 java2.overview.lesson02;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author diepvan
 */
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);
        }
        
        while (true) {            
            synchronized (sharedData) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException ex) {
                    Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
                }
                sharedData.random();
                System.out.println(sharedData.getEN());
                
                sharedData.notifyAll();
                try {
                    sharedData.wait();
                } catch (InterruptedException ex) {
                    Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
    
    
}

#Thread2.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 java2.overview.lesson02;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author diepvan
 */
public class Thread2 extends Thread{
    SharedData sharedData;

    public Thread2(SharedData sharedData) {
        this.sharedData = sharedData;
    }

    @Override
    public void run() {
        while (true) {            
            synchronized (sharedData) {
                sharedData.notifyAll();
                try {
                    sharedData.wait();
                } catch (InterruptedException ex) {
                    Logger.getLogger(Thread2.class.getName()).log(Level.SEVERE, null, ex);
                }
                
                System.out.println(sharedData.getVN());
            }
        }
    }
    
    
}
Tags:

Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)