Java集合寫的通訊錄


這是我們這一學期的實踐大作業~

花了大概七八個小時寫出來的......

用Vector集合儲存信息,實現了增刪改查和排序、退出六個基本功能~

說實話感覺多余且麻煩,但要求是這樣,我也沒辦法......

一共五個文件,分別為Person、Menu、App、Operate、TelNoteRegex:

Person類為實例類

Menu類定義一個主菜單和各個子菜單的方法

App類只有一個作用,在主方法中創建Menu類的對象,並調用主菜單方法......

Operate類就是各個具體操作的實現方法

TelNoteRegex類定義了對信息進行驗證的方法,畢竟輸入的信息不能什么都接受~

 

好了下邊貼代碼:

 

App.java

class Application
{
    public static void main(String[] args)
    {
        Menu menu = new Menu();
        menu.mainMenu();
    }
}

 

Menu.java

import java.util.*;
public class Menu
{
    Operate operate = new Operate();
    Scanner in = new Scanner(System.in);
    public void mainMenu()
    {
        while(true)
        {
            System.out.println("***主菜單***");
            System.out.println("☆1:添加記錄");
            System.out.println("☆2:查找記錄");
            System.out.println("☆3:修改記錄");
            System.out.println("☆4:刪除記錄");
            System.out.println("☆5:排序記錄");
            System.out.println("☆6:退出系統");
            System.out.println("請選擇您要進行的操作(1~6):");
            String choice = in.nextLine().replace(" ","");
            switch(choice)
            {
                case "1":
                    printLine();
                    addMenu();
                    break;
                case "2":
                    printLine();
                    searchMenu();
                    break;
                case "3":
                    printLine();
                    modifyMenu();
                    break;
                case "4":
                    printLine();
                    deleteMenu();
                    break;
                case "5":
                    printLine();
                    orderMenu();
                    break;
                case "6":
                    exitMenu();
                    break;
                default:
                    System.out.println("您的輸入有誤!請重新輸入!");
                    printLine();
            }
        }
    }
    
    //添加記錄對應的菜單
    public void addMenu()
    {
        int a = 1;
        while(true)
        {
            System.out.println("☆1:添加新紀錄");
            System.out.println("☆2:查看全紀錄");
            System.out.println("☆3:返回上一級");
            System.out.println("請選擇您要進行的操作(1~3):");
            String choice01 = in.nextLine().replace(" ","");
            switch(choice01)
            {
                case "1":
                    operate.addOperation();
                    break;
                case "2":
                    operate.showAll();
                    break;
                case "3":
                    a = 0;
                    printLine();
                    break;
                default:
                    System.out.println("您的輸入有誤!請重新輸入!");
                    printLine();
            }
            if(a == 0)
                break;
        }
    }
    
    //查找記錄所對應的菜單
    public void searchMenu()
    {
        int a = 1;
        while(true)
        {
            System.out.println("☆1:按姓名查找");
            System.out.println("☆2:按年齡查找");
            System.out.println("☆3:按性別查找");
            System.out.println("☆4:按號碼查找");
            System.out.println("☆5:按住址查找");
            System.out.println("☆6:查看全記錄");
            System.out.println("☆7:返回上一級");
            System.out.println("請輸入您的選擇(1~7):");
            String choice02 = in.nextLine().replace(" ","");
            switch(choice02)
            {
            case "1":
                operate.searchByName();
                printLine();
                break;
            case "2":
                operate.searchByAge();
                printLine();
                break;
            case "3":
                operate.searchBySex();
                printLine();
                break;
            case "4":
                operate.searchByTelNum();
                printLine();
                break;
            case "5":
                operate.searchByAddress();
                printLine();
                break;
            case "6":
                operate.showAll();
                printLine();
                break;
            case "7":
                a = 0;
                printLine();
                break;
            default:
                System.out.println("您的選擇有誤!請重新輸入!");
                printLine();
            }
            if(a == 0)
                break;
        }
    }
    
    //修改記錄對應的菜單
    public void modifyMenu()
    {
        int a = 1;
        while(true)
        {
            System.out.println("☆1:查看全記錄");
            System.out.println("☆2:修改指定記錄");
            System.out.println("☆3:返回上一級");
            System.out.println("請輸入您的選擇(1~3):");
            String choice03 = in.nextLine().replace(" ","");
            switch(choice03)
            {
            case "1":
                operate.showAll();
                break;
            case "2":
                subModifyMenu();
                break;
            case "3":
                a = 0;
                printLine();
                break;
            default:
                System.out.println("您的輸入有誤!請重新輸入!");
                printLine();
            }
            if(a == 0)
            break;
        }    
    }
    
