Java arraylist检索数据最小/最大值

时间:2011-05-06 22:09:51

标签: java

我正在为一堂课做Java作业。我写了下面的商店程序,用户输入了一个4位数的ID以及他们为该商店ID所拥有的钱。这些信息被放入一个数组中。检索总计和商店ID。 在我的程序的下一部分中,我将从每个数据组中检索最小值和最大值:偶数和奇数存储ID号。我试图通过检索原始数据并将它们放入一个新数组来做到这一点。甚至将数据转换为偶数数组,将奇数数据转换为奇数数组。在下面的代码中,我正在测试偶数部分。一旦它工作,我将在奇数部分复制。
现在,以下代码会跳过我的请求。我不知道如何解决这个问题。 任何见解将不胜感激!

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


public class Bonus
{

    public static void main (String[] arg)
    {

        Scanner in = new Scanner(System.in);
        String storeID, highID;
        double grandTotalSales = 0;
        double evenGrandTotal = 0;
        double oddGrandTotal = 0;
        double evenTotalSale;
        double oddTotalSale;
        double largestYet = 0;
        double maxValue = 0;
        int numPenn, numNick, numDime, numQuar, numHalf, numDol;
        boolean more = true;
        boolean report = true;
        String input;
        int inputopt;
        char cont;
        char check1, highStoreID;
        Store myStore;
        ArrayList<Store> storeList = new ArrayList<Store>();
        ArrayList<Store> evenStoreList = new ArrayList<Store>();

        while(more)
        {
        in = new Scanner(System.in);
        System.out.println("Enter 4 digit store ID");
        storeID = in.nextLine();

        System.out.println("Enter num of Penny");
        numPenn = in.nextInt();

        System.out.println("Enter num of Nickel");
        numNick = in.nextInt();


        System.out.println("Enter num of Dime");
        numDime = in.nextInt();

        System.out.println("Enter num of Quarter");
        numQuar = in.nextInt();


        System.out.println("Enter num of Half dollars");
        numHalf = in.nextInt();

        System.out.println("Enter num of Dollar bills");
        numDol = in.nextInt();

        myStore = new Store(storeID, numPenn, numNick, numDime, numQuar, numHalf, numDol);
        storeList.add(myStore);

        in = new Scanner(System.in);
        System.out.println("More stores: Yes or No");
        input = in.nextLine();
        cont = input.charAt(0);

        if((cont == 'N')||(cont == 'n'))
            more = false;

        }
        while(report)
        {
                in = new Scanner(System.in);
                System.out.println("What would you like to do?  \nEnter: \n1 print Odd Store ID's report \n2 print Even Store ID's report \n3 to Exit");
                inputopt = in.nextInt();
                if(inputopt == 2)
                {

                System.out.println("\nEven Store ID's Report:");
                System.out.println("Store ID" + " | " + " Total Sales" + " | " + "Even Total Sales");

                for(int i = 0; i < storeList.size(); ++i)
                {
                    myStore = (Store)(storeList.get(i));
                    storeID = myStore.getStoreID();

                    check1 = storeID.charAt(3);
                    if(check1 == '0' || check1 == '2' || check1 == '4'|| check1 == '6' || check1 =='8')
                    {
                        myStore.findEvenValue();

                        evenTotalSale = myStore.getEvenValue();
                        evenGrandTotal = evenGrandTotal + Store.getEvenValue();

                        System.out.println((storeList.get(i)).getStoreID() + "     | " + (storeList.get(i)).getEvenValue() + "   | " + (storeList.get(i)).getEvenGrandValue());

                     }

                    }

                         in = new Scanner(System.in);
                         System.out.println("Do want to print the highest and lowest sales?  \nEnter yes or no");
                         input = in.nextLine();
                         cont = input.charAt(0);
                         if((cont == 'Y')||(cont == 'y'))
                         {
                             evenTotalSale = 0;
                             for(int i = 1; i < evenStoreList.size(); ++i)
                             {
                                myStore = (Store)(evenStoreList.get(i));
                                highID = myStore.getStoreID();
                                    myStore.findEvenValue();
                                    largestYet = myStore.getEvenValue();
                                    if(largestYet > evenTotalSale)
                                    {
                                    Collections.copy(storeList, evenStoreList);
                                    System.out.println("Store ID with highest sales is: ");
                                    System.out.println((evenStoreList.get(i)).getStoreID() + "     | " + largestYet);
                                    }
                               }
                            }
                        else if((cont == 'N')||(cont == 'n'))
                        report = true;

            }
                else
                if(inputopt == 1)
                {
                                System.out.println("\nOdd Store ID's Report:");
                                System.out.println("Store ID" + " | " + " Total Sales" + " | " + " Odd Total Sales");

                    for(int i = 0; i < storeList.size(); ++i)
                    {
                                myStore = (Store)(storeList.get(i));
                                storeID = myStore.getStoreID();

                                check1 = storeID.charAt(3);
                                if(check1 == '1' || check1 == '3' || check1 == '5'|| check1 == '7' || check1 =='9')
                                {
                                myStore.findOddValue();

                                oddTotalSale = myStore.getOddValue();
                                oddGrandTotal = oddGrandTotal + Store.getOddValue();

                                System.out.println((storeList.get(i)).getStoreID() + "     | " + (storeList.get(i)).getOddValue() + "   | " + (storeList.get(i)).getOddGrandValue());
                                }
                   }
                }
                 else
                if(inputopt == 3)
                    report = false;

        } // close while report




    }// close of main



} // close class

类商店:

public class Store
{
private String storeID;
private int numPenn, numNick, numDime, numQuar, numHalf, numDol;
Coin penn = new Coin("Penn", 0.01);
Coin nick = new Coin("Nickel", 0.05);
Coin dime = new Coin("Dime", 0.10);
Coin quar = new Coin("Quar", 0.25);
Coin half = new Coin("Half", 0.50);
Coin dol = new Coin("Dollar", 1.00);
private static double evenTotalSale;
private static double oddTotalSale;
static double evenGrandTotal = 0;
static double oddGrandTotal = 0;



    public Store (String storeID, int numPenn, int numNick, int numDime, int numQuar, int numHalf, int numDol)
    {
        this.storeID = storeID;
        this.numPenn = numPenn;
        this.numNick = numNick;
        this.numDime = numDime;
        this.numQuar = numQuar;
        this.numHalf = numHalf;
        this.numDol = numDol;
     }


public void findEvenValue()
{   evenTotalSale = numPenn * penn.getValue() + numNick * nick.getValue() + numDime * dime.getValue()
                + numQuar * quar.getValue() + numHalf * half.getValue() + numDol * dol.getValue();
     evenGrandTotal = evenGrandTotal + evenTotalSale;
}

public static double getEvenValue()
{
    return evenTotalSale;
}

public void findOddValue()
{   oddTotalSale = numPenn * penn.getValue() + numNick * nick.getValue() + numDime * dime.getValue()
                + numQuar * quar.getValue() + numHalf * half.getValue() + numDol * dol.getValue();
     oddGrandTotal = oddGrandTotal + oddTotalSale;
}

public static double getOddValue()
{
    return oddTotalSale;
}

public static double getOddGrandValue()
{
    return oddGrandTotal;
}

public static double getEvenGrandValue()
{
    return evenGrandTotal;
}
public String getStoreID()
{
    return storeID;
}


}

1 个答案:

答案 0 :(得分:2)

您的evenStoreList为空。