Output Problem Statement You are going to build a database that an employer can
ID: 3805037 • Letter: O
Question
Output
Problem Statement You are going to build a database that an employer can use to keep track of their employees. You will need a way to represent a single employee (name, id number, salary) and a way to store an unknown number of employees. You will build the capability to add/remove employees from the database and provide some other functionality described below. Assignment Create an Outlab5 project and paste this into a Driver. You won't need to change the Driver. Here are descriptions of the methods you need: addEmployee. This method reads in a name and salary to represent a new employee. The employee id number is just the number that employee was to join the company. Ie. The first employee added has id number 1, the second id number2,and so on. If an employee ends up leaving the company, employee numbers stay the same and new employees are still given the next number. E.g. If employee i leaves, employee 2 stays employee 2. removeEmployee. This method removes an employee from the database based on the employee id number passed. Ie. If employee 123 is to be removed, find the employee with id number 123 and remove them. If that employee does not exist, print an error message. o print Alphabetically. This method will print out all the employees in the database in alphabetical order. o printByld. This method will print out all the employees in the database by order of their id numbers starting with employee 1. If some employees (e.g. employee number 7) is no longer in the database, just skip that number. pay Increase. Give each employee a pay increase of X% where X is the input parameter. getPayrollCost. Return the total amount of salary of all the employees in the database. Here is some sample output. Rules You are not allowed to use ArrayLists or any other built in java data structures beyond plain arrays. You are not allowed to use any sort of build in sorting functions. You don't need them. Figure out how to do it yourself. You are allowed to use the compare lo method in the String class. You will lose all the points for a method if you break these rules for that method.Explanation / Answer
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package emplyeemanagement;
import java.util.Scanner;
/**
*
* @author Rashmi Tiwari
*/
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
class ConnectionClass
{
static
{
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");//Driver class of oracle
}
catch(ClassNotFoundException e)
{
e.printStackTrace();
}
}
public static Connection getConnection()throws SQLException
{
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","system","123"); //Change Driver url ,username and password according to database
return con;
}
public static void closeConnection(Connection con,Statement stmt,ResultSet rs)
{
try
{
if(con!=null)
{
con.close();
}
if(stmt!=null)
{
stmt.close();
}
if(rs!=null)
{
rs.close();
}
}
catch(SQLException e)
{
e.printStackTrace();
}
}
}
class Database{
Connection con=null;
Statement stmt=null;
ResultSet rs=null;
void addEmployee(String name,double sal){//method to add employee
try
{
con=ConnectionClass.getConnection();
stmt=con.createStatement();
rs=stmt.executeQuery("insert into employee values('"+name+"','"+sal+"')");
System.out.println("Data Submiited");
}
catch(SQLException e)
{
e.printStackTrace();
}
}
void removeEmployee(int id){//method to remove an employee
try{
con=ConnectionClass.getConnection();
stmt=con.createStatement();
rs=stmt.executeQuery("delete from employee where id='"+id+"'");
System.out.println("Data Submiited");
}
catch(SQLException e)
{
e.printStackTrace();
}
}
void printAlphabetically(){//print employee alphabetical order
try
{
con=ConnectionClass.getConnection();
stmt=con.createStatement();
rs=stmt.executeQuery("select * from employee order by name asc");
while(rs.next()){
int id=rs.getInt("id");
String name=rs.getString("name");
double sal=Double.parseDouble(rs.getString("sal"));
System.out.println("Id=>"+id+" Name"+name+" Salary "+sal);
}
}
catch(SQLException e)
{
e.printStackTrace();
}
}
void printById(){//print employee data by id
try
{
con=ConnectionClass.getConnection();
stmt=con.createStatement();
rs=stmt.executeQuery("select * from employee order by id asc");
while(rs.next()){
int id=rs.getInt("id");
String name=rs.getString("name");
double sal=Double.parseDouble(rs.getString("sal"));
System.out.println("Id=>"+id+" Name"+name+" Salary "+sal);
}
}
catch(SQLException e)
{
e.printStackTrace();
}
}
void payIncrease(double per){//increase salary of employee by given percentage
try
{
con=ConnectionClass.getConnection();
stmt=con.createStatement();
rs=stmt.executeQuery("update employee salary=salary*'"+per+"' ");
}
catch(SQLException e)
{
e.printStackTrace();
}
}
double getPayrollCost(){//return total salary
double sal=0;
try
{
con=ConnectionClass.getConnection();
stmt=con.createStatement();
rs=stmt.executeQuery("select sum(salary) from employee ");
sal=rs.getInt(0);
}
catch(SQLException e)
{
e.printStackTrace();
}
return sal;
}
}
public class EmplyeeManagement {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Database db = new Database();
int choice = 0;
System.out.println("Employee Management System");
while (choice != 7)
{
//menu options
System.out.println("1. Add Employee.");
System.out.println("2. Remove Employee.");
System.out.println("3. Print Alphabetically.");
System.out.println("4. Print By ID Number.");
System.out.println("5. Issue Pay Increase.");
System.out.println("6. Get Payroll Cost.");
System.out.println("7. Quit.");
System.out.print(" Please enter your choice > ");
choice = in.nextInt();
in.nextLine();
switch (choice)
{
case 1:
System.out.print(" Name? > ");
String name = in.nextLine();
System.out.print(" Salary? > ");
double salary = in.nextDouble();
in.nextLine();
db.addEmployee(name, salary);
break;
case 2:
System.out.print(" ID Number? > ");
int id = in.nextInt();
in.nextLine();
db.removeEmployee(id);
break;
case 3:
db.printAlphabetically();
break;
case 4:
db.printById();
break;
case 5:
System.out.print(" Percent Increase (e.g. .02)? > ");
double percent = in.nextDouble();
in.nextLine();
db.payIncrease(percent);
break;
case 6:
System.out.println(db.getPayrollCost());
break;
case 7:
break;
default:
System.out.println("That is an invalid choice. Please try again. ");
}
System.out.println();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.