mardi 27 octobre 2020

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();
    }
}




Aucun commentaire:

Enregistrer un commentaire