package com.chap6.demo;
import java.io.Serializable;
public class Goods implements Serializable,Comparable<Goods>{
//商品信息主要包括商品编号、商品名称、销售价格、成本价格、销售数量
//提示:定义学生类Goods,重写hashcode和equals方法,实现Compareble接口,
//重写compareTo方法,按要求定义排序规则,再编写直接插入排序方法,最后在测试类中进行测试。
private int gid;
private String name;
private double priceOfSell;
private double priceOfPrime;
private int number;
public Goods() {
super();
}
public Goods(int gid, String name) {
super();
this.gid = gid;
this.name = name;
}
public int getGid() {
return gid;
}
public void setGid(int gid) {
this.gid = gid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPriceOfSell() {
return priceOfSell;
}
public void setPriceOfSell(double priceOfSell) {
this.priceOfSell = priceOfSell;
}
public double getPriceOfPrime() {
return priceOfPrime;
}
public void setPriceOfPrime(double priceOfPrime) {
this.priceOfPrime = priceOfPrime;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
@Override
public String toString() {
return "商品ID:" + gid + "\t商品名:" + name + "\t销售价格:"
+ priceOfSell + "\t成本价格:" + priceOfPrime + "\t销售数量:"
+ number;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + gid;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Goods other = (Goods) obj;
if (gid != other.gid)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
@Override
public int compareTo(Goods other) {
//(1)首先按照不同商品销售总利润从高到底排序
int result=(int) ((this.getPriceOfSell()-this.getPriceOfPrime())*this.getNumber()-(other.getPriceOfSell()-other.getPriceOfPrime())*other.getNumber());
if(result==0){
//(2)销售总利润相同的,按销售数量从高到底排序
result=this.getNumber()-other.getNumber();
if(result==0){
//(3)销售数量相同的,按销售价格从高到低排序;
result=(int) (this.getPriceOfSell()-other.getPriceOfSell());
if(result==0){
//销售价格的,按商品编号从小到大进行排序
result=this.getGid()-other.getGid();
}
}
}
return -result;
}
}
课堂实践6-1 运用直接插入排序算法对不同商品销售情况进行排序.zip
需积分: 0 188 浏览量
2024-05-16
15:53:23
上传
评论
收藏 10KB ZIP 举报
2301_77121120
- 粉丝: 0
- 资源: 1
最新资源
- C++开发基于ROS实现多差速无人车编队控制源码+使用说明+详细注释 (期末大作业)
- 项目将mzphp结合vuejs、vux、vue-resource做的todoList 代码量不多,重点在于演示 您也可当框架使用
- 基于STC12C5A60S2单片机+ds1302+LCD12864屏实现万年历源程序软件源码KEIL C51工程文件.zip
- 3层独栋别墅-10.20&11.70米- 施工图.dwg
- 简约风车壁纸自动采集小程序源码手机壁纸小程序源码
- UIBezierPath和CAShapeLayer绘图
- Scratch资源 sebcym-v1.4.6.zip
- STM32驱动0.96寸OLED代码
- 中小学教务工具箱v3.7.3
- SI4947ADY-T1-E3-VB一款SOP8封装2个P-Channel场效应MOS管
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