简体   繁体   中英

Why does my sum suddenly decrease?

I have a program that tracks the number of items and cost of the items that you place in a shopping bag and then prints out the number of items, total cost of the items, cost + tax, and average.

When I run the program and I continue to add items it will calculate everything accurately, but then suddenly the total cost will drop down. ex: i add 2 items at 10 dollars each it prints: Items: 2 Total Cost: 20, but then I will add 1 item at 5 dollars and it will print: Items: 3 Total Cost: 15 rather than 25.

I've tried running it multiple times. It seems like if I keep adding items at 10 dollars it is accurate, but if I change it up, the total cost decreases. It seems some what random as far as when the value decreases.

I thought maybe there was some issue with using float, but I can't find anything to verify that.

Here is the shopping bag class

public class ShoppingBag {

    public float taxRate;
    public int items;
    public float cost;
    public float average;
    public float totalCost;
    public float finalCost;

    public ShoppingBag(float taxRate)
    {
        this.taxRate = taxRate;
    }

    public void place(int newitems, float newcost)
    {
        items = newitems;
        cost = newcost;

        cost = items * cost;
    }

    public int getItems()
    {
        return items;
    }

    public float getCost()
    {
        return cost;
    }

    public float getTotal()
    {
        finalCost = cost + (cost * taxRate);
        return finalCost;
    }

    public float getAverage()
    {
        average = finalCost/items;
        return average;
    }

    @Override
    public String toString()
    {
        return("Items: " + items + " Cost: " + cost + " Final cost: " + finalCost + " Average cost: " + average);
    }
}

Here is my main program

import java.util.Scanner;
public class ShoppingBagTracker {

    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);

        float taxRate, cost;
        int items, newItems, choice;
        String receipt;
        String menu = ("1. Add items" +
        "2. Get receipt"
        + "3. Exit");

        System.out.print("Enter the sales tax rate: ");
        taxRate = in.nextFloat();
        ShoppingBag myBag = new ShoppingBag(taxRate);

        items = 0;
        do{
            System.out.println("What would you like to do?");
            System.out.println(menu);
            choice = in.nextInt();

            switch(choice){
                case 1:
                    System.out.print("Enter cost of item: ");
                    cost = in.nextFloat();     

                    System.out.print("Enter number of items: ");
                    newItems = in.nextInt();
                    items = items + newItems;

                    myBag.place(items, cost);
                    myBag.getItems();
                    myBag.getCost();
                    myBag.getTotal();
                    myBag.getAverage();
                    break;
                case 2:
                   receipt = myBag.toString();
                   System.out.println(receipt);
                   break;
                case 3:
                    break;
                default:
                    System.out.println("That is not an option");      
            }
        }while(choice != 3);            
    }    
}

Your place method overwrites the previous cost.

So when you call :

myBag.place(items, cost);

the number of items is correct (since you update it before the call), but the previous cost is lost :

public void place(int newitems, float newcost)
{
    items = newitems;
    cost = newcost;

    cost = items * cost;
} 

You should probably change it to :

public void place(int newitems, float newcost)
{
    items += newitems;
    cost += newitems * newcost;
} 

And pass to it just the new items count :

myBag.place(newitems, cost);

Have a look at the line

cost = items * cost;

cost is the cost of the most recent item added ($5) times the number of items (3), so cost is assigned 3*$5 => $15.

You will need to, either keep track of the prices of all the items added to the shopping cart, or keep a running total of the cost and add the cost of the items being added.

Your ShoppingBag.place(int,float) method doesn't add the new items and cost to the bag. It replaces what's in the bag, so the contents of the bag at any one time are only what you provided in the most recent call to place .

In the main method, you're incrementing items before calling place , but then place multiplies the incremented value of items by only the most recent value of cost .

Also, these two lines in your main method have no effect, since the called methods just return the existing value of a field, and you never assign the returned value to anything:

  myBag.getItems();
  myBag.getCost();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM