对象比较_价值计算,设计该对象的价值计算方法,材质,颜色,形状,尺寸
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);
}
}
/**
* 某游戏中的对象具有属性:
*
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);
}
}
相关文章推荐
- JS中字符串转换为日期对象的方法JS中如何计算时间,如何比较时间
- 面向对象建模与数据库建模两种分析设计方法的比较
- 设计Point类用来定义平面上的一个点,用构造方法传 递坐标位置,方法有计算两点的距离。编写测试类,在该类中实现Point类的对象。
- 面向对象建模与数据库建模两种分析设计方法的比较
- 面向对象建模与数据库建模两种分析设计方法的比较
- 面向对象建模与数据库建模两种分析设计方法的比较
- 【JAVA学习】java中==、equals()、hashCode()都和对象的比较有关,在java中这三者各有什么用处呢,即java中为什么需要设计这三种对象的比较方法呢?
- 面向对象建模与数据库建模两种分析设计方法的比较(转载)
- 给Nsstring增加一个对象方法:计算某个字符串中的阿拉伯数字的个数
- 再探结构化设计和面向对象设计方法
- 知识点干货—关于equals方法,Hashcode方法和两个对象的比较
- 再探结构化设计和面向对象设计方法
- Java程序计算各种对象所占内存的大小的方法
- Integer类型数据比较大小问题:(Integer定义的是对象,养成使用equals方法的好习惯)
- 一种用于ABAP Report重构的面向对象设计方法——MVC
- [原创]详述IComparer,IComparable接口,实现自定义方法比较对象大小并排序(C#)
- c#中的3种对象比较方法
- Js和JQ求对象尺寸的几个方法
- 对象的比较及hashCode、equals方法的使用
- Jquery如何序列化form表单数据为JSON对象 C# ADO.NET中设置Like模糊查询的参数 从客户端出现小于等于公式符号引发检测到有潜在危险的Request.Form 值 jquery调用iframe里面的方法 Js根据Ip地址自动判断是哪个城市 【我们一起写框架】MVVM的WPF框架(三)—数据控件 设计模式之简单工厂模式(C#语言描述)