Core data advise placing existing entity inside existing entity - core-data

Looking for some advice and pointers. I am creating a card app. Like top trumps there are different decks. In core data I have three entities:
Player ->> Decks ->> Cards
what I want to do is when the app loads create 30 cards. Then a player. Then the player creates a deck from 30 cards. The user can only pick 11 of these cards. So my question is, how would I assign existing card entities to the deck?

To populate a To-Many relationship, Deck to Cards in your case, you will use [deck addCardsObject:card]. This will add Card objects to the relationship such that they will be returned by deck.Cards and the deck will be returned by card.Deck. This is how you can go about associating the card objects to the deck. You may want to duplicate the needed cards from your "default deck" when creating a new deck, so your default deck holds all possible cards, and new decks can contain a subset of those cards without interfering with the original relationship of cards to the default deck.

Related

Class associations and multiplicity - UML 2.0 - object oriented

I am learning UML and have a practice question I am working on for class diagrams.
I've put together a first version of the diagram but i'm confused about part e. This is the practice question:
and this is what I have so far:
Where it says 'Each customer can store a number of debit/credit cards used for payments' does it mean that the customer then has an association with the debit and credit card subclasses? or is the credit card type stored in the customer class as an attribute?
The proposal of Thomas Kilian is not yet the complete solution. You should rename "CreditCard" to "PaymentCard" and make it a union type by partitioning it into the two disjoint subclasses "CreditCard" and "DebitCard". Give the Customer-PaymentCard association a 1-* multiplicity. Then add a many-to-one association between Payment and PaymentCard.
It's just that you need an attribute inside Customer for a number of credit card numbers. A card number is less than 2^43. So you can take a (64 bit) integer. Or use a String for that.
The dot-notation says that card is an attribute inside Customer and since it has a multiplicity not equal one it's an array (or a collection). Vice versa the CreditCard has a unique owner.

UML Diagram for program