    //修改信息的子菜單
    public void subModifyMenu()
    {
        int a = 1;
        while(true)
        {
            printLine();
            System.out.println("1:修改姓名");
            System.out.println("2:修改年齡");
            System.out.println("3:修改性別");
            System.out.println("4:修改號碼");
            System.out.println("5:修改地址");
            System.out.println("6:返回上一級");
            System.out.println("請輸入您的選擇(1~6):");
            String choice0301 = in.nextLine().replace(" ","");
            switch(choice0301)
            {
            case "1":
                operate.modify("name");
                break;
            case "2":
                operate.modify("age");
                break;
            case "3":
                operate.modify("sex");
                break;
            case "4":
                operate.modify("telNum");
                break;
            case "5":
                operate.modify("address");
                break;
            case "6":
                a = 0;
                printLine();
                break;
            default:
                System.out.println("您的輸入有誤!請重新輸入!");
                printLine();    
            }
            if(a == 0)
                break;
        }
    }
    
    //刪除記錄對應的菜單
    public void deleteMenu()
    {
        int a= 1;
        while(true)
        {
            System.out.println("☆1:查看全記錄");
            System.out.println("☆2:刪除指定記錄");
            System.out.println("☆3:刪除全部記錄");
            System.out.println("☆4:返回上一級");
            System.out.println("請輸入您的選擇(1~4):");
            String choice04 = in.nextLine().replace(" ","");
            switch(choice04)
            {
            case "1":
                operate.showAll();
                break;
            case "2":
                operate.delete();
                Menu.printLine();
                break;
            case "3":
                operate.deleteAll();
                Menu.printLine();
                break;
            case "4":
                a = 0;
                printLine();
                break;
            default:
                System.out.println("您的輸入有誤!請重新輸入!");
                printLine();
            }
            if(a == 0)
                break;
        }
    }
    
    //排序記錄對應的菜單
    public void orderMenu()
    {
        int a = 1;
        while(true)
        {
            System.out.println("☆1:按姓名排序");
            System.out.println("☆2:按年齡排序");
            System.out.println("☆3:按性別排序");
            System.out.println("☆4:查看全部記錄");
            System.out.println("☆5:返回上一級");
            System.out.println("請輸入您的選擇(1~5):");
            String choice05 = in.nextLine().replace(" ","");
            switch(choice05)
            {
            case "1":
                operate.orderName();
                break;
            case "2":
                operate.orderAge();
                break;
            case "3":
                operate.orderSex();
                break;
            case "4":
                operate.showAll();
                break;
            case "5":
                a = 0;
                printLine();
                break;
            default:
                System.out.println("您的輸入有誤!請重新輸入!");
                printLine();
            }
            if(a == 0)
                break;
        }
    }
    
    //退出操作對應的菜單
    public void exitMenu()
    {
        System.out.println("您確定要退出嗎?(y/n)");
        String choice06 = in.nextLine().replace(" ","");
        if(choice06.equals("Y") || choice06.equals("y"))
        {
            System.out.println("您已經成功退出!");
            printLine();
            System.exit(0);
        }
        else if(choice06.equals("N") || choice06.equals("n"))
        {
            System.out.println("您已經取消了退出操作!");
        }
        else
        {
            System.out.println("您的輸入有誤!默認取消退出操作!");
        }
        printLine();
    }
    
    //打印分割線
    public static void printLine()
    {
        System.out.println("----------------------------------------------------------------"
                +"----------------------------------------------------------");
    }
    
    //打印信息欄
    public static void printInfo()
    {
        System.out.println("序號\t\t"+"姓名\t\t"+"年齡\t\t"+"性別\t\t"+"電話號碼\t\t"+"地址\t\t");
    }
}

 

Operate.java

import java.util.*;
public class Operate 
{
    Scanner in = new Scanner(System.in);
    Vector<Person> vector = new Vector<Person>();
    TelNoteRegex regex = new TelNoteRegex();
    
