我如何将商品订单放在另一个类别中?

时间:2018-08-06 13:18:19

标签: java class arraylist

package project;

import java.util.ArrayList;
import java.util.Scanner;

public class project {

public static void main(String[] args) {
    // TODO Auto-generated method stub


    //Setting up scanner
    Scanner input = new Scanner(System.in);
    //Variable to allow user to quit input
    String quit = "done";


    //Constants
    double salesTax = 0.11;

    double spagPrice = 7;
    double ramenPrice = 4;
    double pepperPrice = 9;
    double steakPrice = 12;
    double tunaPrice = 6;

    //Constants for loop
    double listTime = 1;
    double listMax = 10;

    //Variable for loop
    double itemEntry = 0;
    //Keeps running total of item prices    
    double priceAdd = 0;

    //Initializes array lists
    ArrayList<String> itemList = new ArrayList<String>();
    ArrayList<Integer> priceList = new ArrayList<Integer>();
    ArrayList<Integer> buyList = new ArrayList<Integer>();
    ArrayList<String> buyitemList = new ArrayList<String>();


    //Adds food names to the correct array
    itemList.add("Spaghetti Tacos");
    itemList.add("Ramen Pizza");
    itemList.add("Stuffed Bell Pepper");
    itemList.add("Mushroom Steak");
    itemList.add("Tuna Suprise");

    //Adds prices to the price array
    priceList.add(7);
    priceList.add(4);
    priceList.add(9);
    priceList.add(12);
    priceList.add(6);



    System.out.println("Please enter a number to indicate your desired item. 
Type 'done' when you are done or enter up to 10 items.");

    System.out.println(" " + "1" + " " + " " + " " +  "2" + " " + " " + " " + 
"3" + " " + " " + " " + "4" + " " + " " + " " + "5");
    System.out.println(itemList);
    System.out.println("Prices: " + priceList);

    orderEntry (quit, listTime, listMax, priceAdd, buyitemList);


    /** while (quit != "done" || listTime <= listMax)
     {

         System.out.println("Please enter item " + listTime + " :");
         itemEntry = input.nextInt();

         if (itemEntry == 1){
             priceAdd = spagPrice;
             buyitemList.add("Spaghetti Tacos");

         }
         else if (itemEntry == 2){
             priceAdd = ramenPrice;
             buyitemList.add("Ramen Pizza");
         }
         else if (itemEntry == 3){
             priceAdd = pepperPrice;
             buyitemList.add("Stuffed Bell Pepper");
         }
         else if (itemEntry == 4){
             priceAdd = steakPrice;
             buyitemList.add("Mushroom Steak");
         }
         else if (itemEntry == 5){
             priceAdd = tunaPrice;
             buyitemList.add("Tuna Suprise");
         }
         else {
             priceAdd = 0;

         }


         buyList.add((int) priceAdd);

         listTime += listTime;



     }
     */


    double amntTacos = 0;
    double amntPizza = 0;
    double amntPepper = 0;
    double amntSteak = 0;
    double amntTuna = 0;

    double tacoPrice = 0;
    double pizzaPrice = 0;
    double belPrice = 0;
    double mushPrice = 0;
    double suprisePrice = 0;

    public orderEntry(speTacos)
      {
        this.orderEntry = amntTacos;
      }

    public orderEntry(ramPizza)
      {
        this.orderEntry = amntPizza;
      }

    public orderEntry(belPepper)
      {
        this.orderEntry = amntPepper;
      }

    public orderEntry(musSteak)
      {
        this.orderEntry = amntSteak;
      }
    public orderEntry(tunSup)
      {
        this.orderEntry = amntTuna;
      }

    tacoPrice = spagPrice * amntTacos;

    pizzaPrice = ramenPrice * amntPizza;

    belPrice = pepperPrice * amntPepper;

    mushPrice = steakPrice * amntSteak;

    suprisePrice = tunaPrice * amntTuna;






















     System.out.println("---------------------");
     System.out.println("---------------------");

     double subTotal = 0; 
     double taxAmount = 0;
     double totalPrice = 0;

     //for (int i : buyList) {
    // subTotal = subTotal + i; 
     //}

     subTotal = tacoPrice + pizzaPrice + belPrice + mushPrice + suprisePrice;

     taxAmount = subTotal * salesTax;

     totalPrice = subTotal + taxAmount;


     System.out.println("You have chosen the following items: " + 
buyitemList);
     System.out.println("---------------------");
     System.out.println("Your Subtotal is: $" + subTotal);
     System.out.println("Your Tax amount is: $" + taxAmount);
     System.out.println("Your Final Total is: $" + totalPrice);




package project;
import java.util.ArrayList;
import java.util.Scanner;
public class orderEntry (String quit, double itemEntry, double spagPrice, 
double 
ramenPrice, double pepperPrice, double steakPrice, double tunaPrice, double 
listTime, double listMax, Scanner input, double priceAdd, ArrayList buyList) 
{

Scanner input = new Scanner(System.in);

double speTacos = 0;
double ramPizza = 0;
double belPepper = 0;
double musSteak = 0;
double tunSup = 0;

while (quit != "done" || listTime <= listMax)
 {

     System.out.println("Please enter item " + listTime + " :");
     itemEntry = input.nextInt();

     if (itemEntry == 1){
         priceAdd = spagPrice;
         speTacos = speTacos + 1;

     }
     else if (itemEntry == 2){
         priceAdd = ramenPrice;
         ramPizza = ramPizza + 1;
     }
     else if (itemEntry == 3){
         priceAdd = pepperPrice;
         belPepper = belPepper + 1;
     }
     else if (itemEntry == 4){
         priceAdd = steakPrice;
         musSteak = musSteak + 1;
     }
     else if (itemEntry == 5){
         priceAdd = tunaPrice;
         tunSup = tunSup + 1;
     }
     else {
         priceAdd = 0;

     }


     buyList.add((int) priceAdd);

     listTime += listTime;

 }


public void getSpagetti(){
    return speTacos;
}

public void getRamen(){
    return ramPizza;
}

public void getPepper(){
    return belPepper;
}

public void getSteak(){
    return musSteak;
}

public void getTuna(){
    return tunSup;
}



}

好的,现在尝试一下。还是不太正确。...

说明是:

  

还应使用适当的名称创建名为orderEntry的类   构造函数,访问器和mutator方法。当用户进入   他们想要订购的物品,这些物品应放在单独的位置   按顺序排列。用户输入完项目后,   程序应使用数组输出所有订购的物品   屏幕及其价格。

1 个答案:

答案 0 :(得分:0)

这不是我建议的解决方案,但是为了学习如何使用方法: 只需将其放在另一个类中,然后从那里调用方法即可。给方法指定所需的参数。

public class whileMethod(String quit, double listTime, double listMax, Scanner input, double priceAdd, ArrayList<String> buyItemList, ) {

while (quit != "done" || listTime <= listMax)
     {

         System.out.println("Please enter item " + listTime + " :");
         itemEntry = input.nextInt();

         if (itemEntry == 1){
             priceAdd = spagPrice;
             buyitemList.add("Spaghetti Tacos");

         }
         else if (itemEntry == 2){
             priceAdd = ramenPrice;
             buyitemList.add("Ramen Pizza");
         }
         else if (itemEntry == 3){
             priceAdd = pepperPrice;
             buyitemList.add("Stuffed Bell Pepper");
         }
         else if (itemEntry == 4){
             priceAdd = steakPrice;
             buyitemList.add("Mushroom Steak");
         }
         else if (itemEntry == 5){
             priceAdd = tunaPrice;
             buyitemList.add("Tuna Suprise");
         }
         else {
             priceAdd = 0;

         }


         buyList.add((int) priceAdd);

         listTime += listTime;



     }

您可以从这里在类中调用该方法:

whileMethod(以及所需的所有属性)