I have to complete a program that implements a car Park system.
I started with the UML diagram as I think it is easier for the program to be done after that, but I am a bit stack.
The scenario is:
Design and implement a class Vehicle (abstract) and the subclasses Car, Van, Motorbike. The classes should include appropriate methods and hold information about the ID plate of the vehicle, the brand of the vehicle and the entry time/date in the parking.
In particular:
• The Car class should also include appropriate methods and hold information
about the number of the doors of the car and the color.
• The Van class should also include methods and information about the cargo
volume of the van.
The class Motorbike should also have methods and information about the size
engine of the motorbike.
You should implement a class DateTime to represent the time/date of the entrance of
the vehicle in the parking. Do not use any predefined library.
Design and implement a class called MyCarParkManager, which extends the
interface CarParkManager. MyCarParkManager maintains the list of the
vehicles currently in the parking.
The class should display in the console a menu from which the user can select among
the following management actions:
• Add a new vehicle in the parking if there are free lots (considering that the max number of lots is 20) and return the number of the free lots remaining. Consider that a Van occupied 2 lots. Display a message with the number of free lots or informing that there are no lots available.
• Delete a vehicle, selecting the ID plate, from the list when the vehicle leaves the car park and return the vehicle instance. Display the type of the vehicle leaving the parking (if it is a car, a van or a motorbike).
• Print the list of the vehicles currently parked. For each vehicle print the ID plate, and the entry time and the type of vehicle (if is a car, a van or a motorbike). The list should be ordered chronologically, displaying the last vehicle entered in the parking as the first in the list.
And this is what I've got so far. My Solution
Since class Vehicle is abstract and cannot be instanced, what should I use to create different vehicle objects, might it be an array? And how should the output be changed, I mean depending on what the input is going to be: If it is car, to ask for color also, if it is a van for cargo volume?
Thanks a lot in advance to who take the time to read it and see if this UML seems right.
Analyze the statement
An important skill that you will start to develop in this module is analyzing a problem statement in order to identify the details needed to develop a solution.In this assignment the first task you should perform is a careful analysis of the problem statement in order to make sure you have all the information to elaborate a solution. Do not make assumption about what is needed! If you are not sure, about the information provided, ask questions.
Design a solution:
The design of your system should be consistent with the Object Oriented principles and easy to understand by an independent programmer.
Source: 5COSC001W Object Oriented Programming - Assignment 1
Suggest you:
class: "VehicleCardInfo" for storing cars information and status
class: RulesForCarPark for validate all data in "VehicleCardInfo"
vocabulary for: car types e.t.c.
CarParkManager as Actor use UseCase "Managing Cars" for CRUD operation for "VehicleCardInfo" objects.
Maybe we need some rules for people to logging and use this application.
UML diagram maybe easy for above (we don't use associations, use dependency).

Core Data: To-Many Relationships with Repeat Values

In a mileage-tracking app I am developing, the concepts include "Places," "Legs," and "Routes." Essentially, a Route is a collection of connected Legs, where each Leg is simply a straight line between two Places (with an associated distance).
I am having a problem modeling the Route-Leg relationship in Core Data because a Route may include the same Leg twice or more. For example, a person could travel Home to Office, Office to Worksite, Worksite to Home, Home to Office, and Office to Home as a single, contiguous path of travel on a given day. In that case, the "Home-to-Office" Leg is contained twice in the Route. But Core Data creates the To-Many relationship in a NSManagedObject subclass as an instance of NSOrderedSet, which does not permit duplicate Legs.
Is there a way I can create a relationship between the two Core Data-backed objects (Route and Leg) that allows multiple occurrences of the same Leg in a single Route? I am developing my app in Swift, so any Swift-specific suggestions would be appreciated.
Thank you.
Presumably you currently have a many-many relationship between Leg and Route. One solution is to replace this many-many relationship with an intermediate entity (say RouteLegDetails) which has to-one relationships with Leg and Route. Each Route can have many RouteLegDetails and likewise each Leg can have many RouteLegDetails. In the data model editor, it might look something like this:
This way, if you have a Leg that is traversed twice (or more) on the same Route, each instance is represented by a different RouteLegDetails: which overcomes the restriction on duplicates in the legDetails ordered set.
This will complicate your code slightly: when you want to add a Leg to a Route, you will have to create a RouteLegDetails object, and add it to both the legDetails relationship on the Route and the routeDetails relationship on the Leg. Conversely to remove a Leg from a Route, you will need to delete the corresponding RouteLegDetails object.
It seems like there might be an opportunity to improve your model object to solve this challenge. As I understand it now, you will not be able to fully define a route based on what you are currently modelling. How could you fully define a route from its constituent legs? Since the relationship is inherently unordered there is no way to build a route from the legs by just examining the relationship as you will not know in which order the legs occur.
One option is to store an order property with your leg object. A leg with the same start and end point, but a different order would be a distinct object.
A second and possibly cleaner approach would be to store the order of the legs in the route object (perhaps as a list of leg IDs leg 1 -> leg 3 -> leg 4 -> leg 1 -> leg 2). The legs relationship would get you all legs to build the route, the leg order property stored in the route object would allow you to construct the route from its legs, even if you use the same one twice. An array is the obvious way to store the order of the legs by ID, however an array is not a supported core data type, you may want to consider storing the order of the leg IDs as a string you can parse or a transformable attribute. See Transformable Attribute

What is the relationship between a card and deck in a Class Diagram (Texas Hold 'em)?

What is the relationship between a card and deck in a Class Diagram (Texas Hold 'em)? I'm still trying to understand how the relationships work in a class diagram. If a deck consists of cards, wouldn't that be a composition considering that removing a deck would remove the cards? There is the case where you can remove a deck but still have a card and this would be considered aggregation. So my mind is boggling with this concept. Can someone explain this to me with an example?
Developers lose sight of the intent of OO. A class diagram should resonate with the real-world domain. In the domain of Texas Hold 'em, a Deck contains up to 52 Cards, and a Card is contained in up to one Deck. A Card can also be held in up to one Hand, or laid on the Table, if I recall correctly. This is what you should be modeling, not the implementation details.
While one could model the relationship between a Deck and the Cards as a composition, I doubt that adds any value. When was the last time you set fire to the Deck, taking all the Cards with it? Essentially, you add and remove Cards from the Deck and you shuffle the Deck, until the game is over.
Based on the description of Jim L. we could make the following domain model:
In this model, the association between Deckand Cardis modeled as a composition because a card is part of a deck and cannot be shared with another deck (notice that non-shareability is the defining characteristic of composition, and not the life-cycle dependency).
I'd like to add to what Jim L. said that a class diagram can be used on all three levels of modeling:
(solution-indepnedent) domain modeling
(platform-independent) design modeling
(platform-specific) data modeling

Migrating from Dictionary to Core Data Entity

I've got a data model where there is a Person entity, which has a transformable attribute which is an array of dictionaries containing information. The model is much bigger than that, this is just the part I'm having trouble with. It was designed this way by an old developer, and in taking over the project I need to migrate this to be 100% core data.
So what I need to do is create a new entity, then step through each dictionary in the Person's array and create new instances of that entity with the information from that dictionary. I thought I could use an NSEntityMigrationPolicy to set up a custom migration for this new Entity, but it seems the Core Data migration is expecting X number of source entities to translate to X number of destination entities. Because I technically have 0 source entities right now (because they're in an array that Core Data doesn't really know anything about), I'm not sure how I can make the migration create new entities during the process.
What, or rather where in the migration procedure, is the best way to do what I'm trying to accomplish? I've always used lightweight migration in the past, so this is my first adventure in custom migration.
It would help to have a sense of your data model (schema) - but let's assume that your Person entity now holds home address and list of favorite restaurants. And let's further assume that you will be creating new entities Address and Restaurant along with the following relationships:
Person has one Address, so there's a to-one relationship from Person to Address called "homeAddress". There's an inverse to-many relationship from Address to Person, because many people could live at the same address.
Person has a to-many relationship (called restaurants) to Restaurants. Restaurant could also has a to-many relationship to Person (though this might be one of those cases where bidirectionality doesn't really make sense).
Anyway, the point is that now - in addition to your PersonToPerson NSEntityMigrationPolicy subclass, you will also have PersonToAddress and PersonToRestaurant. These will be the places that you unpack the old data and use it to instantiate and initialize new Address and Restaurant objects.
Of course, there are lots of other complicating issues. For example, you won't want to be creating a new instance of the same Restaurant for every Person who likes it. You will need to keep track of newly created Restaurants.
You will want to order your mappings strategically - probably with PersonToPerson first.
You might want to look at Marcus Zarra's Core Data sample code and maybe even buy his book.

Resources