手记

Java入门第二季项目实战——答答租车

  1. 列表项

package com.imooc;

/**

  • 父类 -- Car
    */
    public abstract class Car
    {
    private String modle; //汽车型号
    private int rent; //汽车租金
    private int capacity; //汽车载客量
    private int cargo; //汽车载货量

    /**
     * 属性--获取车型
     */
    public String getModle() {
        return modle;
    }
    /**
     *属性--设置车型
     */
    public void setModle(String modle) {
        this.modle = modle;
    }
    /**
     * 属性--获取车型价格
     */
    public int getRent() {
        return rent;
    }
    /**
     * 属性--设置车型价格
     */
    public void setRent(int rent) {
        this.rent = rent;
    }
    /**
     * 属性--获取载客人数
     */
    public int getCapacity() {
        return capacity;
    }
    /**
     * 属性--设置载客人数
     */
    public void setCapacity(int capacity) {
        this.capacity = capacity;
    }
    /**
     * 属性--获取车型载货数量
     */
    public int getCargo() {
        return cargo;
    }
    /**
     * 属性--设置车型载货数量
     */
    public void setCargo(int cargo) {
        this.cargo = cargo;
    }

    }

package com.imooc;

/**

  • 子类 -- Bus
    */
    public class Bus extends Car
    {
    //载客用车初始化
    public Bus(String mode, int rent, int capacity)
    {
    this.setModle(mode);
    this.setRent(rent);
    this.setCapacity(capacity);
    }
    }

package com.imooc;

/**

  • 子类 -- Truck
    */
    public class Truck extends Car
    {
    //载货卡车初始化
    public Truck(String mode, int rent, int cargo)
    {
    this.setModle(mode);
    this.setRent(rent);
    this.setCargo(cargo);
    }
    }

package com.imooc;

/**

  • 子类 -- Pickup
    */
    public class Pickup extends Car
    {
    //皮卡车的初始化
    public Pickup(String modle, int rent, int capacity, int cargo)
    {
    this.setModle(modle);
    this.setRent(rent);
    this.setCapacity(capacity);
    this.setCargo(cargo);
    }
    }

package com.imooc;

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

