3

I currently have multiple code like this for different toppings

// Toppings - Egg
System.out.print("Do you want " + egg.getType() + "?");
input = keyboard.nextLine();
choice = input.charAt(0);
if (choice == 'y') {
    l.add(egg.getType());
    c.add((double) egg.getCost());
    numberOfToppings = numberOfToppings + 1;
    totalToppingPrice = totalToppingPrice + egg.getCost();
    toppings = toppings + "Egg";
}

It works fine, however i was hoping i could do all toppings in just one block of code. Because i've got around 5 of these, and it takes up far too much, and i've been advised to do so. Anyone got any ideas for how this could be done ? thanks

4
  • 11
    Make a method that does all the work, pass variables into the method for the things that change each time. Then call the method with the appropriate values five times. Commented Dec 4, 2013 at 12:44
  • 1
    Just use the "Extract method" feature of your IDE. And check out other stuff there as well, because they sure come in handy. Commented Dec 4, 2013 at 12:45
  • In order to fulfill Open/Closed principle => replace primitive (choice) with object to get polymorphism with strategy/state pattern Commented Dec 4, 2013 at 12:53
  • To expand on Jespers fine suggestion: the method should do stuff for one given topping. Commented Dec 4, 2013 at 12:59

2 Answers 2

1

All the toppings should be gathered together in an enumeration, as long as the topping set is closed and cannot change during the program execution.

enum Topping{
    EGG("egg", 22),... ;
    private String type;
    private double cost;

    private Topping(String type, double cost){
        this.type = type;
        this.cost = cost;
    }
    //getters and setters

}

Then, you could write a method inside your class containing your code above that should be able to handle a Topping instance, like this:

private handleTopping(Topping top){
    System.out.print("Do you want "+top.getType() +"?");
    input = keyboard.nextLine();
    choice = input.charAt(0);
    if (choice == 'y'){
        l.add(top.getType());
        c.add(top.getCost());
        numberOfToppings = numberOfToppings + 1;
        totalToppingPrice = totalToppingPrice + top.getCost();
        toppings = toppings + " " + top.getType();
    }
}

Finally, call the method for all toppings available

for(Topping top : topping.values()){
    handleTopping(top);
}

It's all about the DRY (don't repeat yourself principle). It's not even necessarily tied to the object oriented paradigm. Even in procedural programming, one of the core principles is to extract common and frequently used functionalities to parameterized procedures.

Sign up to request clarification or add additional context in comments.

2 Comments

can't use enum unfortunately :(
Why not? Anyway, in that case, make it a regular class instead of an enum.
1

I would suggest you make a Topping class that you can use as follows:

Toppping egg = new Topping ("egg", 0.5); // Cost
ArrayList<Topping> toppings = new ArrayList<Topping>();
toppings.add(egg);

Later you can loop over the toppings vector similar to this:

for (Topping current : toppings) {
  if (wantsTopping(current)) {
     chosenToppings.addObject(current);
  }
}

Note: This is Java like code, but it won't compile. There are still some things you need to look up

2 Comments

I know that it's not java code, but you can really confuse java beginner by: 1. using Vector instead of ArrayList. 2. Using raw generics (without <>)
Your right, I updated the code to use ArrayList. My Java is a bit rusty

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.