我有一个大学的项目要完成,它基本上是从 Java 中的 CSV 文件中返回一堆东西。
我已经设法从以前的尝试中完成了大部分工作,但我坚持的一个部分是如何读取 CSV 文件,但只有文件的特定部分:预订时间最多的房间并显示它在终端中运行 jmh 并创建一个新的 jar 文件。我的第一次尝试是硬编码,虽然它有效,但这不是讲师想要的。任何帮助或方向指针都会是很棒的代码,下面是讲师不想要的方式。
此外,CSV 文件的格式为 A) id B) 房间名称 C) 日期 D) 时间 E) 预订时间 F) 人预订
公共字符串 [] getTopRoomsBooked(int n) {
HashMap<String, Integer> rooms = new HashMap<String, Integer>();
rooms.put("Gower", 281);
rooms.put("Usk", 291);
rooms.put("Wye", 283);
rooms.put("Bala", 282);
rooms.put("Pen y Fan", 292);
rooms.put("Llangorse", 290);
rooms.put("Snowdon", 288);
rooms.put("Taff", 296);
rooms.put("Cadair Idris", 292);
for (String i : rooms.keySet()) {
System.out.println("Room name: " + i + " Amount of time: " + rooms.get(i));
}
System.out.println();
List<Integer> timeBooked = new ArrayList<>(rooms.values());
Collections.sort(timeBooked, Collections.reverseOrder());
if (n == 1) {
System.out.println("RoomName : Taff " + timeBooked.get(n - 1));
} else if (n == 3) {
System.out.println("1: Taff " + timeBooked.get(n - 3));
System.out.println("2: Cadiar Idris " + timeBooked.get(n - 2));
System.out.println("3: Pen y Fan " + timeBooked.get(n - 1));
} else if (n == 5) {
System.out.println("1: Taff " + timeBooked.get(n - 5));
System.out.println("2: Cadair Idris " + timeBooked.get(n - 4));
System.out.println("3: Pen y Fan " + timeBooked.get(n - 3));
System.out.println("4: Usk " + timeBooked.get(n - 2));
System.out.println("5: Llangorse " + timeBooked.get(n - 1));
} else if (n == 9) {
System.out.println("1: Taff " + timeBooked.get(n - 9));
System.out.println("2: Cadair Idris " + timeBooked.get(n - 8));
System.out.println("3: Pen y Fan " + timeBooked.get(n - 7));
System.out.println("4: Usk " + timeBooked.get(n - 6));
System.out.println("5: Llangorse " + timeBooked.get(n - 5));
System.out.println("6: Snowden " + timeBooked.get(n - 4));
System.out.println("7: Wye " + timeBooked.get(n - 3));
System.out.println("8: Bala " + timeBooked.get(n - 2));
System.out.println("9: Gower " + timeBooked.get(n - 1));
}
return null;
}