我是靠谱客的博主 受伤羊,最近开发中收集的这篇文章主要介绍CC150 OOD Parking Lot,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

Design a parking lot using object- oriented principles

https://github.com/gaylemcd/ctci/tree/master/java/Chapter%208/Question8_4

首先: 需要和面试官讨论把问题具体化, 例如 可停的汽车类型, 是否是多层的parking lot 等等。

然后做出如下假设

(1) multiple level, each level has multiple rows of spots

(2) can park motorcycles, cars and buses

(3) parking lot has motorcycle spots, compact spot, and large spot (注意停车位也分为了不同类型!!!)

(4) a motorcycle can park in any spot

(5) car park in single compact spot or large spot

(6) bus can park in five large spots that are consecutive and within same row. it can not park in small spots

然后可以开始设计

Part 1  Vehicle

枚举大小, Vehicle 设为abstract class

//enum type for Vehicle
public enum VehicleSize{
Motorcycle;
Compact;
Large;
}
//abstract Vehicle class
public abstract class Vehicle{
protected int spotsNeeded;
protected VehicleSize size;
protected String licensePlate;
// id for a vehicle
protected ArrayList<ParkingSpot> parkingSpots = new ArrayList<ParkingSpot>(); // id for parking where may occupy multi
public int getSpotsNeeded() {
return spotsNeeded;
}
public VehicleSize getSize() {
return size;
}
/* Park vehicle in this spot (among others, potentially) */
public void parkInSpot(ParkingSpot spot) {
parkingSpots.add(spot);
}
/* Remove car from spot, and notify spot that it's gone */
public void clearSpots() {
for (int i = 0; i < parkingSpots.size(); i++) {
parkingSpots.get(i).removeVehicle();
}
parkingSpots.clear();
}
//this need to be implement in subclass
public abstract boolean canFitInSpot(ParkingSpot spot);
public abstract void print();
}

然后可以通过继承实现三种类型车

public class Motorcycle extends Vehicle {
public Motorcycle() {
spotsNeeded = 1;
size = VehicleSize.Motorcycle;
}
public boolean canFitInSpot(ParkingSpot spot) {
return true;
}
public void print() {
System.out.print("M");
}
}
public class Car extends Vehicle {
public Car() {
spotsNeeded = 1;
size = VehicleSize.Compact;
}
public boolean canFitInSpot(ParkingSpot spot) {
return spot.getSize() == VehicleSize.Large || spot.getSize() == VehicleSize.Compact;
}
public void print() {
System.out.print("C");
}
}
public class Bus extends Vehicle {
public Bus() {
spotsNeeded = 5;
size = VehicleSize.Large;
}
/*********only check whether size fit***********************************/
public boolean canFitInSpot(ParkingSpot spot) {
return spot.getSize() == VehicleSize.Large;
}
public void print() {
System.out.print("B");
}
}

Part 2 Parking Lot

Parking Spot, no need to have classes for LargeSpot, CompactSpot and MotorcycleSpot inheriting from Parking Spot, cause they do not have different behavior, only different size

public class ParkingSpot{
private Vehicle vehicle;
private VehicleSize spotSize;
private int row;
private int spotNumber;
private Level level;
public ParkingSpot(Level lvl, int r, int n, VehicleSize sz) {
level = lvl;
row = r;
spotNumber = n;
spotSize = sz;
//my idea
//vehicle = null;
}
public boolean isAvailable() {
return vehicle == null;
}
/* Checks if the spot is big enough for the vehicle (and is available). This compares
* the SIZE only. It does not check if it has enough spots. */
public boolean canFitVehicle(Vehicle vehicle) {
return isAvailable() && vehicle.canFitInSpot(this);
}
/* Park vehicle in this spot. */
public boolean park(Vehicle v) {
if (!canFitVehicle(v)) {
return false;
}
vehicle = v;
vehicle.parkInSpot(this);
return true;
}
/* Remove vehicle from spot, and notify level that a new spot is available */
public void removeVehicle() {
level.spotFreed();
vehicle = null;
}
public int getRow() {
return row;
}
public int getSpotNumber() {
return spotNumber;
}
public VehicleSize getSize() {
return spotSize;
}
public void print() {
if (vehicle == null) {
if (spotSize == VehicleSize.Compact) {
System.out.print("c");
} else if (spotSize == VehicleSize.Large) {
System.out.print("l");
} else if (spotSize == VehicleSize.Motorcycle) {
System.out.print("m");
}
} else {
vehicle.print();
}
}
}

