三行代码  ›  专栏  ›  技术社区  ›  Mrblue

为什么checkID()函数在这里使用的是bufferReader而不是bufferWriter?

  •  0
  • Mrblue  · 技术社区  · 3 月前

    这是一个航班预订系统,所以这是旅客的等级。在这个类中,如果用户还没有帐户,我们有注册的功能。然而,在本例中,它使用缓冲读取器检查仍在注册的旅行者的id。难道不应该使用缓冲区写入程序将旅行者的ID写入CSV文件吗?请纠正我并详细解释。谢谢

    public  void signUp(String filepath) throws IOException {
        int check = 0;
        String path = filepath;
        FileWriter fw = new FileWriter(path, true);
        BufferedWriter br = new BufferedWriter(fw);
        PrintWriter pw = new PrintWriter(br);
        
        Scanner i = new Scanner(System.in);
        
        System.out.println("Enter your name");
        String name = i.next();
        
        System.out.println("Enter your password");
        String password = i.next();
    
        int travellerID = (int)(Math.random()*90000 + 100000);
    
        check = checkID(travellerID);
        while(check==1) {
            int temp = (int)(Math.random()*90000 + 100000);
            travellerID = temp;
            check = checkID(travellerID);
        }
        
        pw.println(name+","+password+","+travellerID);
    
        pw.println(name+","+password);
        pw.flush();
        pw.close();
    
        System.out.println("Congratulations, you have signed up as a passenger!");
        System.out.printf("Your ID is %d \n", travellerID);
        
    }
    public int checkID(int ID) throws IOException {
        String passengerID = String.valueOf(ID);
        int check = 0;
        BufferedReader br = new BufferedReader(new FileReader("publishedFlights.csv"));
        String line;
        while((line=br.readLine())!=null) {
            String[] values = line.split(",");
            if(values[2].contentEquals(passengerID)) {
                check = 1;
            }
        }
        return check;
    }
    
    0 回复  |  直到 3 月前