javaobjectif-statementinstantiationcustom-object

I don'.t get the correct output in PocketTester


So this project that I started has three classes and each one does a specific thing. The Coin class that can be instantiated with the (String) chosen, Dollar, Quarter, Nickel, Dime, and Penny. Every coin obviously has a different value in cents and will return the value to the caller. My Pocket class instantiate serveral coin objects and use it as a custom object/type as instance fields. Then my last class called PocketTester creates an object called myPocket and will have an input of 5 quarters, 3 dimes, 2 nickels, and 7 cents and prints out the total value.

Output: 172 cents

Here are my classes but for some reason when i run main() it gives me 132 cents instead of 172 cents and I don't know why. I pretty sure I called the variables correctly. Could someone help fix this.

Btw you might ask me why I didn't do this in one class and imported Scanner and used that, I just felt like doing it this way.

And the first time I posted this someone decided to just flag this as a duplicate or repost when this was never a DUPLICATE OR REPORT SO DON'T.

public class Coin
{
    private int value;
    public int dollar;
    public int quarter;
    public int dime;
    public int nickel;
    public int penny;
    public Coin(String s){
        //Use if statement to identify incoming string and provide value in cents
        if(s.equals("Dollar")){
            dollar = 1;
        }
        else if (s.equals("Quarter")){
            quarter = 25;
        }
        else if (s.equals("Dime")){
            dime = 10;
        }
        else if (s.equals("Nickel")){
            nickel = 5;
        }
        else if(s.equals("Penny")){
            penny = 1;
        }
        else{
         
            System.out.println("Give me an actual coin");
        }
        
    }
  
    public int getValue()
    {
        return value;
    }
}



public class Pocket
{
   private int currentValue;
   private int totalValue;
   public int dollar;
   public int quarter;
   public int dime;
   public int nickel;
   public int penny;
   //You need to add more custom type instance variables here
   public Pocket(){  //Set initial value to zero
    totalValue = 0;
    currentValue = 0;
    }
   public void addCoin(String s, int i){
    // s is type of coin, you are using s to instantiate a Coin and get value
    // i is number of coins, you are using i to keep adding value to the totalValue
    if(s == "Dollar" || s == "Quarter" || s == "Dime" || s == "Nickel" || s == "Penny" && i == 0){
        
       System.out.println(" Input an actual Coin ");
       
    }
    if(s == "Quarter" && i == 5){
        
       
       quarter = 125;
    }
    if(s == "Dimes" && i == 3){
        
       
       dime = 30;
    }
    if(s == "Nickels" && i == 2){
        
       
       nickel = 10;
    }
    if(s == "Penny" && i == 7){
        
       
       penny = 7;
    }
   
    currentValue = quarter + dime + nickel + penny;
    
    }
   public int getValue(){
    return totalValue;
    }
   public void printTotal(){
        System.out.println(currentValue+ " cents");
        System.out.println();
    }
}




public class PocketTester
{
    public static void main(String args[])
    {
        Pocket myPocket = new Pocket();
        myPocket.addCoin("Quarter", 5);
        myPocket.addCoin("Dime", 3);
        myPocket.addCoin("Nickel", 2);
        myPocket.addCoin("Penny", 7);
        myPocket.printTotal();
    }
}





Solution

  • If your goal is to write code that only works properly in the exact, specific test case you are using, then what you have should work fine.

    The reason you're getting 132 instead of 172 is because you are only counting the 5 quarters (125) and the 7 pennies (7). 125 + 7 = 132.

    Why? Because in your addCoin() method you are checking for string matches. You add the nickels if the string matches "Nickels", but your test code is passing in "Nickel", without the s, so it's not being added. Same case with pennies.

    All this being said, I would highly recommend you look into how to use variables some more, as you're using several variables that are entirely unnecessary. You're also limiting your code to only the exact test case you're using by checking that the integer you pass to addCoin() matches your pre-set values; if you simply use i and multiply by the value of whatever coin is added (25 for quarter, etc.) you can have this code work no matter what amount of coins you add.

    I would also recommend using equals() instead of == when checking for string matches, which you're doing in the Coin class but not in the Pocket class; you're only seeing any success here because all of your Strings are literals.