Group Group Group Group Group Group Group Group Group

Chapter 2 Challenge Solutions missing?

I couldn’t find the solutions for this chapter, so I don’t really know if I am correct with the solution, so I did this with the challenge about the acceptCashPayment

These are my tests:

func testAcceptCashPayment_subtractsPaymentFromTransactionTotal() {
    // given
    let cashPayment = Decimal(42)
    let expectedTotal = Decimal(0)

    // when
    sut.addItem(itemCost)
    sut.acceptCashPayment(cashPayment)

    // then
    XCTAssertEqual(sut.transactionTotal, expectedTotal)
  }

func testAcceptCashPayment_addsPaymentToAvailableFunds() {
    // given
    let cashPayment = Decimal(100)
    let expectedAvailableFunds = sut.availableFunds + cashPayment

    // when
    sut.acceptCashPayment(cashPayment)

    // then
    XCTAssertEqual(sut.availableFunds, expectedAvailableFunds)
  }

The truth is that I cheat looking the name of the test methods, but I did the logic, well I tried to do it by myself.

This is my production code

func acceptCashPayment(_ cash: Decimal) {
    if transactionTotal > cash {
      transactionTotal -= cash
    } else {
      transactionTotal = 0
      availableFunds += cash - transactionTotal
    }
  }

This was the result after the two methods, I hope somebody could help me to know if this is correct.

Regards, and now happy testing hehe.

@fdorado985 Thank you for sharing your solution - much appreciated!