简体   繁体   中英

How do i print this out?

I want to print out what cook3 did for a preparation. This should come out: "Pebble putting on a apron". But instead i only get: "putting on apron", so with out the name. I am trying to add a strategy pattern, which I think I did but the only problem is this now.

Here is my code:

package cook;

import cook.domein.Restaurant;
import cook.domein.apron;
import cook.domein.classic;
import cook.domein.cook;
import cook.domein.hardRock;
import cook.domein.knives;
import cook.domein.preperation;

public class main {
public static void main(String[] args) {
    Restaurant rest = new Restaurant();

    cook cook1 = new cook("");
    cook cook2 = new cook("Glenn");
    cook cook3 = new cook("Pebble");

    rest.addCook(cook1);
    rest.addCook(cook2);
    rest.addCook(cook3);

    rest.doPreparation(new apron());


}
}

Here is my interface:

   package cook.domein;

  public interface preperation {
          public void doPreparation(String naam);


}

This is my subclass apron

 package cook.domein;

 public class apron implements preperation {
    @Override
    public void doPreparation(String naam) {
    System.out.println(naam+"Putting on a Apron");
}

   }

This is my restaurant class:

    package cook.domein;

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

public class Restaurant implements preperation{ List cooks;

public Restaurant() {
    this.cooks= new ArrayList<cook>();
}

public void addCook(cook cook) {
    this.cooks.add(cook);

}

public void removeCook(cook cook) {
    this.cooks.remove(cook);
}
public String calculateTotal(){
    for(cook cook: cooks){
        cook.getNaam();
    }
    return "";

}
public void doPreparation(preperation prepMethod){
    String naam = calculateTotal();
    prepMethod.doPreparation(naam);
}

@Override
public void doPreparation(String naam) {
    // TODO Auto-generated method stub

}


 }

And cook class: package cook.domein;

     public class cook {
private cook naam;

public cook(cook naam) {

    this.naam = naam;

}

public cook(String string) {
    // TODO Auto-generated constructor stub
}

public cook getNaam() {
    return naam;
}

public void setNaam(cook naam) {
    this.naam = naam;
}




  }

You're passing an empty string to your doPreparation method. Here's why:

public String calculateTotal(){
    for(cook cook: cooks){
        cook.getNaam();
    }
    return "";
}

This method will only ever return an empty string. This is why you're getting the output that you are; it's working as designed.

It's likely you intended to iterate across all cooks in your restaurant and perform preparation with them. Here's one approach to that.

public void doPreparation(preperation prepMethod){
    for(cook currentCook : cooks) {
        prepMethod.doPreparation(currentCook.getNaam());
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM