Saturday, July 30, 2011

CQ Simulation: The Seat Class

Today, I'm going to work on a Seat class. If you remember from the description of the problem, there is a queue of people waiting for a number of computers. They get to use a computer for a length of time. That's what the Seat class will model.

I don't have as much “thinking out loud” stuff for this. It's pretty straight-forward coding, I think. Here's the class as it stands now:

public class Seat

{

private int useTime;

private bool occupied = false;

public Seat (int x)

{

useTime = x;

}

public bool Free

{

get {

return (!occupied);

}

set {

occupied = !value;

}

}

public void tick()

{

if (occupied) {

useTime--;

if (useTime [LESSTHAN] 0) useTime = 0;

}

}

public bool timeLeft()

{

if (useTime [GREATERTHAN] 0) return true;

return false;

}

#if DEBUG

// TESTING CODE

#endif

}


And tests:


# Test the Free property

$testSeat = new Seat(60)


$testSeat.Free = $true

if (!($testSeat.Free)) {"FAIL: Free should be True."}


$testSeat.Free = $false

if ($testSeat.Free) {"FAIL: Free should be False."}



# Test the tick method and the timeLeft method



$testSeat = new Seat(60)

$testSeat.Free = $true


for ($a = 0; $a -lt 100; $a++) {

$testSeat.tick()

}


if (!($testSeat.timeLeft())) { "FAIL: Tick method should not be decrementing variable."}



#test when seat occupied

$testSeat = new Seat(60)


$testSeat.Free = $false


for ($a = 0; $a -lt 59; $a++) {

$testSeat.tick()

}


if (!($testSeat.timeleft())) { "FAIL: Time left should still be above 0."}



$testSeat.tick()



if ($testSeat.timeleft()) { "FAIL: Tick method should have reduced time left to 0."}


I spoke too soon. There is some thinking I need to do.

When a user sits down, the clock is reset to allow the user the full amount of time. This means that I have to do that, to start with.

But how?

I only have one variable for time left over. This object doesn't remember how much time a user can have.

So I did some more coding and came up with this:


public class Seat

{

private int useTime;

private int timeForThisSeat;

private bool occupied = false;

public Seat (int x)

{

timeForThisSeat = x;

useTime = timeForThisSeat;

}

public bool Free

{

get {

return (!occupied);

}

set {

occupied = !value;

useTime = timeForThisSeat;

}

}

public void tick()

{

if (occupied) {

useTime--;

if (useTime [LESSTHAN] 0) useTime = 0;

}

}

public bool timeLeft()

{

if (useTime [GREATERTHAN] 0) return true;

return false;

}

#if DEBUG

// TESTING CODE

#endif

}


And now I need to test that behavior.



$testSeat = new Seat(60)

$testSeat.Free = $false


for ($a = 0; $a -lt 59; $a++) { # decrement to 1

$testSeat.tick()

}


$testsdf = $testSeat.timeLeft()

$testSeat.Free = $true

$testSeat.Free = $false

$testSeat.tick() # decrement to 0, if and only if the reset fails.


if (!($testSeat.timeLeft())) {"FAIL: Value of time left should have been reset."}


I ticked down to 1, then reset the Free value then ticked again. If the reset worked, the seat will have time left after one tick.

(I could have created a seat with 1 tick, but I like to cut and paste from other tests.)

OK, so that is the basic behavior of Seat.

Next time, I'll add Seats to the Simulation class.

No comments:

Post a Comment