手记

Java入门第二季--答答租车系统小测

定义车辆的父类Car
定义载人和载客的两个接口ILoadGoods和ILoadPerson
定义三种车型:客车(AutoCar)、货车(Trunk)和皮卡(PickUpCar)
AutoCar、Trunk和PickUpCar都继承父类Car
AutoCar实现ILoadPerson接口
Trunk实现ILoadGoods接口
PickUpCar实现ILoadPerson和ILoadGoods接口
以下是代码:

Car类

package com.imooc.liukai.dadahire;

public class Car {
    private String name ;
    private int price ;
    private int id;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getPrice() {
        return price;
    }
    public void setPrice(int price) {
        this.price = price;
    }
    public void PrintCarInfo(){
        System.out.print(id+"\t"+name+"\t\t"+price+"元/天\t");
    }

}

ILoadGoods接口

package com.imooc.liukai.dadahire;

public interface ILoadGoods {
    public abstract void setLoadGoodsAttributes(int volume);
    public abstract int getLoadGoodsAttributes();
}

ILoadPerson 接口

package com.imooc.liukai.dadahire;

public interface ILoadPerson {
    public abstract void setLoadPersonAttributes(int volume);
    public abstract int getLoadPersonAttributes();
}

AutoCar 类

package com.imooc.liukai.dadahire;

public class AutoCar extends Car implements ILoadPerson{

    private String loadPersontype = "载人";
    private int loadPersonVolume;
    public AutoCar(int newid, String newname, int newprice, int newloadPersonVolume) {
        // TODO Auto-generated constructor stub
        super();
        super.setId(newid);
        super.setName(newname);
        super.setPrice(newprice);
        this.setLoadPersonAttributes(newloadPersonVolume);
    }

    @Override
    public void setLoadPersonAttributes(int volume) {
        // TODO Auto-generated method stub
        this.loadPersonVolume = volume;
    }

    @Override
    public int getLoadPersonAttributes() {
        // TODO Auto-generated method stub
        return this.loadPersonVolume;
    }

    @Override
    public void PrintCarInfo() {
        // TODO Auto-generated method stub
        super.PrintCarInfo();
        System.out.println("\t"+loadPersontype+":"+loadPersonVolume+"人");
    }

}

Trunk 类

package com.imooc.liukai.dadahire;

public class Trunk extends Car implements ILoadGoods{

    private String loadGoodstype = "载货";
    private int loadGoodsVolume;
    public Trunk(int newid, String newname, int newprice,int newloadGoodsVolume) {
        // TODO Auto-generated constructor stub
        super();
        super.setId(newid);
        super.setName(newname);
        super.setPrice(newprice);
        this.setLoadGoodsAttributes(newloadGoodsVolume);

    }

    @Override
    public void setLoadGoodsAttributes(int volume) {
        // TODO Auto-generated method stub
        this.loadGoodsVolume = volume;
    }

    @Override
    public int getLoadGoodsAttributes() {
        // TODO Auto-generated method stub
        return this.loadGoodsVolume;
    }

    @Override
    public void PrintCarInfo() {
        // TODO Auto-generated method stub
        super.PrintCarInfo();
        System.out.println("\t"+loadGoodstype+":"+loadGoodsVolume+"吨");
    }

}

PickUpCar 类

package com.imooc.liukai.dadahire;

public class PickUpCar extends Car implements ILoadPerson,ILoadGoods{

    private String loadPersontype = "载人";
    private String loadGoodstype = "载货";
    private int loadPersonVolume;
    private int loadGoodsVolume;

    public PickUpCar(int newid, String newname, int newprice , int newloadPersonVolume, int newloadGoodsVolume) {
        // TODO Auto-generated constructor stub
        super();
        super.setId(newid);
        super.setName(newname);
        super.setPrice(newprice);
        this.setLoadPersonAttributes(newloadPersonVolume);
        this.setLoadGoodsAttributes(newloadGoodsVolume);
    }

    @Override
    public void setLoadGoodsAttributes(int volume) {
        // TODO Auto-generated method stub
        this.loadGoodsVolume = volume;
    }

    @Override
    public void setLoadPersonAttributes(int volume) {
        // TODO Auto-generated method stub
        this.loadPersonVolume = volume;
    }

    @Override
    public int getLoadGoodsAttributes() {
        // TODO Auto-generated method stub
        return this.loadGoodsVolume;
    }

    @Override
    public int getLoadPersonAttributes() {
        // TODO Auto-generated method stub
        return this.loadPersonVolume;
    }

    @Override
    public void PrintCarInfo() {
        // TODO Auto-generated method stub
        super.PrintCarInfo();
        System.out.println("\t"+loadPersontype+":"+loadPersonVolume+"人\t"+loadGoodstype+":"+loadGoodsVolume+"吨");
    }

}

测试类

package com.imooc.liukai.dadahire;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Scanner;