    //添加信息方法
    public void addOperation()
    {
        int a = 1;
        String name;
        String age;
        String sex;
        String telNum;
        String address;
        System.out.println("輸入姓名,1~10位字母:");
        name = in.nextLine().replace(" ","");
        System.out.println("輸入年齡,1~100+");
        age = in.nextLine().replace(" "," ");
        System.out.println("輸入性別(男M或m),女(F或f):");
        sex = in.nextLine().replace(" ","");
        System.out.println("輸入電話號碼,6~10位數字:");
        telNum = in.nextLine().replace(" ","");
        System.out.println("輸入地址,1~50位字母或數字:");
        address = in.nextLine().replace(" ","");
        if(!regex.nameRegex(name))
        {
            a = 0;
            System.out.println("您輸入的姓名格式不符合要求");
        }
        if(!regex.ageRegex(age))
        {
            a = 0;
            System.out.println("您輸入的年齡格式不符合要求");
        }
        if(!regex.sexRegex(sex))
        {
            a = 0;
            System.out.println("您輸入的性別格式不符合要求");
        }
        if(!regex.telNumRegex(telNum))
        {
            a = 0;
            System.out.println("您輸入的電話號碼格式不符合要求");
        }
        if(!regex.addressRegex(address))
        {
            a = 0;
            System.out.println("您輸入的地址格式不符合要求");
        }
        if(a == 1)
        {
            Person p1 = new Person(name,age,sex,telNum,address);
            vector.add(p1);
            System.out.println("添加成功!");
        }
        Menu.printLine();
    }
    
    //顯示全部信息方法
    public void showAll()
    {
        if(vector.isEmpty())
            System.out.println("沒有任何信息記錄!");
        else
        {
            Menu.printInfo();
            Enumeration<Person> en = vector.elements();
            int id = 0;
            while(en.hasMoreElements())
            {
                Person p2 = en.nextElement();
                System.out.print(id+1+"\t\t");
                System.out.println(p2);
                id++;
            }
        }
        Menu.printLine();
    }
    
    //按姓名查找
    public void searchByName()
    {
        System.out.println("請輸入要查找的名字:");
        String str = in.nextLine().replace(" ","");
        Menu.printInfo();
        for(Person p3 : vector)
        {
            if(p3.getName().equals(str))
                System.out.println(p3);
        }
    }
    
    //按年齡查找
    public void searchByAge()
    {
        System.out.println("請輸入要查找的年齡:");
        String str = in.nextLine().replace(" ","");
        Menu.printInfo();
        for(Person p3 : vector)
        {
            if(p3.getAge().equals(str))
                System.out.println(p3);
        }
    }
    
    //按性別查找
    public void searchBySex()
    {
        System.out.println("請輸入要查找的性別(男M或m),女(F或f):");
        String str = in.nextLine().replace(" ","");
        Menu.printInfo();
        for(Person p3 : vector)
        {
            if(p3.getSex().equals(str))
                System.out.println(p3);
        }
    }
    
    //按電話號碼查找
    public void searchByTelNum()
    {
        System.out.println("請輸入要查找的電話號碼:");
        String str = in.nextLine().replace(" ","");
        Menu.printInfo();
        for(Person p3 : vector)
        {
            if(p3.getTelNum().equals(str))
                System.out.println(p3);
        }
    }
    
    //按地址查找
    public void searchByAddress()
    {
        System.out.println("請輸入要查找的地址:");
        String str = in.nextLine().replace(" ","");
        Menu.printInfo();
        for(Person p3 : vector)
        {
            if(p3.getAddress().equals(str))
                System.out.println(p3);
        }
    }
    
    //按姓名排序信息
    public void orderName()
    {
        Collections.sort( vector,new nameComparator());
        System.out.println("排序成功!");
        System.out.println("按姓名排序的結果如下:");
        showAll();
    }
    //自定義姓名排序類
    class nameComparator implements Comparator<Person>
    {
        public int compare(Person p1,Person p2)
        {
            return p1.getName().compareTo(p2.getName());
        }
    }
    
    //按年齡排序信息
    public void orderAge()
    {
        Collections.sort( vector,new ageComparator());
        System.out.println("排序成功!");
        System.out.println("按年齡排序的結果如下:");
        showAll();
    }
    //自定義年齡排序類
    class ageComparator implements Comparator<Person>
    {
        public int compare(Person p1,Person p2)
        {
            return Integer.valueOf(p1.getAge()).compareTo(Integer.valueOf(p2.getAge()));
        }
    }
    
