简体   繁体   English

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

[英]Java arraylist retrieving data min/max values

I am doing my Java homework for a class. 我正在为一堂课做我的Java作业。 I wrote the below store program that the user inputs a 4 digit id and what money they had for that store id. 我写了下面的商店程序,用户输入了一个4位数的ID以及他们为该商店ID所拥有的钱。 This information get's put in an array. 这些信息被放入一个数组中。 totals and store id's are retrieved. 检索总计和商店ID。 in the next part of my program I am to retrieve min and max values from each data group:even and odd store id numbers. 在我的程序的下一部分中,我将从每个数据组中检索最小值和最大值:偶数和奇数存储ID号。 I have tried to do this by retrieving the origonal data and putting them into a new array. 我试图通过检索原始数据并将它们放入一个新数组来做到这一点。 even data into an even array and odd data into an odd array. 甚至将数据转换为偶数数组,将奇数数据转换为奇数数组。 in the following code I am testing the even part. 在下面的代码中,我正在测试偶数部分。 Once it works I will replicate in the odd section. 一旦它工作,我将在奇数部分复制。
Right now the following code skips my request. 现在,以下代码会跳过我的请求。 I don't know how to fix this. 我不知道如何解决这个问题。 Any insight would be greatly appreciated! 任何见解将不胜感激!

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

class store: 类商店:

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;
}


}

你的evenStoreList是空的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM