java泛型案例之电话簿

来源:互联网 发布:长颈鹿美语待遇 知乎 编辑:程序博客网 时间:2024/05/29 02:47

1、Person.java。记录电话簿的人名。

/* * To change this template, choose Tools | Templates * and open the template in the editor. */package PhoneText;import java.io.*;import java.util.*;/** * * @author pc */public class Person implements Comparable,Serializable{    private String firstName;    private String surName;        public Person(String firstName,String surName)    {        this.firstName=firstName;        this.surName=surName;    }        public String toString()    {        return firstName+""+surName;    }        public int compareTo(Object person)    {        int result=surName.compareTo(((Person)person).surName);        return result==0?firstName.compareTo(((Person)person).firstName):result;    }        public boolean equals(Object person)    {        return compareTo(person)==0;    }        public int hashCode()    {        return 7 * firstName.hashCode()+13 * surName.hashCode();    }        public static Person readPerson()    {        Scanner in = new Scanner(System.in);        System.out.println("\nEnter first name:");        String firstName = in.nextLine().trim();                System.out.println("Enter surName:");        String surName=in.nextLine().trim();        return new Person(firstName,surName);    }}
2、PhoneNumber.java。记录某个电话号码的信息,包括区号和电话号码。
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package PhoneText;import java.io.*;import java.util.*;/** * * @author pc */class PhoneNumber implements Serializable{    private String areaCode;    private String number;    public PhoneNumber(String areaCode,String number)    {        this.areaCode=areaCode;        this.number=number;    }    public String toString() {        return areaCode + " " + number;    }        public static PhoneNumber readNumber()    {        Scanner in = new Scanner(System.in);        System.out.println("\nEnter the area code:");        String area = in.nextLine();//        System.out.println("Enter the local code:");//        area = in.nextLine();        System.out.println("Enter the number:");        String number =""+in.nextLine();        return new PhoneNumber(area,number);    }}
3、BookEntry.java。记录电话簿的每一项内容,包括人名和电话号码。

/* * To change this template, choose Tools | Templates * and open the template in the editor. */package PhoneText;/** * * @author pc */public class BookEntry {    private Person person;    private PhoneNumber number;    public BookEntry(Person person, PhoneNumber number) {        this.person = person;        this.number = number;    }        public Person getPerson(){return person;}    public PhoneNumber getNumber(){return number;}    public String toString(){ return person.toString()+"\n"+number.toString();}    public static BookEntry readEntry()    {        return new BookEntry(Person.readPerson(),PhoneNumber.readNumber());    }}
4、PhoneBook.java。作为电话簿存储相应信息。
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package PhoneText;import java.util.*;/** * * @author pc */class PhoneBook {    private HashMap<Person,BookEntry>phonebook=new HashMap<Person,BookEntry>();    public void addEntry(BookEntry entry)    {        phonebook.put(entry.getPerson(), entry);    }        public BookEntry getEntry(Person key)    {        return (BookEntry)phonebook.get(key);    }        public PhoneNumber getNumber(Person key)    {        return getEntry(key).getNumber();    }}
5、TryPhoneBook.java。主面板。

/* * To change this template, choose Tools | Templates * and open the template in the editor. */package PhoneText;import java.util.*;/** * * @author pc */public class TryPhoneBook {    public static void main(String[] args) {        PhoneBook book = new PhoneBook();        Scanner in = new Scanner(System.in);        Person someone;        for(;;)        {            System.out.println("Enter 1 to enter a new phone book entry\n"+"Enter 2 to find the number for a name\nEnter 9 to quit");            int what = in.nextInt();            switch(what){                case 1:                    book.addEntry(BookEntry.readEntry());                    break;                case 2:                    someone = Person.readPerson();                    BookEntry entry = book.getEntry(someone);                    if(entry==null)                        System.out.println("The number for"+someone+"was not found.");                    else                        System.out.println("The number for"+someone+"is"+book.getEntry(someone).getNumber());                    break;                case 9:                    System.out.println("Ending program.");                    return;                default:                    System.out.println("Invalid delection,try again.");                    break;            }        }    }}

如果出现中文乱码,将项目的编码设置为GB2312。!!!


原创粉丝点击