import java.util.Date
import java.util.Scanner
/*********************************
* 停车场管理
* author zhang
*2013-12-13
********************************/
public class CarStopManager {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in)
System.out.println("请入车牌号:")
String carno = sc.next()
CarStopManager carStopManager = new CarStopManager()
carStopManager.setCarNo(carno)//设置车牌号
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
String sdate = format.format(new Date())
System.out.println("当前时间(入场时间)是: "+sdate)
System.out.println("需要开出车场吗?yes/no:")
String yesno = sc.next()
if(yesno.equals("yes")){
String edate = format.format(new Date())
System.out.println("出场时间是: "+edate)
//计算方法
carManager(2, sdate, edate,carStopManager)
}
}
/**
* 计算方法
*/
public static void carManager(int type,String starTime,
String endTime,CarStopManager carStopManager){
if(type==1){//按月收费
System.out.println("如若没有缴纳月费请缴纳800元,如若缴纳将不再提示!")
}else{
/**
* 一般不会有停车几个月的吧?先不考虑停车几年或者几个月的
*/
String sDay = starTime.substring(8,10)//入场日期(天)
String sHour = starTime.substring(11, 13)//入场小时
String sMM = starTime.substring(14,16)//入场分钟
String eDay = starTime.substring(8,10)//出场日期(天)
String eHour = endTime.substring(11, 13)//出厂小时
String eMM = endTime.substring(14,16)//出厂分钟
float money = 0//需缴纳的费用
int shour = Integer.parseInt(sHour)
int ehour = Integer.parseInt(eHour)
int smm = Integer.parseInt(sMM)
int emm = Integer.parseInt(eMM)
int rehour = 0//停车几个小时
if(sDay.equals(eDay)){//同一天
//当天6点到20点之间
if((shour>=6 &&shour<=20)){
if(ehour - shour<=6){//6个小时之内
rehour = (ehour - shour)*60+(emm - smm)//停车多少分钟
//需要缴纳的费用 前15分钟免费 以后每15分钟1.5元
money = (rehour/15-15)*1.5f
}else{
int hour = ehour - shour -6//6小时除外剩余小时数
rehour = 6*60+(emm - smm)//停车多少分钟
//前15分钟免费 以后每15分钟1.5元 超过6小时15分钟2元
money = ((rehour/15-15)*1.5f)+(hour*60/2)
}
}
}else{//跨天 20点到 6点之间
//todo
}
System.out.println("您的车牌号是:"+carStopManager.getCarNo()+"\n" +
"您此次停车花费的费用是: "+money+"元")
}
}
/**
* bean属性
*/
private String carNo//车牌号
private String startTime//入场时间
private String endTime//出场时间
/**
* 无参构造
*/
public CarStopManager(){
super()
}
/**
* 有参数构造
* @param carNo
* @param startTime
* @param endTime
*/
public CarStopManager(String carNo, String startTime, String endTime) {
super()
this.carNo = carNo
this.startTime = startTime
this.endTime = endTime
}
/**
* 设置get方法
* @return
*/
public String getCarNo() {
return carNo
}
/**
* 设置set方法
* @param carNo
*/
public void setCarNo(String carNo) {
this.carNo = carNo
}
public String getStartTime() {
return startTime
}
public void setStartTime(String startTime) {
this.startTime = startTime
}
public String getEndTime() {
return endTime
}
public void setEndTime(String endTime) {
this.endTime = endTime
}
}
import java.util.Arraysimport java.util.Comparator
import java.util.HashMap
import java.util.Scanner
import java.util.Stack
import java.util.Vector
import java.util.regex.MatchResult
public class Test {
private CarStop carStop = new CarStop(3)
private CarTunnel tunnel = new CarTunnel()
public void test(){
//存放车辆信息,因为不是顺序输入的,所以放到Map中
HashMap<Integer, Car>carMap = new HashMap<Integer, Car>()
//最早进入车库的时间和最晚出车库的时间
int startTime, endTime
startTime = Integer.MAX_VALUE
endTime = Integer.MIN_VALUE
Scanner scanner = new Scanner(System.in)
//("A"或者"D"或者"E", int, int)
while(scanner.hasNext("\\((A|D|E),(\\d+),(\\d+)\\)")){
scanner.next("\\((A|D|E),(\\d+),(\\d+)\\)")
MatchResult r = scanner.match()
Car car
//如果输入A
if (r.group(1).equalsIgnoreCase("A")){
//该车已经记录过
if (carMap.keySet().contains(Integer.parseInt(r.group(2)))){
// 取出来设置到达时间
car = carMap.get(Integer.parseInt(r.group(2)))
car.arrive = Integer.parseInt(r.group(3))
}else{
// 否则就记录该车
car = new Car(Integer.parseInt(r.group(2)), Integer.parseInt(r.group(3)))
carMap.put(car.no, car)
}
if (car.arrive <startTime) startTime = car.arrive
if (car.leave >endTime) endTime = car.leave
//出库时间和到达时间同样处理
}else if (r.group(1).equalsIgnoreCase("D")){
if (carMap.keySet().contains(Integer.parseInt(r.group(2)))){
car = carMap.get(Integer.parseInt(r.group(2)))
car.leave = Integer.parseInt(r.group(3))
}else{
car = new Car(Integer.parseInt(r.group(2)), 0, Integer.parseInt(r.group(3)))
carMap.put(car.no, car)
}
if (car.arrive <startTime) startTime = car.arrive
if (car.leave >endTime) endTime = car.leave
}else if (r.group(1).equalsIgnoreCase("E")){
break
}
}
// 把记录过的车做成数组并且排序
Car[] cars = new Car[carMap.size()]
cars = carMap.values().toArray(cars)
Arrays.sort(cars, new Comparator<Car>(){
// 排序顺序是到达时间>出库时间>车牌
public int compare(Car c1, Car c2) {
if (c1.arrive!=c2.arrive) return c1.arrive - c2.arrive
if (c1.leave!=c2.leave) return c1.leave - c2.leave
return c1.no - c2.no
}
})
for (int time=startTimetime<=endTimetime++){
System.out.println("TIME:" + time)
for (int k=0k<cars.lengthk++){
Car car = cars[k]
//如果有车在没有进入停车场的时候就已经到了出库时间
if (car.leave == time &&carStop.isFull() &&!carStop.contains(car)){
for (int i=tunnel.size()-1i>=0i--){
Car c = tunnel.get(i)
if (c.equals(car)){
for (int j=i+1j<tunnel.size()j++){
System.out.println(car + "为" + car + "让路,重新进入等待区")
}
tunnel.remove(car)
System.out.println(car + "没进入过停车场就离开了")
}else{
System.out.println(car + "为" + car + "让路")
}
}
}else{
// 如果有车子现在到达
if (car.arrive == time){
// 停车场不满
if (!carStop.isFull()) {
// 进入停车场
carStop.push(car)
// 开始计费
car.chargeStart = time
System.out.println(car + "进入停车场并且开始计费")
}else{
// 停车场满,等待
System.out.println(car + "到达,在等待区等待")
tunnel.push(car)
}
}
}
}
//deal with cars in stop
//the case cars leave at same time is not included
// 按照后进先出的顺序看有没有车要离开
for (int k=carStop.size() - 1k>=0k--){
Car car = carStop.elementAt(k)
//准备离开
if (car.leave == time){
Car otherCar
// 所有在他后面进来的车准备让路
while ((otherCar = carStop.pop())!=car){
// 进入等待区的最前面
tunnel.unshift(otherCar)
System.out.println(otherCar + "准备为" + car + "让路")
}
for (int m=tunnel.size()-1m>=0m--){
System.out.println(tunnel.elementAt(m) + "为" + car + "让路")
}
System.out.println(otherCar + "离开,停车时间:" + (otherCar.leave - otherCar.chargeStart))
for (int m=0m<tunnel.size()m++){
System.out.println(tunnel.elementAt(m) + "让路完毕,重新进入等待区")
}
Car waitingCar
//停车场有空位,等待序列最前面的车入库
while ( !carStop.isFull() &&(waitingCar = tunnel.shift())!=null ){
carStop.push(waitingCar)
// 停车计时开始
if (waitingCar.chargeStart == -1) {
System.out.println(waitingCar + "停车计时时间改为:" + time)
waitingCar.chargeStart = time
}
System.out.println(waitingCar + "进入停车场")
}
}
}
}
}
public static void main(String[] args){
new Test().test()
}
}
@SuppressWarnings("serial")
class CarTunnel extends Vector<Car>{
public CarTunnel(){
super()
}
public Car shift(){
if (size() == 0) return null
return remove(0)
}
public void unshift(Car car){
super.add(0, car)
}
public void push(Car car){
super.add(car)
}
public Car pop(){
if (size() == 0) return null
return remove(size()-1)
}
}
@SuppressWarnings("serial")
class CarStop extends Stack<Car>{
private int size
public CarStop(int size){
this.size = size
}
public boolean isFull(){
return size() == size
}
public Car pop(){
return super.pop()
}
public Car push(Car car){
if (size() <= size){
return super.push(car)
}else{
return null
}
}
}
class Car{
public int no
public int arrive
public int leave
public int chargeStart = -1
public Car(int no, int timeIn, int timeOut){
this.no = no
this.arrive = timeIn
this.leave = timeOut
}
public Car(int no, int timeIn){
this(no, timeIn, -1)
}
public String toString(){
return String.format("Car(%d)", no)
}
}
结果:
(A,6,31)
(A,5,30)
(A,4,20)
(A,3,16)
(A,2,15)
(A,1,10)
(D,1,50)
(D,2,30)
(D,3,31)
(D,4,25)
(D,5,32)
(D,6,40)
(E,0,0)
TIME:10
Car(1)进入停车场并且开始计费
TIME:11
TIME:12
TIME:13
TIME:14
TIME:15
Car(2)进入停车场并且开始计费
TIME:16
Car(3)进入停车场并且开始计费
TIME:17
TIME:18
TIME:19
TIME:20
Car(4)到达,在等待区等待
TIME:21
TIME:22
TIME:23
TIME:24
TIME:25
Car(4)没进入过停车场就离开了
TIME:26
TIME:27
TIME:28
TIME:29
TIME:30
Car(5)到达,在等待区等待
Car(3)准备为Car(2)让路
Car(5)为Car(2)让路
Car(3)为Car(2)让路
Car(2)离开,停车时间:15
Car(3)让路完毕,重新进入等待区
Car(5)让路完毕,重新进入等待区
Car(3)进入停车场
Car(5)停车计时时间改为:30
Car(5)进入停车场
TIME:31
Car(6)到达,在等待区等待
Car(5)准备为Car(3)让路
Car(6)为Car(3)让路
Car(5)为Car(3)让路
Car(3)离开,停车时间:15
Car(5)让路完毕,重新进入等待区
Car(6)让路完毕,重新进入等待区
Car(5)进入停车场
Car(6)停车计时时间改为:31
Car(6)进入停车场
TIME:32
Car(6)准备为Car(5)让路
Car(6)为Car(5)让路
Car(5)离开,停车时间:2
Car(6)让路完毕,重新进入等待区
Car(6)进入停车场
TIME:33
TIME:34
TIME:35
TIME:36
TIME:37
TIME:38
TIME:39
TIME:40
Car(6)离开,停车时间:9
TIME:41
TIME:42
TIME:43
TIME:44
TIME:45
TIME:46
TIME:47
TIME:48
TIME:49
TIME:50
Car(1)离开,停车时间:40
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)