public class HireTest {
    public static void main(String[] args) {
        HireTest hireTest = new HireTest();
        ArrayList<Car> carlist = new ArrayList<>();
        carlist = hireTest.InitCarInfo(carlist);
        System.out.println("欢迎使用答答租车系统:\n请选择您是否要租车:1、是    2、否");
        try {
            InputStream ins = System.in;
            Scanner scanner = new Scanner(System.in);
            int isHire = scanner.nextInt();
            if(isHire == 0){
                ins.close();
            }else if(isHire == 1){
                System.out.println("您可租车的信息及其价目表\n序号\t汽车名称\t\t租金\t\t容量");
                hireTest.ListCarInfo(carlist);
                System.out.println("请输入您需要租车的数量:");
                int sum = 0;
                while( (sum = scanner.nextInt()) > carlist.size()){
                    System.out.println("您输入的数量超过了现有的数量,请重新输入:");
                }
                System.out.println("输入你要租车的序号:");
                ArrayList<Integer> idlist = new ArrayList<>();
                for(int i = 0 ; i < sum ; i++){
                    idlist.add(scanner.nextInt());
                }
                System.out.println("输入你要租车的天数:");
                int day = 0;
                while( (day = scanner.nextInt()) < 0){
                    System.out.println("您输入的天数小于0,请重新输入:");
                }
                System.out.println("您的账单如下:");
                hireTest.ListChoosedCarInfo(carlist, idlist, day);
                ins.close();
            }
            else {
                throw new Exception();
            }
        } catch (Exception e) {
            // TODO: handle exception
            System.out.println("输入的指令错误");
        }
    }

    public ArrayList<Car> InitCarInfo(ArrayList<Car> carlist){
        Car audio = new AutoCar(1, "奥迪A4", 500, 4);
        Car mazida = new AutoCar(2, "马自达6", 400, 4);
        Car pickup = new PickUpCar(3, "皮卡雪6", 450, 4,2);
        Car jinlong = new AutoCar(4, "金龙", 800, 20);
        Car songhj = new Trunk(5, "松花江", 400, 4);
        Car yiwk = new Trunk(6, "依维柯", 1000, 20);
        carlist.add(audio);
        carlist.add(mazida);
        carlist.add(pickup);
        carlist.add(jinlong);
        carlist.add(songhj);
        carlist.add(yiwk);
        return carlist;
    }

    public void ListCarInfo(ArrayList<Car> list){
        for(Car car : list){
            car.PrintCarInfo();
        }
    }

    public void ListChoosedCarInfo(ArrayList<Car> carlist, ArrayList<Integer> idlist, int day){
        StringBuffer personinfo = new StringBuffer();
        int personumber = 0;
        StringBuffer goodsinfo = new StringBuffer();
        int goodsnumber = 0;
        int singledayprice = 0;
        for(Car car : carlist){
            if (idlist.contains(car.getId())) {
                if(car.getClass() == AutoCar.class){
                    personinfo.append(car.getName()+" ");
                    personumber += ((AutoCar)car).getLoadPersonAttributes();
                }else if(car.getClass() == Trunk.class){
                    goodsinfo.append(car.getName()+" ");
                    goodsnumber += ((Trunk)car).getLoadGoodsAttributes();
                }else if(car.getClass() == PickUpCar.class){
                    personinfo.append(car.getName()+" ");
                    personumber += ((PickUpCar)car).getLoadPersonAttributes();
                    goodsinfo.append(car.getName()+" ");
                    goodsnumber += ((PickUpCar)car).getLoadGoodsAttributes();
                }
                singledayprice += car.getPrice();
            }
        }
        System.out.println("***可载人的车有:");
        System.out.println(personinfo.toString()+"共载人:"+personumber+"人");
        System.out.println("***可载货的车有:");
        System.out.println(goodsinfo.toString()+"共载货:"+goodsnumber+"吨");
        System.out.println("***租车总价格:"+singledayprice*day);
    }
}

运行结果:
欢迎使用答答租车系统:
请选择您是否要租车:1、是 2、否
1
您可租车的信息及其价目表
序号 汽车名称 租金 容量
1 奥迪A4 500元/天 载人:4人
2 马自达6 400元/天 载人:4人
3 皮卡雪6 450元/天 载人:4人 载货:2吨
4 金龙 800元/天 载人:20人
5 松花江 400元/天 载货:4吨
6 依维柯 1000元/天 载货:20吨
请输入您需要租车的数量:
4
输入你要租车的序号:
1 2 3 4
输入你要租车的天数:
3
您的账单如下:
可载人的车有:
奥迪A4 马自达6 皮卡雪6 金龙 共载人:32人
可载货的车有:
皮卡雪6 共载货:2吨
租车总价格:6450

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

热门评论

这前面2季都没说到ArrayList。。。我想半天都想不出怎么直接输出汽车列表,只能每辆车变为一个子类

public abstract void setLoadGoodsAttributes(int volume);    

public abstract int getLoadGoodsAttributes();

可以讲一下这两句是什么意思吗?


我是个渣渣,看不懂呀!

查看全部评论