吉林大学Java上机实验(Lab6.xls)

一. 简答题(共1题,100分)

1. (简答题, 100分)

A car rental company rents out a variety of cars and coaches and its rental rates are calculated on a daily basis. The relevant information of vehicle is shown in the following form. You need to develop a vehicle rental system according to the following requirements.

(1)   Design a simple console interactive interface to provide users with the related information about this vehicle rental system including vehicle models, specific information, daily rent and discount information so that the user can make further choices.

(2)   After understanding the relevant information, the user can input the relevant information of the vehicle which he or she wants to rent through the console program.

(3)   The program calculates the rent that the user needs to pay and informs the user through the console program.

(4)   You need to deal with the illegal information input by users through the console with some rules and prompt the corresponding error reason. For example, the user cannot input a negative value for the number of parking days. You need to consider as many misinput cases as possible.

 

Please submit program code and the screenshot of the program output in the answer.

汽车租赁公司出租各种汽车和长途汽车,其租金每天计算。车辆的相关信息如下表所示。您需要根据以下要求开发车辆租赁系统。

(1) 设计一个简单的控制台交互界面,为用户提供有关该租车系统的相关信息,包括车型、具体信息、日租金和折扣信息,以便用户做出进一步的选择。

(2) 在了解相关信息后,用户可以通过控制台程序输入他或她想要租赁的车辆的相关信息。

(3) 该程序计算用户需要支付的租金,并通过控制台程序通知用户。

(4) 你需要用一些规则来处理用户通过控制台输入的非法信息,并提示相应的错误原因。例如,用户不能输入停车天数的负值。你需要考虑尽可能多的错误情况。

请在答案中提交程序代码和程序输出的屏幕截图。

 

import java.text.NumberFormat;
import java.util.Scanner;

public class CarRentalSystem {
   private static final double CAR_DISCOUNT_RATE_7 = 0.9;
   private static final double CAR_DISCOUNT_RATE_30 = 0.8;
   private static final double CAR_DISCOUNT_RATE_150 = 0.7;

   private static final double BUS_DISCOUNT_RATE_3 = 0.9;
   private static final double BUS_DISCOUNT_RATE_7 = 0.8;
   private static final double BUS_DISCOUNT_RATE_30 = 0.7;
   private static final double BUS_DISCOUNT_RATE_150 = 0.6;

   private static final String[] CAR_MODELS = {
           "宝马X6 (京NY28588)",
           "宝马550i (京CNY3284)",
           "别克林荫大道 (京NT37465)",
           "别克GL8 (京NT96968)"
   };

   private static final String[] BUS_MODELS = {
           "金杯 16座 (京6566754)",
           "金龙 16座 (京8696997)",
           "金杯 34座 (京9696996)",
           "金龙 34座 (京8696998)"
   };

   private static final double[] CAR_PRICES = {800, 600, 300, 600};
   private static final double[] BUS_PRICES = {800, 800, 1500, 1500};

   public static void main(String[] args) {
       Scanner input = new Scanner(System.in);

       // 显示车辆信息
       showVehicleInfo();
       System.out.println();

       // 输入租赁信息
       boolean validInput = false;
       double price = 0;
       int days = 0;
       while (!validInput) {
           try {
               System.out.print("请输入要租用的车型编号:");
               int modelIndex = Integer.parseInt(input.nextLine()) - 1;

               String[] models = isCarModel(modelIndex) ? CAR_MODELS : BUS_MODELS;
               double[] prices = isCarModel(modelIndex) ? CAR_PRICES : BUS_PRICES;
               double discountRate = isCarModel(modelIndex) ?
                       getDiscountRateForCar(getRentDays(input)) :
                       getDiscountRateForBus(getRentDays(input));

               System.out.println();
               System.out.println("您要租用的车型是:" + models[modelIndex]);
               System.out.println("日租金是:" + formatCurrency(prices[modelIndex]));
               System.out.println(String.format("折扣率是:%.1f%%", discountRate * 100));
               days = getRentDays(input);
               price = calculateRentalPrice(prices[modelIndex], discountRate, days);
               validInput = true;
           } catch (NumberFormatException e) {
               System.err.println("输入格式有误,请重新输入!");
           } catch (IllegalArgumentException e) {
               System.err.println(e.getMessage());
           }
       }

       // 显示租赁信息和金额
       System.out.println();
       System.out.println("您的租赁信息如下:");
       System.out.println(String.format("租用天数:%d 天", days));
       System.out.println("应付金额:" + formatCurrency(price));
   }

   private static void showVehicleInfo() {
       System.out.println("欢迎使用车辆租赁系统!");
       System.out.println("本系统提供以下车型的租赁服务:");
       System.out.println("轿车(编号从1开始):");
       System.out.println("+------+-----------------------+--------+");
       System.out.println("| 编号 |          汽车           | 租金/日 |");
       System.out.println("+------+-----------------------+--------+");
       for (int i = 0; i < CAR_MODELS.length; i++) {
           System.out.println(String.format("|%4d  | %s | %6s |",
                   i + 1, CAR_MODELS[i], formatCurrency(CAR_PRICES[i])));
       }
       System.out.println("+------+-----------------------+--------+");

       System.out.println("客车(编号从" + (CAR_MODELS.length + 1) + "开始):");
       System.out.println("+------+-----------------------+--------+");
       System.out.println("| 编号 |           车型          | 租金/日 |");
       System.out.println("+------+-----------------------+--------+");
       for (int i = 0; i < BUS_MODELS.length; i++) {
           int index = i + CAR_MODELS.length + 1;
           System.out.println(String.format("|%4d  | %s | %6s |",
                   index, BUS_MODELS[i], formatCurrency(BUS_PRICES[i])));
       }
       System.out.println("+------+-----------------------+--------+");
   }

   private static double calculateRentalPrice(double pricePerDay, double discountRate, int days) {
       return pricePerDay * discountRate * days;
   }

   private static int getRentDays(Scanner input) throws IllegalArgumentException {
       boolean validInput = false;
       int days = 0;
       while (!validInput) {
           try {
               System.out.print("请输入租用天数:");
               days = Integer.parseInt(input.nextLine());
               if (days < 0) {
                   throw new IllegalArgumentException("租用天数不能为负数!");
               }
               validInput = true;
           } catch (NumberFormatException e) {
               System.err.println("输入格式有误,请重新输入!");
           } catch (IllegalArgumentException e) {
               System.err.println(e.getMessage());
           }
       }
       return days;
   }

   private static double getDiscountRateForCar(int days) {
       if (days >= 150) {
           return CAR_DISCOUNT_RATE_150;
       } else if (days >= 30) {
           return CAR_DISCOUNT_RATE_30;
       } else if (days >= 7) {
           return CAR_DISCOUNT_RATE_7;
       } else {
           return 1.0;
       }
   }

   private static double getDiscountRateForBus(int days) {
       if (days >= 150) {
           return BUS_DISCOUNT_RATE_150;
       } else if (days >= 30)
       {
           return BUS_DISCOUNT_RATE_30;
       } else if (days >= 7) {
           return BUS_DISCOUNT_RATE_7;
       } else if (days >= 3) {
           return BUS_DISCOUNT_RATE_3;
       } else {
           return 1.0;
       }
   }

   private static boolean isCarModel(int modelIndex) {
       return modelIndex < CAR_MODELS.length;
   }
   private static String formatCurrency(double amount) {
       NumberFormat nf = NumberFormat.getCurrencyInstance();
       return nf.format(amount);
   }

}

 

正确答案:

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Code Slacker

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

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

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

打赏作者

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

抵扣说明:

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

余额充值