package com.sxt.exception;
public class Programmer implements Comparable<Programmer>{
private String name;
private int age;
private double weight; //体重单位:kg
private double hight; //身高单位:cm
public Programmer() {
super();
}
public Programmer(String name, int age, double weight, double hight) throws NameException,
AgeException, WeightException, HightException {
super();
if(name.trim().length() == 0 || name == null || name.trim().length() > 20) {
throw new NameException("人名不能为空,并且不能超过二十个字符!");
}
this.name = name;
if(age < 0 || age > 120) {
throw new AgeException("年龄不能小于0岁或者大于120岁,请核实!");
}
this.age = age;
if(weight < 40 || weight > 100) {
throw new WeightException("此程序员身体太差,或胖或瘦,请核实!");
}
this.weight = weight;
if(hight < 150 || hight > 200) {
throw new HightException("此人身高可能过低或者过高,请核实!");
}
this.hight = hight;
}
public String getName() {
return name;
}
public void setName(String name) throws NameException {
if(name.trim().length() == 0 || name == null || name.trim().length() > 20) {
throw new NameException("人名不能为空,并且不能超过二十个字符!");
}
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) throws AgeException {
if(age < 0 || age > 120) {
throw new AgeException("年龄不能小于0岁或者大于120岁,请核实!");
}
this.age = age;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) throws WeightException {
if(weight < 40 || weight > 100) {
throw new WeightException("此程序员身体太差,或胖或瘦,请核实!");
}
this.weight = weight;
}
public double getHight() {
return hight;
}
public void setHight(double hight) throws HightException {
if(hight < 150 || hight > 200) {
throw new HightException("此人身高可能过低或者过高,请核实!");
}
this.hight = hight;
}
@Override
public boolean equals(Object obj) {
if(this == obj) return true;
if(obj == null) return false;
if(this.getClass() != obj.getClass()) return false;
if(obj instanceof Programmer) {
Programmer programmer = (Programmer) obj;
return this.name.equals(programmer.name) && this.age==programmer.age &&
this.weight==programmer.weight && this.hight==programmer.hight;
}
return false;
}
@Override
public String toString() {
return this.getClass().getName() + " name=" + name + ", age=" + age + ", weight="
+ weight + ", hight=" + hight;
}
@Override
public int compareTo(Programmer other) {
if(this.age > other.age) return 1;
if(this.age < other.age) return -1;
return 0;
}
}
自定义异常类:NameException
public class NameException extends Exception {
private static final long serialVersionUID = -2992284109668400826L;
public NameException(String message) {
super(message);
}
}
自定义异常类:AgeException
public class AgeException extends Exception {
private static final long serialVersionUID = -707396355691365455L;
public AgeException(String message) {
super(message);
}
}
自定义异常类:WeightException
public class WeightException extends Exception {
private static final long serialVersionUID = 533854535537735838L;
public WeightException(String message) {
super(message);
}
}
自定义异常类:HightException
public class HightException extends Exception {
private static final long serialVersionUID = -5100566806826020540L;
public HightException(String message) {
super(message);
}
}
测试类:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class TestProgrammer {
public static void main(String[] args) {
List<Programmer> list = new ArrayList<>();
Programmer programmer1 = null;
Programmer programmer2 = null;
Programmer programmer3 = null;
try {
programmer1 = new Programmer("李云龙", 45, 67, 180);
programmer2 = new Programmer("佟大为", 32, 75, 185);
programmer3 = new Programmer("刘德华", 54, 76, 175);
} catch (NameException | AgeException | WeightException
| HightException e) {
e.printStackTrace();
}
list.add(programmer1);
list.add(programmer2);
list.add(programmer3);
Programmer[] programmers = new Programmer[list.size()];
list.toArray(programmers);
Arrays.sort(programmers);
System.out.println("按照年龄排序:");
for(Programmer programmer : programmers) {
System.out.println(programmer.toString());
}
System.out.println("==================================================================");
System.out.println(programmer1.toString());
System.out.println(programmer2.toString());
System.out.println("programmer1 = programmer2?:" + programmer1.equals(programmer2));
}
}
转载于:https://my.oschina.net/hasonger/blog/209107