Method returns wrong value in main class

Hockz :

Whenever I run InschrijvingApplicatie, I get a wrong value out of line System.out.printf("Hoeveel broodjes wil je bestellen? (max %d) ", maxBroodjes); because the int should be "10" when I enter 'p' in this line

System.out.printf("Tot welke categorie behoort u?\nTyp w voor een werknemer, p voor een werknemer met partner, g voor een gast: ");

I'm supposing there is something wrong at the line int maxBroodjes = (inschrijving.geefAantalPersonen() * 5); but can't seem to figure out what.

How the output should look like

The excercise is: for a company that is inviting employees ('w' in the code), employee with a partner ('p') and guests ('g') and letting them fill in their name, what sort of visitor (employee + partner, guest or employee) they are, then asking how many sandwiches the person wants (guest and employee can max require 5 sandwiches, employee+partner can request 10) and the max value is shown in the integer (max %d). All of this is in a loop until the user writes "no" (but the first char is used => resulting in 'n') when asked "Zijn er nog inschrijvingen", and if the answer is yes, it repeats.

Inschrijving.java

package domein;
public class Inschrijving {

private String naam;
private char categorie;
private int aantalBroodjes;

public Inschrijving(String naam, char categorie) {
    setNaam(naam);
    setCategorie(categorie);
}

public String getNaam() {
    return naam;
}

private void setNaam(String naam) {
    this.naam = naam;
}

public char getCategorie() {
    return categorie;
}

private void setCategorie(char categorie) {
    if (categorie == 'w' || categorie == 'p' || categorie == 'g') {
        this.categorie = categorie;
    } else {
        this.categorie = 'g';
    }

}

public int getAantalBroodjes() {
    return aantalBroodjes;
}

public void setAantalBroodjes(int aantalBroodjes) {

    if (aantalBroodjes <= (geefAantalPersonen() * 5)) {
        this.aantalBroodjes += aantalBroodjes;
    } else {
        this.aantalBroodjes += (geefAantalPersonen() * 2);
    }
}

public int geefAantalPersonen() {
    switch (categorie) {
        case 'w':
        case 'g':
            return 1;
        default:
            return 2;

    }
  }
}

InschrijvingApplicatie

package ui;

import domein.Inschrijving;
import java.util.Scanner;

public class InschrijvingApplicatie {
 public static void main(String[] args) {

    Scanner invoer = new Scanner(System.in);
    String antwoord;
    char eersteLetter;

    System.out.println("Zijn er nog inschrijvingen? ");
    antwoord = invoer.nextLine();
    eersteLetter = antwoord.toLowerCase().charAt(0);

    String naam = null;
    String categorie;
    char categorieEersteLetter = 0;

    int werknemer = 0;
    int werknemerMetPartner = 0;
    int gast = 0;

    int aantalBroodjes;
    int tijdelijk;

    Inschrijving inschrijving = new Inschrijving(naam, categorieEersteLetter);

    if (eersteLetter != 'n') {
        do {
            System.out.println("Wie mag ik inschrijven? ");
            naam = invoer.next();

            do {
                System.out.printf("Tot welke categorie behoort u?\nTyp w voor een werknemer, p voor een werknemer met partner, g voor een gast: ");

                categorie = invoer.next();
                categorieEersteLetter = categorie.toLowerCase().charAt(0);


                switch (categorieEersteLetter) {
                    case 'w':
                        werknemer++;
                        break;
                    case 'p':
                        werknemerMetPartner++;
                        break;
                    case 'g':
                        gast++;
                        break;
                }

            } while (categorieEersteLetter != 'w' && categorieEersteLetter != 'p' && categorieEersteLetter != 'g');
            int maxBroodjes = (inschrijving.geefAantalPersonen() * 5);
            do {
                System.out.printf("Hoeveel broodjes wil je bestellen? (max %d) ", maxBroodjes);
                tijdelijk = invoer.nextInt();
            } while (tijdelijk > maxBroodjes);
            aantalBroodjes = tijdelijk;
            inschrijving.setAantalBroodjes(aantalBroodjes);

            System.out.println("Zijn er nog inschrijvingen? ");
            antwoord = invoer.next();
            eersteLetter = antwoord.toLowerCase().charAt(0);
        } while (eersteLetter != 'n');

    }
    System.out.printf("Er komen %d werknemer(s) zonder partner, %d werknemer(s) met partner en %d gast(en) naar de receptie. Er dienen %d broodjes besteld te worden.", werknemer, werknemerMetPartner, gast, inschrijving.getAantalBroodjes());

}
}
Hai Hoang :

