第一个功能的目的是让用户在一行中输入最多5个字符的顺序(还没有写任何东西来检查它),对于中型或大型披萨,第一个字符必须是M或L.然后是配对的0到4个字符。
第二个功能目的与第一个相同,只是它允许3个或更多相同的浇头。
public class Exercise_1{
public static void pizzaServiceA(String args[]){
HashMap <Character, String> Toppings = new Hashmap <Character, String>();
//pizza
dictionary.put("m", "meduim");
dictionary.put("l", "large");
//topping
dictionary.put("h", "ham");
dictionary.put("m", "mozzerella");
dictionary.put("o", "olives");
dictionary.put("p", "pineapple");
dictionary.put("s", "spinach");
dictionary.put("H", "ham");
dictionary.put("M", "mozzerella");
dictionary.put("O", "olives");
dictionary.put("P", "pineapple");
dictionary.put("S", "spinach");
HashMap <Character, Double> Prices = new Hashmap <Character, Double>();
//pizza price
dictionary.put("m", 4.00);
dictionary.put("l", 5.00);
//topping price medium
dictionary.put("h", 1.40);
dictionary.put("m", 1.00);
dictionary.put("o", 0.80);
dictionary.put("p", 1.00);
dictionary.put("s", 1.20);
//topping price large
dictionary.put("H", 2.10);
dictionary.put("M", 1.50);
dictionary.put("O", 1.20);
dictionary.put("P", 1.50);
dictionary.put("S", 1.20);
System.out.println("Enter a pizza order: ");
Scanner reader = new Scanner(System.in);
String orders = reader.nextLine();
Char[] orderLetters = orders.toCharArray();
String fullOrder = "";
Double fullPrice = 0.0;
//check if sequence enters it more than 5 characters
if (input.equals("quit")) {
System.out.println("Quitting.");
System.exit(0);
}
else if (!(order[0].equals('l')))
{
System.out.println("Please enter the size of your pizza, m or l");
}
else if (!(order[0].equals('m')))
{
System.out.println("Please enter the size of your pizza, m or l");
}
for(Char orderLetters : c.toCharArray())
{
Double price = Prices.get(orderLetters);
fullPrice += price;
String type = Toppings.get(orderLetters);
if(type == 'm' || type == 'l')
{
fullOrder += type + " pizza with ";
}
else
{
fullOrder += type + ",";
}
}
fullOrder += fullPrice;
System.out.printf("%.2f", "£", fullOrder);
}
public static void pizzaServiceB(){
Map<Character, Integer> map = new Hashmap<Character, Integer>();
for(int i = 0; i <s.length(); i++){
char orderLetters = c.charAt(i); //s.charAt?
if (map.containsKey(orderLetters)){
int c = map.get(orderLetters); //counts letters in orderletters
map.put(orderLetters, ++c);
{
else
{
map.put(orderLetters, 1);
}
}
}
}
if (c.equals() = 3){
System.out.println("You cannot order "); //if topping occurs 3 times print
}
//same functionality of A but orders with more than 3 toppings shoudlnt be allowed
}
public static void main(){
Exercise_1 ex1 = null;
ex1.testpizzaServiceA();
//ex1.testpizzaServiceB();
}
}
答案 0 :(得分:0)
密钥在HashMap
中是唯一的,因此当您尝试将两个值放在相同的密钥上最后添加的值覆盖内容时。
在您的代码中,您使用了密钥m
两次,将Pizza和顶部的值放在同一个HashMap
对象字典中。
//比萨饼
dictionary.put("m", "meduim");
//摘心
dictionary.put("m", "mozzerella");
答案 1 :(得分:0)
您不应该使用HashMap
将字符映射到您的所有信息。例如,您可以创建一个Pizza
类,您可以在其中设置比萨饼(确定大小并添加浇头)。顶部列表可以是List<Topping>
,其中Topping
是枚举。
class Pizza {
public static enum Topping {
HAM, MOZZARELLA, OLIVES, PINEAPPLE, SPINACH,
CHEESE; // I added cheese. I love cheese.
}
public static enum Size {
MEDIUM, LARGE;
}
private List<Topping> toppings = new ArrayList<>();
public void addTopping(Topping topping) {
// First, test if the list with toppings does not contain
// the given topping more than once.
this.toppings.add(topping);
}
}
存储价格
然后你需要以某种方式在某处保存价格,例如HashMap
。但是,如果您愿意,还可以在Topping
类中定义price属性。假设披萨大小和每个顶部之间的每个组合必须有自己的价格,您应该在某处保留一个列表 - 可能是HashMap
- 并确保列表中存在所有组合。如果没有,那么消费者可以免费获得它。 : - )
处理输入
要处理来自Scanner
的输入,您可以制作两种方法,一种用于定义披萨大小,另一种用于定义浇头。这些不应该在你的披萨班。
private void consumeSizeToken(char token) {
// Obtain the pizza object somewhere, otherwise add it to the
// method's parameter list.
switch (token) {
case 'm':
// I assume the Pizza class to have a setSize() method.
pizza.setSize(Size.MEDIUM);
break;
case 'l':
pizza.setSize(Size.LARGE);
break;
default:
throw new IllegalArgumentException("Invalid pizza size");
}
}
private void consumeToppingToken(char token) {
// Do the same as the consumeSizeToken() method, but, of course,
// handle the toppings.
}
为了处理输入,只假设第一个字符是大小,剩下的字符是浇头:
consumeSizeToken(input.charAt(0));
for (int i = 1; i < input.length(); i++) {
consumeToppingToken(input.charAt(i);
}
您还应该考虑到这一点:
HashMap<Character, String> toppings = new Hashmap<Character, String>()
可以替换为HashMap<Character, String> toppings = new Hashmap<>()
。