給定一張Meber和Product表,獲得以下信息:java
1.獲取一個用戶訪問的全部商品信息c++
2.獲取一個商品被哪些用戶瀏覽過的信息app
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() { return products ; } public String getInfo() { return "【用戶信息】姓名:"+this.name + "年齡:"+this.age ; } } class Product { private String name ; private double price ; private Meber mebers[] ; //一個商品被多我的瀏覽 public Product(String name ,double price) { this.name = name ; this.price = price ; } public String getInfo() { return "【商品信息】名稱:"+this.name + "價格:"+this.price ; } public void setMebers(Meber[] mebers) { this.mebers = mebers; } public Meber[] getMebers() { return mebers; } } public class Many_to_many { public static void main(String[] args) { //設置關係 Meber meberA = new Meber("小兔子",35); //設置兩個用戶 Meber meberB = new Meber("小白喵",20);
Product proA = new Product("c++從入門到放棄",50.00); //設置三個商品 Product proB = new Product("java從入門到放棄",49.00); Product proC = new Product("C語言從入門到放棄",46.00);
meberA.setProducts(new Product[]{proA,proB}); //設置用戶瀏覽過的商品 meberB.setProducts(new Product[]{proA,proC});
proA.setMebers(new Meber[]{meberA}); proB.setMebers(new Meber[]{meberA,meberB}); //設置商品被瀏覽的用戶 proC.setMebers(new Meber[]{meberB}); //獲取數據 System.out.println("-----------------根據用戶查看瀏覽商品信息--------------------"); System.out.println(meberA.getInfo()); for(int i = 0 ; i<meberA.getProducts().length ;i++) { System.out.println("\t|-"+meberA.getProducts()[i].getInfo()); } System.out.println("-----------------根據被瀏覽的商品查看用戶--------------------"); System.out.println(proA.getInfo()); for(int i = 0 ; i< proA.getMebers().length ; i++) { System.out.println("\t|-"+proA.getMebers()[i].getInfo()); } } }
result:
-----------------根據用戶查看瀏覽商品信息-------------------- 【用戶信息】姓名:小兔子年齡:35 |-【商品信息】名稱:c++從入門到放棄價格:50.0 |-【商品信息】名稱:java從入門到放棄價格:49.0 -----------------根據被瀏覽的商品查看用戶-------------------- 【商品信息】名稱:c++從入門到放棄價格:50.0 |-【用戶信息】姓名:小兔子年齡:35