Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
3cb2ec7
Initial Test for emptyReturnsTotalEntered()
kmbosworth321 Sep 19, 2018
24ebbc3
Typo fix
kmbosworth321 Sep 19, 2018
91c6dfb
Add Empty() Method
kmbosworth321 Sep 19, 2018
b676e0d
Add cancelDoesNotAddFromEmpty()
kmbosworth321 Sep 19, 2018
68bca30
Another typo fix
kmbosworth321 Sep 19, 2018
3d6299e
Remove redundant access modifiers in interfaces
bnguyen527 Sep 22, 2018
911fac0
Change return type of 'cancel'
bnguyen527 Sep 22, 2018
2d25719
Refactor to add new property insertedMap
bnguyen527 Sep 22, 2018
59c9843
Modify shouldClearAfterCancel and fail test
bnguyen527 Sep 22, 2018
272f5d1
Pass shouldClearAfterCancel test
bnguyen527 Sep 22, 2018
1c05add
Add cancelShouldReturnMapContainingOneCoin test
bnguyen527 Sep 22, 2018
cc41ea0
Pass cancelShouldReturnMapContainingOneCoin test
bnguyen527 Sep 22, 2018
5a35f24
Add documentation for cancelShouldReturnMapContainingOneCoin
bnguyen527 Sep 22, 2018
5e50911
Add and pass cancelShouldReturnMapContainingMixtureOfCoins
bnguyen527 Sep 22, 2018
8476c13
Add and pass cancelShouldReturnMapNotContainingKeyNotEntered
bnguyen527 Sep 22, 2018
584dfb0
Refactor shouldClearAfterCancel
bnguyen527 Sep 22, 2018
163d252
Rewrite Empty() Tests
kmbosworth321 Sep 23, 2018
936fbba
Edit Empty() in Impl to pass tests
kmbosworth321 Sep 23, 2018
2e57cc9
Add final test for empty()
kmbosworth321 Sep 23, 2018
124cfb2
Make totalCollected reset after empty()
kmbosworth321 Sep 23, 2018
a676d8f
Add shouldClearMapAfterBuy() Test
kmbosworth321 Sep 23, 2018
6c40c53
Make shouldClearMapAfterBuy() test pass
kmbosworth321 Sep 23, 2018
ed49fb5
Create abstract class RateStrategy with calculateTime abstract method.
tuf65651 Sep 26, 2018
9d3edfd
Wrote JUnit setup to test existence of Linear Rate Strategy.
tuf65651 Sep 30, 2018
b1c5ca0
Wrote JUnit setup to test existence of Progressive Rate Strategy.
tuf65651 Oct 4, 2018
0474766
Dummy setRateStrategy() method
tuf65651 Oct 5, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 31 additions & 9 deletions src/paystation/domain/PayStation.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
*
* Responsibilities:
*
* 1) Accept payment;
* 2) Calculate parking time based on payment;
* 3) Know earning, parking time bought;
* 4) Issue receipts;
* 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
Expand All @@ -19,6 +19,8 @@
*/
package paystation.domain;

import java.util.Map;

public interface PayStation {

/**
Expand All @@ -28,26 +30,46 @@ public interface PayStation {
* 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;
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();
int readDisplay();

/**
* 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();
Receipt buy();

/**
* Cancel the present transaction. Resets the paystation for a new transaction.
* @return A Map defining the coins returned to the user.
* The key is the coin type and the associated value is the number of these coins that are
* returned.
* The Map object is never null even if no coins are returned.
* The Map will only contain only keys for coins to be returned.
* The Map will be cleared after a cancel or buy.
*/
Map<Integer, Integer> cancel();

/**
* Cancel the present transaction. Resets the machine for a new transaction.
* Return the total amount of money collected since the last call and empties
* it, setting the total to zero. Note: Money only collected after call to Buy
*/
public void cancel();
int empty();

/**
* Dummy header for setting current rate strategy of paystation instance.
* Resolve clashes by overrulling this change. Header is to make tests more coherent.
* @param newStrategy enum indicating selected strategy.
*/
void setRateStrategy(Strategy newStrategy);

}
47 changes: 40 additions & 7 deletions src/paystation/domain/PayStationImpl.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package paystation.domain;

import java.util.HashMap;
import java.util.Map;

