您的位置:首页 > 其它

对象比较_价值计算,设计该对象的价值计算方法,材质,颜色,形状,尺寸

2014-04-10 12:42 295 查看
package cn.bh.oop;

/**

* 某游戏中的对象具有属性:

*

1. 材质: 木材,钢铁,合金,塑料

2. 颜色: 红 黑 黄 白 红

3. 形状: 方 圆 三角 五边

4. 尺寸: 整数 1 ~ 1000

请设计该对象的价值计算方法。属性的重要性从上到下减小。同一属性的值的重要性从左到右减小。

即: 材质好的不管颜色、尺寸,肯定有更高价值。

其它相同,红色的比黑色价值高

尺寸越小越值钱。

*/

import java.util.Comparator;

import java.util.Set;

import java.util.TreeSet;

class Game{

private String materia;

public String getMateria() {

return materia;

}

public void setMateria(String materia) {

this.materia = materia;

}

public String getColor() {

return color;

}

public void setColor(String color) {

this.color = color;

}

public String getShape() {

return shape;

}

public void setShape(String shape) {

this.shape = shape;

}

public int getSize() {

return size;

}

public void setSize(int size) {

this.size = size;

}

private String color;

private String shape;

private int size;

public Game(String materia,String color,String shape,int size){

this.materia=materia;

this.color=color;

this.shape=shape;

this.size=size;

}

@Override

public String toString(){

return materia+">"+color+">"+shape+">"+size;

}

}

/**

* 裁判类又称比较器.为了更好控制比较内容用了枚举

* @author lenovo

*

*/

class K1 implements Comparator{

public enum materiaEnum {//木材好于钢材好于合金好于塑料

木材,钢铁,合金,塑料;

}

public enum colorEnum {//木材好于钢材好于合金好于塑料

红色,黑色,***,白色;

}

public enum shapeEnum {//木材好于钢材好于合金好于塑料

方,圆,三角,五边;

}

public int compare(Object obj1, Object obj2) {

//只有game类型才有比较的意义,如果不是该类型就让它相等不添加

if ((obj1 instanceof Game)&&(obj2 instanceof Game)) {

Game g1=(Game)obj1;

Game g2=(Game)obj2;

//对于材料的比较

int x1=0;

for (int i = 0; i < materiaEnum.values().length; i++) {

if (g1.getMateria().equals(materiaEnum.values()[i].toString())) {

x1=i;

break;

}

}

int x2=0;

for (int i = 0; i <materiaEnum.values().length; i++) {

if (g2.getMateria().equals(materiaEnum.values()[i].toString())) {

x2=i;

break;

}

}

int comp1=x1-x2;

if (comp1!=0) {

return comp1;

}

else {

//对于颜色的比较

int y1=0;

for (int i = 0; i < colorEnum.values().length; i++) {

if (g1.getColor().equals(colorEnum.values()[i].toString())) {

y1=i;

break;

}

}

int y2=0;

for (int i = 0; i <colorEnum.values().length; i++) {

if (g2.getColor().equals(colorEnum.values()[i].toString())) {

y2=i;

break;

}

}

int comp2=y1-y2;

if (comp2!=0) {

return comp2;

}

//对于形状的比较

else {

int z1=0;

for (int i = 0; i < shapeEnum.values().length; i++) {

if (g1.getShape().equals(shapeEnum.values()[i].toString())) {

z1=i;

break;

}

}

int z2=0;

for (int i = 0; i <shapeEnum.values().length; i++) {

if (g2.getShape().equals(shapeEnum.values()[i].toString())) {

z2=i;

break;

}

}

int comp3=y1-y2;

if (comp3!=0) {

return comp3;

}

//对于大小的比较

return g1.getSize()-g2.getSize();

}

}

}

return 0;

}

}

public class qq{

@SuppressWarnings("unchecked")

public static void main(String[] args) {

Set gSet=new TreeSet<Game>(new K1());

gSet.add(new Game("木材","白色","三角",1));

gSet.add(new Game("钢铁","红色","三角",2));

gSet.add(new Game("木材","红色","三角",3));

gSet.add(new Game("塑料","红色","三角",3));

System.out.println(gSet);

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