2015.1.31(打车系统)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2015.1.31(打车系统)相关的知识,希望对你有一定的参考价值。
实现一次uber 打车
乘客 有三种不同的vip等级 打折力度一次递增
其中类有司机 乘客 和系统
public class Driver {
private String name;
private String phoneNumber;
private Car myCar;
private Passenger passenger;
public Driver(){
}
public Driver(String name, String phoneNumber) {
super();
this.name = name;
this.phoneNumber = phoneNumber;
}
public void take(){//接单
System.out.println("接到乘客" + this.passenger.getName());
}
public void drive(){
System.out.println("行驶从" + this.passenger.getStartAddress() + "到" + this.passenger.getTargetAddress() + "完成本行程");
this.passenger.setComplete(true);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public Car getMyCar() {
return myCar;
}
public void setMyCar(Car myCar) {
this.myCar = myCar;
}
public Passenger getPassenger() {
return passenger;
}
public void setPassenger(Passenger passenger) {
this.passenger = passenger;
}
}
public class Passenger {
private String name;
private String phoneNumber;
private String startAddress;//出发地
private String targetAddress;//目的地
private int type;
private Driver myDriver;
private boolean isComplete = false;
private double distance;
public Passenger(){
}
public Passenger(String name, String phoneNumber, String startAddress,
String targetAddress, int type, double distance) {
super();
this.name = name;
this.phoneNumber = phoneNumber;
this.startAddress = startAddress;
this.targetAddress = targetAddress;
this.type = type;
this.distance = distance;
}
public void getCar(){
System.out.println("开始打车");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public String getStartAddress() {
return startAddress;
}
public void setStartAddress(String startAddress) {
this.startAddress = startAddress;
}
public String getTargetAddress() {
return targetAddress;
}
public void setTargetAddress(String targetAddress) {
this.targetAddress = targetAddress;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public Driver getMyDriver() {
return myDriver;
}
public void setMyDriver(Driver myDriver) {
this.myDriver = myDriver;
}
public boolean getComplete() {
return isComplete;
}
public void setComplete(boolean isComplete) {
this.isComplete = isComplete;
}
public double getDistance() {
return distance;
}
public void setDistance(double distance) {
this.distance = distance;
}
}
package com.uber;
/**
* Date: 2016-1-31-下午1:55:05 Class_name: Uber.java Package_name: com.uber
* Author: ZhangYue Description:
*/
public class Uber {
private Passenger[] passengers = new Passenger[100];
private Driver[] drivers = new Driver[100];
private double normalRate = 1.0;
private double uVIPRate = 0.9;
private double sVIPRate = 0.7;
private double price = 1.0;
private int normalPassenger = 0;
private int uPassenger = 1;
private int sPassenger = 2;
public void assignDriver(Driver driver, Passenger pass) {//分配司机给乘客
System.out.println("委派最近的司机给乘客");
driver.setPassenger(pass);// 将乘客分配给该司机
pass.setMyDriver(driver);// 将司机分配给该乘客
}
public void calculateCost(Passenger pass) {//计算费用
if (pass != null && pass.getComplete()) {
int type = pass.getType();
double basicCost = pass.getDistance() * this.price;
if (type == normalPassenger) {
basicCost *= this.normalRate;
} else if (type == uPassenger) {
basicCost *= this.uVIPRate;
} else if (type == sPassenger) {
basicCost *= this.sVIPRate;
}
System.out.println("本次行程收取" + pass.getName() + basicCost
+ "元,本次行程结束,欢迎下次使用Uber");
}
}
}
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
Uber uber = new Uber();
Driver driver = new Driver();
Passenger passenger = new Passenger();
passenger.getCar();
uber.assignDriver(driver, passenger);
driver.take();
driver.drive();
uber.calculateCost(passenger);
}
}
以上是关于2015.1.31(打车系统)的主要内容,如果未能解决你的问题,请参考以下文章