SlideShare a Scribd company logo
1 of 12
Download to read offline
/** 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 implements PayStation {
public void addPayment( int coinValue )
throws IllegalCoinException {
}
public int readDisplay() {
return 2;
}
public int readDisplay2() {
return 10;
}
public Receipt buy() {
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
*/
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();
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
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);
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 {
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
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()
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());
}
}

More Related Content

Similar to The business logic of a Parking Pay Station.Responsibilities.pdf

Mobile Enterprise Applications
Mobile Enterprise ApplicationsMobile Enterprise Applications
Mobile Enterprise Applications
Jason Conger
 
The Business Case for Open Source GIS
The Business Case for Open Source GISThe Business Case for Open Source GIS
The Business Case for Open Source GIS
Joanne Cook
 
MSWD:MERN STACK WEB DEVELOPMENT COURSE CODE
MSWD:MERN STACK WEB DEVELOPMENT COURSE CODEMSWD:MERN STACK WEB DEVELOPMENT COURSE CODE
MSWD:MERN STACK WEB DEVELOPMENT COURSE CODE
annalakshmi35
 
Informatica Command Line Statements
Informatica Command Line StatementsInformatica Command Line Statements
Informatica Command Line Statements
mnsk80
 
Firefox OS, the Open Web & WebAPIs - HTML5DevConf, San Francisco
Firefox OS, the Open Web & WebAPIs - HTML5DevConf, San FranciscoFirefox OS, the Open Web & WebAPIs - HTML5DevConf, San Francisco
Firefox OS, the Open Web & WebAPIs - HTML5DevConf, San Francisco
Robert Nyman
 

Similar to The business logic of a Parking Pay Station.Responsibilities.pdf (20)

Mobile Enterprise Applications
Mobile Enterprise ApplicationsMobile Enterprise Applications
Mobile Enterprise Applications
 
Open source software for IoT – The devil’s in the details
Open source software for IoT – The devil’s in the detailsOpen source software for IoT – The devil’s in the details
Open source software for IoT – The devil’s in the details
 
ESM for Azure 6.9.1 Setup Guide
ESM for Azure 6.9.1 Setup GuideESM for Azure 6.9.1 Setup Guide
ESM for Azure 6.9.1 Setup Guide
 
Elevate Your Career with AZ-204: Mastering Azure Development
Elevate Your Career with AZ-204: Mastering Azure DevelopmentElevate Your Career with AZ-204: Mastering Azure Development
Elevate Your Career with AZ-204: Mastering Azure Development
 
Master Azure Development of AZ-204 with Certifiedumps : Accelerate Your Cloud...
Master Azure Development of AZ-204 with Certifiedumps : Accelerate Your Cloud...Master Azure Development of AZ-204 with Certifiedumps : Accelerate Your Cloud...
Master Azure Development of AZ-204 with Certifiedumps : Accelerate Your Cloud...
 
AZ-204 Exam Dumps (V41.0) - Pass Microsoft AZ-204 Exam (2024)
AZ-204 Exam Dumps (V41.0) - Pass Microsoft AZ-204 Exam (2024)AZ-204 Exam Dumps (V41.0) - Pass Microsoft AZ-204 Exam (2024)
AZ-204 Exam Dumps (V41.0) - Pass Microsoft AZ-204 Exam (2024)
 
ESM_Express_InstallGuide_6.9.0.pdf
ESM_Express_InstallGuide_6.9.0.pdfESM_Express_InstallGuide_6.9.0.pdf
ESM_Express_InstallGuide_6.9.0.pdf
 
Usint Charles Proxy to understand REST
Usint Charles Proxy to understand RESTUsint Charles Proxy to understand REST
Usint Charles Proxy to understand REST
 
ArcSight Connector Appliance 6.4 Release Notes
ArcSight Connector Appliance 6.4 Release NotesArcSight Connector Appliance 6.4 Release Notes
ArcSight Connector Appliance 6.4 Release Notes
 
The Business Case for Open Source GIS
The Business Case for Open Source GISThe Business Case for Open Source GIS
The Business Case for Open Source GIS
 
Readme
ReadmeReadme
Readme
 
Manual Sophos
Manual SophosManual Sophos
Manual Sophos
 
MSWD:MERN STACK WEB DEVELOPMENT COURSE CODE
MSWD:MERN STACK WEB DEVELOPMENT COURSE CODEMSWD:MERN STACK WEB DEVELOPMENT COURSE CODE
MSWD:MERN STACK WEB DEVELOPMENT COURSE CODE
 
Informatica Command Line Statements
Informatica Command Line StatementsInformatica Command Line Statements
Informatica Command Line Statements
 
Master cam x2 5ax
Master cam x2 5axMaster cam x2 5ax
Master cam x2 5ax
 
