運動會報名系統的簡易實現


需求分析:

一、語言和環境

A、實現語言:Java

B、環境要求:JDK 8.0、Eclipse 、MySQL 5.6

二、功能要求

學校即將舉辦第五屆春季運動會,為更方便管理報名信息,現要求開發基於控制台的學校運動會報名系統。具體要求如下:

1.主菜單:顯示系統主菜單,每執行完一項功能后菜單循環顯示,如圖1所示,菜單功能包括:

1) 學生報名

2) 按比賽項目查詢

3) 按班級查詢

4) 取消報名

5) 退出系統

2.學生報名:輸入學生姓名、年齡、班級號、報名項目編號,正確錄入以上信息后,顯示“報名成功!”。

3.按比賽項目查詢:提示選擇要查詢的比賽項目,查詢出相關學生信息,包括項目名、姓名、班級、年齡。

4.按班級查詢:提示選擇要查詢的班級,查詢出相關學生信息,包括項目名、姓名、班級、年齡。

5.取消報名:提示輸入要取消報名的學生姓名,從報名信息表中刪除該學生記錄。成功后提示“取消報名成功!”

6.退出系統:提示“謝謝使用!”后退出系統

三、具體要求及推薦實現步驟

1.創建數據庫表apply_info,添加測試數據不少於4條,表結構如表1所示。

系統目錄結構如下圖所示:

2.創建實體類ApplyInfo,根據業務提供需要的構造方法和setter/getter方法。      

 1 package top.hinux.entity;
 2 
 3 /**
 4  * <p>報名信息實體類</p>
 5  * @author HSH
 6  *
 7  */
 8 public class ApplyInfo {
 9     
10     private int applyId;//編號
11     private String name;//姓名
12     private int age;//年齡
13     private String className;//班級
14     private String game;//比賽項目
15     
16     
17     /**
18      * 生成構造方法
19      * @param applyId
20      * @param name
21      * @param age
22      * @param className
23      * @param game
24      */
25     public ApplyInfo(String name, int age, String className, String game) {
26         this.name = name;
27         this.age = age;
28         this.className = className;
29         this.game = game;
30     }
31     
32     
33     //生成get(),set() 方法。
34     public int getApplyId() {
35         return applyId;
36     }
37     public void setApplyId(int applyId) {
38         this.applyId = applyId;
39     }
40     public String getName() {
41         return name;
42     }
43     public void setName(String name) {
44         this.name = name;
45     }
46     public int getAge() {
47         return age;
48     }
49     public void setAge(int age) {
50         this.age = age;
51     }
52     public String getClassName() {
53         return className;
54     }
55     public void setClassName(String className) {
56         this.className = className;
57     }
58     public String getGame() {
59         return game;
60     }
61     public void setGame(String game) {
62         this.game = game;
63     }
64     
65     
66     @Override
67     public String toString() {
68         return "ApplyInfo [applyId=" + applyId + ", name=" + name + ", age=" + age + ", className=" + className
69                 + ", game=" + game + "]";
70     }
71     
72     
73 
74 }

3.創建JDBCTools類,實現數據庫連接和關閉功能。

 1 package top.hinux.dao;
 2 
 3 import java.io.IOException;
 4 
 5 import java.io.InputStream;
 6 import java.sql.Connection;
 7 import java.sql.DriverManager;
 8 import java.sql.PreparedStatement;
 9 import java.sql.ResultSet;