public class Test {

//  定义一个 Scanner 静态对象
private static Scanner sn = new Scanner(System.in);

/**
 * 可选车型初始化
 */
private Car [ ] initCarList()
{
    Car[] carList = { new Bus("奥迪A4", 500, 4),
                            new Bus("马自达6", 400, 4),
                            new Pickup("皮卡雪6", 450, 4,2),
                            new Bus("金龙", 800, 20),
                            new Truck("送花江", 400, 4),
                            new Truck("依维柯", 1000, 20)
          };
    return carList;
}

/**
 * 打印可选车型信息 
 */
private void printCarList(Car[] carList)
{
    System.out.println("欢迎来到租车系统,请问您要租车吗?"); 
    System.out.println("租车请输入1,退出请输入0");
    String input = sn.next();

    if(input.equals("1"))
    {
        System.out.println("您可租车的类型及其价目表:"); 
        System.out.println("序号\t汽车名称\t租金\t载客量\t载货量");
        int i =1;
        for(Car curCar : carList)
        {
            if(curCar instanceof Bus)
            {
                System.out.println("" + i + "\t" 
                                                 + curCar.getModle()  + "\t" 
                                                 + curCar.getRent() + "元/天" + "\t" 
                                                 + "载人:" + curCar.getCapacity() +  "人" );
            }
            else if(curCar instanceof Truck)
            {
                System.out.println(""   + i + "\t" 
                                                    + curCar.getModle()  + "\t" 
                                                    + curCar.getRent() + "元/天" + "\t" 
                                                    +  "载货:" + curCar.getCargo()+ "吨" );
            }
            else if(curCar instanceof Pickup)
            {
                System.out.println(""   + i + "\t" 
                                                    + curCar.getModle()  + "\t" 
                                                    + curCar.getRent() + "元/天" + "\t" 
                                                    + "载人:" + curCar.getCapacity() +  "人" + "\t" 
                                                    + "载货:" + curCar.getCargo()+ "吨" );
            }

            i++;
        }
    }
    else if(input.equals("0"))
    {
        System.out.println("谢谢光临租车系统,再见!");
    }
    else
    {
        System.out.println("命令输入错误!请根据提示输入命令值!");
    }
}

/**
 * 输入租车数量 
 */
private int setRentCount()
{
    int rentCount;

    do
    {
        System.out.print("请输入您要租车的数量: ");
        rentCount = sn.nextInt();
    }
    while(rentCount <= 0);

    return rentCount;
}

/**
 * 输入租车天数
 */
private int setRentDays()
{
    int rentDays;

    do
    {
        System.out.print("请输入租车天数: ");
        rentDays = sn.nextInt();
    }
    while(rentDays <= 0);

    return rentDays;
}

/**
 * 选择具体车型 
 */
private Car[ ] selectCars(Car[] carList, int rentCount)
{
    Car [] selectCar = new Car[ rentCount];
    int carNo;
    for(int i=0; i< rentCount; i++)
    {
        do
        {
            System.out.print("第" + (i + 1) + "辆车的编号(1-" + carList.length + "):");
            carNo = sn.nextInt();
        }
        while(carNo < 0  ||  carNo >  carList.length);

        selectCar[i] = carList [ carNo - 1];
    }

    return selectCar;
}

/**
 *  打印账单 
 */
private void printBill(Car[] selectCars, int days)
{
    // 计算选择车辆的车型名称 、总载客数、载重量、总租用金额
    List carCapacityNames = new ArrayList();    //载人车型
    List carCargoNames = new ArrayList();       //载货车型

    int sumCapacity = 0; 
    int sumCargo = 0; 
    int sumRent = 0;

    for(Car curCar : selectCars)
    {
        //总租用金额
        sumRent +=curCar.getRent() *  days;

        if(curCar instanceof Bus)
        {
            carCapacityNames.add(curCar.getModle());    //载人车型
            sumCapacity += curCar.getCapacity();            //总载客数

        }
        else if (curCar instanceof Truck)
        {
            carCargoNames.add(curCar.getModle());       //载货车型
            sumCargo +=curCar.getCargo();                   //总载货数
        }
        else if (curCar instanceof Pickup)
        {
            carCapacityNames.add(curCar.getModle());    //载人车型
            carCargoNames.add(curCar.getModle());       //载货车型
            sumCapacity += curCar.getCapacity();            //总载客数
            sumCargo +=curCar.getCargo();                   //总载货数
        }
    }

    System.out.println("---------------您的账单----------------" );
    System.out.println("***可载人的车有:" );
    for(Object carName : carCapacityNames)
    {
        if(carCapacityNames.lastIndexOf(carName)    == carCapacityNames.size() -1)
            System.out.println(carName.toString() + "\t" + "共载人:" + sumCapacity + "人");
        else
            System.out.print(carName.toString() + "\t");
    }

    System.out.println("***可载货的车有:" );
    for(Object carName : carCargoNames)
    {
        if(carCargoNames.lastIndexOf(carName)   == carCargoNames.size() -1)
            System.out.println(carName.toString() + "\t" + "共载货:" + sumCargo + "吨");
        else
            System.out.print(carName.toString() + "\t");
    }

    System.out.println("***租车总价格: "  + sumRent + "元");
}

/**
 * @param args
 */
public static void main(String[] args) 
{
    // TODO Auto-generated method stub
    Test Tt = new Test();                                                   //创建一个新Test对象
    Car[] carList = Tt.initCarList();                                   // 可选车型初始化
    Tt.printCarList(carList);                                               // 打印可选车型信息 
    int rentCount = Tt.setRentCount();                          // 输入租车数量 
    Car[] selectCars = Tt.selectCars(carList, rentCount);   // 选择具体车型 
    int days = Tt.setRentDays();                                        // 输入租车天数
    Tt.printBill(selectCars, days);                                     // 打印账单 
}

}

输入代码
9人推荐
随时随地看视频
慕课网APP

热门评论

是不是超过所学了,感觉导入好多的包不会啊

查看全部评论