Java小型综合型练习 您所在的位置:网站首页 车辆Gl8 Java小型综合型练习

Java小型综合型练习

2023-09-10 05:33| 来源: 网络整理| 查看: 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 vehicleNum=vehicleInfos[3]; } } //创建轿车类对象 vehicle = new Cars(brand, vehicleNum,dayRent, type); }else if (vehicleType.equals("客车")){ //车型选择:客车,接下来让他选择车的品牌 System.out.println("请选择车的品牌:"+"1、金杯"+"\t"+"2、金龙"); String coachNum=""; int choce=input.nextInt(); if (choce==1){ brand="金杯"; //选择相应品牌车型,车的座位数 System.out.println("请选择金杯的座位数:"+"1、16座"+"\t"+"2、34座"); int choce1= input.nextInt(); if (choce1==1){ coachNum="16座"; dayRent=800; }else if (choce1==2){ coachNum="34座"; dayRent=1500; } }else if (choce==2){ brand="金龙"; //选择相应品牌车型,车的座位数 System.out.println("请选择金龙的座位数:"+"1、16座"+"\t"+"2、34座"); int choce2= input.nextInt(); if (choce2==1){ coachNum="16座"; dayRent=800; }else if (choce2==2){ coachNum="34座"; dayRent=1500; } }else { System.out.println("不好意思,客户你输入错误!"); } for (int i = 0; i vehicleNum=vehicleInfos[3]; } } //创建客车类对象 vehicle = new Coach(brand, vehicleNum,dayRent, coachNum); } return vehicle; } }

租车管理类(程序的入口) 作用:用来显示用户界面以及信息输出 代码如下:

public class VehicleMag { public static void main(String[] args) { VehicleBusiness vehicleBusiness =new VehicleBusiness(); Scanner input= new Scanner(System.in); String vehicleType=""; System.out.println("************欢迎光临小胡汽车租赁公司***************"); System.out.println("您选择车型:"+"1、轿车"+"\t"+"2、客车"); int choce= input.nextInt(); if (choce==1){ vehicleType="轿车"; }else if(choce==2){ vehicleType="客车"; }else{ System.out.println("不好意思,您输入的选项,本公司无此产品!"); } //将用户选择的车型,传给汽车管理类 Vehicle vehicle=vehicleBusiness.getVehicle(vehicleType); //选择输入的天数 System.out.println("请输入您需要租赁的天数:"); int days = input.nextInt(); double totalMoney = vehicle.calRent(days); //分配给的车牌号 System.out.println("分配给您的汽车牌号:" +vehicle.vehicleNum); //总的费用 System.out.println("您需要支付的租赁费用:" + totalMoney); } }

汽车的工具类(用于存放汽车的信息)

public class VehicleUtils { //轿车(有型号,而没有座位数这一特殊的属性,故将其用空串占位) 客车同理(有座位数,而无型号) public static final String[][] vehicleInfos = { {"宝马","X6","","京NY28588"}, {"宝马","550i","","京CNY3284"}, {"别克","林荫大道","","京NT37465"}, {"别克","GL8","","京NT96968"}, {"金杯","","16座","京6566754"}, {"金龙","","16座","京8696997"}, {"金杯","","34座","京9696996"}, {"金龙","","34座","京8696998"} }; }

显示效果 在这里插入图片描述



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

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