Firefox OS, the Open Web & WebAPIs - HTML5DevConf, San Francisco
Firefox OS, the Open Web & WebAPIs - HTML5DevConf, San FranciscoFirefox OS, the Open Web & WebAPIs - HTML5DevConf, San Francisco
Firefox OS, the Open Web & WebAPIs - HTML5DevConf, San Francisco
 
Protect your website
Protect your websiteProtect your website
Protect your website
 
ESM Service Layer Developers Guide for ESM 6.8c
ESM Service Layer Developers Guide for ESM 6.8cESM Service Layer Developers Guide for ESM 6.8c
ESM Service Layer Developers Guide for ESM 6.8c
 
Web application penetration testing lab setup guide
Web application penetration testing lab setup guideWeb application penetration testing lab setup guide
Web application penetration testing lab setup guide
 
AWS Container Services – 유재석 (AWS 솔루션즈 아키텍트)
AWS Container Services – 유재석 (AWS 솔루션즈 아키텍트)AWS Container Services – 유재석 (AWS 솔루션즈 아키텍트)
AWS Container Services – 유재석 (AWS 솔루션즈 아키텍트)
 

More from architcreation

Let K be a (p, q)-torus knot sitting on a torus that is the boundary .pdf
Let K be a (p, q)-torus knot sitting on a torus that is the boundary .pdfLet K be a (p, q)-torus knot sitting on a torus that is the boundary .pdf
Let K be a (p, q)-torus knot sitting on a torus that is the boundary .pdf
architcreation
 
How did Charles Lyells ideas contribute to Charles Darwins think.pdf
How did Charles Lyells ideas contribute to Charles Darwins think.pdfHow did Charles Lyells ideas contribute to Charles Darwins think.pdf
How did Charles Lyells ideas contribute to Charles Darwins think.pdf
architcreation
 
Heating and cooling loads very with ____________.List three method.pdf
Heating and cooling loads very with ____________.List three method.pdfHeating and cooling loads very with ____________.List three method.pdf
Heating and cooling loads very with ____________.List three method.pdf
architcreation
 
Compare organelles in both plant and animal cells. Identify the stru.pdf
Compare organelles in both plant and animal cells. Identify the stru.pdfCompare organelles in both plant and animal cells. Identify the stru.pdf
Compare organelles in both plant and animal cells. Identify the stru.pdf
architcreation
 
What are TEs Comment on Are there any distinctive features Why might.pdf
What are TEs Comment on Are there any distinctive features Why might.pdfWhat are TEs Comment on Are there any distinctive features Why might.pdf
What are TEs Comment on Are there any distinctive features Why might.pdf
architcreation
 
SAY. Write your answer in the space provided or on a separate sheet o.pdf
SAY. Write your answer in the space provided or on a separate sheet o.pdfSAY. Write your answer in the space provided or on a separate sheet o.pdf
SAY. Write your answer in the space provided or on a separate sheet o.pdf
architcreation
 

More from architcreation (20)

Mass-spring problem. A mass is suspended from a spring. The mass is p.pdf
Mass-spring problem. A mass is suspended from a spring. The mass is p.pdfMass-spring problem. A mass is suspended from a spring. The mass is p.pdf
Mass-spring problem. A mass is suspended from a spring. The mass is p.pdf
 
List three ways the epinephrine signaling pathway, phosphoinositide .pdf
List three ways the epinephrine signaling pathway, phosphoinositide .pdfList three ways the epinephrine signaling pathway, phosphoinositide .pdf
List three ways the epinephrine signaling pathway, phosphoinositide .pdf
 
Let K be a (p, q)-torus knot sitting on a torus that is the boundary .pdf
Let K be a (p, q)-torus knot sitting on a torus that is the boundary .pdfLet K be a (p, q)-torus knot sitting on a torus that is the boundary .pdf
Let K be a (p, q)-torus knot sitting on a torus that is the boundary .pdf
 
In your own words, explain why shift registers are required to inter.pdf
In your own words, explain why shift registers are required to inter.pdfIn your own words, explain why shift registers are required to inter.pdf
In your own words, explain why shift registers are required to inter.pdf
 
In one or two paragraphs, explain the difference between a suspensio.pdf
In one or two paragraphs, explain the difference between a suspensio.pdfIn one or two paragraphs, explain the difference between a suspensio.pdf
In one or two paragraphs, explain the difference between a suspensio.pdf
 
How did Charles Lyells ideas contribute to Charles Darwins think.pdf
How did Charles Lyells ideas contribute to Charles Darwins think.pdfHow did Charles Lyells ideas contribute to Charles Darwins think.pdf
How did Charles Lyells ideas contribute to Charles Darwins think.pdf
 
Heating and cooling loads very with ____________.List three method.pdf
Heating and cooling loads very with ____________.List three method.pdfHeating and cooling loads very with ____________.List three method.pdf
Heating and cooling loads very with ____________.List three method.pdf
 
Gather information about the latest developments in IIOP and CSI.pdf
Gather information about the latest developments in IIOP and CSI.pdfGather information about the latest developments in IIOP and CSI.pdf
Gather information about the latest developments in IIOP and CSI.pdf
 