/* Represents a level in a parking garage */
public class Level{
private int floor;
private ParkingSpot[] spots;
private int availableSpots = 0; // number of free spots
private static final int SPOTS_PER_ROW = 10;
public Level(int flr, int numberSpots){
floor = flr;
spots = new ParkingSpot[numberSpots];
//divid all spots for 3 type
int largeSpots = numberSpots / 4;
int bikeSpots = numberSpots / 4;
int compactSpots = numberSpots - largeSpots - bikeSpots;
//init size for each spot in array spots
for(int i = 0; i < numberSpots; i++){
VehicleSize sz = VehicleSize.Motorcycle;
if(i < largeSpots){//1/4
sz = VehicleSize.Large;
} else if (i < largeSpots + compactSpots) {//3/4
sz = VehicleSize.Compact;
}
int row = i / SPOTS_PER_ROW;
spots[i] = new ParkingSpot(this, row, i, sz);//level
}
}
/* Try to find a place to park this vehicle. Return false if failed. */
public boolean parkVehicle(Vehicle vehicle) {
if(availableSpots() < vehicle.getSpotsNeeded()){
return false; // no enough spots
}
int spotNumber = findAvaliableSpot(vehicle);
if(spotNumber < 0){
return false;
}
return parkStartingAtSpot(spotNumber, vehicle);
}
/* find a spot to park this vehicle. Return index of spot, or -1 on failure. */
private int findAvailableSpots(Vehicle vehicle) {
int spotsNeeded = vehicle.getSpotsNeeded();
int lastRow = -1;
int spotsFound = 0;
for(int i = 0; i < spots.length; i++){
ParkingSpot spot = spots[i];
if(lastRow != spot.getRow()){
potsFound = 0;
lastRow = spot.getRow();
}
if(spot.canFitVehicle(vehicle)){
spotsFound++;
}else{
spotsFound = 0;
}
if(spotsFound == spotsNeeded){
return i - (spotsNeeded - 1); // index of spot
}
}
return -1;
}
/* Park a vehicle starting at the spot spotNumber, and continuing until vehicle.spotsNeeded. */
private boolean parkStartingAtSpot(int spotNumber, Vehicle vehicle) {
vehicle.clearSpots();
boolean success = true;
for (int i = spotNumber; i < spotNumber + vehicle.spotsNeeded; i++) {
success &= spots[i].park(vehicle);
}
availableSpots -= vehicle.spotsNeeded;
return success;
}
/* When a car was removed from the spot, increment availableSpots */
public void spotFreed() {
availableSpots++;
}
public int availableSpots() {
return availableSpots;
}
public void print() {
int lastRow = -1;
for (int i = 0; i < spots.length; i++) {
ParkingSpot spot = spots[i];
if (spot.getRow() != lastRow) {
System.out.print("
");
lastRow = spot.getRow();
}
spot.print();
}
}
}
最后完成整个parking Lot

可以看做是wrapper class for an array of levels

(or double array or hashtable(level, listofspot)


public class ParkingLot{
private Level[] levels;
private final int NUM_LEVELS = 5;
public ParkingLot() {
levels = new Level[NUM_LEVELS];
for (int i = 0; i < NUM_LEVELS; i++) {
levels[i] = new Level(i, 30);
}
}
/* Park the vehicle in a spot (or multiple spots). Return false if failed. */
public boolean parkVehicle(Vehicle vehicle) {
for (int i = 0; i < levels.length; i++) {
if (levels[i].parkVehicle(vehicle)) {
return true;
}
}
return false;
}
public void print() {
for (int i = 0; i < levels.length; i++) {
System.out.print("Level" + i + ": ");
levels[i].print();
System.out.println("");
}
System.out.println("");
}
}



 


最后

以上就是受伤羊为你收集整理的CC150 OOD Parking Lot的全部内容,希望文章能够帮你解决CC150 OOD Parking Lot所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(45)

评论列表共有 0 条评论

立即
投稿
返回
顶部