1

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);
}
8
  • 3
    Give your indexes (i1 - i5) meaningful names (nameIndex, dateIndex, transactionIndex, typeIndex, etc.)so you can track them better. Commented Apr 25, 2018 at 21:20
  • 1
    Where are the while loops? Commented Apr 25, 2018 at 21:21
  • 2
    Why do you break inside the i5 loop? You may as well just use if (i3 > 0) instead of the for loop, since it won't loop. Commented Apr 25, 2018 at 21:23
  • it is difficult to understand what it is you actually wish to implement. can you provide the actual assignment text you were given ? Commented Apr 25, 2018 at 21:31
  • 2
    This badly needs a redesign. Commented Apr 25, 2018 at 21:32

1 Answer 1

1

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();
        }
    }
Sign up to request clarification or add additional context in comments.

7 Comments

ok, i am just a beginner so this is a little over my head haha. Thank you for your help. I have rewritten the code and will post it above. Seems to be working better now, but it loops back to the name after entering the transaction amount, and i want it to loop only back to transaction number until a 0 is entered, then it should go back to the name.
the issue with have multiple transaction numbers for a single sales report and using arrays to store the values is that you'll lose track of what transaction numbers correspond to which sales report. If you want to have multiple transaction numbers for a single sales report then creating a Sales Report class would be the way to go.
looking at the classes link you posted seems like it would make it easier, but the teacher specifically says that we cannot use stuff that we haven't learned yet... this is basically a beginner class, so it has to be simple even if there are better solutions. I reposted my code above to what i have now. Do you know how i can have it only loop back to trans number until 0 is entered? Is it possible to put something like: else do { code } while (tNum != 0)
added some code to help you loop until a 0 is entered for a transaction number
thank you so much mate, will look at this when i get home, but i'm definitely getting closer! Appreciate all of your help!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.