package Payroll;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
public class Project9_1 extends JFrame implements ActionListener{
// Employee Picture
// Save Files
// Check if First Name or Last Name has two subStrings
// Check if JField still has invalid or any past Errors with boolean
// Delete Account, store ID value to be used again
// Prevent Save If Error Occurs
// Create Read/Write Files store into EmployeeArray
// Add JMenu - Settings - Credits - Save - Log(new large JFrame with JList to view 100 logs)
JFrame second;
JPanel Main, Confirm;
JLabel nameL, lastL, typeL, hRateL, hHourL, payL, logL, IDL;
JTextField name, last, hRate, hHour, pay, ID;
JList log;
DefaultListModel logM;
JScrollPane logS;
JButton enter, one, two, add, delete, edit, clear, save;
JFrame f = new JFrame();
Employee[] EmployeeArray = new Employee[100];
int count = 0;
int eType = 0;
int eHour = 0;
int eID = 0;
int index = 0;
double eRate = 0;
String Error = "";
String Test = "";
boolean ErrorName = false;
boolean ErrorLast = false;
boolean ErrorRate = false;
boolean ErrorHour = false;
boolean ErrorID = false;
boolean ErrorNameLast = false;
boolean ErrorIDRepeat = false;
public Project9_1() {
this.setTitle("Payroll");
this.add(Panel());
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
this.setLocationRelativeTo(null);
this.setVisible(true);
}
public JPanel Panel(){
Main = new JPanel();
Main.setPreferredSize(new Dimension(300,350));
Main.setLayout(null);
nameL = new JLabel("Name:");
nameL.setForeground(Color.RED);
nameL.setBounds(10, 10, 100, 30);
Main.add(nameL);
name = new JTextField();
name.setBounds(10, 35, 100, 30);
name.setEditable(true);
name.requestFocus();
Main.add(name);
lastL = new JLabel("Last Name:");
lastL.setForeground(Color.RED);
lastL.setBounds(150, 10, 100, 30);
Main.add(lastL);
last = new JTextField();
last.setEditable(true);
last.setBounds(150, 35, 100, 30);
last.requestFocus();
Main.add(last);
typeL = new JLabel("Type: " + eType);
typeL.setForeground(Color.RED);
typeL.setBounds(10, 65, 100, 30);
Main.add(typeL);
one = new JButton("Full");
one.setBounds(10, 90, 55, 30);
one.addActionListener(this);
one.setActionCommand("one");
Main.add(one);
two = new JButton("Part");
two.setBounds(70, 90, 60, 30);
two.addActionListener(this);
two.setActionCommand("two");
Main.add(two);
hRateL = new JLabel("Hourly Rate:");
hRateL.setBounds(10, 120, 100, 30);
hRateL.setForeground(Color.RED);
Main.add(hRateL);
hRate = new JTextField();
hRate.setEditable(true);
hRate.requestFocus();
hRate.setBounds(10, 145, 100, 30);
Main.add(hRate);
hHourL = new JLabel("Hours Worked:");
hHourL.setForeground(Color.RED);
hHourL.setBounds(10, 170, 100, 30);
Main.add(hHourL);
hHour = new JTextField();
hHour.requestFocus();
hHour.setEditable(true);
hHour.setBounds(10, 195, 100, 30);
Main.add(hHour);
IDL = new JLabel("Employee ID:");
IDL.setBounds(10, 220, 100, 30);
IDL.setForeground(Color.RED);
Main.add(IDL);
ID = new JTextField();
ID.requestFocus();
ID.setBounds(10, 245, 100, 30);
Main.add(ID);
payL = new JLabel("Pay:");
payL.setBounds(10, 270, 100, 30);
payL.setForeground(Color.RED);
Main.add(payL);
pay = new JTextField();
pay.requestFocus();
pay.setText("0");
pay.setEditable(false);
pay.setBounds(10, 295, 100, 30);
Main.add(pay);
logL = new JLabel("Log:");
logL.setForeground(Color.RED);
logL.setBounds(150, 65, 100, 30);
Main.add(logL);
logM = new DefaultListModel();
// Read File
// logM.addElement("");
log = new JList(logM);
log.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
log.setSelectedIndex(0);
log.setVisibleRowCount(10);
log.setBounds(150, 90, 100, 120);
Main.add(log);
logS = new JScrollPane(log);
logS.setBounds(150, 90, 100, 90);
Main.add(logS);
add = new JButton("Add Log");
add.addActionListener(this);
add.setActionCommand("add");
add.setBounds(150, 180, 100, 30);
Main.add(add);
delete = new JButton("Delete Log");
delete.setBounds(150, 210, 100, 30);
delete.addActionListener(this);
delete.setActionCommand("delete");
Main.add(delete);
edit = new JButton("Select Log");
edit.setBounds(150, 240, 100, 30);
edit.addActionListener(this);
edit.setActionCommand("edit");
Main.add(edit);
clear = new JButton("Clear");
clear.setBounds(150, 270, 100, 30);
clear.addActionListener(this);
clear.setActionCommand("clear");
Main.add(clear);
save = new JButton("Save");
save.setBounds(150, 300, 100, 30);
save.addActionListener(this);
save.setActionCommand("save");
Main.add(save);
return Main;
}
public String Pay(){
String pay = "";
double tax = 0;
double payroll = 0;
payroll = (EmployeeArray[index].getHours() * 52) * EmployeeArray[index].getRate();
//JOptionPane.showMessageDialog(f, "Payroll: " + payroll);
tax = payroll * .1;
//JOptionPane.showMessageDialog(f, "Marginal Tax Rate: " + tax);
payroll = payroll - tax;
//JOptionPane.showMessageDialog(f, "Total: " + payroll);
pay = Double.toString(payroll);
return pay;
}
public JPanel Confirm(){
Confirm = new JPanel();
Main.setPreferredSize(new Dimension(100,100));
Main.setLayout(null);
JLabel info = new JLabel("Full Time or Part Time");
info.setBounds(10, 10, 100, 30);
info.setForeground(Color.RED);
Confirm.add(info);
JButton full = new JButton("Full Time");
full.setBounds(10, 30, 100, 30);
full.addActionListener(this);
full.setActionCommand("full");
Confirm.add(full);
JButton part = new JButton("Part Time");
part.setBounds(50, 30, 100, 30);
part.addActionListener(this);
part.setActionCommand("part");
Confirm.add(part);
return Confirm;
}
public void actionPerformed(java.awt.event.ActionEvent e){
if(e.getActionCommand().equals("one")){
eType = 1;
typeL.setText("Type: " + eType);
}
if(e.getActionCommand().equals("two")){
eType = 2;
typeL.setText("Type: " + eType);
}
if(e.getActionCommand().equals("full")){
eType = 1;
typeL.setText("Type: " + eType);
EmployeeArray[index].setType(eType);
second.dispose();
}
if(e.getActionCommand().equals("part")){
eType = 2;
typeL.setText("Type: " + eType);
EmployeeArray[index].setType(eType);
second.dispose();
}
if(e.getActionCommand().equals("add")){
try{
Test = name.getText();
for(int i = 0; i < Test.length(); ++i){
if(Character.isDigit(Test.charAt(i))){
ErrorName = true;
JOptionPane.showMessageDialog(f, "Error First Name: " + ErrorName);
}
}
}catch(Exception ex){
ex.printStackTrace();
}
try{
Test = last.getText();
for(int i = 0; i < Test.length(); ++i){
if(Character.isDigit(Test.charAt(i))){
ErrorLast = true;
JOptionPane.showMessageDialog(f, "Error Last Name: " + ErrorLast);
}
}
}catch(Exception ex1){
ex1.printStackTrace();
}
try{
eHour = Integer.parseInt(hHour.getText());
}catch(NumberFormatException ex2){
ex2.printStackTrace();
ErrorHour = true;
JOptionPane.showMessageDialog(f, "Error Hour: " + ErrorHour);
}
try{
eRate = Double.parseDouble(hRate.getText());
}catch(NumberFormatException ex3){
ex3.printStackTrace();
ErrorRate = true;
JOptionPane.showMessageDialog(f, "Error Rate: " + ErrorRate);
}
try{
eID = Integer.parseInt(ID.getText());
}catch(NumberFormatException ex4){
ex4.printStackTrace();
ErrorID = true;
JOptionPane.showMessageDialog(f, "Error ID: " + ErrorID);
}
if(count > 0){
for(int i = 0; i < count; ++i){
if(name.getText().equals(EmployeeArray[i].getFirstName()) && last.getText().equals(EmployeeArray[i].getLastName())){
ErrorNameLast = true;
JOptionPane.showMessageDialog(f, "Error Name Repeat: " + ErrorNameLast);
}
if(Integer.parseInt(ID.getText()) == EmployeeArray[i].getID()){
ErrorIDRepeat = true;
JOptionPane.showMessageDialog(f, "Error ID Repeat: " + ErrorIDRepeat);
}
}
}
eHour = Integer.parseInt(hHour.getText());
eRate = Double.parseDouble(hRate.getText());
eID = Integer.parseInt(ID.getText());
if(!hRate.getText().equals(null) && !hRate.getText().equals("") && !hHour.getText().equals(null) && !hHour.getText().equals("") && !ID.getText().equals(null) && !ID.getText().equals("") && !ID.getText().equals("Exists") && !last.getText().equals("Exists") && !name.getText().equals("Exists") && !ID.getText().equals("Full") && !ID.getText().equals("Invalid") && !hRate.getText().equals("Invalid") && !hHour.getText().equals("Invalid") && !name.getText().equals("") && !last.getText().equals("") && !name.getText().equals("Invalid") && !last.getText().equals("Invalid") && !name.getText().equals(null) && !last.getText().equals(null) && eType != 0 && eRate >= 6.75 && eRate <= 30.5 && eHour >= 1 && eHour <= 60 && ErrorName == false && ErrorLast == false && ErrorHour == false && ErrorRate == false && ErrorID == false && eID <= 100 && eID > 0 && count < 100 && ErrorNameLast == false && ErrorIDRepeat == false){
EmployeeArray[count] = new Employee(name.getText(), last.getText(), eType, eRate, eHour, eID);
pay.setText(Pay());
logM.addElement(EmployeeArray[count].getFirstName() + " " + EmployeeArray[count].getLastName() + " " + EmployeeArray[count].getID());
eType = 0;
typeL.setText("Type: " + eType);
eRate = 0;
eHour = 0;
++count;
delete.setEnabled(true);
edit.setEnabled(true);
// Write Into File
}else{
// JOptionPane not opening at all for ErrorHour to ErrorID or the Blank Fields
Error = "Error: Type(How To Fix)" + Error();
JOptionPane.showMessageDialog(f, Error);
Error = "";
ErrorName = false;
ErrorLast = false;
ErrorHour = false;
ErrorRate = false;
ErrorID = false;
ErrorNameLast = false;
ErrorIDRepeat = false;
}
}
if(e.getActionCommand().equals("delete")){
// Store Deleted count into an array to be used again
int index = 0;
index = log.getSelectedIndex();
logM.remove(index);
int size = logM.getSize();
if (size == 0) {
delete.setEnabled(false);
edit.setEnabled(false);
} else {
if (index == logM.getSize()) {
index--;
}
log.setSelectedIndex(index);
log.ensureIndexIsVisible(index);
}
--count;
}
if(e.getActionCommand().equals("edit")){
int index = 0;
for(int i = 0; i <= count; ++i){
Test = EmployeeArray[i].getFirstName() + " " + EmployeeArray[i].getLastName() + " " + EmployeeArray[i].getID();
if(Test.equals(log.getSelectedValue())){
index = i;
name.setText(EmployeeArray[i].getFirstName());
last.setText(EmployeeArray[i].getLastName());
hRate.setText(Double.toString(EmployeeArray[i].getRate()));
hHour.setText(Integer.toString(EmployeeArray[i].getHours()));
eType = EmployeeArray[i].getType();
typeL.setText("Type: " + eType);
ID.setText(Integer.toString(EmployeeArray[i].getID()));
pay.setText(Pay());
}
}
}
if(e.getActionCommand().equals("clear")){
name.setText("");
last.setText("");
eType = 0;
typeL.setText("Type: " + eType);
hHour.setText("");
hRate.setText("");
ID.setText("");
pay.setText("");
}
if(e.getActionCommand().equals("save")){
EmployeeArray[index].setFirstname(name.getText());
EmployeeArray[index].setLastName(last.getText());
EmployeeArray[index].setHours(Integer.parseInt(hHour.getText()));
EmployeeArray[index].setRate(Double.parseDouble(hRate.getText()));
EmployeeArray[index].setID(Integer.parseInt(ID.getText()));
if(eType == 0){
second = new JFrame();
second.setTitle("Confirm Type");
second.add(Confirm());
second.pack();
second.setLocationRelativeTo(null);
second.setVisible(true);
}else{
EmployeeArray[index].setType(eType);
}
}
}
public String Error(){
String error = "";
if(name.getText().equals("") || name.getText().equals(null)){
JOptionPane.showMessageDialog(f, "Checked 1");
error = error + "Error: Invalid Name(Blank)";
name.setText("Invalid");
}
if(last.getText().equals("") || last.getText().equals(null)){
JOptionPane.showMessageDialog(f, "Checked 2");
error = error + "\nError: Invalid Last Name(Blank)";
last.setText("Invalid");
}
if(hHour.getText().equals("") || hHour.getText().equals(null)){
JOptionPane.showMessageDialog(f, "Checked 3");
error = error + "\nError: Invalid Work Hours(Blank)";
hHour.setText("Invalid");
}
if(hRate.getText().equals("") || hRate.getText().equals(null)){
JOptionPane.showMessageDialog(f, "Checked 4");
error = error + "\nError: Invalid Hourly Rate(Blank)";
hRate.setText("Invalid");
}
if(ID.getText().equals("") || ID.getText().equals(null)){
JOptionPane.showMessageDialog(f, "Checked 5");
error = error + "\nError: Invalid Employee ID(Blank)";
ID.setText("Invalid");
}
if(name.getText().equals("Invalid") || name.getText().equals("Exists")){
JOptionPane.showMessageDialog(f, "Checked 6");
error = error + "\nError: Previous Invalid/Existing Error or" +
"\nInvalid/Existing Input Not Accepted(Name)";
}
if(last.getText().equals("Invalid") || last.getText().equals("Exists")){
JOptionPane.showMessageDialog(f, "Checked 7");
error = error + "\nError: Previous Invalid/Existing Error or" +
"\nInvalid/Existing Input Not Accepted(Last)";
}
if(hHour.getText().equals("Invalid")){
JOptionPane.showMessageDialog(f, "Checked 8");
error = error + "\nError: Previous Invalid Error or" +
"\nInvalid Input Not Accepted(Hours Worked)";
}
if(hRate.getText().equals("Invalid")){
JOptionPane.showMessageDialog(f, "Checked 9");
error = error + "\nError: Previous Invalid Error or" +
"\nInvalid Input Not Accepted(Hourly Rate)";
}
if(ID.getText().equals("Full") || ID.getText().equals("Exists") || ID.getText().equals("Invalid")){
JOptionPane.showMessageDialog(f, "Checked 10");
error = error + "\nError: Previous Invalid/Existing/Full Error or" +
"\nInvalid/Existing/Full Input Not Accepted(ID)";
}
if(eType == 0){
JOptionPane.showMessageDialog(f, "Checked 11");
error = error + "\nError: Type Required(Button Full or Part)";
}
if(eHour < 1 || eHour > 60){
JOptionPane.showMessageDialog(f, "Checked 12");
error = error + "\nError: Invalid Working Hours(1 - 60)";
hHour.setText("Invalid");
}
if(eRate < 6.75 || eRate > 30.5){
JOptionPane.showMessageDialog(f, "Checked 13");
error = error + "\nError: Invalid Hourly Rate(6.75 - 30.50)";
hRate.setText("Invalid");
}
if(eID > 100 || eID < 1){
JOptionPane.showMessageDialog(f, "Checked 14");
error = error + "\nError: Invalid ID Value(1-100)";
ID.setText("Invalid");
}
if(ErrorName == true){//Works
JOptionPane.showMessageDialog(f, "Checked 15");
error = error + "\nError: First Name Input(A-Z)";
name.setText("Invalid");
}
if(ErrorLast == true){//Works
JOptionPane.showMessageDialog(f, "Checked 16");
error = error + "\nError: Last Name Input(A-Z)";
last.setText("Invalid");
}
if(ErrorHour == true){
JOptionPane.showMessageDialog(f, "Checked 17");
error = error + "\nError: Working Hour Input(Numberical)";
hHour.setText("Invalid");
}
if(ErrorRate == true){
JOptionPane.showMessageDialog(f, "Checked 18");
error = error + "\nError: Hourly Rate Input(Numberical)";
hRate.setText("Invalid");
}
if(ErrorID == true){
JOptionPane.showMessageDialog(f, "Checked 19");
error = error + "\nError: Employee ID Input(Numberical)";
ID.setText("Invalid");
}
if(ErrorNameLast == true){
JOptionPane.showMessageDialog(f, "Checked 20");
error = error + "\nError: First and Last Name Exists";
name.setText("Exists");
last.setText("Exists");
}
if(ErrorIDRepeat == true){
JOptionPane.showMessageDialog(f, "Checked 21");
error = error + "\nError: Employee ID In Use";
ID.setText("Exists");
}
if(count == 99){
JOptionPane.showMessageDialog(f, "Checked 22");
error = error + "\nError: Employee List Is Full(Delete)";
ID.setText("Full");
}
return error;
}
public static void main(String[] args){
new Project9_1();
}
}