Reputation: 269
First I'll tell you what my problem is and below there's the code:
When I run my Class1
I can register my first object, which is given an unique ID. After that I register my second object, which also has a unique ID. Then I try retrieving my first objects information by asking for the ID of the first object. The problem now is, that the following will always be printed out, when I ask for the first object, which was added to the ArrayList
: "The ID is wrong. You can't retrieve the objects information." However, when I try retrieving my second object (the object added last to the ArrayList
), then it will print out the information just as I want.
My question is, why I only have access to the last object added to the ArrayList
and what can I do to print the first objects information with its unique ID?
Here's Class1
:
import java.util.Scanner;
import java.util.*;
public class Class1 {
public static void main(String[] args){
Class1 class1 = new Class1();
class1.presentoptions();
class1.createObject();
}
Scanner sc = new Scanner(System.in);
String name;
String ID;
double salary;
Class2 class2;
final String END_LINE = System.lineSeparator();
public void presentoptions(){
class2 = new Class2();
while (true){
System.out.println("=== Welcome === ");
System.out.println("Choose an option below: ");
System.out.println(" ");
System.out.println("1. Register an object. ");
System.out.println("2. Retrieve an objects information. ");
System.out.println("3. Quit this program. ");
int option = sc.nextInt();
switch (option) {
case 1:
System.out.println("What type of object? " + END_LINE
+ " - Worker. " + END_LINE);
// other objects
String type = sc.nextLine();
createObject(); // creating the specified employee
break;
case 2:
class2.retrieveObject();
break;
case 3:
System.out.println("You've quitted the program.");
System.exit(0);
default:
System.out.println("Error. Please try again.");
break;
}
}
}
public void createObject(){
class2 = new Class2();
System.out.println("Write the name of the object (worker): ");
String typeofobject = sc.nextLine();
UUID uniqueID = UUID.randomUUID();
String x = "" + uniqueID;
System.out.println("The new ID of the " + typeofobject + " is: " + uniqueID + ".");
System.out.println();
System.out.println("What's the name of the new " + typeofobject + "?");
name = sc.nextLine();
System.out.println("What's the salary of the new " + typeofobject + "?");
salary = sc.nextDouble();
Employee employee = new Employee(x, name, salary);
switch (typeofobject) {
case "Worker":
class2.registerObject(employee);
break;
default:
System.out.println("You missspelled the type of object. Run the program again.");
break;
}
}
}
Here's Class2
:
import java.util.Scanner;
import java.util.ArrayList;
class Class2 extends Class1{
Scanner input = new Scanner(System.in);
ArrayList<Employee> employees = new ArrayList<Employee>();
final String END_OF_LINE = System.lineSeparator();
public void registerObject(Employee employee){
employees.add(employee);
}
public void retrieveObject() {
System.out.println("Which objects information do you want to retrieve? Type in his/her ID.");
String inputNewID = input.nextLine();
for(Employee employee: employees){
if(inputNewID.equals(employee.getID())){
System.out.println("ID: " + employee.ID + END_OF_LINE + "Name: " + employee.name + END_OF_LINE + "Salary: " + employee.grossSalary);
} else {
System.out.println("The ID is wrong. You can't retrieve the objects information.");
}
}
}
}
At last, the Employee
class:
import java.util.*;
public class Employee {
protected String ID;
protected String name;
protected double grossSalary;
final String END_OF_LINE = System.lineSeparator();
public String getID() {
return ID;
}
public Employee (String ID, String name, double grossSalary){
this.ID = ID;
this.name = name;
this.grossSalary = grossSalary;
}
}
Thanks.
Upvotes: 0
Views: 835
Reputation: 640
Notice the for Loop:
for(Employee employee: employees){
if(inputNewID.equals(employee.getID())){
System.out.println("ID: " + employee.ID + END_OF_LINE + "Name: " + employee.name + END_OF_LINE + "Salary: " + employee.grossSalary);
} else {
System.out.println("The ID is wrong. You can't retrieve the objects information.");
}
}
it will print FOR EACH Object in the employees list wherever it equals to the inputNewID or not, i think the intention was to print in ONCE.
you can get the wanted behavior using a boolean flag:
boolean isEmployeeFound = false;
for(Employee employee: employees){
if(inputNewID.equals(employee.getID())){
isEmployeeFound = true;
System.out.println("ID: " + employee.ID + END_OF_LINE + "Name:"+employee.name + END_OF_LINE + "Salary: " + employee.grossSalary);
}
}
if(!isEmployeeFound){System.out.println("The ID is wrong. You can't retrieve the objects information.");}
Also as others mentioned get rid of the class2 = new Class2() line in createObject function.
Upvotes: 0
Reputation: 723
The problem is with the if/else in the for loop of Class2, or more specifically with the else. On the 1st element of the loop the else will be triggered giving the message. One way to handle this would be to add a boolean to check if the ID is found and change the else block to an if check after the loop like this:
boolean idFound = false;
for(Employee employee: employees){
if(inputNewID.equals(employee.getID())){
idFound = true;
System.out.println("ID: " + employee.ID + END_OF_LINE + "Name: " + employee.name + END_OF_LINE + "Salary: " + employee.grossSalary);
}
}
if(! idFound) {
System.out.println("The ID: " + inputNewID + " is not found. You can't retrieve the objects information.");
}
Upvotes: 0
Reputation: 1410
You have the line
class2 = new Class2();
in your createObject()
method. But class2
is where you have the ArrayList of employees. So you're just blowing away the old ArrayList each time you call createObject()
.
I think you can just get rid of that line.
Upvotes: 1