2

私はJavaの初心者で、アイテムの名前、そのアイテムの単位、価格/単位、および合計価格を取得するプログラムをコーディングしています。これらの値を別の配列に入れようとしているので、後で一種の領収書を作成するときにそれらにアクセスできますが、それらの値を配列の位置に割り当ててから、ハードコーディングせずに同じ位置にアクセスする方法がわかりません. ループが最良のオプションですが、これを設定する方法がわかりません。ヘルプと提案をいただければ幸いです。行列や 3D 配列などの高度なことはできないことに注意してください。それをシンプルに保つことができれば、それは素晴らしいことです。

これはメイン クラスです。userInput() と menu() を実行する main() を持つテスター クラスがありますが、コードが 2 行しかないため、それを入れる意味がありません。

import java.util.Scanner;
public class GroceryList {

    // instance variables 
    Scanner Price, Items, NumItems, Units;

    private double myGross, myNet;
    private final double STATETAX;
    private double totalPrice, unitPrice;
    private String itemName;
    private int totalUnits;
    ///////////////////////////////////////////// arrays I will use
    private double[] totalPrice1;
    private double[] unitPrice1;
    private String[] itemName1;
    private int[] totalUnits1;

    public GroceryList()
    {
        STATETAX = 0.06;
        double[] totalPrice = new double[50];
        double[] unitPrice = new double[50];
        String[] itemName = new String[50];
        int[] totalUnits = new int[50];
    }

    public void userInput()
    {
        Scanner Price = new Scanner(System.in);
        Scanner Items = new Scanner(System.in);
        Scanner Units = new Scanner(System.in);
        Scanner NumItems = new Scanner(System.in);

        int u, c, totalItems;// c is the number of items that has to equal totalItems in order for the loop to break
        double price;
        String item;//gets the name of the item
        c=0;

        System.out.println("Welcome to Grocery List ! \n");
        System.out.print("Enter the total number of items you want to buy (not total units !) : ");
        totalItems = NumItems.nextInt();
        System.out.println();

        do 
        {
            c++ ;
            System.out.print("Enter the item name : ");
            item = Items.nextLine();
            System.out.print("Enter the units of " + item + " you want to buy : ");
            u = Units.nextInt();
            System.out.print("Enter the price of a/n " + item + " : $");
            price = Price. nextDouble();

            /*this would make only one value appear at the receipt, which would be only one item name, one unit price, one price/unit, one total price and the other values calculated
   would not appear on the receipt because you cant assign more than 1 value to a variable so thats why I need arrays. 
             */
            itemName = item;
            totalUnits = u;
            unitPrice = price;



            calc(u,price,item);
        }
        while (c < totalItems);
    }

    public void calc(int u, double p, String i)
    {
        double total;//total amount of $ for 1 item
        total = u*p;
        System.out.println("Total price of " + i + " : $" + total + "\n");
        grossPay(total);
        totalPrice = total;
    }


    public void grossPay(double total)
    {
        double gross;
        myGross += total;
    }

    public double tax()
    {
        double temp;
        temp = myGross*STATETAX;
        myNet = myGross - temp;
        return myNet;
    }

    public void menu()
    {

        System.out.printf("%-10s %6s %11s %11s" , "ITEM :" , "UNITS :" , "PRICE :" , "TOTAL :"); 
        System.out.println();
        System.out.printf("%-11s %2d %7s $%4.2f %5s $%2.2f", itemName, totalUnits,"", unitPrice,"", totalPrice);
        System.out.println();



    }

    public void payment()
    {
        System.out.println("Amount before tax : $" + myGross);
        System.out.println("Amount after tax : $" + tax());

    }

}//end GroceryList
4

2 に答える 2

1

理想的には、あなたはそうするでしょう(私があなたのソートの意味を誤解したのでこれを書きました):

ItemフィールドnametotalPriceunitPrice、を持つオブジェクトを作成しますtotalUnits。次に、買い物リストに 4 つの配列を含める必要はなく、Items. インデックスを追跡しなければならないという疑わしい作業を省きます。

ItemComparator実装するクラスも作成する場合はComparator、それらをソートする方法を定義でき、次に使用して配列をソートできます

Collections.sort(Arrays.asList(itemsList), new ItemComparator());

また、4 つのスキャナーは必要ありません。すべてオンになっているため、同じスキャナーを使用できます。System.in

于 2013-10-16T02:12:20.537 に答える