import java.util.*;public classPerson {privateString name;privateInteger age;privateString sex;/*** 兴趣爱好*/
private List hobbies = new ArrayList<>();/*** 喜欢吃的食物*/
private List foods = new ArrayList<>();/*** 个人经历*/
private Map personalHistory = new HashMap<>();publicString getName() {returnname;
}public voidsetName(String name) {if(!checkNameChese(name))throw new RuntimeException(name + "不规范!");this.name =name;
}publicInteger getAge() {returnage;
}public voidsetAge(Integer age) {if(null == age || age <=0 || age > 120)throw new RuntimeException(age + "不规范!");this.age =age;
}publicString getSex() {returnsex;
}public voidsetSex(String sex) {this.sex =sex;
}public ListgetHobbies() {returnhobbies;
}public void setHobbies(Listhobbies) {this.hobbies =hobbies;
}public ListgetFoods() {returnfoods;
}public void setFoods(Listfoods) {this.foods =foods;
}public voidaddFood(String food) {if(null ==food) {throw new RuntimeException("食物不为空!");
}for(String item : hobbies) {if(item.equals(food)) {throw new RuntimeException(food + "已填加!");
}
}
foods.add(food);
}public voiddelFood(String food) {if(null ==food) {throw new RuntimeException("食物不为空!");
}if(!hobbies.contains(food)) {throw new RuntimeException(food + "不存在!");
}
foods.remove(food);
}public voidaddHobby(String hobby) {if(null ==hobby) {throw new RuntimeException("兴趣爱好不为空!");
}for(String item : hobbies) {if(item.equals(hobby)) {throw new RuntimeException(hobby + "已填加!");
}
}
hobbies.add(hobby);
}public voiddelHobby(String hobby) {if(null ==hobby) {throw new RuntimeException("兴趣爱好不为空!");
}if(!hobbies.contains(hobby)) {throw new RuntimeException(hobby + "不存在!");
}
hobbies.remove(hobby);
}public voidaddHistory(Integer age, Object history) {if(null == age || age <=0) {throw new RuntimeException("年龄不符合规定!");
}if(null ==history) {throw new RuntimeException("个人经历不为空!");
}if(age > this.age) {throw new RuntimeException("个人经历不得大于");
}
Iterator> iterator =personalHistory.entrySet().iterator();while(iterator.hasNext()) {
Map.Entry next =iterator.next();if(next.getKey().equals(age) &&next.getValue().equals(history)) {throw new RuntimeException(age + "岁,个人经历:" + history + "已存在!");
}
}
personalHistory.put(age, history);
}public voiddelHistory(Integer age) {if(null == age || age <=0) {throw new RuntimeException("年龄不符合规定!");
}if(age > this.age) {throw new RuntimeException("个人经历不得大于");
}if(!personalHistory.containsKey(age)) {throw new RuntimeException(age + "岁,个人经历不存在!");
}
Iterator> iterator =personalHistory.entrySet().iterator();while(iterator.hasNext()) {
Map.Entry next =iterator.next();if(next.getKey().equals(age)) {
iterator.remove();
}
}
}/*** 判定输入的是否是汉字
*
*@paramc 被校验的字符
*
*@returntrue代表是汉字*/
private static boolean isChinese(charc) {
Character.UnicodeBlock ub=Character.UnicodeBlock.of(c);if (ub ==Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS|| ub ==Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS|| ub ==Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A|| ub ==Character.UnicodeBlock.GENERAL_PUNCTUATION|| ub ==Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION|| ub ==Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {return true;
}return false;
}/*** 校验String是否全是中文
*
*@paramname
* 被校验的字符串
*@returntrue代表全是汉字*/
private static booleancheckNameChese(String name) {boolean res = true;char[] cTemp = name.toCharArray();//转换为数组
for (int i = 0; i < name.length(); i++) {if (!isChinese(cTemp[i])) {//逐个判断是否为中文
res = false;break;
}
}returnres;
}
}