|
Hi all, I am curious to know how you would translate my challenge into code - I am a (very) new developer so please have patience with me :) Currently, I am working on a small tool to support product managers in estimating the cost of their products, with the following logic:
I am currently working with the two classes Product, Component, and Raw material. However, I have difficulty in understanding how to make a component from other components. Could you help kick-start my mental challenge to get past my brain-block? :) Thank you. |
|
@blokhus has a brainblok:
From some of your past posts, it looks like you've worked your way through the 100 Days of SwiftUI course? Data ModelingYou describe a classic parent-child relationship in your product-component-material use case. That is, a product is made from one or more components, your components are made from one or more materials. In a Swift data model you'd represent the product as a class with parameters that describe a single product.
Note the contents of the LunchBasket may be empty! This is just a template. But I think you can figure this out? If you need a refresher, please review the Moonshot project. It implements one-to-many relationships with astronauts. One mission has three astronauts. Review the Mission struct to see how that was implemented. Components made from Material, or other ComponentsThis is tricky. What do components and materials have in common? Think about that and then review @twoStraw's lessons on class inheritance. Perhaps you can have both the components and the material inherit from a common class? They may implement some common parameters, perhaps some required methods. The key difference, of course, is that the Components class may consist of other Components! Then consider how your LunchBasket struct might implement an array of the parent class. I think there were some exercises with cats and dogs inheriting from a common Animal class? Go back to your Checkpoint 7 code in Playgrounds. Can you create an array containing Persian, Lion, Corgi, and Poodle? They are all subclasses of Animal. See -> Cats and Dogs Keep Coding!Please return here and share you Product, Component, and Material models with us! Also, keep asking questions. |
|
Paste this simple example into Playgrounds and noodle over the logic.
Keep CodingAs a homework assignement, please add tomatoes to the sandwich. For extra credit, add another complex food item to the sandwich. Let us know how you solved your problem. Share your knowledge. |
SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!
Sponsor Hacking with Swift and reach the world's largest Swift community!
You need to create an account or log in to reply.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.