10 import java.sql.SQLException;
11 import java.sql.Statement;
12 import java.util.Properties;
13 
14 
15 /**
16  * <p>實現數據庫連接和關閉功能</p>
17  * @author Administrator
18  *
19  */
20 public class JDBCTools {
21     
22     private static String driver;
23     private static String url;
24     private static String user;
25     private static String password;
26     private static Connection conn;
27     private JDBCTools(){
28 
29     }
30     static {
31         InputStream in=DBUtils.class.getResourceAsStream("/db.properties");
32         Properties pro=new Properties();
33         try {
34             pro.load(in);
35             driver=pro.getProperty("driver");
36             url=pro.getProperty("url");
37             user=pro.getProperty("user");
38             password=pro.getProperty("password");
39             Class.forName(driver);
40             conn=DriverManager.getConnection(url,user,password);
41         } catch (IOException e) {
42             e.printStackTrace();
43             System.out.print("配置文件加載失敗!");
44         } catch (ClassNotFoundException e) {
45             e.printStackTrace();
46         } catch (SQLException e) {
47             e.printStackTrace();
48         }
49     }
50 
51     public static Connection getConnection() {
52         try {
53             if (conn == null || conn.isClosed())
54                 conn = DriverManager.getConnection(url, user, password);
55         } catch (SQLException e) {
56             e.printStackTrace();
57         }
58         return conn;
59     }
60     public static void close(){
61         colse(null,null);
62     }
63     public static void close(Statement stm){
64         colse(null,stm);
65     }
66     public static void colse(ResultSet rs, Statement stm){
67         if(rs!=null){
68             try {
69                 rs.close();
70             } catch (SQLException e) {
71                 e.printStackTrace();
72             }
73         }
74         if(stm!=null){
75             try {
76                 stm.close();
77             } catch (SQLException e) {
78                 e.printStackTrace();
79             }
80         }
81     }
82 
83     public static void colse(PreparedStatement sta) {
84 
85     }
86 }

 

創建DBUtils類,寫通用的增刪改查的方法。

package top.hinux.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import top.hinux.entity.ApplyInfo;

/**
 * <p>寫通用的增刪改查的方法。</p>
 * @author HSH
 *
 */
public class DBUtils {
    
