• Post Reply Bookmark Topic Watch Topic
  • New Topic

question on logic and process  RSS feed

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I have a parent's (object) that needs to be populated by child objects based on priority.

Parent gets data from child with priority 1,then child with priority 2, priority 3 and so on. Can you let me know a process to start off with, I dont want to have a lot of if else conditions, some java technique i can use to accomplish this.

Thank you
 
Rancher
Posts: 779
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at the Switch statement, it should get rid of your if/then/else clauses. Also don't forget the break to separate the conditions unless you want to use the fall through feature.
 
Avi Sridhar
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Les Morgan wrote:Look at the Switch statement, it should get rid of your if/then/else clauses. Also don't forget the break to separate the conditions unless you want to use the fall through feature.


Thank you. Is there some kind of priority feature I can use to populate the main object from other objects?.
 
Sheriff
Posts: 4298
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From what you've said, it could be as easy as...

...but I suspect there's more to it than that. Why don't you give us some example code.
 
Avi Sridhar
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:From what you've said, it could be as easy as...

...but I suspect there's more to it than that. Why don't you give us some example code.


Thank you, this is the example code,



I see there is a pattern in the code which can be simplified. Wanted some kind of direction.

Will this help ?
 
Knute Snortum
Sheriff
Posts: 4298
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One possibility is creating an array or List of PriorityObjects:

Then you could use a nested for loop something like this:

I'm still not sure this will work because I don't know how your "foreach" loops use anObject.
 
Avi Sridhar
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:One possibility is creating an array or List of PriorityObjects:

Then you could use a nested for loop something like this:

I'm still not sure this will work because I don't know how your "foreach" loops use anObject.


Thanks, foreach loops use anObject to get values like
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Avi Sridhar wrote:Thanks, foreach loops use anObject to get values like

I hate to say, but that doesn't make sense. From the code you've shown us, 'anObject' is an Object, so it can't possiblty have a getName() method.

I have a feeling that there's quite a lot you're not telling us, so why don't you describe in English (not in Java) what this program is doing, and what these "parent" and "child" classes are. For example: are these "children" all the same type, or are they different types?

It would be much easier if we knew what's really going on, rather than having to deal with "parents" and "children" and "priorities", but in answer to one of your original questions ("I dont want to have a lot of if else conditions, some java technique i can use to accomplish this."), if you really want to get rid of conditions - especially type-checking conditions - use polymorphism; but I'd need a lot more information to advise any further.

Winston
 
Avi Sridhar
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, the anObject is just a particular priority object, e.g., aPriority1Object. Sorry for not being clear.

A main data needs to be populated based on values from other, same type values. This main needs to be populated on priority as the code says. Once particular priority values are read we are removing the considered data.

Please advise if this explanation is ok.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Avi Sridhar wrote:Please advise if this explanation is ok.

Not really. What is this program for? All we know right now is the mechanics of the 'populating' exercise - and I have to admit I find them very confusing - but we still have no idea what any of these types actually are.

What is it dealing with? Bank accounts? Widgets? Inventory? Stock market transactions?

We (or at least I) need something concrete to understand all this 'priority' business, so try to explain it to me as if I was an intelligent kid (which some would say I still am ).

Winston
 
Avi Sridhar
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Avi Sridhar wrote:Please advise if this explanation is ok.


Not really. What is this program for? All we know right now is the mechanics of the 'populating' exercise - and I have to admit I find them very confusing - but we still have no idea what any of these types actually are.

What is it dealing with? Bank accounts? Widgets? Inventory? Stock market transactions?

We (or at least I) need something concrete to understand all this 'priority' business, so try to explain it to me as if I was an intelligent kid (which some would say I still am ).

Winston

Thanks, some details you requested for are below

MasterInventory = Employeeid,Desk #,EmployeeSalary,EmployeeDesignation
HRInventory = Employeeid,EmployeeName,EmployeeTitle,EmployeeSalary,EmployeeDesignation
ITInventory = EmployeeId,Laptop #, Type of Laptop, ...
SuppliesInventory = EmployeeId,Desk #,...,Chair Type,....
..so on Inventories

Each of the above has its own database table. The process needs to create HRInventory,ITInventory,SuppliesInventory objects having EmployeeId are the key and the rest of the details as values.
from the respective tables and populate the MasterInventory table with a subset of values
(only Employeeid,Desk #,EmployeeSalary,EmployeeDesignation ) found in the *Inventory
tables based on a defined priority, as we have spoken about above. The *Inventory are of the same type.

how does this look?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Avi Sridhar wrote:how does this look?

Much better; but still incomplete.

You appear to have a bunch of database tables associated with an Employee.

Question 1: Is Employee ID the primary key of all these tables, or simply a foreign key? That is likely to have a huge bearing on whether the relationship is an "IS-A" relationship (ie a hierarchy) or a "HAS-A" one.

Taking something like ITInventory as a starter: it seems unlikely that each Employee has only one row in this table. What seems much more likely is that each row describes a piece of IT inventory that is associated with an Employee; so an Employee could well have many associated rows in the table - but I could be wrong.

Your MasterInventory table would appear to be some sort of "summary" - and contains redundant data - but whether it refers to an Employee (ie, Employee ID is the primary key) or not, I have no idea. Either way, I'd say it's a different kettle of fish to the other tables in your list.

If indeed EmployeeID IS the primary key of all these tables, then I would say that, in Java terms, they are subclasses of Employee, and you may well be able to use polymorphism to avoid all these if or switch statements. However, we're still too bogged down in 'priority' mechanics (which you still haven't explained) to sort out the wheat from the chaff.

Question 2: What is the object of this exercise: To create an Employee, or simply plough data stored in Java objects into your database?

About the only thing that I can see at the moment is that creation of MasterInventory rows is probably the thing you should do last, because it appears to be dependant on data in the other tables (and theoretically you should then be able to do it with an SQL statement; but it might be more trouble than it's worth).

HIH, and sorry if it doesn't seem like much; but I'm still lacking information.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!