Search code examples
javawhile-loopnested-loops

Java nested while loops not functioning as desired


I'm having trouble with an assignment for a class. I need to be able to print a sales report after inputting certain data and figured the best way to keep track of everything was to use arrays.

I have been trying to figure this out for hours and I'm stumped. Any help would be appreciated.

For reference, the user needs to input:

  1. the employee name
  2. year to date sales
  3. a transaction number
  4. transaction type
  5. transaction amount

Then it should loop back to transaction number and continue that loop until the value 0 is given as input for transaction number.

Then it should loop back to employee name and continue back through that loop until Done is given as input for employee name.

Here is the code (I think this is the only relevant portion, but if you want to see the whole piece of code I can post it.)

Thanks again for all your help or suggestions!

    void salesData() throws IOException {
    for (int i = 0; i < 100; i++) {
        System.out.print("Enter Name: ");
        n = stdin.readLine();

        if (n.equalsIgnoreCase("done")) {
            break;
        }
        else {
            System.out.print("Enter Transaction Number: ");
            t = Integer.parseInt(stdin.readLine());

            if (t == 0) {
                break;
            }
            else {
                System.out.print("Enter Transaction Type: ");
                tp = stdin.readLine();
                System.out.print("Enter Transaction Amount: ");
                a = Double.parseDouble(stdin.readLine());

                totSales = totSales + a;
                totYtd = totYtd + a;
                empTotal = empTotal + a; 
                empBonus = empBonus + (a * 0.05);

                name[i] = n;
                ytd[i] = y;
                tNum[i] = t;
                type[i] = tp;
                amount[i] = a;

                outputUpdate();

                calcSalesData();
            }
        }

    }
    outputSalesData();
}

Ok, so i've been working on this thanks to your guys' help and I've made a lot of progress. Still having one issue though. The array is only saving the trans number, type, and amount for the LAST transaction entered for each employee instead of EACH transaction.

I believe the error is that i need to iterate the arrays for tNum, type, and amount at a different rate than the name and ytd arrays?

Having a bit of trouble still so any help is appreciated... here is my updated code as well as the print statement at the end.

    void salesData() throws IOException {
    for (int i = 0; i < 100; i++) {
        System.out.print("Enter Name: ");
        n = stdin.readLine();
        if (n.equalsIgnoreCase("done")) {
            outputSalesData();
        }
        System.out.print("Enter Year to Date Sales: ");
        y = Double.parseDouble(stdin.readLine());
        ytdSales = ytdSales + y;
        totYtd = totYtd + ytdSales;
        while (t != 0) {
            System.out.print("Enter Transaction Number: ");
            t = Integer.parseInt(stdin.readLine());

            if (t == 0) {
                t = 1;
                empBonus = 0;
                ytdSales = 0;
                break;
            }
            else {
                System.out.print("Enter Transaction Type: ");
                tp = stdin.readLine();
                System.out.print("Enter Transaction Amount: ");
                a = Double.parseDouble(stdin.readLine());

                totSales = totSales + a;
                totYtd = totYtd + a;
                ytdSales = ytdSales + a;
                empTotal = empTotal + a; 
                empBonus = empBonus + (a * 0.05);

                name[i] = n;
                ytd[i] = y;
                tNum[i] = t;
                type[i] = tp;
                amount[i] = a;

                outputUpdate();

                calcSalesData();
                tCount++;
            }
        }
    }
}

and here is the print:

    void rptOut() {
    System.out.println("");
    System.out.println("--------------------------------------------");
    System.out.println("Employee:\tYTD:\t\tT #:\tType:\tAmount:");
    while (index < tCount)
    {
        System.out.println(name[index] + "\t\t$" + df2.format(ytd[index]) + "\t" + tNum[index] + "\t" + type[index] + "\t$" + amount[index]);
        index++;
    }
    System.out.println("--------------------------------------------");
    System.out.println("Total Food & Soft Drink Sales: \t$" + df2.format(totF));
    System.out.println("Total Alcohol Sales: \t\t$" + df2.format(totA));
    System.out.println("Total Sundries Sales: \t$" + df2.format(totS));
    System.out.println("--------------------------------------------");
    System.out.println("Total Sales for Day: \t$" + df2.format(totSales));
    System.out.println("Total YTD: \t\t$" + df2.format(totYtd));
    System.out.println("--------------------------------------------");
    System.out.println("Highest Trans Amount: \t$" + df2.format(hiTrans));
    System.out.println("Employee w/ Highest Trans: \t" + hiEmp);
    System.out.println("--------------------------------------------");
    //System.exit(0);
}

Solution

  • From what I can gather, you want to store the values of Sales Report into arrays, where Array name, Array ytd, Array tNum and Array type all hold values for a specific Sales Report. Now in order to use this concept, you'll want to ensure that index 0 refers to the same Sales Report's data throughout your mirrored arrays.

    Sales Report 0 = {name[0], ytd[0], tNum[0], type[0]} 
    Sales Report 1 = {name[1], ytd[1], tNum[1], type[1]}
    etc....
    

    To do this you can use a single for loop. Try the following

    void salesData() throws IOException {
        for (int srIndex = 0; srIndex < 100; srIndex++)
        {
            System.out.print("Enter Name: ");
            n = stdin.readLine();
            name[srIndex] = n;
            System.out.print("Enter Year to Date Sales: ");
            y = Double.parseDouble(stdin.readLine());
            ytd[srIndex] = y;
            totYtd = totYtd + y;
            System.out.print("Enter Transaction Number: ");
            t = Integer.parseInt(stdin.readLine());
            if (t == 0) {
                break;
            } else {
                tNum[srIndex] = t;
            }
            System.out.print("Enter Transaction Type: ");
            tp = stdin.readLine();
            type[srIndex] = tp;
            System.out.print("Enter Transaction Amount: ");
            a = Double.parseDouble(stdin.readLine());
            totSales = totSales + a;
            totYtd = totYtd + a;
            empTotal = empTotal + a;
            empBonus = empBonus + (a * 0.05);
    
            calcSalesData();
            outputSalesData();
            //ask to enter another sales report
            System.out.print("Do you want to enter another Sales Report? (yes)");
            String userInput = stdin.readLine();
            if(!userInput.equalsIgnoreCase("yes"))
                break;
            }
    }
    

    To clean the code up, one could create a method to grab the value for you. So for your Transaction value you could create method like so

    public double getSalesReportTransaction()
    {
        System.out.print("Enter Transaction Amount: ");
        return Double.parseDouble(stdin.readLine());
    }
    

    creating a method for each value in your Sales Report would be a good way to clean the code up inside your for loop.

    Finally, I would suggest making a Class for your Sales Report and create a container of those Sales Report objects. But I'm not sure how much you know about classes and left it out.

    Here is a link to Java Classes

    To loop until 0 is entered for the transaction you can do the following in your else block

        while(true)
        {
            System.out.print("Enter Transaction Number: ");
            t = Integer.parseInt(stdin.readLine());
    
            if (t == 0) {
                break;
            }
            else {
                System.out.print("Enter Transaction Type: ");
                tp = stdin.readLine();
                System.out.print("Enter Transaction Amount: ");
                a = Double.parseDouble(stdin.readLine());
    
                totSales = totSales + a;
                totYtd = totYtd + a;
                empTotal = empTotal + a; 
                empBonus = empBonus + (a * 0.05);
    
                name[i] = n;
                ytd[i] = y;
                tNum[i] = t;
                type[i] = tp;
                amount[i] = a;
    
                outputUpdate();
    
                calcSalesData();
            }
        }