package shopping;
public class Market {
private String name;
private Product[]products;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Product[] getProducts() {
return products;
}
public void setProducts(Product[] products) {
this.products = products;
}
public Product sell(String productName) {
for(Product product:products) {
if(productName.equals(product.getName())) {
return product;
}
}
return null;
}
}
package shopping;
public class Product {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package shopping;
public class Person {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Product buy(Market market,String productName) {
return market.sell(productName);
}
}
package shopping;
public class Shopping {
public static void main(String[]args) {
Product apple = new Product();
apple.setName("apple");
Product orange = new Product();
orange.setName("orange");
Product juice = new Product();
juice.setName("juice");
Market market=new Market();
market.setName("华润万家");
market.setProducts(new Product[] {apple,orange,juice});
Person yang=new Person();
yang.setName("yang");
String productname="apple";
Product product=yang.buy(market, productname);
if(product==null) {
System.out.println(yang.getName()+"没有买到:"+productname);
}else {
System.out.println(yang.getName()+"买到了"+productname);
}
}
}