Search code examples
javagetter-setter

Java Setters not working as intended on Array object


I´m quite new to Java and want to write code that creates objects, shows (prints) and edits them.

  1. I wrote a class:

    public class Person {

    protected String firstName;
    protected String lastName;
    protected String fullName;
    protected Date dob;
    protected int id;
    
    public Person(String firstName, String lastName, Date dob, int id) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.dob = dob;
        this.fullName = this.firstName + " " + this.lastName;
        this.id = id;
    
    }
    
    public String getFirstName() {return this.firstName;}
    public String getLastName() {return this.lastName;}
    public String getFullName() {
        return this.fullName;
    }
    public Date getDob() {
        return this.dob;
    }
    public int getId() {
        return this.id;
    }
    
    public void printAll() {
        System.out.println(this.fullName);
        System.out.println(this.dob);
        System.out.println(this.id);
    }
    
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    
    public void setDob(Date dob) {
        this.dob = dob;
    }
    
    public boolean containsFullName(String fullName) {
        if (this.fullName.equalsIgnoreCase(fullName)) {
            return true;
        } else {
            return false;
        }
    }
    public boolean containsId(int id) {
        if (this.id == id) {
            return true;
        } else {
            return false;
        }
    }
    

    }

  2. I created an Array of Person in Main Class. (100 objects).

public class Main {

final static int MAX_NUMBER_PERSONS = 100;
static Person[] person = new Person[MAX_NUMBER_PERSONS];
static int mainCount = 0;
static Scanner scan = new Scanner(System.in);
  1. upon corresponding command the user may create an object within this array (It´s german, but basically its reading first name, last name and date of birth). mainCount is used to keep track on which position within the array the object is created:

    public static void createPerson() throws ParseException {

    Scanner scan = new Scanner(System.in);
    
    // Namen erfassen:
    System.out.println("Bitte den Vornamen der Person eingeben:");
    String firstName = scan.nextLine();
    System.out.println("Bitte den Nachnamen der Person eingeben:");
    String lastName = scan.nextLine();
    String fullName = firstName + " " + lastName;
    
    // DOB erfassen:
    System.out.println("Bitte Geburtsdatum von " + fullName + " eingeben (TT/MM/JJJJ):");
    String dob = scan.next();
    Date dobDate = new SimpleDateFormat("dd/MM/yyyy").parse(dob);
    
    int id = mainCount;
    
    person[mainCount] = new Person(firstName, lastName, dobDate, id);
    ++mainCount;
    System.out.println("Du hast Person Nummer " + mainCount + " erstellt");
    

    }

  2. NOW THE ACTUAL ISSUE: calling the following method the user should be able to edit the desired object within the array by using user input and setters:

    public static void editPerson() throws ParseException { Scanner scan = new Scanner(System.in); System.out.println("Bitte die ID der zu bearbeitenden Person eingeben (zu finden über )..."); int id = scan.nextInt(); scan.nextLine(); searchByIdReturn(id).printAll();

    System.out.println("Diese Person bearbeiten? Tippe <ja>, sonst zurück zum Menü.");
    if(scan.next().equalsIgnoreCase("ja")) {
        scan.nextLine();
        System.out.println("Vorname eingeben...");
        String firstName = scan.nextLine();
        searchByIdReturn(id).setFirstName(firstName);
        System.out.println("Nachname eingeben...");
        String lastName = scan.nextLine();
        searchByIdReturn(id).setLastName(lastName);
        System.out.println("Bitte Geburtsdatum eingeben (TT/MM/JJJJ):");
        String dob = scan.next();
        Date dobDate = new SimpleDateFormat("dd/MM/yyyy").parse(dob);
        searchByIdReturn(id).setDob(dobDate);
    }
    

    }

After the Method I look up the object. First and last name remain unchanged. dob however is was changed as required.

What am I missing?

Thank you so much in andvance!


Solution

  • You never change the fullName which is what I assume you are looking at.

    this.fullName = this.firstName + " " + this.lastName;
    

    You don't need to store it, it would be simpler to calculate it each time.

    public String getFullName() {
        return this.firstName + " " + this.lastName;
    }
    

    BTW tests are boolean already so you an write.

    public boolean containsId(int id) {
        return this.id == id;
    }