Chào các bạn! Vì nhiều lý do từ nay Truyen2U chính thức đổi tên là Truyen247.Pro. Mong các bạn tiếp tục ủng hộ truy cập tên miền mới này nhé! Mãi yêu... ♥

StudenIMG$Test

package Student;

import java.io.*;

import java.util.ArrayList;

import java.util.logging.Level;

import java.util.logging.Logger;

/**

 *

 * @author QuocViet

 */

public class StudentImp implements IStudent {

    //Khai bao ArrayList <Generic>

    ArrayList<Student> arr = new ArrayList<Student>();

    public boolean add(Student No) {

        if(No.validate()) {

            //Validate No <No : dai dien cho 1 student>

            arr.add(No);

            System.out.println("Add a new student successfully!

");

            return true;

        }

        System.out.println("Add a new student fail!

");

        return false;

    }

    //Xoa 1 Student thong qua sCode

    public boolean delete(int sCode) {

        //dung vong lap for de duyet mang arr

        for (int i = 0; i < arr.size(); i++) {

            if(arr.get(i).getsCode() == sCode) {

                //neu sCode cua phan tu thu (i) trung voi sCode nhap vao thi

                arr.remove(i); //Remove Student(i)

                System.out.println("

Delete successfully!

");

                return true;

            }

        }

        System.out.println("

Delete fail!

");

        return false;

    }

    public ArrayList<Student> getList(int num) {

        //khai bao 1 ArrayList

        ArrayList<Student> getList = new ArrayList<Student>();

        //Lay mac dinh nam cua hien tai la 2010

        int year = 2010;

        //Dung for duyet arr neu arr(i) co age > num thi save vao list vua khai bao

        for (int i = 0; i < arr.size(); i++) {

            //Khai bao bien tuoi = nam hien tai - nam sinh

            int age = year - arr.get(i).getsBirth();

            if(age > 20) {

                getList.add(arr.get(i));

            }

        }

        //Tra ve list cac phan tu trong arr co age > num

        return getList;

    }

    public void display(ArrayList<Student> list) {

        if( list.size() <= 0 )

        {

            System.out.println("

\tStudents not found !

");

        }

        else

        {

            System.out.println("Code\t\tName\t\tBirth\t\tNote");

            for (int i = 0; i < list.size(); i++) {

                System.out.printf("%-10d %-20s %-20s %s", list.get(i).getsCode(), list.get(i).getsName(), list.get(i).getsBirth(), list.get(i).getsNote());

                System.out.println();

            }

        }

    }

    public boolean save(String fileName) {

        //Dung Stream de ghi object vao file

        FileOutputStream file = null;

        ObjectOutputStream objWriter = null;

        try {

            file = new FileOutputStream(fileName,true);

            objWriter = new ObjectOutputStream(file);

            for (int i = 0; i < arr.size(); i++) {

                objWriter.writeObject(arr.get(i));

            }

            System.out.println("Save to file successfully!");

            return true;

        } catch (FileNotFoundException ex) {

            return false;

        } catch (IOException ex) {

            return false;

        } finally {

            try {

                objWriter.close();

                file.close();

            } catch (IOException ex) {

            }

        }

    }

    //Kiem tra xem code co trung hay khong

    public boolean checkCode(int sCode) {

        for(int i = 0; i < arr.size(); i++) {

            if(arr.get(i).getsCode() == sCode) {

                return true;

            }

        }

        return false;

    }

    public void SortAndDisplay() {

        //Sort

        for(int i = 0; i < arr.size() - 1; i++) {

            for (int j = i + 1; j < arr.size(); j++) {

                if(arr.get(i).getsName().compareTo(arr.get(j).getsName()) > 0) {

                    Student temp = arr.get(i);

                    arr.set(i, arr.get(j));

                    arr.set(j, temp);

                }

            }

        }

        //Display

        System.out.println("Code\t\tName\t\tBirth\t\tNote");

        for(int i = 0; i < arr.size(); i++)

        {

                System.out.printf(arr.get(i).getsCode()+"\t\t"+arr.get(i).getsName()+"\t\t"+arr.get(i).getsBirth()+"\t\t"+arr.get(i).getsNote());

                System.out.println();

        }

    }

    //Read file

    public void read(String fileName) {

        File isfile = new File(fileName);

        if(isfile.exists())

        {

            //Dung Stream doc object tu file

            FileInputStream file = null;

            ObjectInputStream objReader = null;

            try {

                System.out.println("Code\t\tName\t\tBirth\t\tNote");

                file = new FileInputStream(fileName);

                objReader = new ObjectInputStream(file);

                try {

                    Student read;

                    while (file.available() > 0)

                    {

                       read  = (Student)objReader.readObject();

                       System.out.print(read+"

");

                    }

                } catch (ClassNotFoundException ex) {

                    Logger.getLogger(StudentImp.class.getName()).log(Level.SEVERE, null, ex);

                }

            } catch (FileNotFoundException ex) {

                System.out.println("File not found");

            }catch (IOException ex) {

                System.out.println("Don't not connect!");

            } finally {

                try {

                    objReader.close();

                    file.close();

                } catch (IOException ex) {

                    System.out.println("Don't not connect!");

                }

            }

        }

        else

        {

            System.out.println("File not found");

        }

    }

        //search Student from Code

        public void search(int num) {

            Boolean a = false;

            for (int i = 0; i < arr.size(); i++) {

                if(arr.get(i).getsCode() == num) {

                    System.out.println("Code\t\tName\t\tBirth\t\tNote");

                    System.out.printf(arr.get(i).getsCode()+"\t\t"+arr.get(i).getsName()+"\t\t"+arr.get(i).getsBirth()+"\t\t"+arr.get(i).getsNote());

                    System.out.println();

                    a = true;

                    break;

                }

            }

            if(a == false)

            {

                System.out.println("\tStudent not found !");

            }

        }

}

///StudentTest

package Student;

import java.util.Scanner;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

/**

 *

 * @author QuocViet

 */

public class Main {

    /**

     * @param args the command line arguments

     */

    public static void main(String[] args) {

        StudentImp objSTImp = new StudentImp();

        Scanner bf = new Scanner(System.in);

        Pattern sB;

        Matcher B;

        int sCode = 0, sBirth = 0,n,x;

        String sName = "", sNote = "", fileName = "";

        do

        {

            x = 0;

            System.out.println("

---------- Menu ----------");

            System.out.println("1. Add Information of student");

            System.out.println("2. Search all student have age > 20");

            System.out.println("3. Sorting all students in ArrayList by sName");

            System.out.println("4. Delete student from sCode");

            System.out.println("5. Save all remain students ArrayList to text file");

            System.out.println("6. Read all student from text file");

            System.out.println("7. Search Student by student code");

            System.out.println("8. Exit");

            System.out.println("Please choose: ");

            int ch = bf.nextInt();

            bf.nextLine();

            switch (ch)

            {

                case 1:

                {

                    System.out.println("---------- Add Information of student ----------");

                    System.out.println("Enter the number of student: ");

                    n = bf.nextInt();

                    bf.nextLine();

                    for (int i = 0; i < n; i++) {

                        Student objSt = new Student();

                        System.out.println("(+)Information of student " + (i + 1));

                        //The sCode is a random numbers not duplicate

                        sCode = (int) (Math.random()*19 + 1);

                        System.out.println("Code : " + sCode);

                        objSt.setsCode(sCode);             //Save Code to Student objSt

                        sB = Pattern.compile("[[a-zA-Z]+\\s*]*");

                        System.out.println("Name: ");

                        do{

                            sName = bf.nextLine();

                            B = sB.matcher(sName);

                            if(!B.matches()) {

                                System.out.println("Name must have letters required of the alphabet");

                                System.out.println("Enter the Name: ");

                            }

                            else{

                                if(sName.length() <= 5){

                                    System.out.println("Name must have more than 5 letters");

                                    System.out.println("Enter the Name: ");

                                }

                                else{

                                    break;

                                }

                            }

                        }while(Boolean.TRUE);

                        objSt.setsName(sName);             //Save Name to Student objSt

                        sB = Pattern.compile("^\\d{4}$");

                        System.out.println("Birth : ");

                        do{

                            sBirth = bf.nextInt();

                            B = sB.matcher(String.valueOf(sBirth));

                            if(!B.matches()) {

                                System.out.println("Birth must consists of 4 digits");

                                System.out.println("Enter the Birth: ");

                            }

                            else{

                                if(sBirth <= 1982){

                                    System.out.println("Birth must > 1982");

                                    System.out.println("Enter the Birth: ");

                                }

                                else{

                                    break;

                                }

                            }

                        }while(Boolean.TRUE);

                        objSt.setsBirth(sBirth);           //Save Birth to Student objSt

                        bf.nextLine();

                        System.out.println("Note : ");

                        sNote = bf.nextLine();

                        objSt.setsNote(sNote);             //Save Note to Student objSt

                        //Add new Student to Array

                        objSTImp.add(objSt);

                    }

                    break;

                }

                case 2:

                {

                    System.out.println("---------- All student have age > 20 ----------");

                    objSTImp.display(objSTImp.getList(20));

                    break;

                }

                case 3:

                {

                    System.out.println("---------- ArrayList after sort ----------");

                    objSTImp.SortAndDisplay();

                    break;

                }

                case 4:

                {

                    System.out.println("---------- Delete student from sCode ----------");

                    System.out.println("Please enter student code: ");

                    sCode = bf.nextInt();

                    bf.nextLine();

                    objSTImp.delete(sCode);

                    break;

                }

                case 5:

                {

                    //Save all remain students ArrayList to text file

                    System.out.println("---------- Save all remain students ArrayList to text file ----------");

                    System.out.println("Please enter file name : ");

                    fileName = bf.nextLine();

                    objSTImp.save("D:/"+fileName);

                    break;

                }

                case 6:

                {

                    System.out.println("---------- Read all students from text file ----------");

                    objSTImp.read("D:/"+fileName);

                    break;

                }

                case 7:

                {

                    System.out.println("---------- Search Student by student code ----------");

                    System.out.println("Please enter student code: ");

                    sCode = bf.nextInt();

                    bf.nextLine();

                    objSTImp.search(sCode);

                    break;

                }

                case 8:

                {

                    x = 1;

                    break;

                }

                default:

                {

                    System.out.println(" The Number not found ");

                    break;

                }

            }

        }while(x == 0);

    }

}

Bạn đang đọc truyện trên: Truyen247.Pro

Tags: