本帖最后由 陈圳 于 2013-4-13 15:52 编辑
把实现所有功能完善的代码写出来了.但所用知识不局限于前8章了, 我是怎么简单怎么做的.感觉实现的还是很麻烦.希望有高手给出更简单的方法实现这些功能.- package exercises;
- import java.util.Calendar;
- import java.util.Comparator;
- import java.util.Iterator;
- import java.util.TreeSet;
- public class ItCastStudent {
- 用一种效率高的方法把后面两点做出来,所以不管什么知识,想到用上能做到就行了.
- 只能想到到用TreeSet的比较器来实现.这个效率要高点,因为底层好歹是二叉树,而for嵌套暴力查找.
- 程序花费的内存开销是呈:N的平方扩大的.数据越多,程序就越慢.
- public static void main(String[] args) {
- getMsg();
- }
- public static void getMsg(){
- TreeSet<ItStudent> itcast=new TreeSet<ItStudent>(new AgeCompara());
- itcast.add(new ItStudent("老邵","152127198910054350","喝酒","不抽烟",1962));
- itcast.add(new ItStudent("老刘","341222199112266570","喝酒","抽烟",1109));
- itcast.add(new ItStudent("无名","431222199112266570","喝酒","抽烟",1882));
- itcast.add(new ItStudent("老葛","341222198908176310","不喝酒","抽烟",1127));
- itcast.add(new ItStudent("清新她妹","111111199104135420","不喝酒","不抽烟",698));
- itcast.add(new ItStudent("某某","152127199207246580","喝酒","不抽烟",0));
- TreeSet<ItStudent> tempItcast=new TreeSet<ItStudent>(new DistanceCompara());
- for(Iterator<ItStudent> in=itcast.iterator();in.hasNext();tempItcast.add(in.next())); //俩个迭代我试过整合,但不行.
- for(Iterator<ItStudent> in=itcast.iterator();in.hasNext();System.out.println(in.next()));
- }
- }
- }
- class DistanceCompara implements Comparator<ItStudent>{//同乡的比较器
- @Override
- public int compare(ItStudent o1, ItStudent o2) {
- int distance=o1.getDistance()-o2.getDistance();
- if(Math.abs(distance)<=150&&o1.getName()!=o2.getName()){//这个值要自己定我认为150里以内的算是老乡
- o1.setCountryMen(o2.getName());
- o2.setCountryMen(o1.getName());
- }
- if(distance==0)
- return o2.getName().compareTo(o1.getName());
- else return distance;
- }
-
- }
- class AgeCompara implements Comparator<ItStudent>{
- @Override
- public int compare(ItStudent o1, ItStudent o2) {
- int age=o2.getAge()-o1.getAge();
- if(age==0&&o1.getName()!=o2.getName()){//这里就说明这俩个人肯定同年龄了
- //这里有个小问题,如果没有&&o1.getName()!=o2.getName()这一句的话.就会出现o1,o2是一个人的情况,我还没找到原因
- o2.setSameMen(o1.getName());//然后设置同名人
- o1.setSameMen(o2.getName());
- return o2.getName().compareTo(o1.getName());
- }
- else return age;
- }
- }
- class ItStudent{
- private String name;//姓名
- private String id;//身份证号
- private String hobby;//抽烟
- private String unHobby;//喝酒,我以为是讨厌的事.
- private String sameMen=null;//同龄人
- private String countryMen=null;//老乡
- private int nowYear=Calendar.getInstance().get(Calendar.YEAR);//在这与的过程中看到经常用这个,就写在这里.
-
- //计算属性的一些方法
- //然后可以用2013代替,其他的日期也一样,但是不利于维护了.
- public ItStudent(String name,String id,String hobby,String unHobby,int distance){//构造器
- this.name=name;
- this.id=id;
- this.hobby=hobby;
- this.unHobby=unHobby;
- this.distance=distance;
- }
- private int getDays(){//这个人活了多少天;
- int sumDays=0;
- int birthYear=getAge();
- int birthMouths=Integer.parseInt(this.id.substring(11,13));
- int birthDays=Integer.parseInt(this.id.substring(13,15));
- int nowYear=this.nowYear;
- int nowMouths=Calendar.getInstance().get(Calendar.MONTH)+1;
- int nowDays=Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
- sumDays+=getDays(birthYear,birthMouths)+getDays(nowYear,nowMouths)+nowDays-birthDays;//获得这出生日和今年己过的天数
- sumDays+=leapYearCount()*366+(this.nowYear-this.getAge()+leapYearCount())*355;
- return sumDays;
- }
- public int getDays(int year,int month){//获取给定日期的距离年过了多久.
- boolean isLeap=this.isLeapYear(year);
- int sumDays=0;
- while(month-->=0){
- switch(year){
- case 1:case 3:case 5:case 7:case 8:case 10:case 12:
- sumDays+=31;break;
- case 4:case 6:case 9:case 11:
- sumDays+=30;break;
- }
- }
- if(isLeap)
- return sumDays+=29;
- else return sumDays+=28;
- }
- private int leapYearCount(){
- int count=0;//计算活着闰年的次数
- int year=this.nowYear;
- int length=year-this.getAge();
- for(int i=getAge()+1;i<length;i++){
- if(this.isLeapYear(i))
- count++;
- }
- return count;
- }
- private boolean isLeapYear(int year){
- if(year%4==0&&year%100!=0||year%100==0&&year%400==0)
- return true;
- else return false;
- }
- public String getHobby(){
- String hobby="我";
- if(this.hobby.length()<3)
- hobby+=this.hobby;
- if(this.unHobby.length()<3)
- hobby+=this.unHobby;
- if(hobby.length()==1)
- return "我不抽烟,不喝酒,";
- else return hobby;
- }
-
- //私有属性的获取方法
- private int getToBJHour(){
- return this.distance/110;
- }
- private int getToBJTime(){
- return this.getToBJHour()+7;
- }
- public String getSex(){//返回性别
- if(this.id.charAt(16)%2!=0)
- return "男";
- else return "女";
- }
- public int getAge(){//返回年龄
- return Integer.parseInt(this.id.substring(6,10));
- }
- public String getName() {
- return name;
- }
- public String getCountryMen() {
- return countryMen;
- }
- public void setCountryMen(String countryMen) {
- this.countryMen = countryMen;
- }
- private int distance;
- public int getDistance() {
- return distance;
- }
- public String getSameMen() {
- return sameMen;
- }
- public void setSameMen(String same) {//量少的用String还可以,超过5次以上我就用StringBuilder
- if(this.sameMen!=null)
- this.sameMen += same;
- else this.sameMen=same;
- }
-
- //打印信息
- public String toString(){//姓名 年龄 性别 从出生
- StringBuilder msg=new StringBuilder();
- if(this.getToBJHour()==0)
- msg.append("我叫:"+name+" 年龄:"+this.getAge()+" 性别:"+getSex()+
- " 我一共活了:"+getDays()+"天,"+getHobby()+"我家就在北京,所以不花时间~");
- else {
- msg.append("我叫:"+name+" 年龄:"+this.getAge()+" 性别:"+getSex()+
- " 我一共活了:"+getDays()+"天,"+getHobby()+",我来北京用了"+
- this.getToBJHour()+"小时,"+"现在是:"+this.getToBJTime()+"点");
- }
- if(this.countryMen!=null)
- msg.append(",我的老乡是:"+this.countryMen);
- if(this.sameMen!=null)
- msg.append(",与我同龄的娃是:"+this.sameMen);
- return msg.toString();
- }
- }
复制代码 |