【问题标题】:Calculate Rate per hour based on Time根据时间计算每小时费率
【发布时间】:2018-01-04 08:18:54
【问题描述】:
import java.text.SimpleDateFormat;
import java.util.Date;

public class Rate_Per_Hour {

    public static void main(String[] args) {

        String TimeStart = "09.30.00 am";
        String TimeEnd= "10.10.00 am";

        SimpleDateFormat format = new SimpleDateFormat("hh.mm.ss a");
        int total=0;
        Date d1 = null;
        Date d2 = null;

        try {
            d1 = format.parse(TimeStart);
            d2 = format.parse(TimeEnd);

            long diff = d2.getTime() - d1.getTime();
            long diffHours = diff / (60 * 60 * 1000) % 24;
            long diffMinutes = diff / (60 * 1000) % 60;

            if (diffMinutes <= 30) {
               total = 20;
            } 
            else if (diffHours <=1){
               total = 35;
            } 

            System.out.println("Rs." +total);
        }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

我想要这样的输出: (每小时固定费率为 35 卢比)

30 分钟 = 20 卢比

40 分钟 = Rs.25 等等......

1 小时 = 35 卢比

1 小时 10 分钟 = 40 卢比

请帮助我,弄清楚我该怎么做。

【问题讨论】:

  • 你得到了什么输出?

标签: java jsp


【解决方案1】:

由于速率每 10 分钟增加 5,所以只需使用一个简单的函数来返回速率:

public double rate(int minutes) 
{ 
return 20 + 5*((minutes - 30)/10);
}

计算分钟数,然后将其作为参数传递给该函数以获取速率。此外,尽量让您的代码尽可能简短,但同时要简单。

【讨论】:

  • 或者只是5 + minutes / 2
  • 是的,几乎一样。
  • 谢谢大家,但在 1 小时 30 分钟后,将给出 50 卢比而不是 55 卢比
  • 每 10 分钟速率增加 5,因此 1 小时 30 分钟后将是 50 而不是 55。
【解决方案2】:

如果您需要更灵活的而不是固定费率,您可以使用带有预定义止损的enum

您可以为给定时间定义任何您想要的费率。 例如,如果长期运行的任务应该获得更高的比率(从你的问题来看,如果这个比率只是 Manish Kundu 的回答中的计算值,或者是否可能分配其他值,我并不明显。)

使用此代码,您可以为长时间运行的jobs 分配更高的费率(例如,在电脑游戏中,更难实现的工作会返回更高的费率或降低费率,因为玩家花费了太多时间...... )

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;

public class TimeMain {
private static final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("hh.mm.ss[ ]a");

public enum Rate {
    STOP0(0, 0, 0), // default
    STOP1(0, 30, 20), // 30 mins -> Rs.20
    STOP2(0, 40, 25), // 40 mins -> Rs.25
    STOP3(1, 0, 35), // 1 hour -> Rs.35
    STOP4(1, 10, 40); // 1 hour 10 minutes -> Rs.40

    int minutes = 0;
    int rate = 0;

    /*
     * hours is actually not needed as 1h10 = 70mins...
     */
    private Rate(int hours, int minutes, int rate) {
        this.minutes = minutes + hours * 60;
        this.rate = rate;
    }

    public static Rate from(String timeStart, String timeEnd) {
        LocalTime time1 = LocalTime.parse(timeStart.toUpperCase(), dateTimeFormatter);
        LocalTime time2 = LocalTime.parse(timeEnd.toUpperCase(), dateTimeFormatter);
        long minutesBetween = ChronoUnit.MINUTES.between(time1, time2);
        for (int i = 0; i < Rate.values().length; i++) {
            Rate r = Rate.values()[i];
            if (r.minutes > minutesBetween) {
                return Rate.values()[i-1];
            }
        }
        return STOP0;
    }

    public String toString() {
        return String.format("Rs.%s", rate);
    }
}

public static void main(String... args) {
    String timeStart = "09.30.00 am";
    String timeEnd = "10.10.00 am";

    System.out.println(Rate.from(timeStart, timeEnd));
}

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-12
    • 2016-04-07
    • 2022-09-24
    • 1970-01-01
    • 2014-12-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多