源辰项目-1

接口:
==================
package com.huaiwei.ben;

public interface Filter {
public boolean doCheck( Object obj );
}
====================
package com.huaiwei.ben;

public interface Measure {
public double measure( Object obj);
}
========================
package com.huaiwei.ben;

public class Container {

public static final int Length=10000 ;
private Object[ ] objs =new Object
[Length];
private int count;
private Object max;
private Object min;
private double avg;
private double sum=0;


private Filter filter;
private Measure measure1;

public Object[] getobjs(){

Object[] newobjs=new Object
[count];



System.arraycopy( objs
,0,newobjs,0,count);

//根据count来创建一个新数组
//System.out.arraycopy();

return newobjs;

}

public void setFilter( Filter filter){
this .filter=filter;
}

public void setMeasure(Measure
measure1) {
this.measure1 = measure1;
objs= new Object[Length];
count=0;
max=null;
min=null;
avg=0;
sum=0;

}

public Object getMax() {
return max;
}

public Object getMin() {
return min;
}

public double getAvg() {
return avg;
}

public void save( Object obj){
//判断对象是否为空
if( obj==null){
return;
}

if( filter!=null){//判断接口是否
为空

if( !filter.doCheck(
obj) ){
return;
}
}
//判断当前数组是否为0,count〉=数
组。length
//TODO::将数组做成动态数组
if(count>objs.length){
System.out.println("容器
数组已满,无法操作!!");
return ;
}

//将这个obj这个数组放在数组里面
objs[count]=obj;


double
objvalue=measure1.measure(obj);

if(count ==0)
{
max=obj;
min=obj;
}else{
double
maxvalue=measure1.measure(max);
double
minvalue=measure1.measure(min);

if(objvalue>maxvalue){
max=obj;
}
if( objvalue<minvalue){
min=obj;
}
}
count++;
sum+=objvalue;
avg=sum/count;

//调用measure.measure(obj)对obj
进行d

}


}
============================
以上是对com.huaiwei.ben的操作
以下是对另一个包的操作;
===============================
package com.mimi.bean;

public class Person {
private String name;
private double height;
private double weight;

public Person(){
super();
}

public Person(String name, double
height, double weight) {
super();
this.name = name;
this.height = height;
this.weight = weight;
}

 

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public double getHeight() {
return height;
}

public void setHeight(double height) {
this.height = height;
}

public double getWeight() {
return weight;
}

public void setWeight(double weight) {
this.weight = weight;
}


}
=================================
package com.mimi.bean;

import com.huaiwei.ben.Filter;

public class PersonBmiFilter implements Filter
{

@Override
public boolean doCheck(Object obj) {
if( obj == null ){
return false;
}
if( !( obj instanceof Person)){
return false;
}
Person p=(Person)obj;
if( p.getWeight()>300 &&
p.getHeight()>30){
return false;
}
return true;
}
}
==========================
package com.mimi.bean;

import com.huaiwei.ben.Measure;

public class PersonBmiTool implements Measure {

@Override
public double measure(Object obj) {
if( obj ==null){
return 0;
}
if( !(obj instanceof Person) ){
return 0;
}

Person p=(Person)obj;
double bmi=p.getWeight();
return bmi;
}

}
==============================
测试类:
import java.util.Random;

import com.huaiwei.ben.Container;
import com.mimi.bean.Person;
import com.mimi.bean.PersonBmiFilter;
import com.mimi.bean.PersonBmiTool;

public class Test {

/**
* @param args
*/
public static void main(String[] args)
{
Container c = new Container();

PersonBmiFilter fl = new
PersonBmiFilter();
PersonBmiTool pbt = new
PersonBmiTool();

c.setMeasure(pbt);
c.setFilter(fl);
Person p1 = new Person("张三",
433, 600);
Person p2 = new Person("里斯",
1.333, 63);
Person p3 = new Person("王五",
1.44, 95);

c.save(p1);
c.save(p2);
c.save(p3);

/*
* Random r=new Random(); for(
int i=0;i<100;i++){ Person p4=new
* Person("王东
风"+i,1+Math.random(), r.nextInt(80)+30);
*
* c.save(p4); }
*/
Person max = (Person) c.getMax
();
Person min = (Person) c.getMin
();

System.out.println("最大值为" +
max.getName());
System.out.println("最小值" +
min.getName());

System.out.println("平均值" +
c.getAvg());

Object[] objs = c.getobjs();

/*
* for(int i=0;i<2;i++){ Person
p11=(Person)objs[i];
* System.out.println
(p11.getName()); }
*/
for (Object o : objs) {// 增强型
的for,从objs中调用到Object o中
Person pp = (Person) o;
System.out.println
(pp.getName() + "\t" + pp.getHeight()
+ "\t"
+ pp.getWeight());

}

System.out.println
("********************************");

c.setFilter(null);
c.setMeasure(pbt);

c.save(p1);
c.save(p2);
c.save(p3);

objs=c.getobjs();

for (Object o : objs) {// 增强型
的for,从objs中调用到Object o中
Person pp = (Person) o;
System.out.println
(pp.getName() + "\t" + pp.getHeight()
+ "\t"
+ pp.getWeight());

}

}

}

posted @ 2015-03-21 18:47  Animationer  阅读(176)  评论(0编辑  收藏  举报