Search code examples
javaarraysloopsgettergetter-setter

Java_Fill an array by getters


Trying to fill an array using getter, the problem the array is full with the same number and if the value change in the second input the hole array changes again :

public class Payment {

Scanner kb = new Scanner(System.in);
private int operationNum = 0;
private int subOperationNum = 0;
private double reMoney = 0;

public int getOperationNum() {
    return operationNum;
}

public int getSubOperationNum() {
    return subOperationNum;
}

public double getReMoney() {
    return reMoney;
}

public void setOperationNum(int operationNum) {
    this.operationNum = operationNum;
}

public void setSubOperationNum(int subOperationNum) {
    this.subOperationNum = subOperationNum;
}

public void setReMoney(double reMoney) {
    this.reMoney = reMoney;
}

public void operationsLoop() {
    double [] a = new double[17];
    do {
        System.out.println("\n1- Cash");
        System.out.println("2- Car");
        System.out.println("3- Clothing");
        System.out.println("4- Credit Card");
        System.out.println("5- Food");
        System.out.println("6- Education");
        System.out.println("7- Electronics");
        System.out.println("8- Groceries");
        System.out.println("9- Health & Fitness");
        System.out.println("10- Medical");
        System.out.println("11- Travel");
        System.out.println("12- Utilities");
        System.out.println("13- Finish");
        System.out.print("\nEnter the number of operation : ");
        this.setOperationNum(kb.nextInt());
        this.operation2();
        this.operation12();
        this.collectReMoney();
        **for(int i = 0; i<a.length;i++){
            a[i] = this.getReMoney();
            System.out.print(a[i] + ", ");
        }**
    } while (operationNum < 13);
}

public void operation2() {
    if (this.getOperationNum() == 2) {
        System.out.println("\t1- Gas");
        System.out.println("\t2- Repair");
        System.out.println("\t3- Monthly Payment");
        System.out.print("\nEnter your chose : ");
        this.setSubOperationNum(kb.nextInt());
    }
}

public void operation12() {
    if (this.getOperationNum() == 12) {
        System.out.println("\t1- Electricity");
        System.out.println("\t2- Gas");
        System.out.println("\t3- Telephone");
        System.out.println("\t4- Water");
        System.out.print("\nEnter your chose : ");
        this.setSubOperationNum(kb.nextInt());
    }
}


public void collectReMoney() {
    if (this.getOperationNum() == 1) {
        System.out.print("Withdraw = ");
        this.setReMoney(kb.nextDouble());
    } else if (this.getOperationNum() == 4 || (this.getOperationNum() == 2 && this.getSubOperationNum() == 3)) {
        System.out.print("Payment = ");
        this.setReMoney(kb.nextDouble());
    } else if (this.getOperationNum() == 9 || this.getOperationNum() == 10) {
        System.out.print("Pay = ");
        this.setReMoney(kb.nextDouble());
    } else if (this.getOperationNum() != 13) {
        System.out.print("Spend = ");
        this.setReMoney(kb.nextDouble());
    }
}

and if I add to the array loop "this.setReMoney(0);" only the first value change

when the user enter a value i want to insert it in the array according to the operation number and the rest values of the other operations should be zero


Solution

  • In the for loop you are assigning the same number to each of the elements of the array, to avoid that you should take the assigning operation outside the for loop:

    a[operationNum - 1] = this.getReMoney();
    for(int i = 0; i<a.length;i++){
        System.out.print(a[i] + ", ");
    }
    

    Also, make sure that you check for ArrayIndexOutOfBoundsException in the case the user selects an option number grater than the size of the array.