Product Meber and given a table, get the following information:
1. Obtain a user to access all product information
2. Obtain information is a commodity which users viewed
package Mapping_transformation; class Meber { private String name ; private int age ; private Product products[] ; //一个人浏览多个商品 public Meber(String name , int age) { this.name = name ; this.age = age ; } public void setProducts(Product products[] ) { this.products = products ; } public Product[] getProducts() { returnProducts; } public String getInfo () { return "[user] Information Name:" + the this .name + "Age:" + the this .age; } } class Product { Private String name; Private Double . price; Private Meber mebers [] ; // a commodity is more than personal viewing public product (String name, Double . price) { the this .name = name; the this .price = . price; } public String getInfo () { return "[Product] Information Name:" + the this .name + "Price:" + the this .price; } public void setMebers (Meber [] mebers) { the this .mebers = mebers; } public Meber [] getMebers () { return mebers; } } public class MANY_TO_MANY { public static void main (String [] args) { // set the relationship Meber meberA = new new Meber ( "small rabbit", 35 ); // set two users Meber meberB = new newMeber ( "white meow", 20 );
Product proA = new new Product ( "c ++ from entry to give up", 50.00 ); // set three commodity Product the proB = new new Product ( "the Java from entry to give up", 49.00 ) ; product the proC = new new product ( "abort entry from C language", 46.00 );
meberA.setProducts ( new new product [] {proA, the proB}); // set the user viewed items meberB.setProducts ( new new product [] proA {,} the proC);
proA.setMebers ( new new Meber [] {} meberA); proB.setMebers ( new new Meber [] {meberA, meberB}); // set the user commodity being browsed proC.setMebers ( new new )Meber [] {} meberB); // get the data System.out.println ( "----------------- browse product information according to the user to view -------- ----------------------- " ); System.out.println (meberA.getInfo ()); for ( int I = 0; I <meberA.getProducts () length;. I ++ ) { the System. Out.println ( "\ T | -" + meberA.getProducts () [I] .getInfo ()); } System.out.println ( "----------------- See the user is browsing the product -------------------- " ); System.out.println (proA.getInfo ()); for ( int I = 0; . I <proA.getMebers () length; I ++ { System.out.println ("\t|-"+proA.getMebers()[i].getInfo()); } } }
the Result:
----------------- browse product information based on the user to view -------------------- [information] User Name : small rabbit Age: 35 | - [product information] name: c ++ from entry to abandon price: 50.0 | - [product information] name: java from entry to abandon price: 49.0 ------------ ----- according to the view the user browse the merchandise -------------------- [product information] name: c ++ from entry to abandon price: 50.0 | - [user information] name: small rabbit Age: 35