3

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());

}
}
Hockz
  • 33
  • 5
  • so what do you get instead of 10? show your full output if possible – mangusta Nov 10 '18 at 15:23
  • @JBNizet I thought of that aswell but it gives me an error saying "unreachable statement" and doing some research gave me an answer because once it reaches return it doesn't get to break – Hockz Nov 10 '18 at 15:35
  • @mangusta oh yeah sorry, added it now – Hockz Nov 10 '18 at 15:36
  • @user2948118 sorry, disregard my previous comment, which was wrong. I didn't notice you had a return there. – JB Nizet Nov 10 '18 at 15:42

1 Answers1

1

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.

Hai Hoang
  • 974
  • 2
  • 13
  • 30
  • Could you suggest me a solution? Because for me it looks fine.. The reason I gave the char categorieEersteLetter a value of 0 was because I got an error stating it wasn't initialized – Hockz Nov 10 '18 at 15:34
  • Do you want make your `Inschrijving` class immuatable? Because i see you make your setters private? – Hai Hoang Nov 10 '18 at 15:36
  • yeah (it's an excercise from college) and it clearly says I have to make it private – Hockz Nov 10 '18 at 15:38
  • set categorieEersteLetter to 'p' – mangusta Nov 10 '18 at 15:49
  • @HaiHoang I'm sorry I thought you meant if I really wanted the setters to be private, I have no idea what immuatable is (same goes for final). Using the Inschrijving inschrijving = null; and inschrijving = new Inschrijving (naam, categorieEersteLetter); the integer does give the correct value, however aantalBroodjes gives a wrong value now.. I'm really sorry I'm new to Java.. – Hockz Nov 10 '18 at 16:05
  • you should tell what you want to achieve with your code in your question, this should help everybody verify your approach is correct or not. – Hai Hoang Nov 10 '18 at 16:11
  • Oh okay, & regarding your next and nextLine, I did use nextLine first but executing antwoord = invoer.nextLine(); underneath System.out.println("Zijn er nog inschrijvingen? "); (the 2 lines above the while at the bottom of InschrijvingApplicatie) gave me the error " java.lang.StringIndexOutOfBoundsException: String index out of range: 0" – Hockz Nov 10 '18 at 16:31
  • Because you also use `nextInt()` which didn't read the new line character (when you press enter key). This new line will be stored in the input stream and the next time you call `nextLine()` , it see a new line character, and stop immediately, result in an empty string for your `antwoord` . I will update my answer a bit base on your exercise require. – Hai Hoang Nov 10 '18 at 16:38