I would like to fill the "movement" array with all cash receipts and withdrawals (maximum 10), every time a deposit or withdrawal of money is made, it is saved inside the array but it is not printed, I know it is wrong, Could someone recommend me what I can do to get what I want?
public class CuentaBancaria {
private String iban, titular;
private double saldo;
private double[] movimiento = new double[10];
CuentaBancaria() {
this.iban = "ES6621000418401234567891";
this.saldo = 0.00;
}
// getter y setter
public String getiban() {
return iban;
}
public String gettitular() {
return titular;
}
public double getsaldo() {
if (saldo < 0) {
System.out.println("[AVISO] SALDO NEGATIVO");
}
return saldo;
}
public void setiban(String iban) {
this.iban = iban;
}
public void settitular(String titular) {
this.titular = titular;
}
public void setsaldo(Double saldo) {
this.saldo = saldo;
}
public void ingreso(Double cantidad) {
if (cantidad > 3000.00) {
System.out.println("\n[AVISO] Se notificará a hacienda");
}
System.out.println("Ha ingresado " + cantidad + " Euros.");
saldo = saldo + cantidad;
// ingresar la cantidad en el vector
}
public void retirada(Double cantidad) {
if (cantidad > 3000.00) {
System.out.println("\n[AVISO] Se notificará a hacienda");
}
if (saldo > 50) {
saldo = saldo - cantidad;
System.out.println("Ha retirado " + cantidad + " Euros.");
} else {
System.out.println("[ERROR] INGRESE DINERO");
}
}
public void movimientoingreso(Double cantidad) {
for (int i = 0; i < movimiento.length; i++) {
movimiento[i] = cantidad;
}
}
public void movimientos() {
for (int i = 0; i < movimiento.length; i++) {
System.out.println(movimiento[i]);
}
}
You can do this using a counter that we'll call movePosition .
Each time the deposit () or withdraw() method is executed, you will add +1 to the position:
To save the amount of the movement we will use positionMovement to indicate in which position of the vector we should save it:
Being that way:
The problem here is that being
movimiento
an array of primitive objects, you have to know in advance the position where you want to place each value.This could be easily achieved by having a property of the class that controls the number of moves, and you would use that property, both to decide if the transaction limit has not been exhausted, and to record the eventual transaction in that index.
Also, this will help to decide on a loophole that is not expressed in your code at all: do failed attempts count as transactions or not? .
Another thing not controlled in your code is withdrawals. As you have designed your class, as long as I have more than 50 euros, I can withdraw 2,999 euros! The first to go to jail will be the programmer!!!
I have added other improvements in your class, since it is convenient to have a clean code, using for example methods for verifications.
Also, it looks like you were trying two login methods.
I have put comments in various parts of the code that you may find useful.
The class would look like this:
demonstration:VER EN REXTESTER
And a demo from the
main
:Departure:
Postscript
In your class you are not respecting Java's own naming convention . Although this is not mandatory, nor does it cause errors in the code, it should be known and put into practice. For more details you can see the question: What is the convention for writing variables in Java? .