There are some problems with your approach, it may work but you shouldn't do that way.

First, you store the total sandwiches requested for all invited peopled in only one Inschrijving object, it make no sense! (Do I need to know the total sandwiches requested or only ones requested by me?). So, change setAantalBroodjes in your Inschrijving class to :

public void setAantalBroodjes(int aantalBroodjes) {
  this.aantalBroodjes = aantalBroodjes;
}

Second, The requirement is take a list of people and do something with them, so you should consider use a data structure support you store a list of people like an Array or an ArrayList, then you can do whatever you want with your data once user stop input (eersteLetter == 'n' in your case).

List<Inschrijving> inschrijven = new ArrayList<>();
try (Scanner invoer = new Scanner(System.in)) { // http://tutorials.jenkov.com/java-exception-handling/try-with-resources.html
  System.out.println("Zijn er nog inschrijvingen? ");
  String antwoord = invoer.nextLine();
  char eersteLetter = antwoord.toLowerCase().charAt(0);
  while (eersteLetter != 'n') {

    Inschrijving inschrijving = null;
    System.out.println("Wie mag ik inschrijven? ");
    String naam = invoer.nextLine();
    char categorieEersteLetter = 0;
    do {
      System.out.printf(
          "Tot welke categorie behoort u?\nTyp w voor een werknemer, p voor een werknemer met partner, g voor een gast: ");
      String categorie = invoer.nextLine();
      categorieEersteLetter = categorie.toLowerCase().charAt(0);
    } while (categorieEersteLetter != 'w' && categorieEersteLetter != 'p' && categorieEersteLetter != 'g');
    inschrijving = new Inschrijving(naam, categorieEersteLetter);

    int maxBroodjes = (inschrijving.geefAantalPersonen() * 5);
    int tijdelijk;
    do {
      System.out.printf("Hoeveel broodjes wil je bestellen? (max %d) ", maxBroodjes);
      tijdelijk = invoer.nextInt();
      invoer.nextLine(); // https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo 
    } while (tijdelijk > maxBroodjes);
    inschrijving.setAantalBroodjes(tijdelijk);
    inschrijven.add(inschrijving);

    System.out.println("Zijn er nog inschrijvingen? ");
    antwoord = invoer.nextLine();
    eersteLetter = antwoord.toLowerCase().charAt(0);
  }
}

When the user finished their input:

// Do stuffs with your list of people here
int werknemer = 0;
int werknemerMetPartner = 0;
int gast = 0;
int aantalBroodjes = 0;
for (Inschrijving inschrijving : inschrijven) {
  char categorie = inschrijving.getCategorie();
  switch (categorie) {
    case 'w':
      werknemer++;
      break;
    case 'p':
      werknemerMetPartner++;
      break;
    case 'g':
      gast++;
      break;
  }
  aantalBroodjes += inschrijving.getAantalBroodjes();
}
System.out.printf(
        "Er komen %d werknemer(s) zonder partner, %d werknemer(s) met partner en %d gast(en) naar de receptie. Er dienen %d broodjes besteld te worden.",
        werknemer, werknemerMetPartner, gast, aantalBroodjes);

Because you are new to java, I use a foreach loop here to make example, after learning about OOP and familiar with java, I suggest you reaserch Java 8 Stream api and lambda expression to work with collection types.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=107582&siteId=1