2
 0..* 1..* +-------+ +--------+ |Invoice|_______________________|Products| +-------+ | +--------+ |inID | | |proID | |inDate | | | Qty | +-------+ | | Price | | +--------+ +-----------+ |LineProduct| +-----------+ | Qty | | salePrice | +-----------+ 

Is this coding correct for the above class diagram ?

Class Invoice { inID:int; inDate:Date; } Class LineProduct { Qty:int; salePrice:int; //inID:int; <-- this is what I did but I am wrong //prodID:int; <-- this is what I did but I am wrong } Class Products { prodID:int; Qty:int; Price:int; } 

Now for example if an invoice consists two products I have to pass instance of objects to the DB functions like this

invoiceTable.saveInvoice(invoice:Invoice); lineproductTable.saveLine( product instance 1 ); lineproductTable.saveLine( product instance 2 ); 

now again another confusion that the line product table will have inID and proID columns but how to pass an object which will have inID and prod ID ?

PS:Sorry I am stuck in low rep I couldnt post an image and explain my confusion

0

1 Answer 1

2

I think you had it right in the first place.

//inID:int; <-- this is what I did but I am wrong //prodID:int; <-- this is what I did but I am wrong 

Uncomment those lines. It's the only way to associate the product with the invoice.

Sign up to request clarification or add additional context in comments.

5 Comments

Note that UML need not have a direct mapping to exact classes, but that seems to be one "de-normalization" to ensure the references work. I would stress that there is also navigability issues if you only know about Invoice or Product, they have no references or methods to find relationships.
@Ted Johnson I am confused ! LineProduct is an association class ryt ?so is my class code correct ?
Also, your diagram explicitly specifies multiplicities for inID and prodID. So you would probably want to declare them as arrays of integers in your code, and to make sure that at least one instance of Product is present in the prodID array at all times.
If a LineProduct is intended to represent a single line on the invoice, then the LineProduct doesn't need arrays; BUT it does raise the question of how are you going to collect all lineProducts for a single Invoice? The Invoice object needs to have an array of LineProducts, otherwise you'd have to write all LineProducts to the database immediately and do queries everytime you wanted to manipulate the list. Think of someone adding to the invoice before the invoice is complete.
yes what you said was correct regarding the LineProduct array as you said I agree on //immediately writing to the db// is a bad method. In my previous projects the invoice form sends an invoice instance and an <arraylist>LineProducts to the invoiceDB, LineProductDB classes. Since invoiceDB recevices an instance of invoice its easy to insert the details into the invoice database table. When it comes to LineProduct arraylist what I do is I use a foreach and seperate the arraylist then at the end of arraylist 1 by 1 lineProduct instances will be inserted into lineProduct database table

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.