/**
* 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;
* 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
Expand All @@ -20,9 +23,18 @@
* purposes. For any commercial use, see http://www.baerbak.com/
*/
public class PayStationImpl implements PayStation {

private int insertedSoFar;
private HashMap<Integer, Integer> insertedMap; /* map recording coin types and quantities */
private int timeBought;
private int totalCollected; /* Stores the total amount (in cents) collected by paystation */

public PayStationImpl() {
insertedSoFar = 0;
insertedMap = new HashMap<>();
timeBought = 0;
totalCollected = 0;
}

@Override
public void addPayment(int coinValue)
Expand All @@ -35,6 +47,10 @@ public void addPayment(int coinValue)
throw new IllegalCoinException("Invalid coin: " + coinValue);
}
insertedSoFar += coinValue;
if (insertedMap.containsKey(coinValue))
insertedMap.put(coinValue, insertedMap.get(coinValue) + 1);
else
insertedMap.put(coinValue, 1);
timeBought = insertedSoFar / 5 * 2;
}

Expand All @@ -46,16 +62,33 @@ public int readDisplay() {
@Override
public Receipt buy() {
Receipt r = new ReceiptImpl(timeBought);
totalCollected += insertedSoFar; /* Each buy action accrues more total money collected */
reset();
return r;
}

@Override
public void cancel() {
public Map<Integer, Integer> cancel() {
HashMap<Integer, Integer> returnedMap = (HashMap<Integer, Integer>) insertedMap.clone();
reset();
return returnedMap;
}

private void reset() {
timeBought = insertedSoFar = 0;
insertedMap.clear();
}

@Override
/* Returns total money collected in the paystation since the last call to empty, and resets total */
public int empty() {
int retval = totalCollected;
totalCollected = 0;
return retval;
}

@Override
public void setRateStrategy(Strategy newStrategy) {
System.err.println("set rate strategy unimplemented");
}
}
6 changes: 6 additions & 0 deletions src/paystation/domain/RateStrategy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package paystation.domain;

public abstract class RateStrategy {

public abstract int calculateTime(int payment);
}
2 changes: 1 addition & 1 deletion src/paystation/domain/Receipt.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ public interface Receipt {
*
* @return number of minutes parking time
*/
public int value();
int value();
}
7 changes: 7 additions & 0 deletions src/paystation/domain/Strategy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package paystation.domain;

/**
* Convenience enum for setting rate strategy in dummy setRateStrategy method.
* Resolve for merge by overrulling this commit and accepting the work of whoever took on this problem.
*/
public enum Strategy {LINEAR, PROGRESSIVE}
85 changes: 85 additions & 0 deletions test/paystation/domain/LinearRateStrategyTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@

package paystation.domain;

import org.junit.Test;
import static org.junit.Assert.*;
import static paystation.domain.Strategy.LINEAR;

import org.junit.Before;

public class LinearRateStrategyTests {
/**
* Test Cases for behavior of PayStation Using LinearRateStrategyTests
*/
PayStationImpl ps;

/**
* Tests existence of linear rate strategy in setup
*/
@Before
public void setup() {
ps = new PayStationImpl();
ps.setRateStrategy(LINEAR);
}

// @Test
// public void existsClassLinearRateStrategy() {
// rs = new LinearRateStrategy();
// }
// Don't use @Test -- test in setup

/**
* Assert that meter starts with no time on it
*/
@Test
public void shouldHaveNoTimeAfterNoMoney(){
assertEquals("Time should be zero until payment", 0, ps.readDisplay());
}

/**
* Entering 5 cents should make the display report 2 minutes parking time.
*/
@Test
public void shouldDisplay2MinFor5Cents()
throws IllegalCoinException {
ps.addPayment(5);
assertEquals("Should display 2 min for 5 cents",
2, ps.readDisplay());
}

/**
* Entering 25 cents should make the display report 10 minutes parking time.
*/
@Test
public void shouldDisplay10MinFor25Cents() throws IllegalCoinException {
ps.addPayment(25);
assertEquals("Should display 10 min for 25 cents",
10, ps.readDisplay());
}

/**
* Check that linear price model continued into second hour.
*
* Checks divergence between Alpha and Beta.
* Uses whole number of minutes to avoid complication with testing whether time is double or int.
*/
@Test
public void shouldDisplay70Minfor175Cents() throws IllegalCoinException {
for (int i = 0; i < 7; i++) {
ps.addPayment(25);
}
assertEquals("Should display 70 minutes for $1.75.", 70, ps.readDisplay());
}

/**
* Check that linear price model continues into third hour.
*/
@Test
public void shouldDisplay130MinFor310Cents() throws IllegalCoinException {
for(int i = 0; i < 13; i++) {
ps.addPayment(25);
}
assertEquals("Should display 130 minutes for $3.25", 130, ps.readDisplay());
}

}
Loading