继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

Java滴滴打车系统实现(同时实现租车购物车)

DekingChen
关注TA
已关注
手记 6
粉丝 11
获赞 240

实现过程如下:

  1. 构造抽象Auto类:是所有载客类型车的父类,如PassengerAuto类继承了该类
  2. 构造抽象类LoadWeight类:是所有既载客又载货车型的父类,如Truck类继承了该类
  3. 构造储存添加车型的CarList类:储存系统管理员添加车型的类
  4. 构造用户租车购物车Cart类:储存用户租用了哪种车型及费用明细列表
  5. 客户端类index类:调用以上各类实现租车系统

Auto.java:

package com.immoc.lib;

//仅能载客的车
public abstract class Auto {
    private int id;//定义车的编号
    private String carName;//定义车名
    private int price;//定义车价
    private int passengers;//定义载客量

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getCarName() {
        return carName;
    }
    public void setCarName(String carName) {
        this.carName = carName;
    }
    public int getPrice() {
        return price;
    }
    public void setPrice(int price) {
        this.price = price;
    }
    public int getPassengers() {
        return passengers;
    }

    public void setPassengers(int passengers) {
        this.passengers = passengers;
    }
    public Auto(int id,String carName, int price,int passengers) {
        super();
        this.setId(id);
        this.setCarName(carName);
        this.setPassengers(passengers);
        this.setPrice(price);
    }
    public String getMsg(){
        return this.getId()+"\t"+this.getCarName()+"\t"+this.getPrice()+"\t"+this.getPassengers()+"\t"+"0";
    }
    public void print(){
        System.out.println(getMsg());
    }

}

LoadWeight.java:

package com.immoc.lib;
import  com.immoc.lib.Auto;
//即能载客又能载货的车
public abstract class LoadWeight extends Auto {
    private int weight;//定义载货量
    public LoadWeight(int id,String carName, int price ,int passengers,int weight){
        super(id, carName, price, passengers);
        this.weight = weight;
    }
    public int getWeight() {
        return weight;
    }
    public void setWeight(int weight) {
        this.setWeight(weight);
    }
    public String getMsg(){
        return this.getId()+"\t"+this.getCarName()+"\t"+this.getPrice()+"\t"+this.getPassengers()+"\t"+this.getWeight();
    }
    public void print(){
        System.out.println(getMsg());
    }

}

PassengerAuto.java

package com.immoc;

import com.immoc.lib.Auto;

//创建只能载客的子类车--继承于只能载客的类(Auto)
public class PassengerAuto extends Auto {

    public PassengerAuto(int id, String carName, int price ,int passengers){
        super(id, carName,price,passengers);
    }

}

Truck.java

package com.immoc;

import com.immoc.lib.LoadWeight;

//创建能载客又能载货的类(Truck),继承于LoadWeight类
public class Truck extends LoadWeight {

    public Truck(int id ,String carName, int price ,int passengers,int weight){
        super(id, carName, price, passengers, weight);
    }
}

CarList.java:

package com.immoc.lib;

import java.util.ArrayList;

//储存系统中存在的车型--系统管理员使用
public class CarList {

    public static ArrayList<Auto> carlist = new ArrayList<Auto>() ;//储存现有的汽车列表

    /**
     * 向汽车列表中添加不同类型的汽车
     * @param car 汽车对象
     */
    public static boolean addCar(Auto car){
            return carlist.add(car);
    }

    //显示系统中存在的车型信息
    public static void showCars(){
        System.out.println("型号\t"+"名称\t"+"价格\t"+"载客量\t"+"载货量\t");
            for(int i=0;i<carlist.size();i++){
                Auto auto =  carlist.get(i);
                auto.print();
            }
    }

    /**
     * 删除某种车型信息
     * @param id 车型编号
     * @return
     */
    public static boolean removeCar(int id){
        Auto car =null;
        boolean isRemove = false;
        for(int i=0; i<carlist.size();i++){
            car = carlist.get(i);
            if(car.getId()==id)
            {
                isRemove = (carlist.remove(i)!=null)?true:false;
                break;
            }
        }
        return isRemove;
    }

    /**
     * 找到某一种车型的车
     * @param id
     * @return
     */
    public static Auto getCar(int id){
        Auto car =null;
        boolean isGet = false;
        for(int i=0; i<carlist.size();i++){
            car = carlist.get(i);
            if(car.getId()==id)
            {
                car = carlist.get(i);
                isGet = true;
                break;
            }
        }
        car = isGet?car:null;
        return car;
    }
}

