---
# System prepended metadata

title: Untitled

---

exam
```
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Scanner;

public class TxtFileReader {
    private static final String FILE_PATH = "favorite.txt"; // 要存入敘述的txt文件路徑

    public static void main(String[] args) {
        try {
            Scanner scanner = new Scanner(System.in);
            while (true) {
                System.out.print("請輸入網址（或輸入 'done' 結束）: ");
                String url = scanner.nextLine();
                if (url.equalsIgnoreCase("done")) {
                    break;
                }

                System.out.print("請輸入網址敘述: ");
                String description = scanner.nextLine();

                saveUrlDescriptionToFile(url, description, FILE_PATH);
            }

            printFileContents(FILE_PATH);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void saveUrlDescriptionToFile(String url, String description, String filePath) throws IOException {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath, true))) {
            writer.write(url);
            writer.write(description);
            writer.newLine();
        }
    }

    public static void printFileContents(String filePath) throws IOException {
        Files.lines(Paths.get(filePath)).forEach(System.out::println);
    }
}
```

```

import java.util.Random;

class Vehicle implements Runnable {
    private String name;
    private int simulationCount;
    private static final double BASE_FEE = 1.2;
    private static final double DISCOUNT_RATE = 0.75;
    private static final double FINAL_DISCOUNT_RATE = 0.9;
    private static final int BASE_DISTANCE = 20;
    private static final int DISCOUNT_DISTANCE = 200;

    public Vehicle(String name, int simulationCount) {
        this.name = name;
        this.simulationCount = simulationCount;
    }

    @Override
    public void run() {
        double totalFee = 0;
        Random random = new Random();

        for (int i = 0; i < simulationCount; i++) {
            int distance = random.nextInt(351);
            double fee;

            if (distance <= BASE_DISTANCE) {
                fee = 0;
            } else if (distance <= DISCOUNT_DISTANCE) {
                fee = (distance - BASE_DISTANCE) * BASE_FEE;
            } else {
                fee = DISCOUNT_DISTANCE * BASE_FEE + (distance - DISCOUNT_DISTANCE) * BASE_FEE * DISCOUNT_RATE;
            }

            totalFee += fee;
            System.out.println(name + " 第" + (i + 1) + "次模擬，里程：" + distance + "公里，費用：" + fee + "元");

            try {
                Thread.sleep(random.nextInt(3000) + 1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        System.out.println(name + " 總費用：" + (totalFee * FINAL_DISCOUNT_RATE) + "元");
    }
}

public class EtagBillingProgram {
    public static void main(String[] args) {
        String[] vehicleNames = {"車子A", "車子B", "車子C", "車子D", "車子E"};
        int simulationCount = 20;

        Thread[] threads = new Thread[vehicleNames.length];

        for (int i = 0; i < vehicleNames.length; i++) {
            Vehicle vehicle = new Vehicle(vehicleNames[i], simulationCount);
            threads[i] = new Thread(vehicle);
            threads[i].start();
        }

        for (Thread thread : threads) {
            try {
                thread.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        double totalFees = 0;

        for (Thread thread : threads) {
            Vehicle vehicle = (Vehicle) thread.getRunnable();
            totalFees += vehicle.getTotalFee();
        }

        System.out.println("所有車子總費用：" + totalFees + "元");
    }
}
```