java买飞机票打折(调用方法实现)。机票价格按照旺季(5~10月)头等舱9折,经济舱8.5折,淡季(11到4月)头等舱7折,经济舱6.5折。输入机票价格和月份以及对应的舱位输出价格。

package Test;
import java.util.Scanner;
public class Test14 {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入票价:");
        int ticket=sc.nextInt();
        System.out.println("请输入月份:");
        int month=sc.nextInt();
        System.out.println("请输入头等舱(0)或者经济舱(1)");
        int seat=sc.nextInt();
        if(month>=1&&month<=4||month>=11&&month<=12){
            ticket=money(ticket,0.7,0.65,seat);
        }
        else if (month>=5&&month<=10){
            ticket=money(ticket,0.9,0.85,seat);
        }
        else{
                System.out.println("月份错误,请重新输入:");
            }
        System.out.println(ticket);
        }
    public static int money(int ticket,double t,double j,int seat){
        if(seat==0){
           ticket=(int)(ticket*t);
        }
        else if(seat==1){
            ticket=(int)(ticket*j);
        }
        else{
        System.out.println("舱位错误,请重新输入:");
        }
        return ticket;
    }
}

尝试运行结果如下:

 

Java中,我们可以创建一个`AirfareCalculator`类,其中包含计算机票价格方法。下面是一个简单实现的例子: ```java import java.util.Date; public class AirfareCalculator { public static class ClassLevel { private String name; private double discountDuringSeason; // 构造函数其他辅助方法... public ClassLevel(String name, double seasonDiscount) { this.name = name; this.discountDuringSeason = seasonDiscount; } public double calculatePrice(double originalPrice, boolean isSeason) { double adjustedPrice = originalPrice * (1 - discountDuringSeason); if (isSeason) { System.out.println(name + " 价格旺季享受扣"); } else { System.out.println(name + " 价格淡季享受扣"); } return adjustedPrice; } } public static double computeTicketPrice(double originalPrice, Date month, String classLevel) { if (classLevel.equals("头等舱")) { ClassLevel firstClass = new ClassLevel("头等舱", 0.9); // 季节扣 ClassLevel economyClass = new ClassLevel("经济舱", 0.85); // 季节扣 // 判断月份是否在旺季 int monthOfYear = month.getMonth(); boolean isSeason = monthOfYear >= 5 && monthOfYear <= 10; if (isSeason) { if ("头等舱".equals(classLevel)) { return firstClass.calculatePrice(originalPrice, true); } else { return economyClass.calculatePrice(originalPrice, true); } } else { if ("头等舱".equals(classLevel)) { return firstClass.calculatePrice(originalPrice, false); } else { return economyClass.calculatePrice(originalPrice, false); } } } else { // 类似于经济舱的处理... } } // 主程序入口... public static void main(String[] args) { double originalPrice = 1000.00; Date currentMonth = new Date(); // 用于获取当前月份 String classLevel = "头等舱"; // 或者"经济舱" double totalPrice = computeTicketPrice(originalPrice, currentMonth, classLevel); System.out.printf("调整后的 %s 票价: %.2f元\n", classLevel, totalPrice); } } ``` 这个代码片段首先定义了两个类别(头等舱经济舱),每个类别都有自己的季节扣率。然后在`computeTicketPrice`函数中,根据输入月份、原始价格舱位级别计算最终价格,并根据季节扣条件调用相应的类别的价格计算方法
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

香蕉不拉了

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值