Hi Guys, I am a noob at this java programming thing. I have 3 classes. When My program runs, it works good the first time when i Choose option 1(only option that is working for now). But after a loop, the data is over written, what can i do to keep my data even the counter. I understand why because every loop it does, it reinitialize everything which erases everything. I cannot think of a solution (kinda new in this).
package comp2500_1; import java.util.Scanner; public class EmployeeApplication { Scanner input=new Scanner(System.in); Company x=new Company(); public static void main(String[] args) { test p=new test(); EmployeeApplication c=new EmployeeApplication(); int choice =10,num=100; while (num!=0){ num=c.choices(choice); c.sets(num); } } public int choices(int choice){ while (choice !=0){ System.out.println("Please Enter Your Choice\n"); System.out.println("Add a new employee to the company 1"); System.out.println("Query for a particular employee 2"); System.out.println("Raise an employee's Salary 3"); System.out.println("List all the employees at the company 4"); System.out.println("To Exit Press 0"); choice=input.nextInt(); return choice; } return 0; } public void sets(int s){ if (s==1) { System.out.println(" Enter the persons First Name,Last Name and Salary"); String fname=input.next(); String lname=input.next(); double num=input.nextDouble(); x.addEmployee(fname, lname, num); } else if (s==2) { System.out.println("Please enter the employee ID"); int num2=input.nextInt(); // d.getEmployee(num2); } else if (s==3) { System.out.println("Please enter the employees ID"); int num3=input.nextInt(); System.out.println("Please enter the percentage increase of salary"); double sal=input.nextDouble(); x.raiseSalary(num3, sal); } else if (s==4) { // d.getEmployees(); } } }
Thats 1 class and the other is
public class Employee { private String firstName,lastName; private double salary; private int ID=990; private String[] EmpF=new String[100],EmpL=new String[100]; private double[] EmpSal=new double[100]; private int counter=0; EmployeeApplication j=new EmployeeApplication(); public Employee(String firstName, String lastName, double salary){ setFName(firstName); setLName(lastName); setSal(salary); counter++; } public Employee() { } public String toString(){ return String.format("%s %s %f,",firstName, lastName, salary); } public void raiseSalary(double percentIncrease){ salary=salary+(salary*(percentIncrease/100)); } public void setSal(double d) { EmpSal[counter]=d; salary=d; } public void setFName(String fN) { EmpF[counter]=fN; firstName=fN; } public void setLName(String lN) { EmpL[counter]=lN; lastName=lN; } public boolean getEmployee(int employeeID){ employeeID=((employeeID-ID)/10)-1; if(EmpF[employeeID]==null) return false; else return true; } }