Java - Data Structures - java

I came across a Java Data Structures task. I'm looking for a confirmation that I understood it correctly and that my approach is right.
The task:
A product can have many different types of attributes, and they can vary between products, e.g. a Bike can have the following attributes: {price, color, size} and the product Kitchen table: {price, width, depth, height}. Product Attributes are organized in a hierarchical group structure, where a Product Attribute Group can contain one or several Product Attributes and/or Product Attribute Groups.
Write the code needed in java to handle the above described products and a print function that prints the data in such a way that it is clear which attributes belong to which product and which attribute group if any.
There was an image attached with notation:
Image
Figure 1: Note that the order is important. In the figure above Attributes 1, Attribute 3 and Attributes 2 all belong to the Product Attributes group.
So my approach would be to create an abstract Product class, and then make two subclasses(Bike, KitchenTable) extending from Product class. Is it correct? Or is it about something entirely different?

Please have a look at the Composite Pattern.
The composite pattern describes a group of objects that is treated the same way as a single instance of the same type of object.
This matches your task to have product attributes that have to be organized in hierarchical groups.

Related

How to model semistructured data in Java?

I have an agriculture related data that I need to model in java.to mention how the data looks in a nutshell the data is collection of attributes which are collected whenever a new plant variety is produced. Currently the plants I want to model their data are around 135 grouped into 9 groups. The problem I am facing during the modeling process is that every plant has its own attributes that it doesn't share with others and also it have few attributes similar to other plants and there is also some difference in attributes of the same plant released in the same year which makes it difficult to restrict the amount of fields I tried to include in the class.
For example the attributes shown in the red might not be included in other variety or might be included adding other attributes it is not possible to know exactly which attributes can be there or not. The other problem is that some attributes similar to the one shown in the blue rectangle has range values.
What I have tried was to list down every possible attributes by looking over 24 books I have and having unique attributes making them into classes for every plant and extracting values which have ranges into other classes with min and max values and I ended up having over 200 classes which makes it very complicated to build a unified system that I can use to feed the data and retrieve info from it.
What is ur recommended approach to model the data the database I planned to use is mongodb since since some varieties may miss values which other varieties has. I am ready to move to other programming languages like python if I have to thanks.

Is there a library to group list of entities by several attributes

Assume there's a structure Pay with attributes: category, location, department, sum.
Having a collection of such entities I want to group it by category and location to get such a structure:
category - location - collection of Pays
There's a Guava Multimaps.index() function, but it works only for one-level grouping. And I need 2+.
Doesn't matter what type this structure will have - some Map<Map<Map>> implementation or smth else.
I just don't want to write multi-iteration code. Hope there's a library for such operation. Do you know one?
P. S. Great if works on Java 7.

jpa multiple bidirectional many to one relationship to the same field

I have a problem i hope someone can help me solving.
My system has got an entity "Product". On a product it should be possible to set "replacement products" (products that are supposed to be sold instead of the original product). We have two types of replacement. On a product, i need to be able to set either none, one of them or both types of replacement product. But, only one product should be set per type. In addition to this, i want to be able to look at a product and get a list of other products that has this product as their replacement product.
Short version:
Product -> replacement (two types, one of each)
Product -> list of
products that it replaces (of both types of replacement)
My solution this far has been to create an abstract class ReplacementProduct with subclasses(one for each replacement type), with discriminatorvalues.
The replacementproduct table in the database has the following fields:
id, replacementproduct, replacementType
Further, i have placed two fields on the Product entity (instances of the subclasses for replacement types). Like this i have obtained a one way link from product to another product with a replacement type.
My problem is to make this link go both ways. As I said, I want to be able to get a list of products with the Product in question as their replacement product.
Is this possible without using a lot of java logic?
I'm open to any suggestion.
Sounds like a bidirectional ManyToMany is needed between products. The owning side would setup a mapkey mapping, using the type as the key. The other side doesn't need a key, and would reflect all the products it can replace.
This is described here
http://en.wikibooks.org/wiki/Java_Persistence/Relationships#Map_Key_Columns_.28JPA_2.0.29

Should I use a nested enum?

Say I need a data structure in Java involving one set of categories, each with one set of subcategories. For example, let's say the main category is 'brand' (like, of a product) and the subcategory is 'product'. I want to be able to map the combination of brand+product to a piece of data e.g. a price.
I'd like to use an enum type for both 'brand' and 'product' if they were on their own, because
Brand+product has only a small, single piece of data tied to it (the price)
I need to refer to them many times throughout a reasonably large program, so the chance that I'll mistype any string literal keys I assign to them is basically one.
However, the number of brands/products is too large to have a single enum for each brand/product combination (around twenty brands each with ten products and a good chance of adding more later). I'd like to be able to use the structure like this:
getPrice(APPLE.IPOD)
getPrice(APPLE.MACBOOK)
getPrice(HERSHEYS.PEANUT_BUTTER_CUPS)
Should I use some sort of nested enum? If so, how would that be implemented?
Bonus information: I've spent a bit of time googling 'java nested enum' but haven't come up with anything. The problem with structures like the first one in the ticked answer here or thelosts's answer here is that I have too many categories all exhibiting the same behavior to write out very similar enum definitions so many times.
I wouldn't use an enum for this.
I would suggest you load this information from a file or database. Java is not a good place for storing large amounts of data.
You could add a getter and setter to the Brand enum that allows setting a Product enum, but that will not enforce that a Product is actually manufactured by that Brand. Besides, there is ever only one instance of each enum value -- so you could never have APPLE.IPOD and APPLE.IPAD. You either need a single enum type that represents the Cartesian product, or you need to load your values from a data store like Peter Lawrey suggests.

What kind of database and how to create it to hierarchically categorize text?

its an app that i will start from scratch .i wish to use jsp with phpmyadmin and mysql database.but i duno how should i create my database...my data is like this
img833.imageshack.us/img833/9901/89112252.png
EG Input text : "Describe the diagram below" (so describe is in Perception P1 Physchomotor Domain) the output will be : Perception p1, Physchomotor Domain
but there are 3 more domains which make it like a hierarchy where the domains are the parents of the p1,p2 and so on...how shud i build my database like this ? different domain hav different numbers of p1,p2
I'm not entirely sure what your dataset is or what it is supposed to do, but I will give you some tips.
It seems that you have "domains" which would be your object model (one table in your database). These domains can have one or multiple parents or children, so you will want to have a column that contains the ID of the parent (this will enable you to do a query to find parents or children). This is typically all you need for any hierarchical structure. You will then want 1 or more columns for the data. For instance, I think you would want to have a column called "words" which would be a list of strings perhaps stored as text or varchar datatype.
And by the way, if you want good help, you should try to make your question more clear, and use good spelling and grammar. People like helping more when a question is framed properly.

Categories

Resources