手记

JAVA第二季第六章——哒哒

分析:

  1. 俩个接口:
    载货接口ICapacity,抽象方法getCapacity(获取载货量)
    载客接口ILoad,抽象方法getLoad(获取载客数)
  2. 抽象父类:
    Car类,属性:名称name,单价price,构造方法,Getters和Setters方法。
    3.实现类:
    载客Bus类,继承Car类,接口为 载客接口ILoad, 属性:载客数load,构造方法,Getters和Setters方法。
    载货Truck类,继承Car类,接口为载货接口ICapacity,属性:载货量capacity,构造方法,Getters和Setters方法。
    载客载货PickupTruck类,继承Car类,接口为两个接口,属性:载客数load及载货量capacity,构造方法,Getters和Setters方法。
    4.客户端:
    Client类,在main方法里写流程。

代码:
1.

package com.imooc.java2.ch6x;
/**
 * 载人数
 */
public interface ILoad {
    public int getLoad();
}

2.

package com.imooc.java2.ch6x;
/**
 * 载货量
 */
public interface ICapacity {
    public int getCapacity(); 
}

3.

package com.imooc.java2.ch6x;
/**
 * 一个抽象父类Car(车型、单价-->元/天);
 */
public abstract class Car {
    private String name;
    private double price;
    public Car() {
        super();
    }
    public Car(String name, double price) {
        super();
        this.name = name;
        this.price = price;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public double getPrice() {
        return price;
    }
    public void setPrice(double price) {
        this.price = price;
    }

}

4.

package com.imooc.java2.ch6x;
/**
 * 客车
 */
public class Bus extends Car implements ILoad {
    private int load;

    public Bus() {
        super();
    }

    public Bus(String name, double price, int load) {
        super(name, price);
        this.load = load;
    }

    @Override
    public int getLoad() {
        // TODO Auto-generated method stub
        return load;
    }

    public void setLoad(int load) {
        this.load = load;
    }

}

5.

package com.imooc.java2.ch6x;
/**
 * 货车
 */
public class Truck extends Car implements ICapacity {
    private int capacity;

    public Truck() {
        super();
    }

    public Truck(String name, double price, int capacity) {
        super(name, price);
        this.capacity = capacity;
    }

    @Override
    public int getCapacity() {
        return capacity;
    }

    public void setCapacity(int capacity) {
        this.capacity = capacity;
    }

}

6.

package com.imooc.java2.ch6x;
/**
 * 皮卡
 */
public class PickupTruck extends Car implements ILoad, ICapacity {
    private int capacity;
    private int load;

    public PickupTruck() {
        super();
    }

    public PickupTruck(String name, double price, int capacity, int load) {
        super(name, price);
        this.capacity = capacity;
        this.load = load;
    }

    @Override
    public int getCapacity() {
        return capacity;
    }

    @Override
    public int getLoad() {
        return load;
    }

    public void setCapacity(int capacity) {
        this.capacity = capacity;
    }

    public void setLoad(int load) {
        this.load = load;
    }
}

7.

package com.imooc.java2.ch6x;
import java.util.Scanner;
/**
 * 客户端
 */
public class Client {
    private Car[] cars;
    private int[] rent;
    private int rentDays;

    public Client() {
        super();
    }

    public Client(Car[] cars, int[] rent, int rentDays) {
        super();
        this.cars = cars;
        this.rent = rent;
        this.rentDays = rentDays;
    }

    public void initial(){
        cars = new Car[6];
        cars[0]=new Bus("奥迪A4",500,4);
        cars[1]=new Bus("马自达6",400,4);
        cars[2]=new PickupTruck("皮卡雪6",450,2,4);
        cars[3]=new Bus("金龙",800,20);
        cars[4]=new Truck("松花江",400,4);
        cars[5]=new Truck("依维柯",1000,20);
    }

    public void showCars(){
        System.out.println("序号 \t汽车名称\t租金\t\t容量");
        for(int i=0;i<cars.length;i++){
            System.out.print((i+1)+". \t"+cars[i].getName()+"\t"+cars[i].getPrice()+"元/天\t");
            //判定是哪种车,instanceof:载货、载人(接口)
            if(cars[i] instanceof ILoad){
                System.out.print("载客:"+((ILoad)cars[i]).getLoad()+"人");
            }
            if(cars[i] instanceof ICapacity){
                System.out.print("载货:"+((ICapacity)cars[i]).getCapacity()+"吨");
            }
            System.out.println("");
        }
    }

    public void showBill(){
        System.out.println("您的账单:");
        System.out.println("***可载人的车有:");
        int sum =0;
        for(int i=0;i<rent.length;i++){
            //rent[i]为cars的下标,所以cars[rent[i]]为车。
            if(cars[rent[i]] instanceof ILoad){
                System.out.print(cars[rent[i]].getName()+"\t");
                sum+=((ILoad)cars[rent[i]]).getLoad();
            }
        }
        System.out.println("共载人:"+sum+"人");

        System.out.println("***可载货的车有:");
        sum=0;
        for(int i=0;i<rent.length;i++){
            if(cars[rent[i]] instanceof ICapacity){
                System.out.print(cars[rent[i]].getName()+"\t");
                sum+=((ICapacity)cars[rent[i]]).getCapacity();
            }
        }
        System.out.println("共载货:"+sum+"吨");
        sum=0;
        for(int i=0;i<rent.length;i++){
            sum+=cars[rent[i]].getPrice();
        }
        System.out.println("***租车总价格:"+(sum*this.rentDays)+"元");

    }

    public Car[] getCars() {
        return cars;
    }

    public void setCars(Car[] cars) {
        this.cars = cars;
    }

    public int getRentDays() {
        return rentDays;
    }

    public void setRentDays(int rentDays) {
        this.rentDays = rentDays;
    }

    public int[] getRent() {
        return rent;
    }

    public void setRent(int[] rent) {
        this.rent = rent;
    }
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Client dd = new Client();
        dd.initial();
        System.out.println("欢迎使用dada租车系统:");
        System.out.println("您是否要租车:1是 0否");
        int isRent = scanner.nextInt();
        if(isRent==0){
            System.exit(0);//结束
        }
        System.out.println("您可租车的类型及其价目表:");
        dd.showCars();
        System.out.println("请输入您要租汽车的数量");
        int carNum = scanner.nextInt();
        int[] rentCars = new int[carNum];
        for(int i=0;i<carNum;i++){
            System.out.println("请输入第"+(i+1)+"量车的序号");
            rentCars[i]=scanner.nextInt()-1;
        }
        dd.setRent(rentCars);
        System.out.println("请输入租车天数");
        dd.setRentDays(scanner.nextInt());
        dd.showBill();
    }

}

0人推荐
随时随地看视频
慕课网APP