Java 机票价格计算器:根据淡旺季计算优惠价
以下是一个简单的 Java 程序,根据当前日期计算用户购买机票时的优惠价。程序中假设原价为 1000.0,您可以根据实际情况进行修改。根据设定,旺季(5-10 月)头等舱的优惠为九折(0.9),经济舱的优惠为 8.5 折(0.85),淡季(11 月到来年 4 月)头等舱的优惠为七折(0.7),经济舱的优惠为 6.5 折(0.65)。程序根据当前月份判断旺季还是淡季,并计算出相应的优惠价。
import java.time.LocalDate;
public class TicketPriceCalculator {
public static void main(String[] args) {
LocalDate currentDate = LocalDate.now();
double originalPrice = 1000.0; // 根据实际情况设置原价
double discount = calculateDiscount(currentDate);
double discountedPrice = originalPrice * discount;
System.out.println('用户当前机票的优惠价为: ' + discountedPrice);
}
private static double calculateDiscount(LocalDate currentDate) {
int currentMonth = currentDate.getMonthValue();
if (currentMonth >= 5 && currentMonth <= 10) { // 旺季
return 0.9; // 头等舱九折
} else { // 淡季
return 0.7; // 头等舱七折
}
}
}
希望这可以帮助到您!
原文地址: https://www.cveoy.top/t/topic/b10G 著作权归作者所有。请勿转载和采集!