By GokiSoft.com| 14:47 09/11/2022|
Java Advanced

[Source Code] Bài tập quản lý bán vé máy bay - java nâng cao - C2109I

#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.lesson06;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author diepvan
 */
public class Main {
    static List<VeMayBay> veMayBays = new ArrayList<>();
    static Scanner scan = new Scanner(System.in);
    
    public static void main(String[] args) {
        //1)   Nhập vào thông tin cho n chuyến bay với mã chuyến bay theo quy ước ở trên
//        input();
        
        //2. Lưu thông tin vừa nhập vào file : DuLieuBay.txt
//        saveFile();
        
        //3. Đọc thông tin  từ DuLieuBay.txt và hiển thị tất cả thông tin ra màn hình
        readFile();
        
        //4. Hien thi
        display();
        
        splitFile();
    }
    
    static void splitFile() {
        for (VeMayBay veMayBay : veMayBays) {
            appendFile(veMayBay.getFileName(), veMayBay);
        }
        System.out.println("Luu hoan thanh!!!");
    }
    
    static void display() {
        Collections.sort(veMayBays, (o1, o2) -> {
            if(o1.getPrice() < o2.getPrice()) {
                return 1;
            }
            return -1;
        });
        
        for (VeMayBay veMayBay : veMayBays) {
            veMayBay.display();
        }
    }
    
    static void readFile() {
        FileReader reader = null;
        BufferedReader bufferedReader = null;
        
        try {
            reader = new FileReader("vmb.txt");
            bufferedReader = new BufferedReader(reader);
        
            String line;
            while((line = bufferedReader.readLine()) != null) {
                if(line.trim().isEmpty()) continue;
                
                VeMayBay vmb = new VeMayBay();
                vmb.parseLine(line);
                
                veMayBays.add(vmb);
            }
        } 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(bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException ex) {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            if(reader != null) {
                try {
                    reader.close();
                } catch (IOException ex) {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
    
    static void appendFile(String filename, VeMayBay vmb) {
        FileOutputStream fos = null;
        
        try {
            fos = new FileOutputStream(vmb.getFileName(), true);
            
            byte[] data = vmb.toString().getBytes("utf8");
                
            fos.write(data);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedEncodingException 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);
                }
            }
        }
    }
    
    static void saveFile() {
        FileOutputStream fos = null;
        
        try {
            fos = new FileOutputStream("vmb.txt");
            
            for (VeMayBay veMayBay : veMayBays) {
                byte[] data = veMayBay.toString().getBytes("utf8");
                
                fos.write(data);
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedEncodingException 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);
                }
            }
        }
    }
    
    static void input() {
        System.out.println("Nhap so ve can them: ");
        int n = Integer.parseInt(scan.nextLine());
        
        for (int i = 0; i < n; i++) {
            VeMayBay veMayBay = new VeMayBay();
            veMayBay.input();
            
            veMayBays.add(veMayBay);
        }
    }
}

#VeMayBay.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.lesson06;

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author diepvan
 */
public class VeMayBay {
    String code;
    String name;
    String departure;
    String luggage;
    float price;

    public VeMayBay() {
    }

    public String getCode() {
        return code;
    }

    public boolean setCode(String code) {
        //Check VietjetAir => VJABB
        Pattern pattern = Pattern.compile("VJ[1-9]{1}[0-9]{2}$", Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(code);
        boolean matchFound = matcher.find();
        
        if(matchFound) {
            this.code = code;
            return true;
        }
        
        //VNABBC -> Vietnam airline
        pattern = Pattern.compile("VN[1-9]{1}[0-9]{2}[0-9]?$", Pattern.CASE_INSENSITIVE);
        matcher = pattern.matcher(code);
        matchFound = matcher.find();

        if(matchFound) {
            this.code = code;
            return true;
        }
        
        //JETABB -> Jet Start
        pattern = Pattern.compile("JET[1-9]{1}[0-9]{2}$", Pattern.CASE_INSENSITIVE);
        matcher = pattern.matcher(code);
        matchFound = matcher.find();

        if(matchFound) {
            this.code = code;
            return true;
        }
        return false;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDeparture() {
        return departure;
    }

    public void setDeparture(String departure) {
        this.departure = departure;
    }

    public String getLuggage() {
        return luggage;
    }

    public void setLuggage(String luggage) {
        this.luggage = luggage;
    }

    public float getPrice() {
        return price;
    }

    public void setPrice(float price) {
        this.price = price;
    }

    public void input() {
        Scanner scan = new Scanner(System.in);
        
        System.out.println("Nhap ma ve may bay: ");
        String code2 = scan.nextLine();
        
        while(!setCode(code2)) {
            System.out.println("Nhap lai: ");
            code2 = scan.nextLine();
        }
        
        System.out.println("Nhap ten ve may bay: ");
        name = scan.nextLine();
        System.out.println("Nhap ngay khoi hanh: ");
        departure = scan.nextLine();
        System.out.println("Nhap hanh ly: ");
        luggage = scan.nextLine();
        System.out.println("Nhap gia: ");
        price = Float.parseFloat(scan.nextLine());
    }
    
    public void display() {
        System.out.println(this);
    }

    @Override
    public String toString() {
        return "code=" + code + ", name=" + name + ", departure=" + departure + ", luggage=" + luggage + ", price=" + price + "\n";
    }
    
    
    public void parseLine(String line) {
        String[] eles = line.trim().split(",");
        
        //code
        String[] params = eles[0].split("=");
        code = params[1].trim();
        
        params = eles[1].split("=");
        name = params[1].trim();
        
        params = eles[2].split("=");
        departure = params[1].trim();
        
        params = eles[3].split("=");
        luggage = params[1].trim();
        
        params = eles[4].split("=");
        price = Float.parseFloat(params[1].trim());
    }
    
    public String getFileName() {
        if(code.startsWith("VJ")) return "vj.txt";
        if(code.startsWith("VN")) return "vn.txt";
        if(code.startsWith("JET")) return "jet.txt";
        
        return "other.txt";
    }
}
Tags:



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

5

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

Đăng nhập để làm bài kiểm tra

Chưa có kết quả nào trước đó