Created
November 25, 2012 00:15
-
-
Save 0xLeon/4141907 to your computer and use it in GitHub Desktop.
PizzaService Class for PSE homework
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
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
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 ;)