A
A
Amir Kenesbay2021-08-29 20:45:21
Java
Amir Kenesbay, 2021-08-29 20:45:21

How to make correct logic in this code?

I have a training problem, I ran into such a problem, everything seems to be written normally, but there was a problem in the changeEmployee method, all the code is below:

public class Employee {
    private String name;
    private String lastname;
    private String age;
    private String gender;
    private String education;
    private String post;
    private String department;

    public Employee(String name, String lastname, String age, String gender, String education, String post, String department) {
        this.name = name;
        this.lastname = lastname;
        this.age = age;
        this.gender = gender;
        this.education = education;
        this.post = post;
        this.department = department;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getLastname() {
        return lastname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getEducation() {
        return education;
    }

    public void setEducation(String education) {
        this.education = education;
    }

    public String getPost() {
        return post;
    }

    public void setPost(String post) {
        this.post = post;
    }

    public String getDepartment() {
        return department;
    }

    public void setDepartment(String department) {
        this.department = department;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "name='" + name + '\'' +
                ", lastname='" + lastname + '\'' +
                ", age='" + age + '\'' +
                ", gender='" + gender + '\'' +
                ", education='" + education + '\'' +
                ", post='" + post + '\'' +
                ", department='" + department + '\'' +
                '}';
    }
}


import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        List<Employee> list = new ArrayList<>();
        Scanner scanner = new Scanner(System.in);
        while (true) {
            addEmployee(list);
            System.out.println("Введите end для выхода");
            String input = scanner.nextLine();
            if (input.equals("end")) {
                showEmployee(list);
                break;
            }
            changeEmployee(list);
        }
    }

    public static void addEmployee(List<Employee> list) {
        System.out.println("Введите инфоормацию о сотруднике (имя, фамилия, возраст, пол, образование, должность, отдел):");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        String lastname = scanner.nextLine();
        String age = scanner.nextLine();
        String gender = scanner.nextLine();
        String education = scanner.nextLine();
        String post = scanner.nextLine();
        String department = scanner.nextLine();
        Employee employee = new Employee(name, lastname, age, gender, education, post, department);
        list.add(employee);
    }

    public static void showEmployee(List<Employee> list) {
        for (Employee l : list) {
            System.out.println(l.toString());
        }
    }

    public static void changeEmployee(List<Employee> list) {
        Employee employee;
        System.out.println("Введите фамилию и имя работника, для которого хотите изменить данные");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        String lastname = scanner.nextLine();
        int empIndex = searchBySurname(name, lastname, list);
        if (empIndex != -1) {
            System.out.println("Меняем " + lastname + " " + name + "?");
        } else if (lastname.equals(employee.getLastname()) && name.equals(employee.getName())) {
            System.out.println("Введите новую фамилию и имя: ");
            employee.setLastname(scanner.next());
            employee.setName(scanner.next());
            for (Employee value : list) {
                System.out.println(value);
            }
        } else {
            System.out.println("Данный работник не найден");
        }
    }

    public static int searchBySurname(String name, String lastname, List<Employee> list) {
        if (list.equals(name) || list.equals(lastname))
            return 1;
        else
            return -1;
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Arty_Fact, 2021-08-30
@Arty_Fact

I'll work as a telepath for a bit.
In changeEmployee() you are adding Employee employee which is not defined. Because of this, your project does not compile. You need to pass it there somehow, but your searchBySurname() only returns the code. And about no
employee.setLastname(scanner.next());
employee.setName(scanner.next());
out of the question - you just don't have an employee.
In searchBySurname() you are trying to check if the collection matches the string:
if (list.equals(name) || list.equals(lastname))
This will not work, which is logical. Moreover, your method is called searchBySurname, but you also pass the name there, and even try to check against it.
So you have a problem with the idea itself. You have to decide what you want. Perhaps searchBySurname will return the index of the first hit of employee and you will get it from your list, suggesting you change it. Perhaps the search will generate a map of all namesakes and their indexes to give the user an option to select in changeEmployee().
Depending on your decision, you will need to completely rewrite searchBySurname so that it loops through your list, and changeEmployee so that it can change the employee.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question