Está en la página 1de 7

Aleix Collado Serrano

Pr-01-1
A)

CalculoInteresAnual.java
package practica;

import java.util.Scanner;

public class CalculoInteresAnual {

public static void main(String[] args) {


mostrar(calculo(introducir()));

public static Datos introducir(){


while(true){
try{
Datos d = null;
Scanner tmp = new Scanner(System.in);
System.out.println("Por favor, introduzca la cantidad a depositar: ");
int a = tmp.nextInt();
System.out.println("Por favor, introduzca los años de duración del
depósito: ");
int b =tmp.nextInt();
System.out.println("Por favor, introduzca el interés anual: ");
double c =tmp.nextDouble();
d = new Datos(a,b,c);
tmp.close();
return d;}
catch(java.util.InputMismatchException e)
{System.out.println("Error, solo pueden introducirse números.");}
}

public static Interes calculo(Datos a){


Interes i = new Interes(a);
return i;

}
public static void mostrar(Interes c){
for(int i = 0; i < c.getTotal().length;i++){
System.out.println("El año nº "+(i+1)+" ha tenido unos
beneficios netos de: "+c.getGanancias()[i]+" dejando un saldo total de:
"+c.getTotal()[i]+"\n");
}

}
Aleix Collado Serrano

Interes.java
package practica;

public class Interes {


double[] ganancias;
double[] total;
Datos d = null;

Interes(Datos e){
d = e;
ganancias = new double[d.getAños()];
total = new double[d.getAños()];
/*ganancias = */this.calcularGanancias();

//total = calcularTotal();

}
private void/*double[]*/ calcularGanancias(){
//double[] tmp = new double[this.ganancias.length];
this.ganancias[0] = (d.getInteres()/100)*d.getInversion();
total[0] = this.ganancias[0]+d.getInversion();
double guanyacumulatimenys1= 0;

for(int i = 1;i < d.getAños();i++){


/*guanyacumulatimenys1 = 0;
for (int j=0; j<i; j++)
{
guanyacumulatimenys1+=tmp[j];
}*/
guanyacumulatimenys1+=this.ganancias[i-1];
this.ganancias[i] =
(d.getInteres()/100)*(guanyacumulatimenys1+d.getInversion());
total[i]=total[i-1]+this.ganancias[i];

}
//return tmp;
}
/*private double[] calcularTotal(){
double[] tmp = new double[this.ganancias.length];
tmp[0] += (this.ganancias[0]+d.getInversion());
for(int i = 1; i < tmp.length;i++){
tmp[i] += (this.ganancias[i]+(tmp[i-1]));
}
return tmp;
}*/

public double[] getGanancias(){return ganancias;}


public double[] getTotal(){return total;}

}
Aleix Collado Serrano

Datos.java
package practica;

public class Datos {


int inversion;
double interes;
int años;

Datos(int a,int b, double c){


inversion = a;
años = b;
interes = c;

}
public void setInversion(int a){inversion = a;}
public int getInversion(){return inversion;}
public void setAños(int a){años = a;}
public int getAños(){return años;}
public void setInteres(double a){interes = a;}
public double getInteres(){return interes;}
}
Aleix Collado Serrano

B)

CalculoInteresAnual.java
package practica;

import java.util.Scanner;

public class CalculoInteresAnual {

public static void main(String[] args) {


calculo(introducir());

public static Datos introducir(){


Scanner tmp;
while(true){
try{
Datos d = null;
tmp = new Scanner(System.in);
System.out.println("Por favor, introduzca la cantidad a depositar: ");
int a = tmp.nextInt();
/*System.out.println("Por favor, introduzca los años de duración del
depósito: ");
int b =tmp.nextInt();*/
System.out.println("Por favor, introduzca el interés anual: ");
double c =tmp.nextDouble();
d = new Datos(a,c);

return d;}
catch(java.util.InputMismatchException e)
{System.out.println("Error, solo pueden introducirse números.");}
}

public static Interes calculo(Datos a){


Interes i = new Interes(a);
return i;

}
public static void mostrar(Interes c){
for(int i = 0; i < c.getTotal().length;i++){
System.out.println("El año nº "+(i+1)+" ha tenido unos
beneficios netos de: "+c.getGanancias()[i]+" dejando un saldo total de:
"+c.getTotal()[i]+"\n");
}

}
Aleix Collado Serrano

Datos.java
package practica;

public class Datos {


int inversion;
double interes;
int años;

Datos(int a, double c){


inversion = a;
años = 1;
interes = c;

}
public void setInversion(int a){inversion = a;}
public int getInversion(){return inversion;}
public void setAños(int a){años = a;}
public int getAños(){return años;}
public void setInteres(double a){interes = a;}
public double getInteres(){return interes;}
}

Interes.java
package practica;

import java.io.IOException;
import java.util.InputMismatchException;
import java.util.Scanner;

public class Interes {


double[] ganancias;
double[] total;
Datos d = null;

Interes(Datos e){
d = e;
ganancias = new double[d.getAños()];
total = new double[d.getAños()];
/*ganancias = */this.calcularGanancias();

//total = calcularTotal();

}
private void/*double[]*/ calcularGanancias(){
//double[] tmp = new double[this.ganancias.length];
this.ganancias[0] = (d.getInteres()/100)*d.getInversion();

total[0] = this.ganancias[0]+d.getInversion();
System.out.println("El año nº 1 ha tenido unos beneficios netos de:
"+getGanancias()[0]+" dejando un saldo total de: "+getTotal()[0]+"\n");
double guanyacumulatimenys1= 0;

while(true){
Aleix Collado Serrano

for(int i = 1;i < d.getAños();i++){


/*guanyacumulatimenys1 = 0;
for (int j=0; j<i; j++)
{
guanyacumulatimenys1+=tmp[j];
}*/
guanyacumulatimenys1+=this.ganancias[i-1];
arraygananciasexpand();
this.ganancias[i] =
(d.getInteres()/100)*(guanyacumulatimenys1+d.getInversion());
arraytotalexpand();
total[i]=total[i-1]+this.ganancias[i];
System.out.println("El año nº "+(i+1)+" ha tenido unos
beneficios netos de: "+getGanancias()[i]+" dejando un saldo total de:
"+getTotal()[i]+"\n");
}
if(this.preguntar())d.setAños(d.getAños()+1);else{break;}
}
//return tmp;
}
/*private double[] calcularTotal(){
double[] tmp = new double[this.ganancias.length];
tmp[0] += (this.ganancias[0]+d.getInversion());
for(int i = 1; i < tmp.length;i++){
tmp[i] += (this.ganancias[i]+(tmp[i-1]));
}
return tmp;
}*/
private void arraygananciasexpand() {
double[] newArray = new double[ganancias.length + 1];
System.arraycopy(ganancias, 0, newArray, 0, ganancias.length);
ganancias = newArray;

}
private void arraytotalexpand() {
double[] newArray = new double[total.length + 1];
System.arraycopy(total, 0, newArray, 0, total.length);
total = newArray;

}
private boolean preguntar(){
Scanner lector2 = null;

while(true){
try{
boolean respuesta = false;
lector2 = new Scanner(System.in);
System.out.println("¿Desea continuar un año más? (S/N):
");

String tmp = lector2.next();


if(tmp.equals("s") || tmp.equals("S")){
respuesta = true;
}else {respuesta = false;}
return respuesta;
}
catch(InputMismatchException c){}
finally{}
Aleix Collado Serrano

public double[] getGanancias(){return ganancias;}


public double[] getTotal(){return total;}

También podría gustarte