java_(面向对象实例) 假设一个宠物店有1000个动物,一次宠物体检,给每个宠物的 体重、活跃度、灵敏度 进行了打分

主类

package experiment11.exp5;
/**
* 假设一个宠物店有1000个动物,一次宠物体检,给每个宠物的
* 体重、活跃度、灵敏度 进行了打分(100分制),
* 现在要实现宠物按照上述 三个指标的平均值降序排列,并输出所有宠物的详细信息,
* 输出信息包括:名字、年龄、体重、活跃度、灵敏度及平均值,
* <p>
* 请实现相应的程序。
* 额外要求:
* 1)必须包含2个类:宠物类Pet,宠物店类PetShop,
* 宠物店类包含一个属性pets,包含其所有的宠物;
* 2)宠物信息必须包含名字、年龄、体重、活跃度、灵敏度;宠物店信息必须包含名字、所有宠物信息;
* 3)1000个宠物随机产生,每个宠物各指标随机产生(0-100分之间);
* 4)以上属性是规定属性,其它属性根据编程需要自行添加。
*/
public class PetsCheck {
public static void main(String[] args) {
/*一句话实现一个需求。
* 将各个功能尽合适的封装到类中。*/
PetShop petShop = new PetShop("petShop1");
petShop.sort_3();
petShop.printSortedResult();
//System.out.println(petShop.getPetList());
}
}

PetShop类:

package experiment11.exp5;
import java.util.*;
public class PetShop {
String shopName;
List<Pet> petList = new ArrayList<>();
/**
*
* @param shopName
*/
public PetShop(String shopName) {
this.shopName = shopName;
//随机生成宠物
for (int i = 0; i < 1000; i++) {
petList.add(new Pet("pet" + i, 100 * Math.random(), 100 * Math.random(), 100 * Math.random(), 100 * Math.random()));
}
}
public void sort_3() {
Collections.sort(petList, new Comparator<Pet>() {
@Override
public int compare(Pet o1, Pet o2) {
return (int) (o2.getAverage()-o1.getAverage());
}
});
}
public String getShopName() {
return shopName;
}
public List<Pet> getPetList() {
return petList;
}
public void setList(List<Pet> list) {
this.petList = list;
}
@Override
public String toString() {
return "PetShop{" +
"shopName='" + shopName + '\'' +
", petList=" + petList +
'}';
}
/*方法说明注释
一、快捷键
输入/** ,点击“Enter”,自动根据参数和返回值生成注释模板*/
/**
* 打印按平均值降序排列后的结果:
*/
public void printSortedResult(){
for(Pet pet:petList){
System.out.println(pet);
}
}
}

Pet类:

package experiment11.exp5;
public class Pet {
String name;
double age;
double weight;
double activation;
double sensitivity;
//体重、活跃度、灵敏度的平均值
double average;
public Pet(String name, double age, double weight, double activation, double sensitivity) {
this.name = name;
this.age = age;
this.weight = weight;
this.activation = activation;
this.sensitivity = sensitivity;
}
public double getAverage() {
return average=(weight+age+sensitivity)/3;
}
@Override
public String toString() {
return "Pet{" +
"name='" + name + '\'' +
", age=" + age +
", weight=" + weight +
", activation=" + activation +
", sensitivity=" + sensitivity +
", average=" + average +
'}';
}
}
posted @   xuchaoxin1375  阅读(9)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示