import java.util.*;
public class StaffManager
{
private static int numStaff;
private static int[] payNum;
private static String[] surname;
private static String[] firstName;
private static String[] dOB;
private static String[] dept;
private static Scanner keyboard = new Scanner(System.in);
private static int i;
public static void main (String[] args)
{
numStaff = 0;
final int MAX_STAFF = 10;
payNum = new int[MAX_STAFF];
surname = new String[MAX_STAFF];
firstName = new String[MAX_STAFF];
dOB = new String[MAX_STAFF];
dept = new String[MAX_STAFF];
initialiseArrays();
String entry;
char option;
do
{
displayMenu();
System.out.print("\n\t\tEnter choice (1/2/3/4/5/6/7): ");
entry = keyboard.next();
entry = entry.toLowerCase();
option = entry.charAt(0);
switch (option)
{
case '1': displayAllStaff();
break;
case '2': addStaff(MAX_STAFF);
break;
case '3': removeStaffMember();
break;
case '4': displayStaffMember();
break;
case '5': displayDeptStaff();
break;
case '6': changeStaffDept();
break;
case '7': //Do nothing. ('Quit' option)
break;
default: System.out.println("\t\tInvalid entry! Try again...");
}
if(option != 7)
{
System.out.print("\n\t\tPress the enter key to continue");
keyboard.nextLine();
keyboard.nextLine();
}
}while (option != '7');
}
public static void initialiseArrays()
{
int[] tempPayNum = {123456, 234567, 345678, 456789, 567890, 678901, 789012};
String[] tempSurname = {"Brown", "Jenkins", "Turner", "Cavendish", "Williams", "Ford", "Carter"};
String[] tempFirstName = {"James", "Susan", "Jane", "Paul", "James", "Karen", "Daniel"};
String[] tempDOB = {"24/3/85", "11/12/80", "5/7/72", "22/9/76", "19/1/81", "15/8/79", "4/2/62"};
String[] tempDept = {"Marketing", "Sales", "Sales", "Accounts", "Marketing", "Sales", "Accounts"};
for (int i=0; i<6; i++)
{
payNum[i] = tempPayNum[i];
surname[i] = tempSurname[i];
firstName[i] = tempSurname[i];
dOB[i] = tempDOB[i];
dept[i] = tempDept[i];
numStaff++;
}
}
public static void displayMenu()
{
System.out.println("\n\n\n");
System.out.println("\t\t1: Display all Staff.");
System.out.println("\t\t2: Add Staff.");
System.out.println("\t\t3: Remove Staff Member.");
System.out.println("\t\t4: Display Staff Member.");
System.out.println("\t\t5: Display Department Staff.");
System.out.println("\t\t6: Change Staff Department.");
System.out.println("\t\t7: Quit.");
}
public static void displayAllStaff()
{
System.out.println("\n\n\tPay Num\t\tSurname\t\tFirst Name\t\tDoB\t\tDept");
System.out.println("\t-------\t\t-------\t\t----------\t\t---\t\t----\n");
for (int i=0; i<numStaff-1; i++);
{
displayStaffDetails(i);
}
}
public static void addStaff(int MAX_STAFF)
{
if (numStaff == MAX_STAFF)
{
System.out.println("\n\tError: Maximum staff reached!");
return;
}
char reply = 0;
do
{
boolean found;
int payNumber, position;
do
{
found = false;
System.out.print("\n\tEnter pay number: ");
payNumber = keyboard.nextInt();
position = findPayNum(payNumber);
if (position == -1)
{
System.out.println("\n\tError: Invalid Pay Number!");
found = true;
}
}while (! found);
payNum[numStaff] = payNumber;
System.out.print("\n\tEnter surname: ");
surname[numStaff] = keyboard.next();
System.out.print("\n\tEnter first name: ");
firstName[numStaff] = keyboard.next();
System.out.print("\n\tEnter date of birth: ");
dOB[numStaff] = keyboard.next();
System.out.print("\n\tEnter department: ");
dept[numStaff] = keyboard.next();
numStaff++;
System.out.println("\n\tSuccess: Staff details saved!");
if (numStaff < MAX_STAFF)
{
System.out.println("\n\tDo you want to add another staff member?");
System.out.print("\t(y/n): ");
reply = keyboard.next().charAt(0);
}
}while ((reply != 'n') || (numStaff != MAX_STAFF));
}
public static void removeStaffMember()
{
int payNumber, position;
if (numStaff == 0)
{
System.out.println("\n\tError: No staff members!");
return;
}
System.out.print("\n\tEnter pay number: ");
payNumber = keyboard.nextInt();
position = findPayNum(payNumber);
if (position < 0)
System.out.println("\n\tError: Invalid pay number!");
else
moveStaffData(position);
System.out.println("Success: Staff member removed!");
}
public static void displayStaffMember()
{
int payNumber, position;
System.out.print("\n\tEnter pay number: ");
payNumber = keyboard.nextInt();
position = findPayNum(payNumber);
if (position < 0)
System.out.println("\n\tError: Invalid pay number!");
else
System.out.println("\n\n\tPay Num\t\tSurname\t\tFirst Name\t\tDoB\t\tDept");
System.out.println("\t-------\t\t-------\t\t----------\t\t---\t\t----\n");
displayStaffDetails(position);
}
public static void displayDeptStaff()
{
String deptName;
System.out.print("\n\tEnter department name: ");
deptName = keyboard.next();
System.out.println("\n\n\tPay Num\t\tSurname\t\tFirst Name\t\tDoB\t\tDept");
System.out.println("\t-------\t\t-------\t\t----------\t\t---\t\t----\n");
for (int i=0; i<numStaff-1; i++);
{
if (dept[i] == deptName)
{
displayStaffDetails(i);
}
}
}
public static void changeStaffDept()
{
int payNumber, position;
String deptName;
System.out.print("\n\tEnter pay number: ");
payNumber = keyboard.nextInt();
position = findPayNum(payNumber);
if (position < 0)
{
System.out.println("\n\tError: Invalid pay number!");
return;
}
System.out.print("\n\tEnter department name: ");
deptName = keyboard.next();
dept[position] = deptName;
System.out.println("\n\tSuccess: Staff member department changed!");
}
public static void displayStaffDetails(int position)
{
System.out.println("\n\n\t" + payNum[position] + "\t\t" + surname[position] + "\t\t" + firstName[position] + "\t\t" + dOB[position] + "\t\t" + dept[position]);
}
public static int findPayNum(int payNumber)
{
if (numStaff == 0)
{
System.out.println("\n\tError: No staff members!");
return -1;
}
boolean found = false;
int count = 0;
do
{
if (payNum[count] == payNumber)
found = true;
else
count ++;
}while (! found | (count != numStaff));
if (! found)
{
count = -1;
}
return count;
}
public static void moveStaffData(int startPos)
{
for (int i=startPos; i<numStaff-2; i++);
{
payNum[i] = payNum[i+1];
surname[i] = surname[i+1];
firstName[i] = firstName[i+1];
dOB[i] = dOB[i+1];
dept[i] = dept[i+1];
}
numStaff--;
}
}