使用JAVA編寫電話薄程序,具備添加,查找,刪除等功能


//該程序需要連接數據庫。根據word文檔要求所有功能均已實現。
//大部分方法基本差不多,
//在查詢修改的時候能輸出 最大ID號 和最小ID號,並且可以對輸入的ID號進行判斷是否存在(具體方法請查看 修改電話號碼, 其他修改方法暫時沒有相對完善,
                                        只針對修改電話號碼的方法進行了修改,方便后期對編寫程序思路的查看)。










package
電話薄1; public class Menu { //主菜單 public void mainMenu() { for(int i = 0 ; i >= 0 ; i ++ ) { System.out.println("***********************\n** 1.添加記錄 **\n" + "** 2.查找記錄 **\n" + "** 3.修改記錄 **\n" + "** 4.刪除記錄 **\n" + "** 5.排序記錄 **\n" + "** 6.退出系統 ** \n" + "***********************"); System.out.println("請輸入正確的數字,最小是:1 最大是:6"); TelNoteRegex rtn = new TelNoteRegex(); Operate a = new Operate(); switch(rtn.menuRegex(1, 6)) { case 1: a.addLogic(); break; case 2: a.searchLogic(); break; case 3: a.modifyLogicLogic(); break; case 4: a.deleteLogic(); break; case 5: a.orderLogic(); break; case 6: System.out.println("程序已關閉,歡迎下次使用"); i=-2; break; } } } //添加用戶菜單 public void addMenu() { System.out.println("***********************\n" + "** 1.添加新記錄 **\n" + "** 2.查看全記錄 **\n" + "** 3.返回上一級 **\n" + "***********************"); System.out.println("請輸入正確的數字,最小是:1 最大是:3"); } //查找用戶菜單 void searchMenu() { System.out.println("***********************\n" + "** 1.按姓名查找 **\n" + "** 2.按年齡查找 **\n" + "** 3.按性別查找 **\n" + "** 4.按號碼查找 **\n" + "** 5.按住址查找 **\n" + "** 6.查看全記錄 **\n" + "** 7.返回上一級 **\n" + "***********************"); System.out.println("請輸入正確的數字,最小是:1 最大是:7"); } //修改用戶信息菜單 void modifyMenu() { System.out.println("***********************\n" + "** 1.查看全記錄 **\n" + "** 2.修改指定記錄 **\n" + "** 3.返回上一級 **\n" + "***********************"); System.out.println("請輸入正確的數字,最小是:1 最大是:3"); } //修改用戶信息子菜單 void subModifyMenu () { System.out.println("***********************\n" + "** 1.修改姓名 **\n" + "** 2.修改年齡 **\n" + "** 3.修改性別 **\n" + "** 4.修改號碼 **\n" + "** 5.修改住址 **\n" + "** 6.返回上一級 **\n" + "***********************"); System.out.println("請輸入正確的數字,最小是:1 最大是:6"); } //刪除用戶信息菜單 void deleteMenu () { System.out.println("***********************\n" + "** 1.查看全記錄 **\n" + "** 2.刪除指定記錄 **\n" + "** 3.刪除全部記錄 **\n" + "** 4.返回上級 **\n" + "************************"); System.out.println("請輸入正確的數字,最小是:1 最大是:6"); } //排序用戶信息菜單 void orderMenu () { System.out.println("***********************\n" + "** 1.按姓名排序 **\n" + "** 2.按年齡排序 **\n" + "** 3.按性別排序 **\n" + "** 4.查看全部記錄 **\n" + "** 5.返回上一級 **\n" + "***********************"); System.out.println("請輸入正確的數字,最小是:1 最大是:6"); } }
package 電話薄1;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class Operate {
    
    // 連接池
    ComboPooledDataSource cp = new ComboPooledDataSource("helloc3p0");
    
    


    // 用戶添加信息業務邏輯控制
    public void addLogic() {
        TelNoteRegex rtn = new TelNoteRegex();
        Operate i = new Operate();
        Menu m = new Menu();
        m.addMenu();
        switch (rtn.menuRegex(1, 3)) {
        case 1:
            addOperation();
//            break;

        case 2:
            List<Person> lb = i.showAll();
//            break;
        }
    }

    // 用戶查詢信息業務邏輯控制
    public void searchLogic() {
        TelNoteRegex rtn = new TelNoteRegex();
        Menu m = new Menu();
        m.searchMenu();
        switch (rtn.menuRegex(1, 7)) {
        case 1:
            searchByName();
            break;

        case 2:
            searchByAge();
            break;

        case 3:
            searchBySex();
            break;

        case 4:
            searchByTelNum();
            break;

        case 5:
            searchByAdd();
            break;

        case 6:
            showAll();
            break;
        }
    }

    // 修改信息業務邏輯控制
    public void modifyLogicLogic() {
        TelNoteRegex rtn = new TelNoteRegex();
        Menu m = new Menu();
        m.modifyMenu();
        switch (rtn.menuRegex(1, 3)) {
        case 1:
            showAll();

        case 2:
            modify();
            break;
        }
    }

    // 修改指定記錄信息
    public void modify() {
        TelNoteRegex rtn = new TelNoteRegex();
        Menu m = new Menu();
        m.subModifyMenu();
        switch (rtn.menuRegex(1, 6)) {
        case 1:
            modName();
            break;

        case 2:
            modAge();
            break;

        case 3:
            modSex();
            break;
            
        case 4:
            modTelNum();
            break;    
            
        case 5:
            modAdd();
            break;    
            
        case 6:
            modifyLogicLogic();
            break;
        }
    }

    // 刪除信息業務邏輯控制
    public void deleteLogic() {
        TelNoteRegex rtn = new TelNoteRegex();
        Menu m = new Menu();
        m.deleteMenu();
        switch (rtn.menuRegex(1, 4)) {
        case 1:
            showAll();
            break;

        case 2:
            delete();
            break;

        case 3:
            deleteAll();
            break;
        }
    }

    // 排序信息業務邏輯控制
    public void orderLogic() {
        TelNoteRegex rtn = new TelNoteRegex();
        Menu m = new Menu();
        m.orderMenu();
        switch (rtn.menuRegex(1, 5)) {
        case 1:
            orderName();
            break;

        case 2:
            orderAge();
            break;

        case 3:
            orderSex();
            break;

        case 4:
            showAll();
            break;
        }
    }



    // 添加新用戶信息
    public List<Person> addOperation() {

        List<Person> rtn = new ArrayList<>();
        try {
            
            Connection conn = cp.getConnection();
            Statement st = conn.createStatement();

            // 輸入姓名並判斷
            TelNoteRegex name1 = new TelNoteRegex();
            String name = name1.nameRegex();

            // 輸入年齡並判斷
            TelNoteRegex age1 = new TelNoteRegex();
            String age = age1.ageRegex();

            // 輸入性別並判斷
            TelNoteRegex sex1 = new TelNoteRegex();
            String sex = sex1.sexRegex();

            // 輸入電話號碼並判斷
            TelNoteRegex telnum1 = new TelNoteRegex();
            String telnum = telnum1.telnumRegex();

            // 輸入地址
            TelNoteRegex address1 = new TelNoteRegex();
            String address = address1.addressRegex();

            String sql = "insert into person    values(sq_person.nextval,?,?,?,?,?)";

            PreparedStatement ps = conn.prepareStatement(sql);

            ps.setString(1, name);
            ps.setString(2, age);
            ps.setString(3, sex);
            ps.setString(4, telnum);
            ps.setString(5, address);

            ps.executeUpdate();
            System.out.println("添加成功");

            ps.close();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rtn;
    }

    // 查詢全部用戶信息
    public List<Person> showAll() {

        List<Person> rtn = new ArrayList<>();
        try {
            Connection conn = cp.getConnection();

            Statement st = conn.createStatement();

            ResultSet rs = st.executeQuery("select * from person");

            // 遍歷結果集
            while (rs.next()) {
                Person pe = new Person();

                pe.setId(rs.getInt("id"));
                pe.setName(rs.getString("name"));
                pe.setAge(rs.getString("age"));
                pe.setSex(rs.getString("sex"));
                pe.setTelNum(rs.getString("telnum"));
                pe.setAddress(rs.getString("address"));

                rtn.add(pe);
                System.out.println(pe);
            }
            rs.close();
            st.close();
            cp.close();

        } catch (Exception e) {

            e.printStackTrace();
        }

        return rtn;
    }

    // 按姓名查找
    public List<Person> searchByName() {
        List<Person> rtn = new ArrayList<>();
        try {
            Connection conn = cp.getConnection();

            Statement st = conn.createStatement();

            // 輸入姓名並判斷
            TelNoteRegex name1 = new TelNoteRegex();
            String name = name1.nameRegex();

            String sql = "select * from person where name = '" + name + "'";

            ResultSet rs = st.executeQuery(sql);

            // 遍歷結果集
            if (!rs.next()) {
                System.out.println("沒有相關數據");
            } else {
                rs = st.executeQuery(sql);
                while (rs.next()) {
                    Person pe = new Person();

                    pe.setId(rs.getInt("id"));
                    pe.setName(rs.getString("name"));
                    pe.setAge(rs.getString("age"));
                    pe.setSex(rs.getString("sex"));
                    pe.setTelNum(rs.getString("telnum"));
                    pe.setAddress(rs.getString("address"));

                    rtn.add(pe);

                    System.out.println(pe);
                }
            }
            rs.close();
            st.close();
            cp.close();

        } catch (Exception e) {

            e.printStackTrace();
        }
        return rtn;
    }

    // 按年齡查找
    public List<Person> searchByAge() {
        List<Person> rtn = new ArrayList<>();
        try {
            Connection conn = cp.getConnection();

            Statement st = conn.createStatement();

            // 輸入年齡並判斷
            TelNoteRegex age1 = new TelNoteRegex();
            String age = age1.ageRegex();

            String sql = "select * from person where age = '" + age + "'";

            ResultSet rs = st.executeQuery(sql);

            // 遍歷結果集
            if (!rs.next()) {
                System.out.println("沒有相關數據");
            } else {
                rs = st.executeQuery(sql);
                while (rs.next()) {
                    Person pe = new Person();

                    pe.setId(rs.getInt("id"));
                    pe.setName(rs.getString("name"));
                    pe.setAge(rs.getString("age"));
                    pe.setSex(rs.getString("sex"));
                    pe.setTelNum(rs.getString("telnum"));
                    pe.setAddress(rs.getString("address"));

                    rtn.add(pe);

                    System.out.println(pe);
                }
            }
            rs.close();
            st.close();
            cp.close();

        } catch (Exception e) {

            e.printStackTrace();
        }
        return rtn;
    }

    // 按性別查詢
    public List<Person> searchBySex() {
        List<Person> rtn = new ArrayList<>();
        try {
            Connection conn = cp.getConnection();

            Statement st = conn.createStatement();

            // 輸入性別並判斷
            TelNoteRegex sex1 = new TelNoteRegex();
            String sex = sex1.sexRegex();

            String sql = "select * from person where sex = '" + sex + "'";

            ResultSet rs = st.executeQuery(sql);

            // 遍歷結果集
            if (!rs.next()) {
                System.out.println("沒有相關數據");
            } else {
                rs = st.executeQuery(sql);
                while (rs.next()) {
                    Person pe = new Person();

                    pe.setId(rs.getInt("id"));
                    pe.setName(rs.getString("name"));
                    pe.setAge(rs.getString("age"));
                    pe.setSex(rs.getString("sex"));
                    pe.setTelNum(rs.getString("telnum"));
                    pe.setAddress(rs.getString("address"));

                    rtn.add(pe);

                    System.out.println(pe);
                }
            }
            rs.close();
            st.close();
            cp.close();

        } catch (Exception e) {

            e.printStackTrace();
        }
        return rtn;
    }

    // 按號碼查找
    public List<Person> searchByTelNum() {
        List<Person> rtn = new ArrayList<>();
        try {
            Connection conn = cp.getConnection();

            Statement st = conn.createStatement();

            // 輸入號碼並判斷
            TelNoteRegex telnum1 = new TelNoteRegex();
            String telnum = telnum1.telnumRegex();

            String sql = "select * from person where telnum = '" + telnum + "'";

            ResultSet rs = st.executeQuery(sql);

            // 遍歷結果集
            if (!rs.next()) {
                System.out.println("沒有相關數據");
            } else {
                rs = st.executeQuery(sql);
                while (rs.next()) {
                    Person pe = new Person();

                    pe.setId(rs.getInt("id"));
                    pe.setName(rs.getString("name"));
                    pe.setAge(rs.getString("age"));
                    pe.setSex(rs.getString("sex"));
                    pe.setTelNum(rs.getString("telnum"));
                    pe.setAddress(rs.getString("address"));

                    rtn.add(pe);

                    System.out.println(pe);
                }
            }
            rs.close();
            st.close();
            cp.close();

        } catch (Exception e) {

            e.printStackTrace();
        }
        return rtn;
    }

    // 按號碼查找
    public List<Person> searchByAdd() {
        List<Person> rtn = new ArrayList<>();
        try {
            Connection conn = cp.getConnection();

            Statement st = conn.createStatement();

            // 輸入地址並判斷
            TelNoteRegex address1 = new TelNoteRegex();
            String address = address1.addressRegex();

            String sql = "select * from person where address = '" + address + "'";

            ResultSet rs = st.executeQuery(sql);

            // 遍歷結果集
            if (!rs.next()) {
                System.out.println("沒有相關數據");
            } else {
                rs = st.executeQuery(sql);
                while (rs.next()) {
                    Person pe = new Person();

                    pe.setId(rs.getInt("id"));
                    pe.setName(rs.getString("name"));
                    pe.setAge(rs.getString("age"));
                    pe.setSex(rs.getString("sex"));
                    pe.setTelNum(rs.getString("telnum"));
                    pe.setAddress(rs.getString("address"));

                    rtn.add(pe);

                    System.out.println(pe);
                }
            }
            rs.close();
            st.close();
            cp.close();

        } catch (Exception e) {

            e.printStackTrace();
        }
        return rtn;
    }

    // 修改姓名
    public void modName() {
        List<Person> rtn = new ArrayList<>();
        try {
            Connection conn = cp.getConnection();

            Statement st = conn.createStatement();

            // 獲取ID的最小值
            ResultSet rs1 = st.executeQuery("select min(id) from person");
            int min = 0;
            rs1.next();
            min = rs1.getInt(1);

            // 獲取ID的最大值
            ResultSet rs2 = st.executeQuery("select max(id) from person");
            int max = 0;
            rs2.next();
            max = rs2.getInt(1);

            System.out.println("請輸入要修改的ID號,最小ID號為:" + min + "最大ID號為:" + max);
            Scanner scan = new Scanner(System.in);
            int id = scan.nextInt();

            // 輸入姓名並判斷
            TelNoteRegex name1 = new TelNoteRegex();
            String name = name1.nameRegex();

            String sql = "update person set  name = '" + name + "'   where id = '" + id + "'";

            ResultSet rs = st.executeQuery(sql);

            ResultSet rs5 = st.executeQuery("select * from person where id = '" + id + "'");

            // 遍歷結果集
            if (!rs5.next()) {
                System.out.println("沒有相關數據,系統退回到主菜單");
            } else {
                rs = st.executeQuery(sql);
                System.out.println("修改成功");
            }

            rs.close();
            st.close();
            cp.close();

        } catch (Exception e) {

            e.printStackTrace();
        }
        return;

    }

    // 修改年齡
    public void modAge() {
        List<Person> rtn = new ArrayList<>();
        try {
            Connection conn = cp.getConnection();

            Statement st = conn.createStatement();

            // 獲取ID的最小值
            ResultSet rs1 = st.executeQuery("select min(id) from person");
            int min = 0;
            rs1.next();
            min = rs1.getInt(1);

            // 獲取ID的最大值
            ResultSet rs2 = st.executeQuery("select max(id) from person");
            int max = 0;
            rs2.next();
            max = rs2.getInt(1);

            System.out.println("請輸入要修改的ID號,最小ID號為:" + min + "最大ID號為:" + max);
            Scanner scan = new Scanner(System.in);
            int id = scan.nextInt();

            // 輸入姓名並判斷
            TelNoteRegex age1 = new TelNoteRegex();
            String age = age1.ageRegex();

            String sql = "update person set  age = '" + age + "'   where id = '" + id + "'";

            ResultSet rs = st.executeQuery(sql);

            ResultSet rs5 = st.executeQuery("select * from person where id = '" + id + "'");

            // 遍歷結果集
            if (!rs5.next()) {
                System.out.println("沒有相關數據,系統退回到主菜單");
            } else {
                rs = st.executeQuery(sql);
                System.out.println("修改成功");
            }

            rs.close();
            st.close();
            cp.close();

        } catch (Exception e) {

            e.printStackTrace();
        }
        return;

    }

    // 修改性別
    public void modSex() {
        List<Person> rtn = new ArrayList<>();
        try {
            Connection conn = cp.getConnection();

            Statement st = conn.createStatement();

            // 獲取ID的最小值
            ResultSet rs1 = st.executeQuery("select min(id) from person");
            int min = 0;
            rs1.next();
            min = rs1.getInt(1);

            // 獲取ID的最大值
            ResultSet rs2 = st.executeQuery("select max(id) from person");
            int max = 0;
            rs2.next();
            max = rs2.getInt(1);

            System.out.println("請輸入要修改的ID號,最小ID號為:" + min + "最大ID號為:" + max);
            Scanner scan = new Scanner(System.in);
            int id = scan.nextInt();

            // 輸入性別並判斷
            TelNoteRegex sex1 = new TelNoteRegex();
            String sex = sex1.sexRegex();

            String sql = "update person set  sex = '" + sex + "'   where id = '" + id + "'";

            ResultSet rs = st.executeQuery(sql);

            ResultSet rs5 = st.executeQuery("select * from person where id = '" + id + "'");

            // 遍歷結果集
            if (!rs5.next()) {
                System.out.println("沒有相關數據,系統退回到主菜單");
            } else {
                rs = st.executeQuery(sql);
                System.out.println("修改成功");
            }

            rs.close();
            st.close();
            cp.close();

        } catch (Exception e) {

            e.printStackTrace();
        }
        return;

    }

    // 修改號碼
    public void modTelNum() {
        List<Person> rtn = new ArrayList<>();
        try {
            Connection conn = cp.getConnection();

            Statement st = conn.createStatement();

            // 獲取ID的最小值
            ResultSet rs1 = st.executeQuery("select min(id) from person");
            int min = 0;
            rs1.next();
            min = rs1.getInt(1);

            // 獲取ID的最大值
            ResultSet rs2 = st.executeQuery("select max(id) from person");
            int max = 0;
            rs2.next();
            max = rs2.getInt(1);

            System.out.println("請輸入要修改的ID號,最小ID號為:" + min + "最大ID號為:" + max);
            Scanner scan = new Scanner(System.in);
            int id = scan.nextInt();
            ResultSet rs5 = st.executeQuery("select * from person where id = '" + id + "'");
            if (!rs5.next()) {
                System.out.println("沒有相關數據,系統退回到主菜單");

        
            // 遍歷結果集
        
            } else {
                // 輸入號碼並判斷
                TelNoteRegex telnum1 = new TelNoteRegex();
                String telnum = telnum1.telnumRegex();

                String sql = "update person set  telnum = '" + telnum + "'   where id = '" + id + "'";

                ResultSet rs = st.executeQuery(sql);
                rs = st.executeQuery(sql);
                System.out.println("修改成功");
            }

            
            st.close();
            cp.close();

        } catch (Exception e) {

            e.printStackTrace();
        }
        return;

    }

    // 修改地址
    public void modAdd() {
        List<Person> rtn = new ArrayList<>();
        try {
            Connection conn = cp.getConnection();

            Statement st = conn.createStatement();

            // 獲取ID的最小值
            ResultSet rs1 = st.executeQuery("select min(id) from person");
            int min = 0;
            rs1.next();
            min = rs1.getInt(1);

            // 獲取ID的最大值
            ResultSet rs2 = st.executeQuery("select max(id) from person");
            int max = 0;
            rs2.next();
            max = rs2.getInt(1);

            System.out.println("請輸入要修改的ID號,最小ID號為:" + min + "最大ID號為:" + max);
            Scanner scan = new Scanner(System.in);
            int id = scan.nextInt();

            // 輸入地址並判斷
            TelNoteRegex add1 = new TelNoteRegex();
            String add = add1.addressRegex();

            String sql = "update person set  address     = '" + add + "'   where id = '" + id + "'";

            ResultSet rs = st.executeQuery(sql);

            ResultSet rs5 = st.executeQuery("select * from person where id = '" + id + "'");

            // 遍歷結果集
            if (!rs5.next()) {
                System.out.println("沒有相關數據,系統退回到主菜單");
            } else {
                rs = st.executeQuery(sql);
                System.out.println("修改成功");
            }

            rs.close();
            st.close();
            cp.close();

        } catch (Exception e) {

            e.printStackTrace();
        }
        return;

    }

    // 刪除指定用戶信息
    public void delete() {
        List<Person> rtn = new ArrayList<>();
        try {
            Connection conn = cp.getConnection();

            Statement st = conn.createStatement();

            ResultSet rs1 = st.executeQuery("select min(id) from person");

            ResultSet rs2 = st.executeQuery("select max(id) from person");

            System.out.println(rs1);

            System.out.println("請輸入要刪除的ID");
            Scanner scan = new Scanner(System.in);

            int id = scan.nextInt();

            String sql = "delete from Person where id = '" + id + "'";

            ResultSet rs = st.executeQuery(sql);

            rs = st.executeQuery(sql);

            System.out.println("刪除成功");

            rs.close();
            st.close();
            cp.close();

        } catch (Exception e) {

            e.printStackTrace();
        }
        return;
    }

    // 刪除全部記錄
    public void deleteAll() {
        List<Person> rtn = new ArrayList<>();
        try {
            Connection conn = cp.getConnection();

            Statement st = conn.createStatement();

            String sql = "truncate table person";

            ResultSet rs = st.executeQuery(sql);

            rs = st.executeQuery(sql);

            System.out.println("全部數據已經清空");

            rs.close();
            st.close();
            cp.close();

        } catch (Exception e) {

            e.printStackTrace();
        }
        return;
    }

    // 按用戶姓名排序
    public List<Person> orderName() {

        List<Person> rtn = new ArrayList<>();
        try {
            Connection conn = cp.getConnection();

            Statement st = conn.createStatement();

            ResultSet rs = st.executeQuery("select * from person order by name");

            // 遍歷結果集
            while (rs.next()) {
                Person pe = new Person();

                pe.setId(rs.getInt("id"));
                pe.setName(rs.getString("name"));
                pe.setAge(rs.getString("age"));
                pe.setSex(rs.getString("sex"));
                pe.setTelNum(rs.getString("telnum"));
                pe.setAddress(rs.getString("address"));

                rtn.add(pe);
                System.out.println(pe);
            }
            rs.close();
            st.close();
            cp.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return rtn;
    }

    // 按用戶年齡排序
    public List<Person> orderAge() {

        List<Person> rtn = new ArrayList<>();
        try {
            Connection conn = cp.getConnection();

            Statement st = conn.createStatement();

            ResultSet rs = st.executeQuery("select * from person order by age");

            // 遍歷結果集
            while (rs.next()) {
                Person pe = new Person();

                pe.setId(rs.getInt("id"));
                pe.setName(rs.getString("name"));
                pe.setAge(rs.getString("age"));
                pe.setSex(rs.getString("sex"));
                pe.setTelNum(rs.getString("telnum"));
                pe.setAddress(rs.getString("address"));

                rtn.add(pe);
                System.out.println(pe);
            }
            rs.close();
            st.close();
            cp.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return rtn;
    }

    // 按用戶性別排序
    public List<Person> orderSex() {

        List<Person> rtn = new ArrayList<>();
        try {
            Connection conn = cp.getConnection();

            Statement st = conn.createStatement();

            ResultSet rs = st.executeQuery("select * from person order by sex");

            // 遍歷結果集
            while (rs.next()) {
                Person pe = new Person();

                pe.setId(rs.getInt("id"));
                pe.setName(rs.getString("name"));
                pe.setAge(rs.getString("age"));
                pe.setSex(rs.getString("sex"));
                pe.setTelNum(rs.getString("telnum"));
                pe.setAddress(rs.getString("address"));

                rtn.add(pe);
                System.out.println(pe);
            }
            rs.close();
            st.close();
            cp.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return rtn;
    }

}
package 電話薄1;



public class Person {
    private int id;                         //用戶ID 屬性
    private String name;            // 用戶姓名屬性
    private String age;                 //用戶年齡屬性
    private String sex;                 //用戶性別屬性
    private String telNum;            // 用戶電話號碼屬性
    private String address;         //用戶地址屬性
    
    //有參數構造方法
    public Person(int id, String name, String age, String sex, String telNum, String address) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.sex = sex;
        this.telNum = telNum;
        this.address = address;
    }
    
    //無參構造方法
    public Person() {
        super();
        // TODO 自動生成的構造函數存根
    }

    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;
    }

    @Override
    public String toString() {
        return "[序號=" + id + ", 姓名=" + name + ", 年齡=" + age + ", 性別=" + sex + ", 號碼=" + telNum
                + ", 地址=" + address + "]";
    }
    
    
    


}
package 電話薄1;

import java.util.Scanner;

public class TelNoteRegex {
    
    //對菜單輸入選項的驗證
    public int menuRegex (int min, int max )
    {
        Scanner scan = new Scanner(System.in);
        int z  = 0;
        try{
        
        while(true)
        {
            z  = scan.nextInt();
            
            if(z>=min && z<=max)
            {
                break;
            }
            else{
                System.out.println("輸入有誤,請重新輸入"+min+"到"+max);
            }
        }
            return z;
        }
        catch(Exception e){
            System.out.println("輸入錯誤");
        }
        return    z ;
    }
    
    //對用戶輸入姓名的驗證
    public String nameRegex()
    {
        System.out.println("請輸入姓名,1-10位字母");
        Scanner scan = new Scanner(System.in);
        String name = "";
        while(true)
        {
            name= scan.nextLine();
            if(name.matches("[A-Za-z]{1,10}"))
            {
                break;    
            }
            else
            {
                System.out.println("輸入錯誤,請重新輸入1-10位字母");
            }
        }
        return name;
    }
    
    //對用戶輸入年齡的驗證
        public String ageRegex()
        {
            System.out.println("請輸入年齡,1-100+");
            Scanner scan = new Scanner(System.in);
            String age = "";
            while(true)
            {
                age= scan.nextLine();
                if(age.matches("[0-9]{1,2}"))
                {
                    break;    
                }
                else
                {
                    System.out.println("輸入錯誤,請重新輸入1-100");
                }
            }
            return age;
        }

        //對用戶輸入性別的驗證
        public String sexRegex()
        {
            System.out.println("輸入性別,(男 m or     M)    (女 f or F)");
            Scanner scan = new Scanner(System.in);
            String sex = "";
            while(true)
            {
                sex= scan.nextLine();
                if(sex.matches("男|女|m|M|f|F"))
                {
                    break;    
                }
                else
                {
                    System.out.println("輸入錯誤,請重新輸入性別,(男 m or     M)    (女 f or F)");
                }
            }
            return sex;
        }
        
        //對用戶輸入年齡的驗證
        public String telnumRegex ()
        {
            System.out.println("輸入電話號碼,6-11位數字");
            Scanner scan = new Scanner(System.in);
            String telNum = "";
            while(true)
            {
                telNum= scan.nextLine();
                if(telNum.matches("[0-9]{6,11}"))
                {
                    break;    
                }
                else
                {
                    System.out.println("輸入錯誤,請重新輸入電話號碼,6-11位數字");
                }
            }
            return telNum;
        }

        //對用戶輸入地址的驗證
                public String addressRegex  ()
                {
                    System.out.println("輸入地址,1-50位字母或數字");
                    Scanner scan = new Scanner(System.in);
                    String address = "";
                    while(true)
                    {
                        address= scan.nextLine();
                        if(address.matches("[A-Za-z0-9]{1,50}"))
                        {
                            break;    
                        }
                        else
                        {
                            System.out.println("輸入錯誤,請重新輸入地址,1-50位字母或數字");
                        }
                    }
                    return address;
                }
        
        
        
        
}

 


免責聲明!

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



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