Search code examples
javainfinite-loopexit

Trouble having program terminate


I am doing a program for class. Basically the program walks through some sales information for the day and prints a report at the end.

My only issue is that the program does not seem to exit at the end. When i type "done" for the employee name it prints the report but the program loops back and continues to run asking for an employees year to date sales again.

I'm having trouble figuring out why the program is doing this. If anyone could help point me towards what is causing it to loop instead of exit so that I can fix it that would be greatly appreciated. Thanks!

import java.io.*;
import java.text.DecimalFormat;

public class Project04Driver {
    public static void main(String args[]) throws IOException {
        Project04 app;
        app = new Project04();
        app.appMain();
    }
}

class Project04 {
private static DecimalFormat df2 = new DecimalFormat ("$#,###.00");
BufferedReader stdin;   
int trans, tCount;
double ytd, amount, empTotal, empBonus, totA, totF, totS, totSales, totYtd, hiTrans, ytdSales;
String date, hiEmp, name, type;
public void appMain() throws IOException {
    rptInit();
    displayHeader();
    getDate();
    while (!name.equalsIgnoreCase("Done")) {
        salesData();
    }
}

void rptInit() {
    name = "f";
    trans = 1;
    amount = totF = totA = totS = totSales = totYtd = hiTrans = ytdSales = 0;
    stdin = new BufferedReader(new InputStreamReader(System.in));
}

void displayHeader() {
    System.out.println("--------------------------------------------");
    System.out.println("Project #04 Solution");
    System.out.println("Written by Jordan Hawkes");
    System.out.println("--------------------------------------------");
    System.out.println("");
}

void getDate() throws IOException {
    System.out.print("Enter Date: ");
    date = stdin.readLine();
}

void salesData() throws IOException {
    System.out.print("Enter Name: ");
    name = stdin.readLine();
    if (name.equalsIgnoreCase("done")) {
        rptOut();
        return;
    }

    System.out.print("Enter Year to Date Sales: ");
    ytd = Double.parseDouble(stdin.readLine());
    ytdSales = ytdSales + ytd;
    totYtd = totYtd + ytdSales;

    trans();

}

void trans() throws IOException {
    while (trans != 0) {
    System.out.print("Enter Transaction Number: ");
    trans = Integer.parseInt(stdin.readLine());

    if (trans == 0) {
        outputSalesData();
        empTotal = 0;
        empBonus = 0;
        ytdSales = 0;
        trans = 1;
        salesData();
    } else {
        System.out.print("Enter Transaction Type: ");
        type = stdin.readLine();
        System.out.print("Enter Transaction Amount: ");
        amount = Double.parseDouble(stdin.readLine());

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

        outputUpdate();

        calcSalesData();
        tCount++;
    }
}
}

void calcSalesData() {
    if (type.equalsIgnoreCase("F")) {
        totF = totF + amount;
    }
    if (type.equalsIgnoreCase("A")) {
        totA = totA + amount;
    }
    if (type.equalsIgnoreCase("S")) {
        totS = totS + amount;
    }

    hiTrans();
}

void hiTrans() {
    if (amount > hiTrans) {
        hiTrans = amount;
        hiEmp = name;
    }
}

void outputUpdate() {
    System.out.println("");
    System.out.println("--------------------------------------------");
    System.out.println(name + "'s Total Sales for Day: " + df2.format(empTotal));
    System.out.println("--------------------------------------------");
    System.out.println("");
}

void outputSalesData() {
    System.out.println("");
    System.out.println("--------------------------------------------");
    System.out.println("Employee Name: \t\t" + name);
    System.out.println("YTD Sales: \t\t" + df2.format(ytdSales));
    System.out.println("Employee Bonus: \t" + df2.format(empBonus));
    System.out.println("--------------------------------------------");
    System.out.println("");
}

void rptOut() {
     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\t" + df2.format(totS));
    System.out.println("--------------------------------------------");
    System.out.println("Total Sales for Day: \t\t" + df2.format(totSales));
    System.out.println("Total YTD: \t\t\t" + df2.format(totYtd));
    System.out.println("--------------------------------------------");
    System.out.println("Highest Trans Amount: \t\t" + df2.format(hiTrans));
    System.out.println("Employee w/ Highest Trans: \t" + hiEmp);
    System.out.println("--------------------------------------------");
    return;
}

}


Solution

  • To allow the program to exit naturally, you should not call methods in a cycle, rather allow methods to exit and control the flow from the main loop.

    Here is the redesigned approach:

    The new main loop calls salesData() and trans() in a pair (assuming name is not "done")

    while (!name.equalsIgnoreCase("Done")) {
        salesData();
        if (!name.equalsIgnoreCase("Done"))
            trans();
    }
    

    Now, method salesData() must not call trans():

    void salesData() throws IOException {
        System.out.print("Enter Name: ");
        name = stdin.readLine();
        if (name.equalsIgnoreCase("done")) {
            rptOut();
            return;
        }
        // ... processing ...
     }
    

    And also trans() must not call salesData():

    void trans() throws IOException {
       while (trans != 0) {
       System.out.print("Enter Transaction Number: ");
       trans = Integer.parseInt(stdin.readLine());
    
       if (trans == 0) {
         outputSalesData();
         empTotal = 0;
         empBonus = 0;
         ytdSales = 0;
       } else {
         System.out.print("Enter Transaction Type: ");
         type = stdin.readLine();
         System.out.print("Enter Transaction Amount: ");
         amount = Double.parseDouble(stdin.readLine());
         // ... processing ...
         outputUpdate();
         calcSalesData();
         tCount++;
       }
     }
    }
    

    Summary: Now that methods salesData() and trans() do not call each other (they return naturally after completing the method), and since the control is now from the main loop, your application will exit as expected.