Skip to content

Instantly share code, notes, and snippets.

@0xLeon
Created November 25, 2012 00:15
Show Gist options
  • Select an option

  • Save 0xLeon/4141907 to your computer and use it in GitHub Desktop.

Select an option

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
Copy Markdown

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
Copy Markdown

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