停车计费小系统

面向对象设计问题:

要求:1 有一个停车场,有N个车位,初始化为空,每个车位可以停一辆车

     2.停车前检查是否有空位,是否车牌号已停在停车场内,没有则可以

     3.车入车位开始计时,离开时则停止计时,算出停车费。

 

import java.util.*;

class Car{

    public String carNum; //车牌号
    public int inHour;
    public int inMinute;
    public int inSecond;
    public int outHour;
    public int outMinute;
    public int outSecond;
    
    Car(String num){
        this.carNum = num;
        this.inHour = 0;
        this.inMinute = 0;
        this.outHour = 0;
        this.outMinute = 0;
        this.inSecond = 0;
        this.outSecond = 0;
    }
    
    Car(){
        this.carNum = "";
        this.inHour = 0;
        this.inMinute = 0;
        this.outHour = 0;
        this.outMinute = 0;
        this.inSecond = 0;
        this.outSecond = 0;
    }
    
    void setInTime(int h, int m, int s){
        this.inHour = h;
        this.inMinute = m;
        this.inSecond = s;
    }
    void setOutTime(int h, int m, int s){
        this.outHour = h;
        this.outMinute = m;
        this.outSecond = s;
    }
    
    public int parkTime(){
        int time = (this.outHour * 3600 + this.outMinute * 60 + this.outSecond) - 
                   (this.inHour *3600 + this.inMinute * 60 + this.inSecond);
        if(time < 0)
            return 0;
        else
            return time;
    }
    
}

class ParkSpace{
    public Car[] park;  //停车场停车位多少
    public Integer[] isPark; //判断停车位是否已停车
    public int last;   //剩余空车位
    public int fee;   //停车花费
    
    ParkSpace(int len, int fee){
        this.park = new Car[len];
        this.isPark = new Integer[len];
        for(int i = 0; i < isPark.length; i++)
            isPark[i] = 0;
        this.last = len;
        this.fee = fee;        
    }
    
    public boolean isHaveSpace(){
        if(last < 1)
            return false;
        else
            return true;
    }
    
    public int park(String num){
        //寻找空车位
        int i;
        if(isPark(num))
            return 0;
        Car newcar = new Car(num);
        Date now = new Date();
        
        for(i = 0; i < isPark.length && isPark[i] != 0; i++) ;
        
        this.park[i] = newcar;
        this.isPark[i] = 1;
        this.last--;
        
        newcar.setInTime(now.getHours(),now.getMinutes(),now.getSeconds());
        
        System.out.println("停车位置为:" + i +" 车位");
        System.out.println("停车时间为:" + park[i].inHour+"-"+park[i].inMinute+"-"+park[i].inSecond);
        
        return 1;
        
    }
    
    public int leave(String num){
        int i;
        int cost;
        int time;
        Date now = new Date();
    
        for(i = 0; i < park.length; i++){
            if(num.equals(park[i].carNum))
                break;
        }
        
        park[i].setOutTime(now.getHours(),now.getMinutes(),now.getSeconds());
        //System.out.println("outHour:"+park[i].outHour+",parkTime:"+park[i].parkTime());
        time = park[i].parkTime();
        cost = this.fee * time;
        
        System.out.println("离开时间为:" + park[i].outHour + "-" + park[i].outMinute+"-"+park[i].outSecond+"停车时间为:"+time+"秒");
        System.out.println("空出车位位置:"+ i +"车位");
        
        park[i] = null;
        isPark[i] = 0;
        this.last++;
        
        return cost;
    }
    
    public boolean isPark(String num){
        for(int i = 0; i < isPark.length; i++){
            if(isPark[i] != 0){
                if(num.equals(park[i].carNum))
                    return true;
            }
        }
        return false;
    }
    public void status(){
        System.out.println("remain "+this.last+" park space.\n The status :");
        for(int i = 0; i < isPark.length; i++){
            if(isPark[i] == 0){
                System.out.print(" " + i);
            } else {
                System.out.print(" " +park[i].carNum);
            }
        }
        
        System.out.println("");
        
    }
    
    
}

public class Dd11_04{
    public static void main(String[] args){
        ParkSpace ps = new ParkSpace(10, 2);
        Scanner sc = new Scanner(System.in);
        int n = 0;
        int cost = 0;
        String num = "";
        
        while(n != 5){
            System.out.println("                                            ");
            System.out.println("             欢迎进入停车场系统             ");
            System.out.println("*********************************************");
            System.out.println("*   本停车场每秒收费"+ps.fee+"元 停车场内仍有"+ps.last+"个车位   *");
            System.out.println("*********************************************");
            System.out.println("        1:开进");
            System.out.println("        2:开出");
            System.out.println("        3:车位情况");
            System.out.println("        4:查询是否在停车场内");            
            System.out.println("        5:退出");   //+++++++++++++++++++++++++++
            System.out.print("请选择:");
            
            n = sc.nextInt();
            
            if( n == 1){
                System.out.println("请输入车牌号:");
                num = sc.next();
                if(ps.isHaveSpace()){
                    if(ps.park(num) == 0)
                        System.out.println("该车牌号已在停车场系统中登记,请检查!");
                } else {
                    System.out.println("已无停车位!");
                }
                
            }    
            else if( n == 2){
                System.out.println("请输入车牌号:");
                num = sc.next();
                cost = ps.leave(num);
                if(cost > 0){
                    System.out.println("停车花费为:"+cost+"元!");
                }
            }
            else if(n == 3){
                ps.status();
            }
            else if(n == 4){
                System.out.println("请输入车牌号:");
                num = sc.next();
                if(ps.isPark(num)){
                    System.out.println("已停在停车场内");
                } else {
                    System.out.println("未停在停车场内");
                }
                
            }
        
        
        
        }
    }
}

 

posted @ 2013-04-06 21:48  wannianma  阅读(722)  评论(0编辑  收藏  举报