-1

I am writing a simple program for class. I am not sure why the math is not showing correctly? When I ask for a tax and input 0.08 it is not computing to the right number. For example entering 1234.55 as the retail amount gives a transaction amount of 1266.6483 instead of the correct amount which should be 1333.31. Any help would be appreciated... I'll post the code below

package Project03Driver;

import java.io.*;

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

class Project03
{
    BufferedReader stdin;
    int tNum, tCount, smallTnum;
    double tax, rA, tA, raTot, disTot, taTot, taAvg, smallTa;
    boolean disc;
    String store, date, dFlag, inString;

    public void appMain() throws IOException
    {
        rptInit();
        displayHeader();
        getStore();
        while(tNum != 0)
        {
            salesData();
        }
        calcAvg();
        rptOut();
    }

    void rptInit()
    {
        tNum = 1;
        tCount = smallTnum = 0;
        raTot = disTot = taTot = taAvg = 0;
        smallTa = 10000;
        stdin = new BufferedReader (new InputStreamReader(System.in));
    }

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

    void getStore() throws IOException
    {
        System.out.println("Enter Store: ");
        store = stdin.readLine();
        System.out.println("Enter Date: ");
        date = stdin.readLine();
        System.out.println("Enter Tax as Decimal (ex. .05): ");
        tax = Double.parseDouble(stdin.readLine());
    }

    void salesData() throws IOException
    {
        getTransNum();
        if (tNum != 0)
        {
            inputRa();
            calcSalesData();
            outputSalesData();
        }
    }

    void getTransNum() throws IOException
    {
        System.out.println("Enter Transaction Number: ");
        tNum = Integer.parseInt(stdin.readLine());
    }

    void inputRa() throws IOException
    {
        System.out.println("Enter Retail Amount: ");
        rA = Double.parseDouble(stdin.readLine());
    }

    void calcSalesData()
    {
        tCount = tCount + 1;
        raTot = raTot + rA;
        if (tA > 1500)
        {
            disc = true;
            dFlag = "Discounted";
        }
        else
        {
            disc = false;
            dFlag = "No";
        }

        transAmt();
        discTot();

        taTot = taTot +tA;

        smallTrans();
    }

    void transAmt()
    {
        if (disc = true)
        {
            tA = (rA * 0.95) + ((rA * 0.95) * tax);
        }
        else
        {
            tA = (rA * tax) + rA;
        }
    }

    void discTot()
    {
        if (disc = true)
        {
            disTot = disTot + (tA - rA);
        }
        else
        {
            disTot = disTot;
        }
    }

    void smallTrans()
    {
        if (tA < smallTa)
        {
            smallTa = tA;
            smallTnum = tNum;
        }
    }  

    void outputSalesData()
    {
        System.out.println("");
        System.out.println("--------------------------------------------");
        System.out.println("Transaction Number: " + tNum);
        System.out.println("Retail Amount: " + rA);
        System.out.println("Discount Flag: " + dFlag);
        System.out.println("Transaction Amount: " + tA);
        System.out.println("--------------------------------------------");
        System.out.println("");
    }

    void calcAvg()
    {
        taAvg = taTot / tCount;
    }

    void rptOut()
    {
        System.out.println("");
        System.out.println("--------------------------------------------");
        System.out.println("Store: " + store);
        System.out.println("Date: " + date);
        System.out.println("Tax Rate: " + tax);
        System.out.println("--------------------------------------------");
        System.out.println("Retail Amount Total: " + raTot);
        System.out.println("Discount Total: " + disTot);
        System.out.println("Transaction Amount Total: " + taTot);
        System.out.println("Average Transaction Amount: " + taAvg);
        System.out.println("Smallest Transaction Amount: " + smallTa);
        System.out.println("Smallest Transaction Number: " + smallTnum);
        System.out.println("--------------------------------------------");
    }
}
david
  • 17,925
  • 4
  • 43
  • 57
Jordan Hawkes
  • 75
  • 1
  • 10
  • https://stackoverflow.com/questions/588004/is-floating-point-math-broken?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – Reimeus Apr 04 '18 at 22:43
  • And https://stackoverflow.com/questions/8148684/what-is-the-best-data-type-to-use-for-money-in-java-app – jarmod Apr 04 '18 at 22:45
  • 1
    As a general java coding style suggestion, have your methods return values that other methods can use for processing. ie transAmt returns the transaction amount, not setting an external variable. Ditto the args are passed vs class vars. – Totoro Apr 04 '18 at 22:47

1 Answers1

1
if (disc = true)

should be

if (disc == true)

I think you should delete the question, because that is a typo. According to https://stackoverflow.com/help/on-topic

leonardkraemer
  • 6,573
  • 1
  • 31
  • 54
  • also, is there a way to track variables as i'm running the program to see what the program is storing for the variable? This would make it a lot easier to figure out – Jordan Hawkes Apr 04 '18 at 23:07
  • 2
    That's one of the things a debugger can help you with - you can step through the method line by line, and see the value of each variable at each step. Integrated development environments (like IntelliJ and Eclipse) have good graphical debuggers built in; if you're brave enough to work entirely on the command line, there's a debugger called jdb that's part of the JDK, but it's pretty hairy to work with. – Tom Anderson Apr 04 '18 at 23:20