Private Integer Variable not updating more after being updated once (Java)

Ryan S :

I'm writing a program where the user can enter an amount (as a sale), enter whether they want to pay with cash or card and then ask if they wish to enter another amount (just to practice programming in Java). Whenever I enter an amount and choose cash, it should tell me the number of cash sales that have been recorded so far.

However, whenever I add another amount after adding a first, the count of cash sales still stays the same (1). Also, the total amount of cash / card transactions should also display whenever the user no longer wishes to enter any more transactions. Another problem with this is that the total amount of cash doesn't add up and only displays the last entered value. Any help to fix these problems would be greatly appreciated.

Here is the code for both classes:

SalesRegister.java

package Week2;

public class SalesRegister {
    private int numCashSales;
    private double valueCashSales;
    private int numCardSales;
    private double valueCardSales;
    private String transactions;

    public SalesRegister() {
        this.numCashSales = 0;
        this.valueCashSales = 0.0;
        this.numCardSales = 0;
        this.valueCardSales = 0.0;
        this.transactions = "";
    }

    public int getNumCashSales() {
        return this.numCashSales;
    }

    private void addTransaction(boolean isCash, double amount) {
        if (isCash) {
            transactions += "CASH SALE\t";
        } else {
            transactions += "CARD SALE\t";
        }
        transactions += amount + "\n";
    }

    public void cashSale(double saleValue) {
        if (saleValue > 0.0) {
            System.out.println("Cash sale recorded.");
            valueCashSales += saleValue;
            numCashSales++;
            System.out.println(numCashSales);
            addTransaction(true, saleValue);
        }
    }

    public void cardSale(double saleValue) {
        if (saleValue > 0.0) {
            numCardSales++;
            valueCardSales += saleValue;
            addTransaction(false, saleValue);
        }
    }

    public String getTransactions() {
        String result = "";
        result += "Cash Sales: " + numCashSales + " Total: " + valueCashSales + "\n";
        result += "Card Sales: " + numCardSales + " Total: " + valueCardSales + "\n";
        result += "Transaction List \n";
        result += "+--------------+ \n";
        result += transactions + "\n";
        return result;
    }
}

saleTest.java

package Week2;

import java.util.Scanner;

public class saleTest {

    public static void main(String[] args) {
        double amount = 0;
        int i = 0;
        int y = 0;
        boolean isCash = false;
        Scanner in = new Scanner(System.in);
        i = 0;
        do {
            i = 0;
            System.out.print("Please enter the amount: ");
            amount = in.nextDouble();

            SalesRegister register = new SalesRegister();
            while (i == 0) {
                System.out.print("Pay with cash or card? ");
                String ans = in.next();

                if (ans.equalsIgnoreCase("cash")) {
                    System.out.println("Cash selected.");
                    isCash = true;
                    register.cashSale(amount);
                    i = 1;
                } else if (ans.equalsIgnoreCase("card")) {
                    isCash = false;
                    System.out.println("Card selected.");
                    register.cardSale(amount);
                    i = 1;
                } else {
                    System.out.println("PLEASE ENTER A VALID OPTION, ENTER 'card' OR 'cash'");
                    i = 0;
                }

                if (i != 0) {
                    System.out.println("Would you like to add another amount?  ('yes' or 'no')");
                    String a = in.next();
                    if (a.equalsIgnoreCase("yes")) {
                        y = 1;
                        System.out.println(a);
                    } else if (a.equalsIgnoreCase("no")) {
                        y = 0;
                        System.out.println("\n\n\n" + register.getTransactions());
                    } else {
                        System.out.println("PLEASE ENTER A VALID OPTION, ENTER 'yes' OR 'no'");
                    }
                }
            }
        } while (y == 1);
    }
}

Finally, here is the output window from the console: enter image description here

Turamarth :

You are creating a new SalesRegister object with every iteration of the loop and thereby resetting the stored values.
You need to move the constructor before the do-while-loop i.e.

Scanner in = new Scanner(System.in);
SalesRegister register = new SalesRegister();
i=0;
do {
    i=0;
...

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324469&siteId=1