/** The business logic of a Parking Pay Station.
Responsibilities:
1) Accept payment;
2) Calculate parking time based on payment;
3) Know earning, parking time bought;
4) Issue receipts;
5) Handle buy and cancel events.
This source code is from the book
\"Flexible, Reliable Software:
Using Patterns and Agile Development\"
published 2010 by CRC Press.
Author:
Henrik B Christensen
Department of Computer Science
Aarhus University
Please visit http://www.baerbak.com/ for further information.
Licensed under the Apache License, Version 2.0 (the \"License\");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an \"AS IS\" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
public interface PayStation {
/**
* Insert coin into the pay station and adjust state accordingly.
* @param coinValue is an integer value representing the coin in
* cent. That is, a quarter is coinValue=25, etc.
* @throws IllegalCoinException in case coinValue is not
* a valid coin value
*/
public void addPayment( int coinValue ) throws IllegalCoinException;
/**
* Read the machine\'s display. The display shows a numerical
* description of the amount of parking time accumulated so far
* based on inserted payment.
* @return the number to display on the pay station display
*/
public int readDisplay();
public int readDisplay2();
/**
* Buy parking time. Terminate the ongoing transaction and
* return a parking receipt. A non-null object is always returned.
* @return a valid parking receipt object.
*/
public Receipt buy();
/**
* Cancel the present transaction. Resets the machine for a new
* transaction.
*/
public void cancel();
}
/** Implementation of the pay station.
Responsibilities:
1) Accept payment;
2) Calculate parking time based on payment;
3) Know earning, parking time bought;
4) Issue receipts;
5) Handle buy and cancel events.
This source code is from the book
\"Flexible, Reliable Software:
Using Patterns and Agile Development\"
published 2010 by CRC Press.
Author:
Henrik B Christensen
Department of Computer Science
Aarhus University
Please visit http://www.baerbak.com/ for further information.
Licensed under the Apache License, Version 2.0 (the \"License\");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an \"AS IS\" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
public class PayStationImpl imple.
The business logic of a Parking Pay Station.Responsibilities.pdf
1. /** The business logic of a Parking Pay Station.
Responsibilities:
1) Accept payment;
2) Calculate parking time based on payment;
3) Know earning, parking time bought;
4) Issue receipts;
5) Handle buy and cancel events.
This source code is from the book
"Flexible, Reliable Software:
Using Patterns and Agile Development"
published 2010 by CRC Press.
Author:
Henrik B Christensen
Department of Computer Science
Aarhus University
Please visit http://www.baerbak.com/ for further information.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
public interface PayStation {
/**
* Insert coin into the pay station and adjust state accordingly.
* @param coinValue is an integer value representing the coin in
2. * cent. That is, a quarter is coinValue=25, etc.
* @throws IllegalCoinException in case coinValue is not
* a valid coin value
*/
public void addPayment( int coinValue ) throws IllegalCoinException;
/**
* Read the machine's display. The display shows a numerical
* description of the amount of parking time accumulated so far
* based on inserted payment.
* @return the number to display on the pay station display
*/
public int readDisplay();
public int readDisplay2();
/**
* Buy parking time. Terminate the ongoing transaction and
* return a parking receipt. A non-null object is always returned.
* @return a valid parking receipt object.
*/
public Receipt buy();
/**
* Cancel the present transaction. Resets the machine for a new
* transaction.
*/
public void cancel();
}
/** Implementation of the pay station.
Responsibilities:
1) Accept payment;
2) Calculate parking time based on payment;
3) Know earning, parking time bought;
4) Issue receipts;
5) Handle buy and cancel events.
3. This source code is from the book
"Flexible, Reliable Software:
Using Patterns and Agile Development"
published 2010 by CRC Press.
Author:
Henrik B Christensen
Department of Computer Science
Aarhus University
Please visit http://www.baerbak.com/ for further information.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
public class PayStationImpl implements PayStation {
public void addPayment( int coinValue )
throws IllegalCoinException {
}
public int readDisplay() {
return 2;
}
public int readDisplay2() {
return 10;
}
public Receipt buy() {
4. return null;
}
public void cancel() {
}
}
/** The receipt returned from a pay station.
Responsibilities:
1) Know the minutes parking time the receipt represents
This source code is from the book
"Flexible, Reliable Software:
Using Patterns and Agile Development"
published 2010 by CRC Press.
Author:
Henrik B Christensen
Department of Computer Science
Aarhus University
Please visit http://www.baerbak.com/ for further information.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
public interface Receipt {
/**
* Return the number of minutes this receipt is valid for.
* @return number of minutes parking time
*/
5. public int value();
}
import org.junit.*;
import static org.junit.Assert.*;
/** Testcases for the Pay Station system.
This source code is from the book
"Flexible, Reliable Software:
Using Patterns and Agile Development"
published 2010 by CRC Press.
Author:
Henrik B Christensen
Department of Computer Science
Aarhus University
Please visit http://www.baerbak.com/ for further information.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
public class TestPayStation {
/**
* Entering 5 cents should make the display report 2 minutes
* parking time.
*/
@Test
public void shouldDisplay2MinFor5Cents() throws IllegalCoinException {
PayStation ps = new PayStationImpl();
6. ps.addPayment( 5 );
assertEquals( "Should display 2 min for 5 cents",
2, ps.readDisplay() );
}
@Test
public void shouldDisplay10MinFor25Cents() throws IllegalCoinException {
PayStation ps = new PayStationImpl();
ps.addPayment( 25 );
assertEquals( "Should display 10 min for 25 cents",
10, ps.readDisplay2() );
}
}
public class IllegalCoinException extends Exception {
public IllegalCoinException(String e) {
super(e);
}
}
Solution
Any queries please comment
import java.util.HashMap;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.Before;
public class PayStationImplTest
{
PayStationImpl ps;
public void setup()
{
ps = new PayStationImpl();
}
@Test
7. public void shouldDisplay2MinFor5Cents()
throws IllegalCoinException {
ps.addPayment(5);
assertEquals("Should display 2 min for 5 cents",
2, ps.readDisplay());
}
@Test
public void shouldDisplay10MinFor25Cents() throws IllegalCoinException {
ps.addPayment(25);
assertEquals("Should display 10 min for 25 cents",
10, ps.readDisplay());
}
@Test(expected = IllegalCoinException.class)
public void shouldRejectIllegalCoin() throws IllegalCoinException {
ps.addPayment(17);
}
@Test
public void shouldDisplay14MinFor10And25Cents()
throws IllegalCoinException
{
ps.addPayment(10);
ps.addPayment(25);
assertEquals("Should display 14 min for 10+25 cents",
14, ps.readDisplay());
}
@Test
public void shouldReturnCorrectReceiptWhenBuy()
throws IllegalCoinException
{
ps.addPayment(5);
ps.addPayment(10);
ps.addPayment(25);
Receipt receipt;
receipt = ps.buy();
assertNotNull("Receipt reference cannot be null",
receipt);
8. assertEquals("Receipt value must be 16 min.",
16, receipt.value());
}
@Test
public void shouldReturnReceiptWhenBuy100c()
throws IllegalCoinException {
ps.addPayment(10);
ps.addPayment(10);
ps.addPayment(10);
ps.addPayment(10);
ps.addPayment(10);
ps.addPayment(25);
ps.addPayment(25);
Receipt receipt;
receipt = ps.buy();
assertEquals(40, receipt.value());
}
@Test
public void shouldClearAfterBuy()
throws IllegalCoinException {
ps.addPayment(25);
ps.buy();
assertEquals("Display should have been cleared",
0, ps.readDisplay());
ps.addPayment(10);
ps.addPayment(25);
assertEquals("Next add payment should display correct time",
14, ps.readDisplay());
Receipt r = ps.buy();
assertEquals("Next buy should return valid receipt",
14, r.value());
assertEquals("Again, display should be cleared",
0, ps.readDisplay());
}
public void shouldClearAfterCancel()
throws IllegalCoinException {
9. ps.addPayment(10);
ps.cancel();
assertEquals("Cancel should clear display",
0, ps.readDisplay());
ps.addPayment(25);
assertEquals("Insert after cancel should work",
10, ps.readDisplay());
}
@Test
public void shouldReturnTotalAmountEntered()
throws IllegalCoinException{
int i = ps.empty();
ps.addPayment(25);
ps.buy();
ps.addPayment(5);
ps.buy();
i = ps.empty();
assertEquals("Should be total bought(30)", 30, i);
}
public void shouldNotAddCanceledEntry()
throws IllegalCoinException{
int i = ps.empty();
ps.addPayment(10);
ps.cancel();
i = ps.empty();
assertEquals("Should equal 0", i, 0);
}
public void shouldResetTotalAfterEmpty()
throws IllegalCoinException{
int i;
ps.addPayment(10);
ps.empty();
i = ps.empty();
assertEquals("Should eqaul 0 after empty", i, 0);
}
@Test
10. public void shouldReturnMapOneCoin()
throws IllegalCoinException{
HashMap m;
Integer five = 5;
Integer one = 1;
ps.addPayment(5);
m = ps.cancel();
Integer n = (Integer)m.get(five);
assertEquals("Should equal 1", n, one);
}
public void shouldReturnMapMix()
throws IllegalCoinException{
HashMap m;
Integer five = 5;
Integer ten = 10;
Integer tf = 25;
Integer one = 1;
Integer two = 2;
Integer three = 3;
Integer x;
ps.addPayment(5);
ps.addPayment(10);
ps.addPayment(10);
ps.addPayment(25);
ps.addPayment(25);
ps.addPayment(25);
m = ps.cancel();
x = (Integer)m.get(five);
assertEquals("should equal 1", x, one);
x = (Integer)m.get(ten);
assertEquals("should equal 2", x, two);
x = (Integer)m.get(tf);
assertEquals("should equal 3", x, three);
}
@Test
public void shouldReturnMapEmpty()
11. throws IllegalCoinException{
HashMap m;
m = ps.cancel();
assertEquals("Should be true", m.isEmpty(), true);
}
@Test
public void shouldReturnClearMap()
throws IllegalCoinException{
HashMap m;
ps.addPayment(5);
ps.cancel();
m = ps.cancel();
assertEquals("Should be true", m.isEmpty(), true);
}
@Test
public void shouldReturnClearMapBuy()
throws IllegalCoinException{
HashMap m;
ps.addPayment(5);
ps.buy();
m = ps.cancel();
assertEquals("Should be true", m.isEmpty(), true);
}
@Test
public void shouldDisplay2MinFor5CentsProgressive()
throws IllegalCoinException {
ps.addPayment(5);
assertEquals("Should display 2 min for 5 cents",
2, ps.readDisplay());
}
@Test
public void shouldDisplay10MinFor25CentsProgressive() throws IllegalCoinException {
ps.addPayment(25);
assertEquals("Should display 10 min for 25 cents",
10, ps.readDisplay());
}