Good day! I was writing a code that will compute the salary of each employee on our school depending on their position using a FileReader(File Handling). I only managed to do this code and don't know how to start again
import java.io.*;
import static java.lang.System.out;
public class admin
{
static BufferedReader kboard=new BufferedReader(new InputStreamReader(System.in));
static File file = new File("Employee.txt");
static String[][] Employee;
static int numOfEmployee;
static int numRow;
//****************************MAIN MENU*************************
public static int UserChoice()throws Exception
{
out.print("\n\n\n\n\n\n\nMenu:\n");
out.print("[1] Add New Employee\n");
out.print("[2] Edit Employee\n");
out.print("[3] Delete Employee\n");
out.print("[4] Search Employee\n");
out.print("[5] View All Employees\n");
out.print("[6] Exit\n");
out.print("Enter Your Choice:\n");
int choice=Integer.parseInt(kboard.readLine());
return choice;
}
//********************************END OF MAIN MENU**************************
//********************************ADD*****************************
public static String[] EnterValues()throws Exception
{
out.println("Add Employee");
int random = (int)( Math.random() * 20000);
String[] info = new String[7];
info[0] = Integer.toString(random);
out.print("Enter Employee's First Name:");
info[1] = kboard.readLine();
out.print("Enter Employee's Middle Name:");
info[2] = kboard.readLine();
out.print("Enter Employee's Last Name:");
info[3] = kboard.readLine();
out.print("Enter Employee's Age:");
info[4] = Integer.toString(Integer.parseInt(kboard.readLine()));
out.print("Enter Employee Address:");
info[5] = kboard.readLine();
out.print("Enter Employee Position:");
info[6] = kboard.readLine();
return info;
}
public static void AddEmployee(String[]info)throws Exception
{
FileWriter fw = new FileWriter(file,true);
for ( int x =0; x < info.length; x++)
{
if( x == info.length - 1)
fw.write(info[x] + "\r\n");
else
fw.write(info[x] + ",");
}
fw.close();
out.println("Added New Employee");
}
//****************************END OF ADD***********************************
//***************************SEARCHING*************************************
public static Boolean SearchEmployee()throws Exception
{
out.print("Enter Employee ID:");
String id = kboard.readLine();
boolean result = false;
int x;
for ( x=0; x< numOfEmployee;x++)
{
if(id.equals(Employee[x][0]))
{
out.println("Search Result:");
out.println("Employee Name: " + Employee[x][1] + " " + Employee[x][2] + " " + Employee[x][3]);
out.println("Employee Age: " + Employee[x][4]);
out.println("Address:" + Employee[x][5]);
out.println("Position:" + Employee[x][6]);
result = true;
break;
}
}
numRow = x;
return result;
}
//*************************END OF SEARCHING**************************
public static void EditEmployee(int row) throws Exception
{
out.println("Edit Employee:");
out.println("Enter New Employee's First Name:");
Employee[row][1]=kboard.readLine();
out.println("Enter New Employee's Middle Name:");
Employee[row][2]=kboard.readLine();
out.println("Enter New Employee's Last Name:");
Employee[row][3]=kboard.readLine();
out.println("Enter New Employee's Age:");
Employee[row][4]=kboard.readLine();
out.println("Enter New Employee's Address:");
Employee[row][5]=kboard.readLine();
out.println("Enter New Employee's Position:");
Employee[row][6]=kboard.readLine();
SaveChanges();
}
public static void DeleteEmployee(int row) throws Exception
{
out.println("Delete Employee");
for ( int col = 0; col < 7; col++)
{
Employee[row][col]=null;
}
SaveChanges();
}
public static void SaveChanges()throws Exception
{
FileOutputStream fos = new FileOutputStream(file);
PrintStream ps = new PrintStream(fos);
for ( int x = 0; x < numOfEmployee; x++)
{
for ( int y = 0; y < 7; y++)
{
if(Employee[x][y] != null)
ps.print(Employee[x][y] + ",");
if( y == 6 && Employee[x][y] != null)
ps.println();
}
}
ps.close();
}
public static void LoadEmployee() throws Exception
{
FileInputStream fis = new FileInputStream(file);
BufferedReader brfile = new BufferedReader(new InputStreamReader (fis));
int row=0; String all ="";
while (brfile.ready())
{
all += brfile.readLine();
row++;
}
numOfEmployee = row;
String[] temp = all.split(",");
Employee = new String[row][7];
int ctr=0;
for ( int x=0; x<row; x++)
{
for (int y=0; y<6; y++)
{
Employee[x][y] = temp[y+ctr];
}
}
ctr+=6;
}
public static void DisplayAll(int row)throws Exception
{
out.println("List of Employee("+numOfEmployee+")records");
for (int x=0; x < row ;x++)
{
for ( int y =0; y< 6; y++)
{
out.print(Employee[x][y] + "\t");
}
out.println();
}
}
public static void main(String[] args) throws Exception
{
for ( char reply ='y'; reply == 'y' || reply == 'Y';)
{
LoadEmployee(); // int Array
int choice = UserChoice();// display Menu
if ( choice == 1 )
{
AddEmployee(EnterValues());
}
else if ( choice == 2)
{
if(SearchEmployee())
{
out.print("Do You Really Want to Modify This Record? (Y/N):");
char rep=kboard.readLine().charAt(0);
if(rep == 'y' || rep == 'Y')
{
EditEmployee(numRow);
}
else
{
out.println("No Record found!");
}
}
}
else if ( choice == 3)
{
if(SearchEmployee())
{
out.print("Do you really want to delete this record?(Y/N)");
char rep = kboard.readLine().charAt(0);
if ( rep == 'y' || rep == 'Y')
{
DeleteEmployee(numRow);
}
else
{
out.println("No Record Found!");
}
}
}
else if ( choice == 4)
{
if (SearchEmployee())
{
out.println("No Record Found!");
}
}
else if (choice == 5)
{
DisplayAll(numOfEmployee);
}
else if ( choice == 6)
{
break;
}
{
out.println("Invalid Input!");
}
out.print("Go back to menu? (Y/N):");
reply = kboard.readLine().charAt(0);
}
}
}
can someone give me an idea of what should I do?