如何允许用户从java中的列表中检索值?

kqqjbcuj  于 2021-08-25  发布在  Java
关注(0)|答案(2)|浏览(289)

我在flightbookingsystem java类中创建了一个列表,如下所示:

public List<Flight> getFlights() {
    List<Flight> out = new ArrayList<>(flights.values());
    return Collections.unmodifiableList(out);
}

我从文本文件中导入的,如下所示:

1::LX2500::Birmingham::Munich::2020-11-25::

2::LX2500::Denmark::London::2021-07-01::

3::LY2380::London::France::2021-06-28::

这是一个基本的文本文件,保存着每一次航班的信息
以下是我希望调整的代码:

public Flight execute(FlightBookingSystem flightBookingSystem, int id)
        throws FlightBookingSystemException {
    List<Flight> flights = flightBookingSystem.getFlights();
    for (Flight Flight : flights) {
        if (Flight.getFlightNumber() == flightNumber) {
            System.out.println(Flight.getFlightNumber() + " flight(s)");
            return flights.get(id);
        }
        System.out.println(((Flight) flights).getFlightNumber() + " flight(s)");

    }
    return flights.get(id);
}

如何更改该代码,以便允许用户从文本文件中检索一条记录?

ubof19bj

ubof19bj1#

为什么不使用 HashMap ?
如果仍然需要另一个选项,可以逐行读取文本文件,并检查它是否正确 startsWith(...) 和检索此行的方法。
代码示例:

try (BufferedReader br = new BufferedReader(new FileReader(file)))
{
    String line;
    while ((line = br.readLine()) != null)
    {
       // Add here 'if' condition and parse your line
    }
}
mfpqipee

mfpqipee2#

你的问题有点让人困惑。你的头衔上写着:

How do you allow a user to retrieve values from a list in Java?

你文章的最后一行写道:

How do I change that code so that it allows the user to retrieve
one single record from the text file?

是从列表还是从文本文件?
如果它来自一个列表,因为您已经有了可用的机制,那么它可能类似于以下内容:

public String getFlightInfo(String flightNumber) {
    List<Flight> flights = FlightBookingSystem.getFlights();
    for (Flight flite : flights) {
        if(flite.getFlightNumber().equalsIgnoreCase(flightNumber)){
            return flite.toString();
        }
    }
    JOptionPane.showMessageDialog(null, "<html>Flight number <font color=red><b>" 
            + flightNumber + "</b></font> could not be found!</html>", "Flight Not "
            + "Found", JOptionPane.INFORMATION_MESSAGE);
    return null;
}

上面的代码假定您有一个重写的 toString() 方法应用于飞行类。如果没有,那么创建一个。
如果它实际上来自文件,那么它可能是这样的:

public String getFlightInfo(String flightNumber) {
    // 'Try With Resouces' to auto-close reader.
    try (BufferedReader reader = new BufferedReader(new FileReader("Flights.txt"))) {
        String fileLine = "";
        while ((fileLine = reader.readLine()) != null) {
            fileLine = fileLine.trim();
            // If by chance the file line read in is blank then skip it.
            if (fileLine.isEmpty()) {
                continue;
            }
            // First, remove the double colons at the end of line (if any). 
            if (fileLine.endsWith("::")) {
                fileLine = fileLine.substring(0, fileLine.lastIndexOf("::")).trim();
            }
            /* Split each read in file line based on a double colon delimiter. 
               The "\\s*" within the regex for the split method handles any 
               cases where the might be one or more whitespaces before or after 
               the double-colon delimiter.                */
            String[] lineParts = fileLine.split("\\s*\\:\\:\\s*");
            if(lineParts[1].equalsIgnoreCase(flightNumber)){
                // At this point you could just return the line, for example:
                // return fileLine;
                // or you can return a string with a little more structure:
                return new StringBuilder("Flight ID: ").append(lineParts[0])
                        .append(", Flight #: ").append(lineParts[1]).append(", From: ")
                        .append(lineParts[2]).append(", To: ").append(lineParts[3])
                        .append(", Date: ").append(lineParts[4]).toString();
            }
        }
    }
    catch (FileNotFoundException ex) {
        JOptionPane.showMessageDialog(null, ex.getMessage());
    }
    catch (IOException ex) {
        JOptionPane.showMessageDialog(null, ex.getMessage());
    }
    JOptionPane.showMessageDialog(null, "<html>Flight number <font color=red><b>" 
            + flightNumber + "</b></font> could not be found!</html>", "Flight Not "
            + "Found", JOptionPane.INFORMATION_MESSAGE);
    return null;
}

相关问题