    /**
     * <p>插入數據</p>
     * @param applyinfo
     * @return 
     */
    public static int insert(ApplyInfo applyinfo) {
        Connection conn = JDBCTools.getConnection();
        int i = 0;
        String sql = "insert into Apply_Info (Name,Age,class,Game) values(?,?,?,?)";
        PreparedStatement pstmt;
        try {
            pstmt = (PreparedStatement) conn.prepareStatement(sql);
            pstmt.setString(1, applyinfo.getName());
            pstmt.setLong(2, applyinfo.getAge());
            pstmt.setString(3, applyinfo.getClassName());
            pstmt.setString(4, applyinfo.getGame());
            i = pstmt.executeUpdate();
            pstmt.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return i;
    }
    /**
     * <p>刪除記錄</p>
     * @param sql
     * @param args
     */
    public static int delete(String name) {
        Connection conn = JDBCTools.getConnection();
        int i = 0;
        String sql = "delete from Apply_Info where Name='" + name + "'";
        PreparedStatement pstmt;
        try {
            pstmt = (PreparedStatement) conn.prepareStatement(sql);
            i = pstmt.executeUpdate();
            //System.out.println("resutl: " + i);
            pstmt.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return i;
    }
    /**
     * <p>修改記錄</p>
     * @param sql
     * @param args
     */
    public static int update(ApplyInfo applyinfo) {
        Connection conn = JDBCTools.getConnection();
        int i = 0;
        String sql = "update apply_info set Age='" + applyinfo.getAge() + "' where Name='" + applyinfo.getName() + "'";
        PreparedStatement pstmt;
        try {
            pstmt = (PreparedStatement) conn.prepareStatement(sql);
            i = pstmt.executeUpdate();
            System.out.println("resutl: " + i);
            pstmt.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return i;
    }
    /**
     * <p>查詢記錄</p>
     * @param sql
     * @param args
     */
    public static Integer getTableInfo(String sql,Object...prarm) {
        Connection conn = JDBCTools.getConnection();
        PreparedStatement pstmt;
        try {
            pstmt = (PreparedStatement)conn.prepareStatement(sql);
            if (prarm!=null) {
                for (int i = 0; i <prarm.length ; i++) {
                    pstmt.setObject(i+1,prarm[i]);
                }
            }
            ResultSet rs = pstmt.executeQuery();
            int col = rs.getMetaData().getColumnCount();
            System.out.println("============================================");
            System.out.println("編號\t姓名\t年齡\t班級\t項目");
            while (rs.next()) {
                for (int i = 1; i <= col; i++) {
                    System.out.print(rs.getString(i) + "\t");
                    
                    if ((i == 2) && (rs.getString(i).length() < 8)) {
//                        System.out.print("\t");
//                        System.out.println("aaa");
                    }
                 }
                System.out.println("");
            }
                System.out.println("============================================");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    
}

 

4.創建DAO接口ApplyInfoDao,定義學生報名,按班級查詢,按比賽項目,取消報名的方法。

 1 package top.hinux.service;
 2 
 3 import top.hinux.entity.ApplyInfo;
 4 
 5 /**
 6  * 定義學生報名,按班級查詢,按比賽項目,取消報名的方法。
 7  * @author Administrator
 8  *
 9  */
10 public interface ApplyInfoDao {
11     /**
12      * <p>定義學生報名</p>
13      */
14     void studentReg(ApplyInfo appinfo);
15     /**
16      * <p>按班級查詢</p>
17      */
18     void selectClass(String sql,String ClassName);
19     /**
20      * <p>按比賽項目查詢</p>
21      */
22     void selectGame(String sql,String game);
23     /**
24      * <p>取消報,</p>
25      */
26     void removeMessage(String name);
27 }

5.創建DAO實現類ApplyInfoDaoImpl,實現ApplyInfoDao接口,使用JDBC完成相應數據庫操作。

 1 package top.hinux.utils;
 2 
 3 import top.hinux.dao.DBUtils;
 4 import top.hinux.entity.ApplyInfo;
 5 import top.hinux.service.ApplyInfoDao;
 6 
 7 public class ApplyInfoDaoImpl implements ApplyInfoDao{
 8 
 9     @Override
10     public void studentReg(ApplyInfo appinfo) {
11         // TODO Auto-generated method stub
12         DBUtils.insert(appinfo);
13         System.out.println("信息插入成功!");
14     }
15 
16     @Override
17     public void selectClass(String sql,String ClassName) {
18         // TODO Auto-generated method stub
19         DBUtils.getTableInfo(sql, ClassName);
20     }
21 
22     @Override
23     public void selectGame(String sql,String game) {
24         // TODO Auto-generated method stub
25          DBUtils.getTableInfo(sql, game);
26     }
27 
28     @Override
29     public void removeMessage(String name) {
30         // TODO Auto-generated method stub
31         DBUtils.delete(name);
32     }
33 
34 }

6.創建ApplyMgr類,完成在控制台的報名信息操作,啟動和運行系統。

package top.hinux.utils;

import java.util.Scanner;

import top.hinux.entity.ApplyInfo;

/**
 * <p>完成在控制台的報名信息操作,啟動和運行系統。</p>
 * @author HSH
 *
 */
public class ApplyMgr {

    //菜單數組
    private static String[] munu = { "1、學生報名", "2、按比賽項目查詢", "3、按班級查詢", "4、取消報名", "5、退出系統" };
    //班級列表
    private static String[] classNames = { "一班", "二班", "三班" };
    //運動會項目列表
    private static String[] games = { "跳遠", "接力跑", "跳繩" };

    // 取消報名
    public static void dropUser() {
        Scanner sc = new Scanner(System.in);
        System.out.println("請輸入您的名字:");
        String userName = sc.next();
        System.out.println("*********************正在取消報名*****************");
        
        //生成ApplyInfoDaoImpl對象
        ApplyInfoDaoImpl apdi = new ApplyInfoDaoImpl();
        apdi.removeMessage(userName);
        
        System.out.println(userName + "取消報名成功!");
    }

    // 按班級查詢
    public static void selectClass() {
        Scanner sc = new Scanner(System.in);
        System.out.println(" 請選擇您的班級,輸入編號:(1、一班,2、二班,3、三班)");
        int className = sc.nextInt();
        
        System.out.println("正在按照班級信息查詢!");
        System.out.println("********************");
        System.out.println("按班級信息查詢結果如下:");
        //生成ApplyInfoDaoImpl對象
        ApplyInfoDaoImpl apdi = new ApplyInfoDaoImpl();
        String sql = "select *from Apply_Info where class=?";
        apdi.selectClass(sql, classNames[className-1]);
    }
    // 查詢比賽項目
    public static void selectGame() {
        Scanner sc = new Scanner(System.in);
        System.out.println("請選擇您的項目,輸入編號:(1、跳遠,2、接力跑,3、跳繩)");
        int game = sc.nextInt();
        System.out.println("正在按照比賽項目信息查詢!");
        System.out.println("********************");
        System.out.println("按比賽項目信息查詢結果如下:");
        //生成ApplyInfoDaoImpl對象
        ApplyInfoDaoImpl apdi = new ApplyInfoDaoImpl();
        String sql ="select *from Apply_Info where game=?";
        apdi.selectGame(sql, games[game-1]);
    }

    /**
     * <p>
     * 學生報名
     * </p>
     * @author HSH
     */
    public  static void regUser() {
        try {
            Scanner sc = new Scanner(System.in);
            System.out.println("請輸入姓名:");
            String userName = sc.next();
            System.out.println("請輸入您的年齡:");
            int age = sc.nextInt();
            System.out.println(" 請選擇您的班級,輸入編號:(1、一班,2、二班,3、三班)");
            int className = sc.nextInt();
            System.out.println("請選擇您的項目,輸入編號:(1、跳遠,2、接力跑,3、跳繩)");
            int game = sc.nextInt();
            System.out.println("user:" + userName + "  age:" + age + "  className:" + classNames[className - 1]
                    + "  game:" + games[game-1]);
            // 在此處寫報名數據存取操作
            ApplyInfo ai = new ApplyInfo( userName, age, classNames[className-1], games[game-1]);
            //生成ApplyInfoDaoImpl對象
            ApplyInfoDaoImpl apdi = new ApplyInfoDaoImpl();
            apdi.studentReg(ai);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            //e.printStackTrace();
            System.out.println("信息輸入格式不正確!請重新輸入!");
        }
    }

    /**
     * <p>
     * 顯示菜單
     * <p>
     * 
     * @author HSH
     */
    public static void showMune() {
        System.out.println("*********************************************");
        System.out.println("****\t\t歡迎進入運動會報名系統\t\t****");
        for (String string : munu) {
            System.out.print(string + " ");
        }
        System.out.println();
        System.out.println("*********************************************");
    }

    /**
     * <p>
     * 退出系統成功
     * </p>
     * 
     * @author HSH
     */
    public static void exitSys() {
        System.out.println("退出系統成功!");
    }
}

7.創建主類(Manager)用於實現系統整體流程

package top.hinux;

import java.util.Scanner;
import top.hinux.utils.ApplyMgr;

public class Manager {
    
    /**
     * <p> 程序主函數,實現系統功能</p>
     *  @author HSH
     * @param args
     */
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        ApplyMgr.showMune();
        while (true) {
            try {
                System.out.println("請輸入您要辦理的業務!");
                int select = sc.nextInt();
                switch (select) {
                case 1:
                    ApplyMgr.regUser();//學生報名
                    break;
                case 2:
                    ApplyMgr.selectGame();//查詢比賽項目
                    break;
                case 3:
                    ApplyMgr.selectClass();// 按班級查詢
                    break;
                case 4:
                    ApplyMgr.dropUser();//取消報名
                    break;
                case 5:
                    ApplyMgr.exitSys();//退出系統
                    return;
                default:
                    System.out.println("輸入錯誤!");
                }
            } catch (Exception e) {
                //e.printStackTrace();
                sc.nextLine();
                System.out.println("信息輸入錯誤,請重新輸入:");
            }
            ApplyMgr.showMune();
        }
    }    
}

 


免責聲明!

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



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