Java小型综合型练习 您所在的位置:网站首页 宝马m550i参数 Java小型综合型练习

Java小型综合型练习

2024-07-03 17:32| 来源: 网络整理| 查看: 265

需求说明:某汽车租赁公司出租多种轿车和客车,出租费用以日为单位计算。 出租车型及其信息如下: 在这里插入图片描述 汽车类(抽象类) 属性: (1)品牌(brand) (2)日租金(dayRent) (3)车牌号(vehicleNum)

方法:抽象类方法(计算总租金):calRent(int days)

代码如下:

public abstract class Vehicle { //汽车类的属性(由于轿车、客车要继承汽车这个抽象,故属性要设置为public,默认为public static final) //汽车品牌 String brand; //车牌号 String vehicleNum; //日租金 int dayRent; //汽车类的带参构造 public Vehicle(String brand, String vehicleNum, int dayRent) { this.brand = brand; this.vehicleNum = vehicleNum; this.dayRent = dayRent; } //计算总租金的方法 public abstract double calRent(int days); }

轿车类(继承汽车类) 属性(轿车特有的):型号(carType) 宝马 ->x6/550i 别克->林荫达到/GL8

方法:实现汽车类的计算总租金的方法 代码如下:

public class Cars extends Vehicle{ //轿车类(特有属性:型号) String carsType; //轿车的有参构造方法 public Cars(String brand, String vehicleNum, int dayRent,String carsType) { super(brand, vehicleNum, dayRent); this.carsType=carsType; } @Override public double calRent(int days) { double rent=dayRent*days; if (days>150){ rent*=0.7; }else if (days>30){ rent*=0.8; }else if (days>7){ rent*=0.9; } return rent; } }

客车类(继承汽车类) 属性(客车特有的):座位(coachSeat) 金杯->16座/34座 金龙->16座/34座

方法:实现汽车类的计算总租金的方法

代码如下:

public class Coach extends Vehicle{ //客车(特有属性:座位数) String coachSeat; //客车的有参构造 public Coach(String brand, String vehicleNum, int dayRent,String coachSeat) { super(brand, vehicleNum, dayRent); this.coachSeat=coachSeat; } @Override public double calRent(int days) { double rent= dayRent*days; if (days>=150){ rent*=0.6; }else if (days>=30){ rent*=0.7; }else if (days>=7){ rent*=0.8; }else if (days>=3){ rent*=0.9; } return rent; } }

租车业务类(实现具体车型的租金计算)

代码如下:

public class VehicleBusiness { Scanner input =new Scanner(System.in); //从程序入口获得具体租用车型 public Vehicle getVehicle(String vehicleType){ Vehicle vehicle=null; //初始化Vehicle属性 //品牌 String brand=""; //车牌号 String vehicleNum=""; //日租金 int dayRent=0; //根据传入的车型,分别进行计算租金 if (vehicleType.equals("轿车")){ //车型选择:轿车,接下来让他选择车的品牌 System.out.println("请选择车的品牌:"+"1、宝马"+"\t"+"2、别克"); int choce=input.nextInt(); String type=""; if (choce==1){ brand="宝马"; //选择相应品牌车型,车的型号 System.out.println("请选择宝马的型号:"+"1、X6"+"\t"+"2、550i"); int choce1= input.nextInt(); if (choce1==1){ type="X6"; dayRent=800; } else if (choce1==2) { type="550i"; dayRent=600; } }else if (choce==2){ brand="别克"; //选择相应品牌车型,车的型号 System.out.println("请选择别克的型号:"+"1、林荫大道"+"\t"+"2、GL8"); int choce2=input.nextInt(); if (choce2==1){ type="林荫大道"; dayRent=300; } else if (choce2==2) { type="GL8"; dayRent=600; } }else { System.out.println("不好意思,客户你输入错误!"); } for (int i = 0; i


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有