改善我的应用程序,以提高可读性和可维护性

时间:2020-10-24 19:57:01

标签: java hashmap

我做了一个小的祷告时间应用程序,从一个小例子开始。我想每天计算两次经过的时间。

我有以下固定的数据时间和日期

今天说“ 2020-10-24” //“ 12:55”,“ 22:57”,“ 23:05”

明天说“ 2020-10-25” //“ 08:30”,“ 22:02”,“ 23:06”

我用制作了LinkedHashMap。

使用键在每个循环中重复键,然后解析日期。

通过列表中的“值”重复获取时间数据的方式。

我已经使用LocalTime将文本解析为时间。

我做了简单的经过时间逻辑。

有没有更好的方法来实现我的示例,您可以看到我在彼此之间使用了很多循环,这将导致难以维护或发现错误。

如果表包含全年数据,我应该使用2D数组,Jtable还是Sqllite。

public class elapsedTime {
public static void main(String[] args) throws ParseException {

    List<String> listToday = Arrays.asList("12:55", "22:57", "23:05");
    List<String> listTomorrow = Arrays.asList("08:30", "22:02", "23:06");

    LinkedHashMap<String, List<String>> map = new LinkedHashMap<>();
    map.put("2020-10-24", listToday);
    map.put("2020-10-25", listTomorrow);


    for (String key : map.keySet()) {
        LocalDate loopDate = LocalDate.parse(key);

        if (loopDate.isEqual(LocalDate.now())) {

            for (String str : map.get(key)) {
                List<String> loopLocal = new ArrayList<>();
                for (String s : str.split(" ")) {
                    loopLocal.add(s);
                    LocalTime listToTime = LocalTime.parse(loopLocal.get(0));
                    
                    while (true) {
                        if (listToTime.isAfter(LocalTime.now())) {

                            long period = Duration.between(LocalTime.now(), listToTime).toMillis();
                            System.out.print(millisFormat.timeFormat(period) + "\r");
                        } else {
                            break;
                        }
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException ex) {
                            System.out.println("Error! exit");
                        }
                    }

                }

            }
        }
    }
    System.out.println("See u Morgen");

}}

0 个答案:

没有答案