手记

答答租车系统

1.car类

public class Car {
    protected int price;
    protected String name;
    protected int getPrice(){
        return price;
    }
    protected String getName(){
        return name;
    }
    public int getPersonNum(){
        return 0;
    }
    public int getGoodsNum(){
        return 0;
    }
}

2.mannedCar类

public class MannedCar extends Car{
    private int personNum;

    public MannedCar(){
        this.personNum =0;
        this.price=0;
        this.name="";
    }
    public MannedCar(int personNum,int price,String name){
        this.price=price;
        this.name=name;
        this.personNum=personNum;
    }
    public int getPersonNum(){
        return personNum;
    }

}

3.Truck类

public class Truck extends Car{
    private int goodsNum;

    public Truck(){
        this.price = 0;
        this.name="";
        this.goodsNum=0;
    }
    public Truck(int goodsNum,int price,String name){
        this.price=price;
        this.name=name;
        this.goodsNum=goodsNum;
    }
    public int getGoodsNum(){
        return goodsNum;
    }
}

4.bothCarry类

public class BothCarry extends Car{
    private int personNum;
    private int goodsNum;

    public BothCarry(){
        this.personNum=0;
        this.goodsNum=0;
        this.name="";
        this.price=0;
    }
    public BothCarry(int personNum,int price,String name,int goodsNum){
        this.price=price;
        this.name=name;
        this.personNum=personNum;
        this.goodsNum=goodsNum;
    }
    public int getPersonNum(){
        return personNum;
    }
    public int getGoodsNum(){
        return goodsNum;
    }
}

5.carSystem 租车系统类

import java.util.ArrayList;
import java.util.Scanner;

public class CarSystem {
    private static String goodByeInfo = "欢迎再次使用本系统,再见";
    private static int dayBorrow;

    public static void beginSystem(){
        CarSystem.SystemWelcome();
        Scanner scanner = new Scanner(System.in);
        String userCommand = scanner.next();

        switch (userCommand){
            case "1":
                CarSystem.getUserInput();
            case "0":
                System.out.println(goodByeInfo);
                break;
            default:
                System.out.println("输入错误..End running..");
                System.exit(0);
                break;
        }
    }
    public static void SystemWelcome(){
        System.out.println("欢迎使用答答租车系统:");
        System.out.println("您是否要租车:1-是  0-否");
    }
    public static void getUserInput(){
        int numCarBorrow =0;
        ArrayList<Car> carList = new ArrayList<Car>(6);
        carList.add(new MannedCar(4,500,"奥迪"));
        carList.add(new MannedCar(4,400,"马自达"));
        carList.add(new BothCarry(450,4,"皮卡",2));
        carList.add(new MannedCar(20,800,"奥迪A3"));
        carList.add(new Truck(400,400,"松花江"));
        carList.add(new Truck(1000,200,"bear"));

        System.out.println("请输入您要租车的数量");
        Scanner sr = new Scanner(System.in);

        numCarBorrow = sr.nextInt();

        int[] carNumlist = new int[numCarBorrow];
        for (int i =0;i<numCarBorrow;i++){
            System.out.println("请输入第"+ (i+1) + "辆车的序号:");
            if(sr.hasNext()){
                carNumlist[i] = sr.nextInt();
            }
        }
        System.out.println("请输入租车天数:");
        if(sr.hasNext()){
            dayBorrow = sr.nextInt();
        }
        sr.close();

        StringBuilder manned = new StringBuilder();
        int numOfManned =0;
        StringBuilder goods = new StringBuilder();
        int numOfGoods =0;
        int totalCost =0;

        for(int i=0;i<carNumlist.length;i++){
            if(carNumlist[i]>0 && carNumlist[i]<3 || carNumlist[i] ==4){
                manned.append(" ");
                manned.append(carList.get(carNumlist[i]-1).getName());
                numOfManned+= carList.get(carNumlist[i]-1).getPersonNum();
            }
            else if(carNumlist[i] ==3){
                manned.append(" ");
                manned.append(carList.get(carNumlist[i]-1).getName());
                goods.append(" ");
                goods.append(carList.get(carNumlist[i]-1).getName());
                numOfManned+= carList.get(carNumlist[i]-1).getPersonNum();
                numOfGoods+= carList.get(carNumlist[i]-1).getGoodsNum();
            }else{
                goods.append(" ");
                goods.append(carList.get(carNumlist[i]-1).getName());
                numOfManned+= carList.get(carNumlist[i]-1).getPersonNum();
            }
            totalCost +=carList.get(carNumlist[i]-1).getPrice();
        }
        System.out.println("您的账单:\n***可载人的车有:");
        System.out.println(manned + "    共载人: " + numOfManned);
        System.out.println("***载货的车有:\n" + goods + "    共载货 : " + numOfGoods + "吨");
        System.out.println("***租车总价格: " + totalCost * dayBorrow + "元");
    }
}

启动类:

public class CarSystemTest {
    public static void main(String[] args) {
        CarSystem.beginSystem();
    }
}

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