zoukankan      html  css  js  c++  java
  • 运动会报名系统的简易实现

    需求分析:

    一、语言和环境

    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("编号	姓名	年龄	班级	项目");
                while (rs.next()) {
                    for (int i = 1; i <= col; i++) {
                        System.out.print(rs.getString(i) + "	");
                        
                        if ((i == 2) && (rs.getString(i).length() < 8)) {
    //                        System.out.print("	");
    //                        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("****		欢迎进入运动会报名系统		****");
            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();
            }
        }    
    }
    只有O和1的世界是简单的!
  • 相关阅读:
    localhost 和 127.0.0.1 认识
    postgreSQL可视化工具pgAdmin3 导入表结构和数据
    posgreSQL安装失败解决方案
    C#面向对象基本概念总结
    关于数据存储(关系型数据库和非关系型数据库)
    ADO.NET中的五大内置对象
    关于XML
    WPF中TreeView控件SelectedItemChanged方法的MVVM绑定
    WPF中使用MVVM进行multibinding
    WPF中TreeView控件数据绑定和后台动态添加数据(二)
  • 原文地址:https://www.cnblogs.com/hinux/p/9739945.html
Copyright © 2011-2022 走看看