How to calculate total currencies - java

Asked

Viewed 68 times

-1

The program shows any amount of coins and their values. I need to create a public method that makes the total sum of coins, I’m not getting.

package primeiro;

public class ControleMoedas {
    private String nomeTitular;
    private int qtde1c;
    private int qtde5c;
    private int qtde10c;
    private int qtde25c;
    private int qtde50c;
    private int qtde1r;

    ///acrescenta as qtde de cada moeda.
    public void depositarMoeda1c(int moeda1c) {
        this.qtde1c += moeda1c;
    }
    public void depositarMoeda5c(int moeda5c) {
        this.qtde1c += moeda5c;
    }
    public void depositarMoeda10c(int moeda10c) {
        this.qtde1c += moeda10c;
    }
    public void depositarMoeda25c(int moeda25c) {
        this.qtde1c += moeda25c;
    }
    public void depositarMoeda50c(int moeda50c) {
        this.qtde1c += moeda50c;
    }
    public void depositarMoeda1r(int moeda1r) {
        this.qtde1c += moeda1r;
    }
    //calcular a qtde total das moedas. TRANCADO AQUI
    public void somarQtdeMoedas(int totalMoedas) {

    }
    //não recebem parâmetros e não retornam valores.
    public String getNomeTitular() {
        return nomeTitular;
    }
    public void setNomeTitular(String nomeTitular) {
        this.nomeTitular = nomeTitular;
    }
    public int getQtde1c() {
        return qtde1c;
    }
    public int getQtde5c() {
        return qtde5c;
    }
    public int getQtde10c() {
        return qtde10c;
    }
    public int getQtde25c() {
        return qtde25c;
    }
    public int getQtde50c() {
        return qtde50c;
    }
    public int getQtde1r() {
        return qtde1r;
    }
}

1 answer

0

Hello @En.Fabiano, good afternoon. Follow a possible approach,

package stackoverflow;

public class ControleMoedas {
   private String nomeTitular;
   private int qtde1c;
   private int qtde5c;
   private int qtde10c;
   private int qtde25c;
   private int qtde50c;
   private int qtde1r;

///acrescenta as qtde de cada moeda.
   public void depositarMoeda1c(int moeda1c) {
        this.qtde1c += moeda1c;
   }
   public void depositarMoeda5c(int moeda5c) {
        this.qtde5c += moeda5c;
   }
   public void depositarMoeda10c(int moeda10c) {
       this.qtde10c += moeda10c;
   }
   public void depositarMoeda25c(int moeda25c) {
       this.qtde25c += moeda25c;
   }
   public void depositarMoeda50c(int moeda50c) {
       this.qtde50c += moeda50c;
   }
   public void depositarMoeda1r(int moeda1r) {
      this.qtde1r += moeda1r;
   }

  public int somarQtdeMoedas() {
        return this.getQtde1c() + this.getQtde5c() + this.qtde10c + this.qtde25c + 
        this.qtde50c + this.qtde1r;
  }

  public double somarValorMoedas() {
       return (this.getQtde1c() * 0.01) + (this.getQtde5c() * 0.05) + (this.qtde10c * 0.10) + (this.qtde25c * 0.25) + (this.qtde50c * 0.50) + (this.qtde1r * 1);
}

//não recebem parâmetros e não retornam valores.
   public String getNomeTitular() {
        return nomeTitular;
   }
   public void setNomeTitular(String nomeTitular) {
       this.nomeTitular = nomeTitular;
   }
   public int getQtde1c() {
       return qtde1c;
   }
   public int getQtde5c() {
       return qtde5c;
   }
   public int getQtde10c() {
       return qtde10c;
   }
   public int getQtde25c() {
      return qtde25c;
   }
   public int getQtde50c() {
       return qtde50c;
   }
   public int getQtde1r() {
       return qtde1r;
   }

}

Well, I made some changes to the code, saw that you were using a parameter to receive the total of coins, in case I believe you wanted to carry it out of the method, but, I followed the following approach, giving the return of the method to the main class of an integer with the sum of all currencies in the following method. I took the liberty of creating another method too, which calculates the value of all the coins, in case you want to practice on some changes. Well, that’s it, I hope I helped! Hug!

public int somarQtdeMoedas() {
    return this.getQtde1c() + this.getQtde5c() + this.qtde10c + this.qtde25c + 
    this.qtde50c + this.qtde1r;
 }


public double somarValorMoedas() {
   return (this.getQtde1c() * 0.01) + (this.getQtde5c() * 0.05) + (this.qtde10c * 0.10) + (this.qtde25c * 0.25) + (this.qtde50c * 0.50) + (this.qtde1r * 1);
}
  • Helped a lot!! Perfectly met and other suggestion tbm.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.