Cart.java

package com.immoc.lib;

import java.util.ArrayList;
//租车购物车列表:用来储存租车信息--用户使用
public class Cart {
    public static ArrayList<CarNode> cartlist = new ArrayList<CarNode>();//储存租车信息
    public static int total=0;//储存租车总费用

    //计算租车费用总数
    public static int getTotal(){
        for(CarNode car:cartlist){
            int id = car.getId();
            int sum = car.getSum();
            Auto auto = CarList.getCar(id);
            if(auto!=null){
                total+=auto.getPrice()*sum;
            }
        }
        return total;
    }

    //显示购物车中的信息
    public static void showOrders(){
        System.out.println("======================================================");
        System.out.println("型号\t"+"名称\t"+"价格\t"+"载客量\t"+"载货量\t"+"租车数目\t"+"总价");
        String list ="";
        int totalsum=0;
        for(CarNode car:cartlist){
            int id = car.getId();
            int sum = car.getSum();
            Auto auto = CarList.getCar(id);
            list+= auto.getMsg()+"\t"+sum+"\t"+auto.getPrice()*sum+"\t\n";
            totalsum+=auto.getPrice()*sum;
        }
        System.out.print(list);
        System.out.println("======================================================");
        System.out.println("租车总费用:"+totalsum);
        System.out.println("======================================================");
    }
    //租用的车型和数目
    public static boolean addCar(int id, int sum){
        int index = Cart.getCar(id);
        if(index==-1){
            //如果购物车中不存在则创建对象
            CarNode car = new CarNode(id,sum);
            return cartlist.add(car);
        }else{
            //如果存在则修改数量
            CarNode car = cartlist.get(index);
            car.setSum(car.getSum()+sum);
            cartlist.set(index, car);
        }
        return true;
    }

    //删除一种车型
    public static boolean removeCar(int id){
        CarNode car =null;
        boolean isRemove = false;
        for(int i=0; i<cartlist.size();i++){
            car = cartlist.get(i);
            if(car.getId()==id)
            {
                isRemove = (cartlist.remove(i)!=null)?true:false;
                break;
            }
        }
        return isRemove;
    }

    //找到一种车型
    public static int getCar(int id){
        CarNode car =null;
        for(int i=0; i<cartlist.size();i++){
            car = cartlist.get(i);
            if(car.getId()==id)
                return i;
        }
        return -1;
    }

}

//储存租车信息
class CarNode{
    private int id;//车型编号
    private int sum;//租车的数目
    public CarNode(int id, int sum){
        this.setId(id);
        this.setSum(sum);
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public int getSum() {
        return sum;
    }
    public void setSum(int sum) {
        this.sum = sum;
    }

}

index.java

package com.immoc;
import com.immoc.lib.Auto;
import com.immoc.lib.CarList;
import com.immoc.lib.Cart;
//主方法
public class Index   {

    public static void show(Object obj){
        System.out.println(obj.toString());
    }

    public static void main(String[] args){
        Auto car01 = new PassengerAuto(100,"奥迪A4",500,4);
        Auto car02 = new PassengerAuto(110,"马自达6 ",400,4);
        Auto car03 = new Truck(120,"松花江",300,2,4);
        Auto car04 = new Truck(130,"依维柯",200,2,20);
        Auto car05 = new PassengerAuto(140,"东风日产 ",100,4);
        Auto car06 = new PassengerAuto(150,"宝马 ",1000,4);
        //管理员添加车型
        CarList.addCar(car01);
        CarList.addCar(car02);
        CarList.addCar(car03);
        CarList.addCar(car04);
        CarList.addCar(car05);
        CarList.addCar(car06);
        //CarList.removeCar(4);
        show("显示系统中所有的车型信息:");
        CarList.showCars();
        //用户添加租车的车型和租车数目
        Cart.addCar(100,2);
        Cart.addCar(110,2);
        Cart.addCar(120,2);
        Cart.addCar(130,2);
        Cart.addCar(150,1);
        Cart.removeCar(150);//移除车型
        //租车系统结账并显示租车具体信息
        show("您的租用信息如下:");
        Cart.showOrders();

    }

}

运行结果:
图片描述

打开App,阅读手记
10人推荐
发表评论
随时随地看视频慕课网APP

热门评论

我运行了一下,发现程序Cart.java里Auto auto = CarList.getCar(id);

有错,虚心求教,解答一下为什么,谢谢

查看全部评论