简体   繁体   中英

Why is my code giving false output?

I am trying to write a program that mimics the actions of a vending machine for my CS class. I have a double array stock that represents the the number of items at a particular "slot" [my vending machine is weird and is kinda like one long vending machine with 1 column of different items]. Here is my code so far:

 public class VendingMachine
{
    // define fields here
    public static double itemPrice[];
    public static String[] itemName; 
    public static int stock[][];
    public static  int maxPerSlot;
    public static double cashAmmount;

    public VendingMachine(int numslots, int maxperslot, double cash)
    {   
        final  int numSlots = numslots;
        maxPerSlot = maxperslot;
        cashAmmount = cash;
        stock = new int[numSlots][1];

        itemPrice = new double[numSlots];
        itemName = new String[numSlots];

        // complete this method
    }

    public void setProduct(int slot, String product, double price)
    {   int Slot = slot;
        itemPrice[Slot] = price; 
        itemName[Slot] = product;
        stock[Slot][0] = 0; 

        //
    }

    public void restockProduct(String product, int quantity)
    {   
        String Product = product;
        int Quantity = quantity;

        for(int i = 0; i < itemName.length;i++){
            if (Quantity > (maxPerSlot-stock[i][0])){
            return;
            }
            if (Product.equals(itemName[i])&&Quantity < maxPerSlot){
                    stock[i][0] += Quantity;


            }else if ((maxPerSlot-stock[i][0])==0){

                continue;

            }
        }

        //Put # of products in slot that holds it and if that slot is full put the rest in the next 
        //available slot that holds that product, if all full return error.
    }

    public double getCashOnHand()
    {
        return cashAmmount; // replace this line with your code
    }

    public int getQuantity(int slot)
    {
        return stock[slot][0]; // replace this line with your code
    }

    public int getQuantity(String product)
    {   int total = 0;

        for (int i = 0; i<itemName.length;i++){
            if (product == itemName[i]){
                total += this.getQuantity(i);
            }
        }
        return total;
    }

    public boolean buyItem(int slot)
    {   int snum = slot;
        double price = 0;
        if (stock[snum][0] != 0){
            stock[snum][0]--;
        price= itemPrice[snum];
        cashAmmount += price;
        return true;
        } else {
        return false;}
        // replace this line with your code
    }
}

and the main method that runs it:

public class vmd
{
    public static void main(String[] args)
    {
        boolean success;

        // vending machine w/ 20 slots, 10 items maximum per slot, $5 cash on hand
        VendingMachine v = new VendingMachine(20, 10, 5.00);
        v.setProduct(0, "Cheesy Poofs", 0.75);
        v.setProduct(1, "Red Bull", 1.25);
        v.setProduct(2, "Cheesy Poofs", 0.75);
        v.restockProduct("Cheesy Poofs", 8);
        v.restockProduct("Red Bull", 7);
        v.restockProduct("Cheesy Poofs", 5); // 2 more go into slot 0, remaining 3 into slot 2

        success = v.buyItem(0);
        System.out.println(success); // should print "true"

        System.out.println(v.getCashOnHand()); // should print "5.75"

        System.out.println(v.getQuantity(2));// should print "9"    
        System.out.println(v.getQuantity("Cheesy Poofs")); // should print "12"
    }
}

When I run this thought I consistently get:

true
5.75
8
15 

as my out put when I am suppose to get:

true
5.75
9
12

as my output. Why is this? I am assuming it has something to do with the restockProduct() method but I can't seem to narrow it down and its really getting on my nerves. According to my CS teacher the restockProduct() method is suppose to add the given quantity of the specified product to the vending machine and Put as many of the items as possible into the first slot that has been designated to hold that particular kind of product (using setProduct()).

If not all of the items will fit into the first slot, put as many of the rest as possible into the second slot that holds that kind of product, etc. For partial credit, your method should at least be able to find the first slot designated for the specified product and put all of the items there".

You are right, restockProducts doesn't do what you want it to. Here's what you have:

public void restockProduct(String product, int quantity)
{   
    String Product = product;
    int Quantity = quantity;

    for(int i = 0; i < itemName.length;i++){
        if (Quantity > (maxPerSlot-stock[i][0])){
        return;
        }
        if (Product.equals(itemName[i])&&Quantity < maxPerSlot){
                stock[i][0] += Quantity;


        }else if ((maxPerSlot-stock[i][0])==0){

            continue;

        }
    }

So when you restock "Cheesy Poofs" , the first time, here's what happens:

  1. On loop 0, you are Cheesy Poofs, so you put 8 items in there.
  2. On loop 1, you have the wrong type, so nothing goes there
  3. On loop 2, you have Cheesy Poofs, so you put 8 there.

Somehow, you need to remember that you've put 8 in the first slot. Also, you need to have a mechanism to put some into one slot, and some into another, right now I don't see that being possible in your code.

Your problem is here:

for(int i = 0; i < itemName.length;i++){
    if (Quantity > (maxPerSlot-stock[i][0])){
        return;
}

Your first call to restock "Cheesy Poofs"

v.restockProduct("Cheesy Poofs", 8);

puts 8 items into the machine.

Your second call to restock cheesy poofs:

v.restockProduct("Cheesy Poofs", 5);

Fails to do anything. Your if statement says that if the quantity (5) is greater than maxPerSlot - current stock which is (10 - 8) or just 2, then return.

5 is greater than 2 so the method ends and nothing is added to your machine.

Additionally you need to put some sort of control in there to break out of the loop once you've added all 8 items to the machine. As it stands you're adding 8 cheesy poofs to two different slots. Once you add the 8 to the first Cheesy Poof row you should remove 8 from what you have left to stock.

I took the liberty of reconstructing that method and this is what I think you're trying to achieve:

public void restockProduct(String product, int quantity)
{   
    String Product = product;
    int Quantity = quantity;

    for(int i = 0; i < itemName.length;i++){

        if (Product.equals(itemName[i])){

            if (Quantity > (maxPerSlot-stock[i][0])){
                Quantity -= maxPerSlot-stock[i][0];
                stock[i][0] += maxPerSlot-stock[i][0];

            }
            if (Quantity <= (maxPerSlot-stock[i][0])){
                stock[i][0] += Quantity;
                return;
            }
        }
    }
}

you have several problems in your restockProduct described by others, anyway you can change you restockProduct function to this one:

public void restockProduct(final String product, int quantity)
{
    for (int i = 0; i < itemName.length; i++)
    {
        if ( (product.equals(itemName[i]) || "".equals(product) ) && (maxPerSlot - stock[i][0]) > 0)
        {
            stock[i][0] += quantity;
            if (stock[i][0] > maxPerSlot)
            {
                quantity = stock[i][0] - maxPerSlot;
                stock[i][0] = maxPerSlot;
            }
            else
                return;
        }
    }

    if (quantity > 0)
        throw new IllegalArgumentException("Cannot stock product");
}

NOTE: we're either inserting product to slot where product already is OR where no products at all NOTE2: after inserting we're checking is there any rest we should insert further or everything is here

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