Múltiplos Empréstimos

Classe Emprestimo

Retomando o exercício anterior, considere a seguinte classe que representa um empréstimo.


In [1]:
public class Emprestimo {
   float s;
   int   n;
   float j;
   int   corrente;
   float p;

   Emprestimo(float s, int n, float j) {
      this.s = s;
      this.n = n;
      this.j = j;
      corrente = 1;
      this.p = s;
   }

   float proximaParcela() {
      float retorno = p;
      corrente++;
      if (corrente <= n)
         p = p + (p * (j/100));
      else
         p = 0;
      return retorno;
   }
}


Out[1]:
com.twosigma.beaker.javash.bkrdb720e50.Emprestimo

Dois objetos Emprestimo

A aplicação a seguir ilustra como a classe pode ser usada para se criar dois objetos Emprestimo.


In [2]:
Emprestimo emprestimoA = new Emprestimo(200, 5, 1),
           emprestimoB = new Emprestimo(500, 7, 2);

int i = 1;
float pA = emprestimoA.proximaParcela();
float pB = emprestimoB.proximaParcela();
while (pA > 0 || pB > 0) {
   if (pA > 0)
      System.out.println("Emprestimo A: parcela " + i + " eh " + pA);
   if (pB > 0)
      System.out.println("Emprestimo B: parcela " + i + " eh " + pB);
   pA = emprestimoA.proximaParcela();
   pB = emprestimoB.proximaParcela();
   i++;
}


Emprestimo A: parcela 1 eh 200.0
Emprestimo B: parcela 1 eh 500.0
Emprestimo A: parcela 2 eh 202.0
Emprestimo B: parcela 2 eh 510.0
Emprestimo A: parcela 3 eh 204.02
Emprestimo B: parcela 3 eh 520.2
Emprestimo A: parcela 4 eh 206.06021
Emprestimo B: parcela 4 eh 530.604
Emprestimo A: parcela 5 eh 208.12082
Emprestimo B: parcela 5 eh 541.21606
Emprestimo B: parcela 6 eh 552.0404
Emprestimo B: parcela 7 eh 563.08124
Out[2]:
null

Vetor de objetos Emprestimo

O vetor do Java pode ser usado para se armazenar ponteiros para múltiplos objetos Emprestimo. Para isso, basta se criar um vetor do tipo Emprestimo. O exemplo a seguir mantém cinco ponteiros para objetos empréstimo.


In [3]:
Emprestimo vEmprestimos[] = new Emprestimo[5];

vEmprestimos[0] = new Emprestimo(200, 3, 1);
vEmprestimos[1] = new Emprestimo(500, 4, 2);
vEmprestimos[2] = new Emprestimo(300, 2, 1);
vEmprestimos[3] = new Emprestimo(450, 3, 2);
vEmprestimos[4] = new Emprestimo(700, 2, 3);

int i = 1;
boolean temParcela = true;
while (temParcela) {
   temParcela = false;
   for (int e = 0; e < 5; e++) {
      float p = vEmprestimos[e].proximaParcela();
      if (p > 0) {
         temParcela = true;
         System.out.println("Emprestimo " + e + ": parcela " + i + " eh " + p);
      }
   }
   i++;
}


Emprestimo 0: parcela 1 eh 200.0
Emprestimo 1: parcela 1 eh 500.0
Emprestimo 2: parcela 1 eh 300.0
Emprestimo 3: parcela 1 eh 450.0
Emprestimo 4: parcela 1 eh 700.0
Emprestimo 0: parcela 2 eh 202.0
Emprestimo 1: parcela 2 eh 510.0
Emprestimo 2: parcela 2 eh 303.0
Emprestimo 3: parcela 2 eh 459.0
Emprestimo 4: parcela 2 eh 721.0
Emprestimo 0: parcela 3 eh 204.02
Emprestimo 1: parcela 3 eh 520.2
Emprestimo 3: parcela 3 eh 468.18
Emprestimo 1: parcela 4 eh 530.604
Out[3]:
null

Exercício: Classe para Conjunto de Empréstimos

Classe ConjuntoEmprestimos

Escreva uma classe denominada ConjuntoEmprestimos que será responsável por controlar um conjunto de empréstimos. Essa classe deve definir pelo menos os seguintes métodos:

  • construtor - recebe como parâmetro o número máximo de empréstimos;
  • adicionaEmprestimo - recebe como parâmetro um objeto da classe empréstimo e o armazena (se não ultrapassar o número máximo);
  • proximasParcelas - mostra as próximas parcelas de todos os empréstimos cadastrados (para fins de simplificação, considere que o número da próxima parcela é igual para todos); o método retorna um status de verdadeiro se houve pelo menos um empréstimo com próxima parcela.

In [4]:
public class ConjuntoEmprestimos {
   Emprestimo vEmprestimos[];
   int corrente = -1;
   
   ConjuntoEmprestimos(int quantidade) {
      vEmprestimos = new Emprestimo[quantidade];
   }
   
   boolean adicionaEmprestimo(Emprestimo emp) {
      boolean resultado = false;
      if (corrente+1 < vEmprestimos.length) {
         corrente++;
         vEmprestimos[corrente] = emp;
         resultado = true;
      }
      return resultado;
   }
   
   boolean proximasParcelas() {
      boolean status = false;
      for (int e = 0; e < vEmprestimos.length; e++) {
         float p = vEmprestimos[e].proximaParcela();
         if (p > 0) {
            status = true;
            System.out.println("Emprestimo " + (e+1) + ": " + p);
         }
      }
      return status;
   }
}


Out[4]:
com.twosigma.beaker.javash.bkrdb720e50.ConjuntoEmprestimos

Programa com cinco empréstimos

Escreva um programa que crie um objeto da classe ConjuntoEmprestimo, crie cinco objetos da classe Emprestimo e os cadastre na classe ConjuntoEmprestimo. Ao final, mostre as parcelas de todos os empréstimos usando ConjuntoEmprestimo.


In [5]:
ConjuntoEmprestimos ce = new ConjuntoEmprestimos(5); 

ce.adicionaEmprestimo(new Emprestimo(200, 3, 1));
ce.adicionaEmprestimo(new Emprestimo(500, 4, 2));
ce.adicionaEmprestimo(new Emprestimo(300, 2, 1));
ce.adicionaEmprestimo(new Emprestimo(450, 3, 2));
ce.adicionaEmprestimo(new Emprestimo(700, 2, 3));

boolean status;
do {
   status = ce.proximasParcelas();
} while (status);


Emprestimo 1: 200.0
Emprestimo 2: 500.0
Emprestimo 3: 300.0
Emprestimo 4: 450.0
Emprestimo 5: 700.0
Emprestimo 1: 202.0
Emprestimo 2: 510.0
Emprestimo 3: 303.0
Emprestimo 4: 459.0
Emprestimo 5: 721.0
Emprestimo 1: 204.02
Emprestimo 2: 520.2
Emprestimo 4: 468.18
Emprestimo 2: 530.604
Out[5]:
null