Market
package market;
public class Market {
private String marname;
private Product[] productarr;
public Product[] getProductarr() {
return productarr;
}
public String getMarname() {
return marname;
}
public void setMarname(String marname) {
this.marname = marname;
}
public void setProductarr(Product[] productarr) {
this.productarr = productarr;
}
Product sell(String name){
for (int i = 0; i < productarr.length; i++) {
if (productarr.getProname() == name){
return productarr;
}
}
return null;
}
}
Person
package market;
public class Person {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
Product shopping(Market market,String name){
return market.sell(name);
}
}
Product
package market;
public class Product {
private String proname;
public void setProname(String proname) {
this.proname = proname;
}
public String getProname() {
return proname;
}
}
Shopping
package market;
public class Shopping {
public static void main(String[] args) {
Product p1 = new Product();
Product p2 = new Product();
Product p3 = new Product();
Product p4 = new Product();
Product p5 = new Product();
// 创建商品类
p1.setProname("1");
p2.setProname("2");
p3.setProname("3");
p4.setProname("4");
p5.setProname("5");
// 给商品类赋名
Market m = new Market();
m.setMarname("k商店");
m.setProductarr(new Product[]{p1,p2,p3,p4,p5});
// 创建商店类、赋名、添加商品
Person p = new Person();
p.setName("alex");
Product result = p.shopping(m,"20");
if (result != null){
System.out.println(p.getName()+"在"+m.getMarname()+"买到了"+result.getProname());
} else {
System.out.println(p.getName()+"在"+m.getMarname()+"白跑了一趟");
}
}
}
|
|