如何设置限制

时间:2017-06-13 14:26:14

标签: java arrays eclipse arraylist

如何创建股票?

我正在研究一个班级项目,在这个项目中,我正在制作像麦当劳这样的自订界面。问题在于我不知道如何创建一个股票,因为这个原因,有人可以为我所关心的所有人订购一百万个汉堡,用我目前的工作。从我所做的研究中,我发现我必须为每种食物上课,然后做一个Arraylist。我不能把它们放在一起。

请告诉我你该怎么做的建议。以下是我在项目中完成的内容:

    import java.util.*;
import javax.swing.*;
import java.util.ArrayList;
import java.text.*;
public class hg {

 public static void main(String[] args) {

  InternationalFoods();
 }


 public static Component InternationalFoods() {

  Scanner input = new Scanner(System.in);
  double nafTotal = 0, afTotal = 0, efTotal = 0, mefTotal = 0;
  String cont = "n";
  Component result = null;

  System.out.println("International Foods\nWelcome to the International Foods online ordering menu.");
  do {
   System.out.println("\nEnter the number for the corespondong menu you would like to order from:" + "\n\t1. North American\n\t2. Asian\n\t3. European \n\t4. Middle Eastern");
   int menu = input.nextInt();

   if (menu == 1) {
    //North American Menu
    int nafChoice[] = new int[13];
    String nafItem[] = {
     "Hotdogs",
     "Ribs",
     "Chicken",
     "Mashed Potatoes",
     "Classic Cheese Burger",
     "Double Bacon Cheese Burger",
     "Veggie Burger",
     "Mild Wings",
     "Hot Wings",
     "Barbeque Steak",
     "Beef Manhattan",
     "Beef Wellington",
     "Main menu"
    };
    double nafCost[] = new double[13];
    String nafAnswer = "y";
    //North American Menu
    System.out.println("Food\t\t\t\tCost\n");
    System.out.println("1.Hotdogs\t\t\t$2.99");
    System.out.println("2.Ribs\t\t\t\t$14.99");
    System.out.println("3.Chicken\t\t\t$14.99");
    System.out.println("4.Mashed Potatoes\t\t$10.89");
    System.out.println("\nBurgers");
    System.out.println("5.Classic Cheese Burger\t\t$8.99 ");
    System.out.println("6.Double Bacon Cheese Burger\t$10.99");
    System.out.println("7.Veggie Burger\t\t\t$10.99");
    System.out.println("\nWings");
    System.out.println("8.Mild Wings\t\t\t$8.89");
    System.out.println("9.Hot Wings\t\t\t$8.89");
    System.out.println("Steak\n");
    System.out.println("10.Grilled Steak\t\t$15.99");
    System.out.println("11.Beef Manhattan\t\t$15.99");
    System.out.println("12.Beef Wellington\t\t$15.99");
    System.out.println("\n13.To go back to the main menu ");
    for (int i = 0; nafAnswer.equalsIgnoreCase("y"); i++) {
     //Ask for the user choice
     System.out.println("\nEnter a number choice from one of the options in the menu above:");
     nafChoice[i] = input.nextInt();
     if (nafChoice[i] == 1) {
      System.out.println(nafItem[nafChoice[i] - 1]);
      nafCost[i] = 2.99;
     } else if (nafChoice[i] == 2) {
      System.out.println(nafItem[nafChoice[i] - 1]);
      nafCost[i] = 14.99;
     } else if (nafChoice[i] == 3) {
      System.out.println(nafItem[nafChoice[i] - 1]);
      nafCost[i] = 14.99;
     } else if (nafChoice[i] == 4) {
      System.out.println(nafItem[nafChoice[i] - 1]);
      nafCost[i] = 10.89;
     } else if (nafChoice[i] == 5) {
      System.out.println(nafItem[nafChoice[i] - 1]);
      nafCost[i] = 8.99;
     } else if (nafChoice[i] == 6) {
      System.out.println(nafItem[nafChoice[i] - 1]);
      nafCost[i] = 10.99;
     } else if (nafChoice[i] == 7) {
      System.out.println(nafItem[nafChoice[i] - 1]);
      nafCost[i] = 10.99;
     } else if (nafChoice[i] == 8) {
      System.out.println(nafItem[nafChoice[i] - 1]);
      nafCost[i] = 8.89;
     } else if (nafChoice[i] == 9) {
      System.out.println(nafItem[nafChoice[i] - 1]);
      nafCost[i] = 8.89;
     } else if (nafChoice[i] == 10) {
      System.out.println(nafItem[nafChoice[i] - 1]);
      nafCost[i] = 15.99;
     } else if (nafChoice[i] == 11) {
      System.out.println(nafItem[nafChoice[i] - 1]);
      nafCost[i] = 15.99;
     } else if (nafChoice[i] == 12) {
      System.out.println(nafItem[nafChoice[i] - 1]);
      nafCost[i] = 15.99;
     } else if (nafChoice[i] == 13) {
      break;
     } else {
      System.out.println("Invalid option. Please try again.");
     }

     System.out.println("Do you want to order more from this menu?\nIf yes then please enter Y," + " if not then enter N");
     nafAnswer = input.next();
    }
    System.out.println("\nYour order:");
    for (int i = 0; i < nafChoice.length && nafChoice[i] != 13; i++) {
     if (nafChoice[i] > 0 && nafChoice[i] < 14) {
      System.out.println(nafChoice[i] + ". " + nafItem[nafChoice[i] - 1]);
      nafTotal = nafTotal + nafCost[i];
     }
    }
   } else if (menu == 2) {
    //Asian Menu
    int afChoice[] = new int[13];
    String afItem[] = {
     "Smoked Salmon Sushi Roll(10)",
     "Spicy Tuna Sushi Roll(10)",
     "Cucumber and Avocado Sushi(10)",
     "Chow Mien",
     "Oyakodon",
     "Chinese Chicken Fried Rice",
     "Chicken Biryani",
     "Vegetarian Korma",
     "Maple Syrup Korean Teriyaki Chicken",
     "Chinese Shrimp and Tofu Soup",
     "Thai Pumpkin Soup",
     "Thai Ginger Soup",
     "Main menu"
    };
    double afCost[] = new double[13];
    String afAnswer = "y";
    //Asian Menu
    System.out.println("Food\t\t\t\t\tCost\n");
    System.out.println("Sushi\n");
    System.out.println("1.Smoked Salmon Sushi Roll(10)\t\t$4.99");
    System.out.println("2.Spicy Tuna Sushi Roll(10)\t\t$4.99");
    System.out.println("3.Cucumber and Avocado Sushi(10)\t$4.99\n");
    System.out.println("Main Dishes\n");
    System.out.println("4.Chow Mien\t\t\t\t$5.99");
    System.out.println("5.Oyakodon\t\t\t\t$5.99");
    System.out.println("6.Chinese Chicken Fried Rice\t\t$6.99");
    System.out.println("7.Chicken Biryani\t\t\t$5.99");
    System.out.println("8.Vegetarian Korma\t\t\t$5.99");
    System.out.println("9.Maple Syrup Korean Teriyaki Chicken\t$5.99\n");
    System.out.println("Soups\n");
    System.out.println("10.Chinese Shrimp and Tofu Soup\t\t$6.99");
    System.out.println("11.Thai Pumpkin Soup\t\t\t$6.99");
    System.out.println("12.Thai Ginger Soup\t\t\t$6.99\n");
    System.out.println("13.To go back to the main menu ");
    for (int i = 0; afAnswer.equalsIgnoreCase("y"); i++) {
     //Ask for the user choice
     System.out.println("\nEnter a number choice from one of the options in the menu above:");
     afChoice[i] = input.nextInt();

     if (afChoice[i] == 1) {
      System.out.println(afItem[afChoice[i] - 1]);
      afCost[i] = 4.99;
     } else if (afChoice[i] == 2) {
      System.out.println(afItem[afChoice[i] - 1]);
      afCost[i] = 4.99;
     } else if (afChoice[i] == 3) {
      System.out.println(afItem[afChoice[i] - 1]);
      afCost[i] = 4.99;
     } else if (afChoice[i] == 4) {
      System.out.println(afItem[afChoice[i] - 1]);
      afCost[i] = 5.99;
     } else if (afChoice[i] == 5) {
      System.out.println(afItem[afChoice[i] - 1]);
      afCost[i] = 4.99;
     } else if (afChoice[i] == 6) {
      System.out.println(afItem[afChoice[i] - 1]);
      afCost[i] = 5.99;
     } else if (afChoice[i] == 7) {
      System.out.println(afItem[afChoice[i] - 1]);
      afCost[i] = 6.99;
     } else if (afChoice[i] == 8) {
      System.out.println(afItem[afChoice[i] - 1]);
      afCost[i] = 5.99;
     } else if (afChoice[i] == 9) {
      System.out.println(afItem[afChoice[i] - 1]);
      afCost[i] = 5.99;
     } else if (afChoice[i] == 10) {
      System.out.println(afItem[afChoice[i] - 1]);
      afCost[i] = 6.99;
     } else if (afChoice[i] == 11) {
      System.out.println(afItem[afChoice[i] - 1]);
      afCost[i] = 6.99;
     } else if (afChoice[i] == 12) {
      System.out.println(afItem[afChoice[i] - 1]);
      afCost[i] = 6.99;
     } else if (afChoice[i] == 13) {
      break;
     } else {
      System.out.println("Invalid option. Please try again.");
     }
     System.out.println("Do you want to order more from this menu?\nIf yes then please enter Y," + " if not then enter N");
     afAnswer = input.next();
    }
    System.out.println("\nYour order:");
    for (int i = 0; i < afChoice.length; i++) {
     if (afChoice[i] > 0 && afChoice[i] < 14) {
      System.out.println(afChoice[i] + ". " + afItem[afChoice[i] - 1]);
      afTotal = afTotal + afCost[i];
     }
    }
   } else if (menu == 3) {
    //European Menu
    int efChoice[] = new int[13];
    String efItem[] = {
     "Pasta",
     "Lasagna",
     "Spaghetti and meat balls",
     "Souvlakia (Pork Kebab)",
     "Zurich Veal",
     "Italian Vegetable Soup",
     "Baklava",
     "Italian Breaded Pork Chops",
     "Greek Pasta Salad",
     "Main menu"
    };
    double efCost[] = new double[13];
    String nafAnswer = "y";
    //European Menu
    System.out.println("Food\t\t\t\tCost\n");
    System.out.println("1.Pasta\t\t\t\t$10.99");
    System.out.println("2.Lasagna\t\t\t$11.99");
    System.out.println("3.Spaghetti and meat balls\t$12.99");
    System.out.println("4.Souvlakia (Pork Kebab)\t$8.99");
    System.out.println("5.Zurich Veal\t\t\t$10.99");
    System.out.println("6.Italian Vegetable Soup\t\t$6.99");
    System.out.println("7.Baklava\t\t\t$8.99");
    System.out.println("8.Italian Breaded Pork Chops\t\t$10.99");
    System.out.println("9.Greek Pasta Salad\t\t\t$6.99");
    System.out.println("10.To go back to the main menu ");
    for (int i = 0; nafAnswer.equalsIgnoreCase("y"); i++) {
     //Ask for the user choice
     System.out.println("Enter a number choice from one of the options in the menu above:");
     efChoice[i] = input.nextInt();
     if (efChoice[i] == 1) {
      System.out.println(efItem[efChoice[i] - 1]);
      efCost[i] = 4.99;
     } else if (efChoice[i] == 2) {
      System.out.println(efItem[efChoice[i] - 1]);
      efCost[i] = 4.99;
     } else if (efChoice[i] == 3) {
      System.out.println(efItem[efChoice[i] - 1]);
      efCost[i] = 4.99;
     } else if (efChoice[i] == 4) {
      System.out.println(efItem[efChoice[i] - 1]);
      efCost[i] = 4.99;
     } else if (efChoice[i] == 5) {
      System.out.println(efItem[efChoice[i] - 1]);
      efCost[i] = 4.99;
     } else if (efChoice[i] == 6) {
      System.out.println(efItem[efChoice[i] - 1]);
      efCost[i] = 6.99;
     } else if (efChoice[i] == 7) {
      System.out.println(efItem[efChoice[i] - 1]);
      efCost[i] = 8.99;
     } else if (efChoice[i] == 8) {
      System.out.println(efItem[efChoice[i] - 1]);
      efCost[i] = 10.99;
     } else if (efChoice[i] == 9) {
      System.out.println(efItem[efChoice[i] - 1]);
      efCost[i] = 6.99;
     } else if (efChoice[i] == 10) {
      break;
     } else {
      System.out.println("Invalid option. Please try again.");
     }
     System.out.println("Do you want to order more from this menu?\nIf yes then please enter Y," + " if not then enter N");
     nafAnswer = input.next();
    }
    System.out.println("\nYour order:");
    for (int i = 0; i < efChoice.length; i++) {
     if (efChoice[i] > 0 && efChoice[i] < 11) {
      System.out.println(efChoice[i] + ". " + efItem[efChoice[i] - 1]);
      efTotal = efTotal + efCost[i];
     }
    }
   } else if (menu == 4) {
    //Middle Eastern Menu
    int mefChoice[] = new int[13];
    String mefItem[] = {
     "Kebob (beef & chicken)",
     "Falafel",
     "Kebob roll",
     "Shawarma",
     "Hummus",
     "Lamb chops",
     "Chicken tikka",
     "Samosa",
     "Main menu"
    };
    double mefCost[] = new double[13];
    String nafAnswer = "y";
    //Middle Eastern Menu
    System.out.println("Food\t\t\t\tCost\n");
    System.out.println("1.Kebob (beef & chicken)\t$8.99");
    System.out.println("2.Falafel\t\t\t$4.79");
    System.out.println("3.Kebob roll\t\t\t$7.89 ");
    System.out.println("4.Shawarma\t\t\t$8.46");
    System.out.println("5.Hummus\t\t\t$6.77");
    System.out.println("6.Lamb chops\t\t\t$11.97");
    System.out.println("7.Chicken tikka\t\t\t$9.43");
    System.out.println("8.Samosa\t\t\t$1.25");
    System.out.println("9.To go back to the main menu ");
    for (int i = 0; nafAnswer.equalsIgnoreCase("y"); i++) {
     //Ask for the user choice
     System.out.println("Enter a number choice from one of the options in the menu above:");
     mefChoice[i] = input.nextInt();
     if (mefChoice[i] == 1) {
      System.out.println(mefItem[mefChoice[i] - 1]);
      mefCost[i] = 8.99;
     } else if (mefChoice[i] == 2) {
      System.out.println(mefItem[mefChoice[i] - 1]);
      mefCost[i] = 4.99;
     } else if (mefChoice[i] == 3) {
      System.out.println(mefItem[mefChoice[i] - 1]);
      mefCost[i] = 4.99;
     } else if (mefChoice[i] == 4) {
      System.out.println(mefItem[mefChoice[i] - 1]);
      mefCost[i] = 4.99;
     } else if (mefChoice[i] == 5) {
      System.out.println(mefItem[mefChoice[i] - 1]);
      mefCost[i] = 4.99;
     } else if (mefChoice[i] == 6) {
      System.out.println(mefItem[mefChoice[i] - 1]);
      mefCost[i] = 4.99;
     } else if (mefChoice[i] == 7) {
      System.out.println(mefItem[mefChoice[i] - 1]);
      mefCost[i] = 4.99;
     } else if (mefChoice[i] == 8) {
      System.out.println(mefItem[mefChoice[i] - 1]);
      mefCost[i] = 4.99;
     } else if (mefChoice[i] == 9) {
      break;
     } else {
      System.out.println("Invalid option. Please try again.");
     }
     System.out.println("Do you want to order more from this menu?\nIf yes then please enter Y," + " if not then enter N");
     nafAnswer = input.next();
    }
    System.out.println("\nYour order:");
    for (int i = 0; i < mefChoice.length; i++) {
     if (mefChoice[i] > 0 && mefChoice[i] < 10) {
      System.out.println(mefChoice[i] + ". " + mefItem[mefChoice[i] - 1]);
      mefTotal = mefTotal + mefCost[i];
     }
    }
   } else if (menu < 1 || menu > 4) {
    System.out.println("Invalid input");
    continue;
   }
   System.out.println("Would you like to proceesd to checkout? Enter 'N' to order more " + "or enter 'Y' to check out");
   cont = input.next();
  } while (cont.equalsIgnoreCase("n"));
  bill(nafTotal, afTotal, efTotal, mefTotal);
  input.close();
  return result;
 }
 public static void bill(double nafTotal, double afTotal, double efTotal, double mefTotal) {
  //calculate costs and output bill 
  double cost, tax, totalCost = 0;
  cost = nafTotal + afTotal + efTotal + mefTotal;
  tax = cost * 0.13;
  totalCost = cost + tax;
  DecimalFormat df = new DecimalFormat("#.##"); //changes the amount of decimal places, from: https://docs.oracle.com/javase/7/docs/api/java/text/DecimalFormat.html
  System.out.println("Sub Total:\t\t$" + df.format(cost) + "\nTax:\t\t\t$" + df.format(tax) +
   "\nTotal:\t\t\t$" + df.format(totalCost) + "\nThank you for ordering from International Foods!");
 }

}

2 个答案:

答案 0 :(得分:0)

如果您被允许使用第三方库,则Google GuavaApache Commons Collections都会提供Bag / Multiset集合的实现。

Multisets似乎非常适合您想要完成的任务。

你可以添加汉堡包,拿走汉堡包,并要求汉堡包的数量。

答案 1 :(得分:-2)

我想我的朋友你必须从头开始编写代码。 你可以在同一个班级但不同的方法中完成这个程序。