Skip to content

Instantly share code, notes, and snippets.

@kencoba
Created July 10, 2014 14:53
Show Gist options
  • Save kencoba/6ac7537c993d1802ed50 to your computer and use it in GitHub Desktop.
Save kencoba/6ac7537c993d1802ed50 to your computer and use it in GitHub Desktop.
The code after second iteration [Mame night(2014/7/10)](http://www.mamezou.com/event/mn_20140710.html)
package iteration.second;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class Scenario2 {
public static void main(String[] args) {
ParkingArea pa = new ParkingArea(2);
Parking t1 = pa.checkin(0);
System.out.println(t1);
Parking t2 = pa.checkin(1);
System.out.println(t2);
Parking t3 = pa.checkin(1);
System.out.println(t3);
t2 = pa.checkout(t2);
System.out.println(t2);
Parking t4 = pa.checkin(1);
System.out.println(t4);
ParkingArea pb = new ParkingArea(1);
Parking t5 = pb.checkin(0);
System.out.println(t5);
Parking t6 = pb.checkin(0);
System.out.println(t6);
pb.checkout(t5);
System.out.println(t5);
}
}
class ParkingArea {
int capacity;
List<ParkingArea> partitions;
boolean isFree = true;
ParkingArea(int capacity) {
this.capacity = capacity;
if (capacity > 1) {
partitions = new ArrayList<ParkingArea>();
for (int i = 0; i < capacity; i++) {
partitions.add(new ParkingArea(1));
}
}
}
Parking checkin(int partitionNumber) {
if (partitions != null) {
if (partitions.get(partitionNumber).isFree) {
partitions.get(partitionNumber).isFree = false;
return new Parking(partitionNumber, new Date());
} else {
return null;
}
} else {
if (isFree) {
this.isFree = false;
return new Parking(partitionNumber, new Date());
} else {
return null;
}
}
}
Parking checkout(Parking ticket) {
if (partitions != null) {
ParkingArea p = partitions.get(ticket.partitionNumber);
p.isFree = true;
} else {
this.isFree = true;
}
ticket.outTime = new Date();
return ticket;
}
}
class Parking {
int partitionNumber;
Date inTime;
Date outTime;
Parking(int partitionNumber, Date inTime) {
this.partitionNumber = partitionNumber;
this.inTime = inTime;
}
@Override
public String toString() {
return "partitionNumber=" + partitionNumber + ":inTime=" + inTime
+ ":outTime=" + outTime;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment