计算机票打折

package day07;

import java.util.Scanner;

/**
 * 需求:
 *   机票价格按照季节(淡季、旺季)、舱位(头等舱、商务舱、经济舱)收费
 * 要求:
 *   输入机票原价、月份和舱位,实现不同的折扣
 *   --旺季(5月到10月)时,头等舱9折,商务舱85折,经济舱8折
 *   --淡季(11月到来年4月)时,头等舱7折,商务舱65折,经济舱6折
 */
public class CalAirPrice {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入机票原价:");
        double price = scan.nextDouble();
        System.out.println("请输入月份:");
        int month = scan.nextInt();
        System.out.println("请选择舱位: 1.头等舱 2.商务舱 3.经济舱");
        int type = scan.nextInt();

        double finalPrice = calFinalPrice(price,month,type);
        if(finalPrice!=-1){ //数据合法
            System.out.println("机票的最终价格为:"+finalPrice);
        }
    }

    /**
     * 根据原价、月份、舱位,计算飞机票的最终价格
     */
    public static double calFinalPrice(double price,int month,int type){
        double finalPrice = 0.0; //最终价格
        //只要数据输入错误,都统一返回-1
        if(price<0){
            System.out.println("机票原价输入错误");
            return -1;
        }
        if(month<1 || month>12){
            System.out.println("月份输入错误");
            return -1;
        }
        if(type<1 || type>3){
            System.out.println("舱位输入错误");
            return -1;
        }

        //程序能走到这,说明数据一定是合法的
        if(month>=5 && month<=10){ //旺季
            switch(type){ //根据舱位类型做不同折扣
                case 1:
                    finalPrice = price*0.9;
                    break;
                case 2:
                    finalPrice = price*0.85;
                    break;
                case 3:
                    finalPrice = price*0.8;
                    break;
            }
        }else{ //淡季
            switch(type){ //根据舱位类型做不同折扣
                case 1:
                    finalPrice = price*0.7;
                    break;
                case 2:
                    finalPrice = price*0.65;
                    break;
                case 3:
                    finalPrice = price*0.6;
                    break;
            }
        }
        return finalPrice;
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

zjx0519

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

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

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

打赏作者

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

抵扣说明:

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

余额充值