Skip to content

Homework Submission for OOP & POP Programming #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jdelacuesta
Copy link
Owner

Added XC Playground file for Exercises 1-3

Added XC Playground file for Exercises 1-3
@jdelacuesta jdelacuesta self-assigned this Mar 4, 2025
Copy link
Collaborator

@ericjenkinson ericjenkinson left a comment

Choose a reason for hiding this comment

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

Excellent work! I find it great that you are taking the time to experiment and add to the exercise.

Meets expectations!

}

// Calling the main function
main()
Copy link
Collaborator

Choose a reason for hiding this comment

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

Many languages require some form of entry point to be defined that instructs the compiler where execution should start. That is not the case with Swift, especially in Playgrounds. In Playground execution, always start at the top of the file and continue line by line until the end of the file. Lines 27 through 35 do not need to be wrapped in a function. This applies to the "main functions" below as well.

}
}

//MARK: - DiscountStrategy Protocol and Implementations
Copy link
Collaborator

Choose a reason for hiding this comment

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

I like seeing that you are experimenting with the concepts you are learning.


class ShoppingCartSingleton {
// Static property for the singleton instance
private static var instance: ShoppingCartSingleton?
Copy link
Collaborator

Choose a reason for hiding this comment

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

If you are using Xcode 16, you may have received the error: Static property 'instance' is not concurrency-safe because it is nonisolated global shared mutable state. Swift 6 introduced strict concurrency checking so this error will not be present on an older version of Xcode.

With this error, the compiler is trying to communicate that Static mutable properties are generally unsafe because they can be concurrently modified from any thread/actor. Will go over concurrency later in the course, for now since instance is only accessed by the ShoppingCartSingleton class we can add nonisolated(unsafe) to the definition.

private static nonisolated(unsafe) var instance: ShoppingCartSingleton?

print("Remaining cash in register: $\(cashInRegister)")
}
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

You missed adding the code to try processing payments with different processors and handle potential errors using try-catch blocks.


// Method to calculate the total price
func getTotalPrice() -> Double {
let subtotal = products.reduce(0.0) { $0 + ($1.price * Double($1.quantity)) }
Copy link
Collaborator

Choose a reason for hiding this comment

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

Excellent use of higher order methods!

}

let subtotal = products.reduce(0.0) { $0 + ($1.price * Double($1.quantity)) }
let discount = discountStrategy.calculateDiscount(total: subtotal)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could you use the getTotalPrice() method instead?

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