Skip to content

Instantly share code, notes, and snippets.

@0xLeon
Created November 25, 2012 00:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0xLeon/4141907 to your computer and use it in GitHub Desktop.
Save 0xLeon/4141907 to your computer and use it in GitHub Desktop.
PizzaService Class for PSE homework
import java.util.HashMap;
/**
* @author Stefan
*/
public class PizzaService {
public double calculatePizzaCost(int numberOfPizza, String typeOfPizza) throws IllegalArgumentException {
double[] extraCost = {4.3, 3.75, 2.5, 1.75};
HashMap<String, Number> typeToIndex = new HashMap<String, Number>();
typeToIndex.put("thunfisch", 0);
typeToIndex.put("salami", 1);
typeToIndex.put("paprika", 2);
typeToIndex.put("zwiebel", 3);
if (!typeToIndex.containsKey(typeOfPizza.toLowerCase())) {
throw new IllegalArgumentException("Ungültiger Pizza-Typ");
}
return ((4.35 + 0.65 + extraCost[typeToIndex.get(typeOfPizza.toLowerCase()).intValue()]) * numberOfPizza);
}
}
@TimWolla
Copy link

Just a note: You don´t have to supply the Types for the Hashmap twice. Have a look at: http://openbook.galileocomputing.de/javainsel/javainsel_09_001.html#dodtp511af877-15c3-4be2-954f-b72ce7e1d666 ;)

@TimWolla
Copy link

And the types better would be an enum.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment