-
Notifications
You must be signed in to change notification settings - Fork 0
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
base: main
Are you sure you want to change the base?
Conversation
Added XC Playground file for Exercises 1-3
There was a problem hiding this 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() |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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? |
There was a problem hiding this comment.
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)") | ||
} | ||
} | ||
|
There was a problem hiding this comment.
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)) } |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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?
Added XC Playground file for Exercises 1-3