实训:新闻管理系统(Java)

JavaSE巩固--------新闻管理系统

注:!!!!! 项目源码https://github.com/Hardman233/MyLearnProject.git 需要可自取仅用于学习

前言

给自己:

步骤:

  • 对需求进行分析
    实训:新闻管理系统(Java)_第1张图片

  • 对程序进行设计
    实训:新闻管理系统(Java)_第2张图片

  • 编码

  • 解耦、优化

主要实现:

一、 分三层

1.建包com.dao com.daoimpl com.service com.serviceimpl com.main com.data com.util 其中date包就对应数据库的每一张表,设计数据库
实训:新闻管理系统(Java)_第3张图片在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
DATE包对应表

package com.date;

public class user {
    private int id;
    private String u_uname;
    private String u_pwd;
    private String u_rname;

    public user(int id, String u_uname, String u_pwd, String u_rname) {
        this.id = id;
        this.u_uname = u_uname;
        this.u_pwd = u_pwd;
        this.u_rname = u_rname;
    }

    public user(String u_uname, String u_pwd, String u_rname) {
        this.u_uname = u_uname;
        this.u_pwd = u_pwd;
        this.u_rname = u_rname;
    }

    public user(String u_pwd, String u_rname) {
        this.u_pwd = u_pwd;
        this.u_rname = u_rname;
    }

    public user(int id, String dAdminName, String dAdminPassword) {
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getU_uname() {
        return u_uname;
    }

    public void setU_uname(String u_uname) {
        this.u_uname = u_uname;
    }

    public String getU_pwd() {
        return u_pwd;
    }

    public void setU_pwd(String u_pwd) {
        this.u_pwd = u_pwd;
    }

    public String getU_rname() {
        return u_rname;
    }

    public void setU_rname(String u_rname) {
        this.u_rname = u_rname;
    }

}

package com.date;

public class nav {
    private int id;
    private String n_name;
    private String n_heavy;

    public nav(int id, String n_name, String n_heavy) {
        this.id = id;
        this.n_name = n_name;
        this.n_heavy = n_heavy;
    }

    public nav(String n_name, String n_heavy) {
        this.n_name = n_name;
        this.n_heavy = n_heavy;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getN_name() {
        return n_name;
    }

    public void setN_name(String n_name) {
        this.n_name = n_name;
    }

    public String getN_heavy() {
        return n_heavy;
    }

    public void setN_heavy(String n_heavy) {
        this.n_heavy = n_heavy;
    }
}

package com.date;

public class art {
    private int id;
    private String a_title;
    private String a_nav;
    private String a_time;

    public art(String a_title, String a_nav, String a_time) {
        this.a_title = a_title;
        this.a_nav = a_nav;
        this.a_time = a_time;
    }

    public art(int id, String a_title, String a_nav, String a_time) {
        this.id = id;
        this.a_title = a_title;
        this.a_nav = a_nav;
        this.a_time = a_time;
    }

    public art(String a_title, String a_nav) {
        this.a_title = a_title;
        this.a_nav = a_nav;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getA_title() {
        return a_title;
    }

    public void setA_title(String a_title) {
        this.a_title = a_title;
    }

    public String getA_nav() {
        return a_nav;
    }

    public void setA_nav(String a_nav) {
        this.a_nav = a_nav;
    }

    public String getA_time() {
        return a_time;
    }

    public void setA_time(String a_time) {
        this.a_time = a_time;
    }
}

2.总结功能并设计接口,

DAO层接口:

package com.dao;

import com.date.user;

import java.util.List;

public interface INewsUserDao {

    public boolean isQueryExist(int id);

    public boolean addUser (user user);

    public boolean deleteUserById (int id);

    public boolean updateUserById (int id,user user);

    public List<user> queryAllUsers();

    public user queryUserById(int id);

    public boolean loginUserQuery(String uname, String pwd);

}
package com.dao;

import com.date.nav;

import java.util.List;

public interface INewsNavDao {

    public boolean isQueryExist(int id);

    public boolean addNav (nav nav);

    public boolean deleteNavById (int id);

    public boolean updateNavById (int id,nav nav);

    public List<nav> queryAllNavs();

    public nav queryNavById(int id);

    public boolean loginUserQuery(String uname, String pwd);
}

package com.dao;

import com.date.art;

import java.util.List;

public interface INewsArtDao {

    public boolean isQueryExist(int id);

    public boolean addArt (art art);

    public boolean deleteArtById (int id);

    public boolean updateArtById (int id, art art);

    public List<art> queryAllArts();

    public art queryArtById(int id);

    public boolean loginUserQuery(String uname, String pwd);
}

3.自定义DB的工具类提高代码复用率
注意:为了降低代码的冗余程度进行了方法重构,例如getconnect(),closeAll()等方法

package com.util;

import java.sql.*;


public class DBUtil {
    private static final String URL = "jdbc:mysql://localhost:3306/javawork?characterEncoding=utf8";
    private static final String ADMINNAME = "root";
    private static final String ADMINPWD = "2511880";

    public static Connection con = null;
    public static PreparedStatement pstmt = null;
    public static ResultSet rs = null;

    public static Connection getConnection() throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.cj.jdbc.Driver");
        return con = DriverManager.getConnection(URL,ADMINNAME,ADMINPWD);
    }

    public static PreparedStatement createPreparedStatement(String sql,Object[] params) throws SQLException, ClassNotFoundException {
        pstmt = getConnection().prepareStatement(sql);
        //不知道需要几个pstmt.setXXX 不知道每个set的类型 所以:
        if (params != null) {
            for (int i = 0; i < params.length; i++) {
                pstmt.setObject(i + 1, params[i]);
            }
        }
        return pstmt;
    }

    public static void closeAll(ResultSet rs,Statement stmt,Connection con){
        try {
            if (rs != null) rs.close();
            if (stmt != null) stmt.close();
            if (con != null) con.close();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    public static boolean excuteUpdate (String sql,Object[] params){

        try {
            getConnection();
            //预编译+执行
            pstmt =createPreparedStatement(sql,params);
            int count = pstmt.executeUpdate();
            if (count > 0)
                return true;
            else
                return false;

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return false;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return false;
        } catch (Exception e){
            e.printStackTrace();
            return false;
        } finally {
            //关
            closeAll(null,pstmt,con);
        }

    }


    public static ResultSet executeQuery(String sql,Object[] params){

        try {
            getConnection();
            pstmt =createPreparedStatement(sql,params);
            rs = pstmt.executeQuery();
            return rs;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return null;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return null;
        } catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    public static boolean loginQuery(String sql,Object[] params){
        try {
            getConnection();
            pstmt =createPreparedStatement(sql,params);
            rs = pstmt.executeQuery();
            return rs.next();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return false;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return false;
        } catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }


}

4.实现DAO层

package com.daoimpl;

import com.dao.INewsUserDao;
import com.date.user;
import com.util.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class NewsUserDaoimpl implements INewsUserDao {
    @Override
    public boolean isQueryExist(int id) {
            return queryUserById(id)==null?false:true;
    }

    @Override
    public boolean addUser(user user) {
        String sql = "insert into user (id,u_uname,u_pwd,u_rname) values(?,?,?,?)";
        Object[] params = {user.getId(),user.getU_uname(),user.getU_pwd(),user.getU_rname()};
        return DBUtil.excuteUpdate(sql,params);
    }

    @Override
    public boolean deleteUserById(int id) {
        String sql = "delete from user where id = ?";
        Object[] params = {id};
        return DBUtil.excuteUpdate(sql,params);
    }

    @Override
    public boolean updateUserById(int id, user user) {
        String sql = "update user set u_pwd = ?,u_rname = ? where id = ?";
        Object[] params = {user.getU_pwd(),user.getU_rname(),id};
        return DBUtil.excuteUpdate(sql,params);
    }

    @Override
    public List<user> queryAllUsers() {
        PreparedStatement pstmt = null;
        user user = null;
        List<user> users = new ArrayList<>();
        ResultSet rs = null;
        try {
            String sql = "select * from user";
            rs = DBUtil.executeQuery(sql,null);
            while (rs.next()) {
                int id = rs.getInt("id");
                String uname = rs.getString("u_uname");
                String upwd = rs.getString("u_pwd");
                String rname = rs.getString("u_rname");
                user = new user(id,uname,upwd,rname);
                users.add(user);
            }
            return users;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return null;
        }finally {
            //关
            DBUtil.closeAll(rs,pstmt,DBUtil.con);
        }
    }

    @Override
    public user queryUserById(int id) {
        Connection con = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        user user = null;
        try {
            String sql = "select * from user where id = ?";
            Object[] params = {id};
            //预编译
            rs = DBUtil.executeQuery(sql,params);
            if (rs.next()){
                int dId = rs.getInt("id");
                String uname = rs.getString("u_uname");
                String pwd = rs.getString("u_pwd");
                String rname= rs.getString("u_rname");
                user = new user(dId,uname,pwd,rname);
            }
            return user;

        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return null;
        } catch (Exception e){
            e.printStackTrace();
            return null;
        } finally {
            //关
            DBUtil.closeAll(rs,pstmt,DBUtil.con);
        }
    }

    @Override
    public boolean loginUserQuery(String uname, String pwd){
        String sql = "select * from user where u_uname =? and u_pwd =?";
        Object[] params = {uname,pwd};
        return DBUtil.loginQuery(sql,params);
    }

}

package com.daoimpl;


import com.dao.INewsNavDao;

import com.date.nav;
import com.date.user;
import com.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class NewsNavDaoimpl implements INewsNavDao {

    @Override
    public boolean isQueryExist(int id) {
        return queryNavById(id)==null?false:true;
    }

    @Override
    public boolean addNav(nav nav) {
        String sql = "insert into nav (n_name,n_heavy) values(?,?)";
        Object[] params = {nav.getN_name(),nav.getN_heavy()};
        return DBUtil.excuteUpdate(sql,params);
    }

    @Override
    public boolean deleteNavById(int id) {
        String sql = "delete from nav where id = ?";
        Object[] params = {id};
        return DBUtil.excuteUpdate(sql,params);
    }

    @Override
    public boolean updateNavById(int id, nav nav) {
        String sql = "update nav set n_name = ?, n_heavy = ? where id = ?";
        Object[] params = {nav.getN_name(),nav.getN_heavy(),id};
        return DBUtil.excuteUpdate(sql,params);
    }

    @Override
    public List<nav> queryAllNavs() {
        PreparedStatement pstmt = null;
        nav nav = null;
        List<nav> navs = new ArrayList<>();
        ResultSet rs = null;
        try {
            String sql = "select * from nav";
            rs = DBUtil.executeQuery(sql,null);
            while (rs.next()) {
                int dId = rs.getInt("id");
                String nname = rs.getString("n_name");
                String nheavy = rs.getString("n_heavy");
                nav = new nav(dId,nname,nheavy);
                navs.add(nav);
            }
            return navs;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return null;
        }finally {
            //关
            DBUtil.closeAll(rs,pstmt,DBUtil.con);
        }
    }

    @Override
    public nav queryNavById(int id) {
        Connection con = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        nav nav = null;
        try {
            String sql = "select * from nav where id = ?";
            Object[] params = {id};
            //预编译
            rs = DBUtil.executeQuery(sql,params);
            if (rs.next()){
                int dId = rs.getInt("id");
                String nname = rs.getString("n_name");
                String nheavy = rs.getString("n_heavy");
                nav = new nav(dId,nname,nheavy);
            }
            return nav;

            } catch (SQLException throwables) {
            throwables.printStackTrace();
            return null;
        } catch (Exception e){
            e.printStackTrace();
            return null;
        } finally {
            //关
            DBUtil.closeAll(rs,pstmt,DBUtil.con);
        }

    }

    @Override
    public boolean loginUserQuery(String uname, String pwd) {
        return false;
    }
}

package com.daoimpl;

import com.dao.INewsArtDao;
import com.date.art;
import com.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class NewsArtDaoimpl implements INewsArtDao {
    @Override
    public boolean isQueryExist(int id) {
        return queryArtById(id)==null?false:true;
    }

    @Override
    public boolean addArt(art art) {
        String sql = "insert into art (a_title,a_nav,a_time) values(?,?,?)";
        Object[] params = {art.getA_title(),art.getA_nav(),art.getA_time()};
        return DBUtil.excuteUpdate(sql,params);
    }

    @Override
    public boolean deleteArtById(int id) {
        String sql = "delete from art where id = ?";
        Object[] params = {id};
        return DBUtil.excuteUpdate(sql,params);
    }

    @Override
    public boolean updateArtById(int id, art art) {
        String sql = "update art set a_title = ?, a_nav = ? where id = ?";
        Object[] params = {art.getA_title(),art.getA_nav(),id};
        return DBUtil.excuteUpdate(sql,params);
    }

    @Override
    public List<art> queryAllArts() {
        PreparedStatement pstmt = null;
        art art = null;
        List<art> arts = new ArrayList<>();
        ResultSet rs = null;
        try {
            String sql = "select * from art";
            rs = DBUtil.executeQuery(sql,null);
            while (rs.next()) {
                int dId = rs.getInt("id");
                String atitle = rs.getString("a_title");
                String anav = rs.getString("a_nav");
                String atime = rs.getString("a_time");
                art = new art(dId,atitle,anav,atime);
                arts.add(art);
            }
            return arts;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return null;
        }finally {
            //关
            DBUtil.closeAll(rs,pstmt,DBUtil.con);
        }
    }

    @Override
    public art queryArtById(int id) {
        Connection con = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        art art = null;
        try {
            String sql = "select * from art where id = ?";
            Object[] params = {id};
            //预编译
            rs = DBUtil.executeQuery(sql,params);
            if (rs.next()){
                int dId = rs.getInt("id");
                String atitle = rs.getString("a_title");
                String anav = rs.getString("a_nav");
                String atime = rs.getString("a_time");
                art = new art(dId,atitle,anav,atime);
            }
            return art;

        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return null;
        } catch (Exception e){
            e.printStackTrace();
            return null;
        } finally {
            //关
            DBUtil.closeAll(rs,pstmt,DBUtil.con);
        }

    }

    @Override
    public boolean loginUserQuery(String uname, String pwd) {
        return false;
    }
}

5.实现Service层

package com.serviceimpl;

import com.dao.INewsUserDao;
import com.daoimpl.NewsUserDaoimpl;
import com.date.user;
import com.service.INewsUserService;

import java.util.List;

public class NewsUserServiceimpl implements INewsUserService {
    INewsUserDao userDao = new NewsUserDaoimpl();

    @Override
    public List<user> queryAllUser() {
        return userDao.queryAllUsers();
    }

    @Override
    public boolean updateUser(int id, user user) {
        if (userDao.isQueryExist(id))
            return userDao.updateUserById(id,user);
        else
            return false;
    }

    //增----->增+查
    @Override
    public boolean addUser(user user) {
        //不存在时增
        if (!userDao.isQueryExist(user.getId()))
            return userDao.addUser(user);
        else
            return false;
    }

    @Override
    public boolean deleteUser(int id) {
        if (userDao.isQueryExist(id))
            return userDao.deleteUserById(id);
        else
            return false;
    }

    @Override
    public user queryUserById(int id) {
        return userDao.queryUserById(id);
    }

    //登录
    @Override
    public boolean loginUserQuery(String uname,String pwd){
        return userDao.loginUserQuery(uname, pwd);
    }
}

package com.serviceimpl;

import com.dao.INewsNavDao;

import com.daoimpl.NewsNavDaoimpl;

import com.date.nav;
import com.service.INewsNavService;

import java.util.List;

public class NewsNavServiceimpl implements INewsNavService {

    INewsNavDao navDao = new NewsNavDaoimpl();

    @Override
    public List<nav> queryAllNavs() {
        return navDao.queryAllNavs();
    }

    @Override
    public boolean updateNav(int id, nav nav) {
        if (navDao.isQueryExist(id))
            return navDao.updateNavById(id,nav);
        else
            return false;
    }

    @Override
    public boolean addNav(nav nav) {
        //不存在时增
        if (!navDao.isQueryExist(nav.getId()))
            return navDao.addNav(nav);
        else
            return false;
    }

    @Override
    public boolean deleteNav(int id) {
        if (navDao.isQueryExist(id))
            return navDao.deleteNavById(id);
        else
            return false;
    }

    @Override
    public nav queryNavById(int id) {
        return navDao.queryNavById(id);
    }

    //空实现
    @Override
    public boolean loginUserQuery(String uname, String pwd) {
        return false;
    }

}

package com.serviceimpl;

import com.dao.INewsArtDao;
import com.daoimpl.NewsArtDaoimpl;
import com.date.art;
import com.service.INewsArtService;

import java.util.List;

public class NewsArtServiceimpl implements INewsArtService {

    INewsArtDao artDao = new NewsArtDaoimpl();

    @Override
    public List<art> queryAllArts() {
        return artDao.queryAllArts();
    }

    @Override
    public boolean updateArt(int id, art art) {
        if (artDao.isQueryExist(id))
            return artDao.updateArtById(id,art);
        else
            return false;
    }


    @Override
    public boolean addArt(art art) {
        //不存在时增
        if (!artDao.isQueryExist(art.getId()))
            return artDao.addArt(art);
        else
            return false;
    }

    @Override
    public boolean deleteArt(int id) {
        if (artDao.isQueryExist(id))
            return artDao.deleteArtById(id);
        else
            return false;
    }

    @Override
    public art queryArtById(int id) {
        return artDao.queryArtById(id);
    }

    @Override
    public boolean loginUserQuery(String uname, String pwd) {
        return false;
    }
}

6.实现UI层

package com.main;

import java.util.Scanner;

public class index {
    public static void main(String[] args) {
        //登录
        show.loginUI();
        show.mainShow();
    }
}

package com.main;

import com.date.art;
import com.date.nav;
import com.date.user;
import com.service.INewsArtService;
import com.service.INewsNavService;
import com.service.INewsUserService;
import com.serviceimpl.NewsArtServiceimpl;
import com.serviceimpl.NewsNavServiceimpl;
import com.serviceimpl.NewsUserServiceimpl;

import java.util.List;
import java.util.Scanner;

public class show {
    //子登录界面(简易管理员登录界面)
    public static boolean adminLoginUI(){
        Scanner sc = new Scanner(System.in);
        System.out.println("-----管理员登录界面-----");
        System.out.println("请输入管理员账号:");
        String adminName = sc.next();
        System.out.println("请输入管理员密码:");
        String adminPassword = sc.next();
        if (adminName.equals("adminstrator")&&adminPassword.equals("2511880"))
            return true;
        else
            return false;
    }


    //主登录界面
    public static void loginUI(){
        Scanner sc = new Scanner(System.in);
        INewsUserService userService = new NewsUserServiceimpl();
        System.out.println("-----欢迎来到新闻发布系统-----");
        for (int i=3 ; i>=0 ;i --){
            System.out.print("请输入您的账号:"+"\n");
            String userName = sc.next();
            System.out.print("请输入您的密码:"+"\n");
            String userPassword = sc.next();
            if (userService.loginUserQuery(userName,userPassword) == true){
                System.out.println("---欢迎用户"+userName+"进入新闻管理系统界面---");
                break;
            }
            else{
                if (i == 0){
                    System.out.println("次数用完,退出系统,再次登录请联系管理员");
                    System.exit(0);
                }
                System.out.println("用户名密码错误,请重新输入还剩"+i+"次机会");
            }
        }
    }

    //登陆后主界面
    public static void mainShow(){
        try {
            while (true) {
                Scanner sc = new Scanner(System.in);
                System.out.println("--1.用户管理");
                System.out.println("--2.类别管理");
                System.out.println("--3.文章管理");
                System.out.println("--4.退出系统(注意:输入操作序号以外的字符将自动退出系统!!!)");
                System.out.print("请输入操作序号:");
                int num = sc.nextInt();
                switch (num) {
                    case 1:
                        userMa();
                        break;
                    case 2:
                        navUI();
                        break;
                    case 3:
                        artUI();
                        break;
                    case 4:
                        System.out.println("即将推出系统,欢迎下次使用!");
                        System.exit(0);
                        break;
                    default:
                        System.exit(0);
                }
            }
        } catch (Exception e) {
            System.out.println("&&&&&&&&------WARNING:即将退出系统!!!");
            System.out.println("异常信息:"+e.getMessage());
            e.printStackTrace();
            System.exit(0);
        }
    }

    //子界面:用户管理界面
    public static void userMa(){
        INewsUserService userService = new NewsUserServiceimpl();
        user user = null;
        boolean flag = true;
        while (true) {
            Scanner sc = new Scanner(System.in);
            System.out.println("--1.注册用户");
            System.out.println("--2.修改用户信息(管理员权限)");
            System.out.println("--3.删除用户信息(管理员权限)");
            System.out.println("--4.查询所有用户信息(管理员权限)");
            System.out.println("--5.返回");
            System.out.println("----------注意:输入操作序号以外的字符将退出系统!!!");
            System.out.print("请输入操作序号:");
            try {

                int num = sc.nextInt();
                switch (num) {
                    case 1:
//                        System.out.println("请输入注册id值(熟记:id值用来删除,修改,查询用户):");
//                        int id = sc.nextInt();
                        System.out.println();
                        System.out.println("请输入注册的用户名:");
                        String uname = sc.next();
                        System.out.println("请输入注册密码:");
                        String pwd = sc.next();
                        System.out.println("请输入真实姓名(请用英文):");
                        String rname = sc.next();
                        user = new user(uname, pwd, rname);
                        flag = userService.addUser(user);
                        if (flag == true) {
                            System.out.println("注册成功!");
                            System.out.println("----------------");
                        }
                        else {
                            System.out.println("注册失败!");
                            System.out.println("----------------");
                        }
                        break;

                    //可以修改真实姓名
                    case 2:
                        flag = adminLoginUI();
                        if (flag == true){
                            System.out.println("已获得管理员权限!");
                            System.out.println("请输入所要修改账户的id:");
                            int id = sc.nextInt();
                            System.out.println("请输入修改后的密码:");
                            String cPwd = sc.next();
                            System.out.println("请输入修改后的真实姓名:");
                            String cRname = sc.next();
                            user = new user(cPwd,cRname);
                            flag = userService.updateUser(id,user);
                            if (flag == true) {
                                System.out.println("修改成功!");
                                System.out.println("----------------");
                            }
                            else{
                                System.out.println("修改失败!");
                                System.out.println("----------------");
                            }
                        }
                        else{
                            System.out.println("账号或密码不正确,未获得管理员权限------>注意:由于安全问题系统即将关闭!");
                            System.exit(0);
                        }
                        break;

                    case 3:
                        flag = adminLoginUI();
                        if (flag == true){
                            System.out.println("已获得管理员权限!");
                            System.out.println("请输入所要删除账户的id号:");
                            int id = sc.nextInt();
                            flag = userService.deleteUser(id);
                            if (flag == true) {
                                System.out.println("删除成功!");
                                System.out.println("----------------");
                            }
                            else{
                                System.out.println("删除失败!");
                                System.out.println("----------------");
                            }
                        }
                        else{
                            System.out.println("账号或密码不正确,未获得管理员权限------>注意:由于安全问题系统即将关闭!");
                            System.exit(0);
                        }
                        break;

                    case 4:
                        List<user> users = null;
                        flag = adminLoginUI();
                        if (flag == true){
                            System.out.println("已获得管理员权限!");
                            System.out.println("-----全部用户信息显示-----");
                            users = userService.queryAllUser();

                            for(int i = 0;i < users.size();i++) {
                                System.out.println();
                                user users1 = (user)users.get(i);
                                System.out.println("id:"+users1.getId()+" "+"用户名:"+users1.getU_uname()+" "+"密码:"+users1.getU_pwd()+" "+"真实姓名:"+users1.getU_rname());
                            }
                        }
                        else{
                            System.out.println("账号或密码不正确,未获得管理员权限------>注意:由于安全问题系统即将关闭!");
                            System.exit(0);
                        }
                        break;

                    case 5:
                        mainShow();
                        break;
                    default:
                        System.exit(0);
                }
            } catch (Exception e) {
                System.out.println("&&&&&&&&------WARNING:即将退出系统!!!");
                System.exit(0);
                e.printStackTrace();
            }
        }
    }

    //子界面:类别管理界面
    public static void navUI(){
        INewsNavService navService = new NewsNavServiceimpl();
        nav nav = null;
        boolean flag = true;
        while (true) {
            Scanner sc = new Scanner(System.in);
            System.out.println("--1.增加类别");
            System.out.println("--2.修改类别(管理员权限)");
            System.out.println("--3.删除类别(管理员权限)");
            System.out.println("--4.查询所有类别");
            System.out.println("--5.返回");
            System.out.println("----------注意:输入操作序号以外的字符将退出系统!!!");
            System.out.print("请输入操作序号:");
            try {

                int num = sc.nextInt();
                switch (num) {
                    case 1:
//                        System.out.println("请输入注册id值(熟记:id值用来删除,修改,查询用户):");
//                        int id = sc.nextInt();
                        System.out.println();
                        System.out.println("添加类别:");
                        String nNav = sc.next();
                        System.out.println("添加权重:");
                        String nHeavy = sc.next();
                        nav = new nav(nNav, nHeavy);
                        flag = navService.addNav(nav);
                        if (flag == true) {
                            System.out.println("添加成功!");
                            System.out.println("----------------");
                        }
                        else {
                            System.out.println("添加失败!");
                            System.out.println("----------------");
                        }
                        break;

                    case 2:
                        flag = adminLoginUI();
                        if (flag == true){
                            System.out.println("已获得管理员权限!");
                            System.out.println("请输入所要修改类别的id:");
                            int id = sc.nextInt();
                            System.out.println("请输入修改后的类别:");
                            String cNav = sc.next();
                            System.out.println("请输入修改后的权重:");
                            String cHeavy = sc.next();
                            nav = new nav(cNav,cHeavy);
                            flag = navService.updateNav(id,nav);
                            if (flag == true) {
                                System.out.println("修改成功!");
                                System.out.println("----------------");
                            }
                            else{
                                System.out.println("修改失败!");
                                System.out.println("----------------");
                            }
                        }
                        else{
                            System.out.println("账号或密码不正确,未获得管理员权限------>注意:由于安全问题系统即将关闭!");
                            System.exit(0);
                        }
                        break;

                    case 3:
                        flag = adminLoginUI();
                        if (flag == true){
                            System.out.println("已获得管理员权限!");
                            System.out.println("请输入所要删除账户的id号:");
                            int id = sc.nextInt();
                            flag = navService.deleteNav(id);
                            if (flag == true) {
                                System.out.println("删除成功!");
                                System.out.println("----------------");
                            }
                            else{
                                System.out.println("删除失败!");
                                System.out.println("----------------");
                            }
                        }
                        else{
                            System.out.println("账号或密码不正确,未获得管理员权限------>注意:由于安全问题系统即将关闭!");
                            System.exit(0);
                        }
                        break;

                    case 4:
                        List<nav> navs = null;
                            navs = navService.queryAllNavs();
                            for(int i = 0;i < navs.size();i++) {
                                System.out.println();
                                nav navs1 = (nav)navs.get(i);
                                System.out.println("id:"+navs1.getId()+" "+"类别名:"+navs1.getN_name()+" "+"权重:"+navs1.getN_heavy());
                            }
                        break;

                    case 5:
                        mainShow();
                        break;
                    default:
                        System.exit(0);
                }
            } catch (Exception e) {
                System.out.println("&&&&&&&&------WARNING:即将退出系统!!!");
                System.exit(0);
                e.printStackTrace();
            }
        }
    }

    public static void artUI(){
        INewsArtService artService = new NewsArtServiceimpl();
        art art = null;
        boolean flag = true;
        while (true) {
            Scanner sc = new Scanner(System.in);
            System.out.println("--1.增加文章信息");
            System.out.println("--2.修改文章信息(管理员权限)");
            System.out.println("--3.删除文章(管理员权限)");
            System.out.println("--4.查询所有文章");
            System.out.println("--5.返回");
            System.out.println("----------注意:输入操作序号以外的字符将退出系统!!!");
            System.out.print("请输入操作序号:");
            try {

                int num = sc.nextInt();
                switch (num) {
                    case 1:
//                        System.out.println("请输入注册id值(熟记:id值用来删除,修改,查询用户):");
//                        int id = sc.nextInt();
                        System.out.println();
                        System.out.println("添加文章标题:");
                        String aArt = sc.next();
                        System.out.println("添加文章类别:");
                        String aNav = sc.next();
                        System.out.println("添加文章发布时间:");
                        String aTime = sc.next();
                        art = new art(aArt, aNav,aTime);
                        flag = artService.addArt(art);
                        if (flag == true) {
                            System.out.println("添加成功!");
                            System.out.println("----------------");
                        }
                        else {
                            System.out.println("添加失败!");
                            System.out.println("----------------");
                        }
                        break;

                    case 2:
                        flag = adminLoginUI();
                        if (flag == true){
                            System.out.println("已获得管理员权限!");
                            System.out.println("请输入所要修改文章的id:");
                            int id = sc.nextInt();
                            System.out.println("请输入修改后的文章的标题:");
                            String cTitle = sc.next();
                            System.out.println("请输入修改后的文章的类别:");
                            String cNav = sc.next();
                            art = new art(cTitle,cNav);
                            flag = artService.updateArt(id,art);
                            if (flag == true) {
                                System.out.println("修改成功!");
                                System.out.println("----------------");
                            }
                            else{
                                System.out.println("修改失败!");
                                System.out.println("----------------");
                            }
                        }
                        else{
                            System.out.println("账号或密码不正确,未获得管理员权限------>注意:由于安全问题系统即将关闭!");
                            System.exit(0);
                        }
                        break;

                    case 3:
                        flag = adminLoginUI();
                        if (flag == true){
                            System.out.println("已获得管理员权限!");
                            System.out.println("请输入所要删除账户的id号:");
                            int id = sc.nextInt();
                            flag = artService.deleteArt(id);
                            if (flag == true) {
                                System.out.println("删除成功!");
                                System.out.println("----------------");
                            }
                            else{
                                System.out.println("删除失败!");
                                System.out.println("----------------");
                            }
                        }
                        else{
                            System.out.println("账号或密码不正确,未获得管理员权限------>注意:由于安全问题系统即将关闭!");
                            System.exit(0);
                        }
                        break;

                    case 4:
                        List<art> arts = null;
                        arts = artService.queryAllArts();
                        for(int i = 0;i < arts.size();i++) {
                            System.out.println();
                            art arts1 = (art)arts.get(i);
                            System.out.println("id:"+arts1.getId()+" "+"标题:"+arts1.getA_title()+" "+"类别:"+arts1.getA_nav()+" "+"发布时间:"+arts1.getA_time());
                        }
                        break;

                    case 5:
                        mainShow();
                        break;
                    default:
                        System.exit(0);
                }
            } catch (Exception e) {
                System.out.println("&&&&&&&&------WARNING:即将退出系统!!!");
                System.exit(0);
                e.printStackTrace();
            }
        }
    }

}


7.展示时序图
实训:新闻管理系统(Java)_第4张图片

你可能感兴趣的:(Java程序设计,java,后端)