Explain microscope in simplest termsDefinition and significance of.pdf
Explain microscope in simplest termsDefinition and significance of.pdfExplain microscope in simplest termsDefinition and significance of.pdf
Explain microscope in simplest termsDefinition and significance of.pdf
 
Find the conditional probability of the indicated event when two fai.pdf
Find the conditional probability of the indicated event when two fai.pdfFind the conditional probability of the indicated event when two fai.pdf
Find the conditional probability of the indicated event when two fai.pdf
 
eukaryotic cell or prokaryotic cellSolutionProkaryotic cells a.pdf
eukaryotic cell or prokaryotic cellSolutionProkaryotic cells a.pdfeukaryotic cell or prokaryotic cellSolutionProkaryotic cells a.pdf
eukaryotic cell or prokaryotic cellSolutionProkaryotic cells a.pdf
 
Describe how to make 10ml of a 125 dilution from a 1 times 10^12 cel.pdf
Describe how to make 10ml of a 125 dilution from a 1 times 10^12 cel.pdfDescribe how to make 10ml of a 125 dilution from a 1 times 10^12 cel.pdf
Describe how to make 10ml of a 125 dilution from a 1 times 10^12 cel.pdf
 
Compare organelles in both plant and animal cells. Identify the stru.pdf
Compare organelles in both plant and animal cells. Identify the stru.pdfCompare organelles in both plant and animal cells. Identify the stru.pdf
Compare organelles in both plant and animal cells. Identify the stru.pdf
 
Soap molecules have a hydrophilic head and a hydrophobic tail. What .pdf
Soap molecules have a hydrophilic head and a hydrophobic tail. What .pdfSoap molecules have a hydrophilic head and a hydrophobic tail. What .pdf
Soap molecules have a hydrophilic head and a hydrophobic tail. What .pdf
 
What are TEs Comment on Are there any distinctive features Why might.pdf
What are TEs Comment on Are there any distinctive features Why might.pdfWhat are TEs Comment on Are there any distinctive features Why might.pdf
What are TEs Comment on Are there any distinctive features Why might.pdf
 
Which myth of motivation is the most important Are there other myth.pdf
Which myth of motivation is the most important Are there other myth.pdfWhich myth of motivation is the most important Are there other myth.pdf
Which myth of motivation is the most important Are there other myth.pdf
 
What is an interface How is extending a class different from implem.pdf
What is an interface How is extending a class different from implem.pdfWhat is an interface How is extending a class different from implem.pdf
What is an interface How is extending a class different from implem.pdf
 
What is the probability that Bo, Colleen, Jeff, and Rohini win the f.pdf
What is the probability that Bo, Colleen, Jeff, and Rohini win the f.pdfWhat is the probability that Bo, Colleen, Jeff, and Rohini win the f.pdf
What is the probability that Bo, Colleen, Jeff, and Rohini win the f.pdf
 
SAY. Write your answer in the space provided or on a separate sheet o.pdf
SAY. Write your answer in the space provided or on a separate sheet o.pdfSAY. Write your answer in the space provided or on a separate sheet o.pdf
SAY. Write your answer in the space provided or on a separate sheet o.pdf
 
What is validity What is reliability Discuss the relationship c.pdf
What is validity What is reliability Discuss the relationship c.pdfWhat is validity What is reliability Discuss the relationship c.pdf
What is validity What is reliability Discuss the relationship c.pdf
 

Recently uploaded

會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
中 央社
 
SPLICE Working Group: Reusable Code Examples
SPLICE Working Group:Reusable Code ExamplesSPLICE Working Group:Reusable Code Examples
SPLICE Working Group: Reusable Code Examples
Peter Brusilovsky
 

Recently uploaded (20)

Improved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio AppImproved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio App
 
An overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismAn overview of the various scriptures in Hinduism
An overview of the various scriptures in Hinduism
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
 
Observing-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxObserving-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptx
 
PSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptxPSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptx
 
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
 
Major project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategiesMajor project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategies
 
Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"
 
Scopus Indexed Journals 2024 - ISCOPUS Publications
Scopus Indexed Journals 2024 - ISCOPUS PublicationsScopus Indexed Journals 2024 - ISCOPUS Publications
Scopus Indexed Journals 2024 - ISCOPUS Publications
 
diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....
 
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptxAnalyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
 
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
 
8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management
 
Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptx
 
e-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopale-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopal
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
 
SPLICE Working Group: Reusable Code Examples
SPLICE Working Group:Reusable Code ExamplesSPLICE Working Group:Reusable Code Examples
SPLICE Working Group: Reusable Code Examples
 
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community PartnershipsSpring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
 
Book Review of Run For Your Life Powerpoint
Book Review of Run For Your Life PowerpointBook Review of Run For Your Life Powerpoint
Book Review of Run For Your Life Powerpoint
 

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()); }
  • 12. }