Skip to content

Conversation

@sookim-1
Copy link
Contributor

@sookim-1 sookim-1 commented Feb 4, 2025

  • Read CONTRIBUTING.md
  • Only edited files inside source folder (IMPORTANT) and commited them with a meaningful message
  • Ran generate-playground.sh, no errors
  • Opened playground, it worked fine
  • Did not commit the changes caused by generate-playground.sh
  • Linked to and/or created issue
  • Added a description to PR

📌 Summary

This PR enhances the flexibility of the ATM class by replacing its fixed currency properties (hundred, fifty, twenty, ten) with a dynamic moneyPileList array.

🔍 Problem

  • The original ATM implementation had hardcoded properties for each denomination (hundred, fifty, twenty, ten).
  • Adding a new denomination required modifying the ATM class.
  • The withdrawal process was fixed to start from hundred, making it difficult to change the order dynamically.

✅ Solution

  • Introduced a moneyPileList array to hold MoneyPile instances.
  • ATM now starts the withdrawal process from the first element of moneyPileList, allowing for a flexible chain configuration.
  • Now, adding new denominations or modifying the withdrawal order requires no changes to the ATM class, improving scalability.

🛠 Changes

  • Replaced fixed hundred, fifty, twenty, ten properties with moneyPileList: [Withdrawing].
  • The withdrawal process starts from moneyPileList.first!, making it adaptable.

🔬 Example Usage

let five = MoneyPile(value: 5, quantity: 10, next: nil)
let ten = MoneyPile(value: 10, quantity: 6, next: five)
let twenty = MoneyPile(value: 20, quantity: 2, next: ten)
let atm = ATM(moneyPileList: [twenty, ten, five]) // Customizable order!
atm.withdraw(amount: 25) // Can withdraw using 1x20 + 1x5

🚀 Benefits

  • More maintainable: No need to modify ATM for new denominations.
  • More flexible: Users can configure withdrawal order dynamically.
  • Backward-compatible: Works with existing MoneyPile implementation.

Would love to hear any feedback! 😊


private var startPile: Withdrawing {
return self.hundred
return self.moneyPiles.first!
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I appreciate your contribution. Force unwrap is not allowed, we are trying to teach folks good architecture, small infractions like that may give wrong impression that such code is acceptable, it's not.


private var startPile: Withdrawing {
return self.hundred
return self.moneyPiles.first!
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another force unwrap. Backwards compatibility is not worth it imho

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants