//在控制台输出的简易的人力资源管理系统 实现对员工的增删改查
//共分为7个类:JDBCUtil、JDBCOperation、Main、Staff、StaffOperation、Input、UI
//1、JDBCUtil类  jdbc工具类
package com.etc.sys;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JDBCUtil {
	static String CLASS_NAME = "com.mysql.jdbc.Driver";
	static String URL = "jdbc:mysql://127.0.0.1:3306/day08?useSSL=true";
	static String USERNAME = "root";
	static String PASSWORD = "123456";

	public Connection getConn(Connection conn) {
		try {
			// 加载驱动
			Class.forName(CLASS_NAME);
			// 获取连接
			conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		}
		return conn;
	}

	public void close(PreparedStatement pstmt, Connection conn) {
		try {
			pstmt.close();
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public void close(ResultSet rs, Statement ps, Connection conn) {
		try {
			rs.close();
			ps.close();
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}
//2、JDBCOperation  增删改查操作
package com.etc.sys;

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

public class JDBCOperation {
	JDBCUtil jdbcUtil = new JDBCUtil();
	Connection conn = null;
	public void insert_DB(Staff s) throws ClassNotFoundException, SQLException {
		conn = jdbcUtil.getConn(conn);
		// 发送语句
		String sql = "insert into staff(s_num,s_name,s_salary) value(?,?,?)";
		PreparedStatement pstmt = conn.prepareStatement(sql);
		pstmt.setString(1, s.getNum());
		pstmt.setString(2, s.getName());
		pstmt.setString(3, s.getSalary());
		pstmt.executeUpdate();
		// 关闭连接
		jdbcUtil.close(pstmt, conn);
	}

	public void delete_DB(String num) throws ClassNotFoundException, SQLException {
		// 发送语句
		conn = jdbcUtil.getConn(conn);
		String sql = "delete from staff where s_num=?";
		PreparedStatement pstmt = conn.prepareStatement(sql);
		pstmt.setString(1, num);
		pstmt.executeUpdate();
		// 关闭连接
		jdbcUtil.close(pstmt, conn);
	}

	public void modify_DB(String num, String salary) throws ClassNotFoundException, SQLException {
		conn = jdbcUtil.getConn(conn);
		// 发送语句
		String sql = "update staff set s_salary=? where s_num=?";
		PreparedStatement pstmt = conn.prepareStatement(sql);
		pstmt.setString(1, salary);
		pstmt.setString(2, num);
		pstmt.executeUpdate();
		// 关闭连接
		jdbcUtil.close(pstmt, conn);
	}

	public void query_DB() throws ClassNotFoundException,SQLException {
		conn = jdbcUtil.getConn(conn);
		// 发送语句
		List<Staff> staffList = new ArrayList<Staff>();
		Statement ps = conn.createStatement();
		ResultSet rs = ps.executeQuery("select * from staff");
		while (rs.next()) {
			Staff s = new Staff();
			s.setNum(rs.getString(1));
			s.setName(rs.getString(2));
			s.setSalary(rs.getString(3));
			staffList.add(s);
		}
		for (Staff s : staffList) {
			s.printStaffInfo();
		}
		// 关闭连接
		jdbcUtil.close(rs, ps, conn);
	}
}



//3、Main  主方法
package com.etc.sys;

import java.sql.SQLException;
import java.util.Scanner;
import com.etc.sys.Staff;

public class Main {
	public static void main(String[] args) throws ClassNotFoundException, SQLException {
		// UI界面
		UI.welcome();
		StaffOperation staffOperation = new StaffOperation();
		Scanner s = new Scanner(System.in);
		while (true) {
			UI.menu();
			int menuInt = Input.getMenuInt(s);
			if (menuInt == 1) {
				String num = Input.getNum(s);
				String name = Input.getName(s);
				String salary = Input.getSalary(s);
				Staff staff = new Staff(num, name, salary);
				staff.printStaffInfo();
				boolean confirm = Input.getConfirm(s);
				if (confirm) {
					staffOperation.insertStaff(staff);
				}
			} else if (menuInt == 2) {
				System.out.println("查询员工如下:");
				staffOperation.selectStaff();
			} else if (menuInt == 3) {
				String num = Input.getNum(s);
				staffOperation.deleteSatff(num);
			} else if (menuInt == 4) {
				String num = Input.getNum(s);
				String salary = Input.getSalary(s);
				staffOperation.updateStaff(num, salary);
			}
			if (menuInt == 5) {
				UI.exitSys();
				break;
			}
		}
	}
}

//4、Staff  员工类
package com.etc.sys;

public class Staff {
	private String num;
	private String name;
	private String salary;
	
	
	public String getNum() {
		return num;
	}
	public void setNum(String num) {
		this.num = num;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSalary() {
		return salary;
	}
	public void setSalary(String salary) {
		this.salary = salary;
	}
	public Staff() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Staff(String num, String name, String salary) {
		super();
		this.num = num;
		this.name = name;
		this.salary = salary;
	}
	@Override
	public String toString() {
		return "Staff [num=" + num + ", name=" + name + ", salary=" + salary + "]";
	}
	public void printStaffInfo() {
		System.out.println("员工信息为,员工编号:"+num+",员工姓名:"+name+",员工薪资:"+salary);
	}
}

//5、StaffOperation 员工操作
package com.etc.sys;
import java.sql.SQLException;
import com.etc.sys.Staff;

public class StaffOperation {
	JDBCOperation jdbcOperation = new JDBCOperation();

	public void insertStaff(Staff s) throws ClassNotFoundException, SQLException {
		jdbcOperation.insert_DB(s);
		System.out.println(s.getName() + "已入职");
	}

	public void deleteSatff(String num) throws ClassNotFoundException, SQLException {

		jdbcOperation.delete_DB(num);
		System.out.println("员工离职成功");

	}

	public void updateStaff(String num, String salary) throws ClassNotFoundException, SQLException {
		jdbcOperation.modify_DB(num, salary);
		System.out.println("员工调薪成功");
	}

	public void selectStaff() throws ClassNotFoundException, SQLException {
		jdbcOperation.query_DB();
	}
}

//6、Input类  从键盘键入
package com.etc.sys;
import java.util.Scanner;
public class Input {
	public static int getMenuInt(Scanner s) {
		System.out.println("请选择功能,按回车或空格结束...");
		return s.nextInt();
	}
	public static boolean getConfirm(Scanner s) {
		System.out.println("确认输入Y取消任意输入,按回车结束...");
		boolean result = false;
		String inputStr = s.next();
		if("Y".equalsIgnoreCase(inputStr)) {
			result = true;
		}
		return result;
	}
	public static String getNum(Scanner s) {
		System.out.println("请输入员工编号,以空格或回车隔开:");
		return s.next();
	}
	public static String getName(Scanner s) {
		System.out.println("请输入员工姓名,以空格或回车隔开:");
		return s.next();
	}
	public static String getSalary(Scanner s) {
		System.out.println("请输入员工薪资,以空格或回车隔开:");
		return s.next();
	}
}

//7、UI  简易界面
package com.etc.sys;

public class UI {
	public static void welcome() {
		System.out.println("欢迎使用XX人力资源系统\"");
	}
	public static void menu() {
		System.out.println("欢迎使用XX公司人力资源系统");
		System.out.println("1:员工入职");
		System.out.println("2:员工查询");
		System.out.println("3:员工辞退");
		System.out.println("4:员工加薪");
		System.out.println("5:退出系统");
	}
	public static void exitSys() {
		System.out.println("您已退出系统");
		
	}
}