    //按性別排序信息
    public void orderSex()
    {
        Collections.sort( vector,new sexComparator());
        System.out.println("排序成功!");
        showAll();
    }
    //自定義性別排序類
    class sexComparator implements Comparator<Person>
    {
        public int compare(Person p1,Person p2)
        {
            return p1.getSex().compareTo(p2.getSex());
        }
    }
    
    //刪除指定信息方法
    public void delete()
    {
        if(vector.isEmpty())
            System.out.println("沒有任何信息記錄!");
        else
        {
            Menu.printInfo();
            Enumeration<Person> en = vector.elements();
            int id = 0;
            while(en.hasMoreElements())
            {
                Person p2 = en.nextElement();
                System.out.print(id+1+"\t\t");
                System.out.println(p2);
                id++;
            }
            System.out.println("請輸入您要刪除的信息的序號(1~"+id+")");
            String num = in.nextLine().replace(" ","");
            if(!regex.isAllDigit(num))
            {
                System.out.println("您的輸入有誤!");
            }
            else
            {
                int num1 = Integer.valueOf(num);
                if(num1<1)
                    System.out.println("您的輸入有誤!");
                else
                {
                    vector.remove(num1-1);
                    System.out.println("刪除成功!");
                }
            }
        }
    }
    
    //刪除所有信息方法
    public void deleteAll()
    {
        System.out.println("您確定要刪除全部信息嗎?(y/n)");
        String choice = in.nextLine().replace(" ","");
        if(choice.equals("Y") || choice.equals("y"))
        {
            Enumeration<Person> en = vector.elements();
            while(en.hasMoreElements())
            {
                vector.clear();
                System.out.println("信息已經被全部刪除!");
            }
        }
        else if(choice.equals("N") || choice.equals("n"))
        {
            System.out.println("已取消刪除操作!");
        }
        else
        {
            System.out.println("您的輸入有誤!默認取消刪除操作!");
        }
    }
    
    //修改信息方法
    public void modify( String choice)
    {
        int id = 0;
        if(vector.isEmpty())
            System.out.println("沒有任何信息記錄!");
        else
        {
            Menu.printInfo();
            Enumeration<Person> en = vector.elements();
            while(en.hasMoreElements())
            {
                Person p2 = en.nextElement();
                System.out.print(id+1+"\t\t");
                System.out.println(p2);
                id++;
            }
        }
        System.out.println("請輸入您要修改的信息的序號(1~"+id+")");
        String num = in.nextLine().replace(" ","");
        if(!regex.isAllDigit(num))
        {
            System.out.println("您的輸入有誤!");
        }
        else
        {
            int num1 = Integer.valueOf(num);
            if(num1<1 || num1>id)
                System.out.println("您的輸入有誤!");
            else
            {
                if(choice.equals("name"))
                {
                    System.out.println("請輸入新的姓名");
                    String newName = in.nextLine().replace(" ","");
                    if(!regex.nameRegex(newName))
                        System.out.println("您輸入的新姓名不符合格式!");
                    else
                    {
                        vector.elementAt(num1-1).setName(newName);
                        System.out.println("姓名修改成功!");
                    }
                }
                else if(choice.equals("age"))
                {
                    System.out.println("請輸入新的年齡:");
                    String newAge = in.nextLine().replace(" ","");
                    if(!regex.ageRegex(newAge))
                        System.out.println("您輸入的新姓名不符合格式!");    
                    else
                    {
                        vector.elementAt(num1-1).setAge(newAge);
                        System.out.println("年齡修改成功!");
                    }
                }
                else if(choice.equals("sex"))
                {
                    System.out.println("請輸入新的性別(男M或m),女(F或f):");
                    String newSex = in.nextLine().replace(" ","");
                    if(!regex.sexRegex(newSex))
                        System.out.println("您輸入的新性別不符合格式!");    
                    else
                    {
                        vector.elementAt(num1-1).setSex(newSex);
                        System.out.println("性別修改成功!");
                    }
                }
                else if(choice.equals("telNum"))
                {
                    System.out.println("請輸入新的年齡:");
                    String newTelNum = in.nextLine().replace(" ","");
                    if(!regex.telNumRegex(newTelNum))
                        System.out.println("您輸入的新電話號碼不符合格式!");    
                    else
                    {
                        vector.elementAt(num1-1).setTelNum(newTelNum);
                        System.out.println("電話號碼修改成功!");
                    }
                }
                else if(choice.equals("address"))
                {
                    System.out.println("請輸入新的地址:");
                    String newAddress = in.nextLine().replace(" ","");
                    if(!regex.addressRegex(newAddress))
                        System.out.println("您輸入的新地址不符合格式!");    
                    else
                    {
                        vector.elementAt(num1-1).setAddress(newAddress);
                        System.out.println("地址修改成功!");
                    }
                }
            }
        }
    }
}

 

