JAVA学习homework的Car、Bus、motoVehicle

MotoVehicle

package com.mashibing.homework;

/**
 * @author: 马士兵教育
 * @create: 2019-08-31 15:33
 */
public abstract class MotoVehicle {
          
   

    private int no;
    private String brand;

    public MotoVehicle(){
          
   

    }

    public MotoVehicle(int no,String brand){
          
   
        this.no = no;
        this.brand = brand;
    }

    public int getNo() {
          
   
        return no;
    }

    public void setNo(int no) {
          
   
        this.no = no;
    }

    public String getBrand() {
          
   
        return brand;
    }

    public void setBrand(String brand) {
          
   
        this.brand = brand;
    }

    public abstract int calcRent(int day);
}

Bus

package com.mashibing.homework;

/**
 * @author: 马士兵教育
 * @create: 2019-08-31 15:34
 */
public class Bus extends MotoVehicle {
          
   

    private int seatcount;

    public Bus(){
          
   

    }

    public Bus(int no,String brand,int seatcount){
          
   
        super(no,brand);
        this.seatcount = seatcount;
    }

    public int getSeatcount() {
          
   
        return seatcount;
    }

    public void setSeatcount(int seatcount) {
          
   
        this.seatcount = seatcount;
    }

    @Override
    public int calcRent(int day) {
          
   
        if(seatcount>16){
          
   
            return 1500*day;
        }else{
          
   
            return 800*day;
        }
    }
}

Car

package com.mashibing.homework;

/**
 * @author: 马士兵教育
 * @create: 2019-08-31 15:34
 */
public class Car  extends MotoVehicle{
          
   

    private String type;

    public Car(){
          
   

    }

    public Car(String type) {
          
   
        this.type = type;
    }

    public Car(int no,String brand,String type){
          
   
        super(no,brand);
        this.type= type;
    }

    public String getType() {
          
   
        return type;
    }

    public void setType(String type) {
          
   
        this.type = type;
    }

    @Override
    public int calcRent(int day) {
          
   

        if(type.equals("0")){
          
   
            return 600*day;
        }else if(type.equals("1")){
          
   
            return 500*day;
        }else if(type.equals("2")){
          
   
            return 300*day;
        }else{
          
   
            System.out.println("类型不匹配");
            return 0;
        }
    }
}

TestMotoVehicle

package com.mashibing.homework;

/**
 * @author: 马士兵教育
 * @create: 2019-08-31 15:44
 */
public class TestMotoVehicle {
          
   
    public static void main(String[] args) {
          
   
//        MotoVehicle moto = new MotoVehicle();
//    Car car = new Car(1,"宝马","1");
//    System.out.println("租金是:"+car.calcRent(5));
//    Bus bus = new Bus(2,"金龙",20);
//        System.out.println("租金是:"+bus.calcRent(5));
        MotoVehicle[] moto = new MotoVehicle[5];
        moto[0] = new Car(1,"宝马","1");
        moto[1] = new Car(1,"宝马","1");
        moto[2] = new Car(2,"别克","2");
        moto[3] = new Bus(3,"金龙",34);
        moto[4] = new Track(4,"解放",50);
        int totalMoney = calcTotal(moto);
        System.out.println("总租金是:"+totalMoney);
    }

    public static int calcTotal(MotoVehicle[] moto){
          
   
        int totalMoney = 0;
        for(int i = 0;i<moto.length;i++){
          
   
            totalMoney+=moto[i].calcRent(5);
        }
        return totalMoney;
    }
}

Track

package com.mashibing.homework;

/**
 * @author: 马士兵教育
 * @create: 2019-08-31 16:50
 */
public class Track extends MotoVehicle {
          
   
    private int weight;

    public Track(){
          
   

    }

    public Track(int no,String brand,int weight){
          
   
        super(no,brand);
        this.weight = weight;
    }
    @Override
    public int calcRent(int day) {
          
   
        return 50*day*weight;
    }
}
经验分享 程序员 微信小程序 职场和发展