zoukankan      html  css  js  c++  java
  • JAVA多态练习题_某汽车租赁公司出租多种车辆,车型及租金情况如下:

    某汽车租赁公司出租多种车辆,车型及租金情况如下:

     

    编写程序实现计算租赁价  MotoVehicle

    分析

     

    4、需求说明:

    输入提示需求,计算出对应租金

    5、需求说明:

    新购置了卡车,根据吨位,租金每吨每天50

    对系统进行扩展,计算汽车租赁的总租金

     

     1 package day13;
     2 //机动车类
     3 public abstract class MotoVehicle {
     4     //车牌号    
     5     String no=null;
     6     //品牌
     7     String brand=null;
     8     //颜色
     9     char color;
    10     //里程
    11     int mileage;
    12     //总金额
    13     int num;
    14     
    15     
    16     
    17     public abstract int CalcRent(int day);
    18     
    19 }
     1 package day13;
     2 //大车类
     3 public class Bus extends MotoVehicle{
     4     
     5     private int seatCount;
     6     
     7 
     8     
     9     public Bus(String no,int seatCount) {
    10         super();
    11         this.no=no;
    12         this.seatCount = seatCount;
    13     }
    14 
    15 
    16      //方法重写
    17     @Override
    18     public int CalcRent(int day) {
    19         
    20         return 0;
    21     }
    22     //方法重载
    23     public int CalcRent(int days,int seatCount){
    24         if(seatCount<=16){
    25             num=800*days;
    26         }
    27         else if(seatCount>16){
    28             num=1500*days;
    29         }
    30         System.out.println("费用为:"+num);
    31         return num;
    32     }
    33 
    34     
    35     
    36 }
     1 package day13;
     2 //小车类
     3 public class Car extends MotoVehicle{
     4     
     5     private String type;
     6     
     7     public Car(String no,String type) {
     8         super();
     9         this.no=no;
    10         this.type = type;
    11     }
    12     //方法重写
    13     @Override
    14     public int CalcRent(int day) {
    15         
    16         return 0;
    17     }
    18     //方法重载
    19     public int CalcRent(int days,String type){
    20         //
    21         switch (type) {
    22         case "1":
    23             num=600*days;
    24             break;
    25         case "2":
    26             num=500*days;
    27             break;
    28         case "3":
    29             num=300*days;
    30             break;
    31         
    32         default:
    33             System.out.println("输入有误");
    34             break;
    35         }
    36         System.out.println("费用为"+num);
    37         return num;
    38     }
    39 }
     1 package day13;
     2 
     3 public class Truck extends MotoVehicle{
     4     int  tonnage;
     5     
     6     public Truck(String no,int tonnage) {
     7         super();
     8         this.no=no;
     9         this.tonnage = tonnage;
    10     }
    11 
    12     //重写方法
    13     @Override
    14     public int CalcRent(int day) {
    15         
    16         return 0;
    17     }
    18     //重载方法
    19     public int CalcRent(int day,int tonnage){
    20         num=tonnage*day*50;
    21         System.out.println("费用为"+num);
    22         return num;
    23     }
    24     
    25 
    26 }
    package day13;
    
    import java.util.Scanner;
    
    public class Test {
    
        public static void main(String[] args) {
            Scanner reader=new Scanner(System.in);
            System.out.println("请输入数字选择车型:(1轿车),(2客车),(3.卡车)");
            int m=reader.nextInt();
            if(m==1){
                System.out.println("1.别克商务舱GL8");
                System.out.println("2.宝马55i");
                System.out.println("3.别克林荫大道");
                System.out.println("请输入轿车品牌数字:");
                String b=reader.next();
                
            
                System.out.println("请输入租赁天数:");
                int days=reader.nextInt();
                
                //创建对象
                Car car=new Car("0",b);
                car.CalcRent(days, b);
                
            }else if(m==2){
                System.out.println("请输入座位数:");
                int c=reader.nextInt();
                System.out.println("请输入租赁天数:");
                int  days=reader.nextInt();
                
                //创建对象并调用
                Bus bus=new Bus("0",c);
                bus.CalcRent(days, c);
            }else if(m==3){
                System.out.println("请输入吨位:");
                int d=reader.nextInt();
                System.out.println("请输入要租赁的天数:");
                int days=reader.nextInt();
                
                //创建对象并使用
                Truck truck=new Truck("0", d);
                truck.CalcRent(days, d);
            }
            else{
                System.out.println("输入有误!");
            }
    
        }
    
    }

    运行截图:

  • 相关阅读:
    ios学习Day3xiawu
    ios学习Day3
    linux系统设置回收站
    Linux sort命令的注意点
    markdown文件管理
    PG创建只读用户
    PG游标引用实例
    小狼毫输入法配置双拼输入
    日期相关
    PG数组使用
  • 原文地址:https://www.cnblogs.com/yumu77/p/13714596.html
Copyright © 2011-2022 走看看