TelNoteRegex.java

public class TelNoteRegex 
{
    
    //檢查姓名是否符合格式
    public boolean nameRegex(String name)
    {
        boolean b = true;
        if(name.length()<1 || name.length()>10)
        {
            b = false;
        }
        char [] array = name.toCharArray();
        for(int i=0;i<array.length;i++)
        {
            if( !(array[i] >='A' && array[i] <='Z') && !(array[i] >='a' && array[i] <='z') )
            {
                b = false;
                break;
            }
        }
        return b;
    }
    
    //檢查年齡是否符合各式
    public boolean ageRegex(String age)
    {
        boolean b = true;
        if(!isAllDigit(age))
            return false;
        else
        {
            int num = Integer.parseInt(age);
            if(num<=0 || num>200)
                return false;
        }
        return b;
    }
    
    //檢查性別是否符合要求
    public boolean sexRegex(String sex)
    {
        boolean b = true;
        if( !sex.equals("M") && !sex.equals("m") && !sex.equals("F") && !sex.equals("f"))
            b = false;
        return b;
    }
    
    //檢查電話號碼
    public boolean telNumRegex(String telNum)
    {
        boolean b = true;
        if(!isAllDigit(telNum))
            return false;
        else
        {
            if( !( telNum.length() >=6 && telNum.length() <=10))
                return false;
        }
        return b;
    }
    
    //檢查地址
    public boolean addressRegex(String address)
    {
        boolean b = true;
        if( address.length()<1 || address.length()>50 )
            return false;
        char array[] = address.toCharArray();
        for(int i=0;i<array.length;i++)
        {
            if( !( ( array[i]>='0' && array[i]<='9' ) || ( array[i]>='a' && array[i]<='z' ) 
                    || ( array[i]>='A' && array[i]<='Z' ) ) )
                b = false;
        }
        return b;
    }
    
    //檢查傳入的字符串是否全部為數字
    public boolean isAllDigit(String str)
    {
        boolean b = true;
        char array[] = str.toCharArray();
        for(int i=0;i<array.length;i++)
        {
            if( !(array[i] >='0' && array[i] <='9'))
            {
                b = false;
                break;
            }
        }
        return b;
    }
}

 

Person.java

public class Person
{
    private int id;
    private String name;
    private String age;
    private String sex;
    private String telNum;
    private String address;
    
    public Person(){}
    
    public Person(String name, String age, String sex, String telNum, String address) {
        super();
        this.name = name;
        this.age = age;
        this.sex = sex;
        this.telNum = telNum;
        this.address = address;
    }
    public int getId() 
    {
        return id;
    }
    public void setId(int id) 
    {
        this.id = id;
    }

    public String getName() 
    {
        return name;
    }
    public void setName(String name) 
    {
        this.name = name;
    }
    
    public String getAge() 
    {
        return age;
    }
    public void setAge(String age) 
    {
        this.age = age;
    }

    public String getSex() 
    {
        return sex;
    }
    public void setSex(String sex) 
    {
        this.sex = sex;
    }

    public String getTelNum() 
    {
        return telNum;
    }
    public void setTelNum(String telNum) 
    {
        this.telNum = telNum;
    }

    public String getAddress() 
    {
        return address;
    }
    public void setAddress(String address) 
    {
        this.address = address;
    }
    
    public String toString()
    {
        String str = null;
        if( sex.equals("M") || sex.equals("m") )
            str = "男";
        else if( sex.equals("F") || sex.equals("f") )
            str = "女";
        return name+"\t\t"+age+"\t\t"+str+"\t\t"+telNum+"\t\t"+address;
    }
}

 

其實Person類中定義的Id屬性根本就沒用上,因為在后邊的排序操作時所有的信息的順序都可能會被打亂......所以正在顯示全部信息的時候才給它們加上了Id,這個Id是在showAll()方法體內定義的,在其他需要輸出的地方也都是這樣解決的......我才疏學淺,暫時沒想到更好的解決辦法~如果有大神有辦法,還請在評論區中不吝賜教!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM