0

I'm trying to create a branch Object from Bank class using the addBranch method, but it seems the object is not created since the printOut in the Branch constructor prints something like

"Branch empty created" . Printing a branch list using the corresponding method in Bank class confirms this, as the list is empty.

What's wrong?

//Main

    import java.util.Scanner;

public class Main {

    public static Scanner scanner = new Scanner(System.in);
    private static Bank banco = new Bank("Banco Central");

    public static void main(String[] args) {

        printOptions();

        boolean quit = false;
        while (!quit) {
            System.out.println("6. Imprimir lista de opciones");
            int option = scanner.nextInt();
            switch (option) {
                case 0:
                    quit = true;
                    break;
                case 1:
                    addBranch();
                    break;
                case 2:
                    printBranches();
                    break;
                case 6:
                    printOptions();
                    break;
            }
        }
    }

    public static void printOptions() {
        System.out.println("Enter option");
        System.out.println("0. Quit");
        System.out.println("1. Agregar sucursal");
        System.out.println("2. Imprimir sucursales");
        System.out.println("3. Agregar cliente nuevo a sucursal con transaccion inicial");
        System.out.println("4. Agregar transaccion a cliente existente");
        System.out.println("5. Imprimir lista de clientes de sucursal"); //agregar opcional de mostrar transacciones

    }

    public static void addBranch() {
        System.out.println("Ingrese el nombre de la sucursal");
        String name = scanner.nextLine();
        scanner.nextLine();

        banco.addBranch(name);
    }

    public static void printBranches() {
        banco.printBranches();

    }


}

//Bank class

import java.util.ArrayList;

public class Bank {

    private String name;
    private ArrayList<Branch> branches = new ArrayList<Branch>();

    //CONSTRUCTOR
    public Bank(String name) {
        this.name = name;
        this.branches = new ArrayList<Branch>();
    }

    public void addBranch(String branchName) {
        Branch newBranch = new Branch(branchName);
        branches.add(newBranch);
        //System.out.println(nuevaSucursal.getName() + "creada");

    }


    //2

    public void printBranches() {
        System.out.println("Lista de sucursales");
        for (int i = 0; i < this.branches.size(); i++) {
            System.out.println(this.branches.get(i).getName());

        }


    }


    //4
    public void addTransaction(Client client, double transaccion) {
        Branch.addTransaction(client, transaccion); //al ser STATIC no hace falta crear un Objeto Sucursal para poder llamarlo

    }


    //GETTERS & SETTERS

    public ArrayList<Branch> getBranches() { //devolver sout mejor?
        return branches;
    }

    public void addSucursal(String sucursal) {
        this.branches.add(new Branch(sucursal));
    }
}

//Branch class

import java.util.ArrayList;

public class Branch {

    private String branchName;
    ArrayList<Client> clients;

    public Branch(String name) {
        this.branchName = name;
        this.clients = new ArrayList<Client>();
        System.out.println("Sucursal " + name + " creada.");
    }

    public void agregarCliente(String name, double initialTransaction) {
        clients.add(new Client(name, initialTransaction));
        System.out.println("Cliente " + name + " agregado a esta sucursal. Transaccion inicial: " + initialTransaction);
    }

    public static void addTransaction(Client client, double transaccion) {
        client.addTransactions(transaccion); //al ser STATIC no hace falta crear un Objeto Sucursal para poder llamarlo

    }

    public static Branch agregarSucursal(String nombre) {
        return new Branch(nombre);
    }

//GETTERS SETTERS

    public String getName() {
        return this.branchName;
    }
}

//Client

    import java.util.ArrayList;

public class Client {

    private String clientName;
    ArrayList<Double> transactions = new ArrayList<Double>();

    public Client(String clientName, double initialTransactions) {
        this.clientName = clientName;
        this.transactions.add(initialTransactions);
        System.out.println("Cliente creado");
    }

    public String getClientName() {
        return clientName;
    }

    public void setClientName(String clientName) {
        this.clientName = clientName;
    }

    public ArrayList<Double> getTransactions() {
        return transactions;
    }

    public void addTransactions(double transaction) {
        this.transactions.add(transaction);
    }
}
pinkfloyd90
  • 163
  • 7

1 Answers1

3

You're mistaken -- you're creating the object just fine with this:

Branch newBranch= new Branch(branchName);

And so object creation is not the problem. Rather, the problem is that you're just doing nothing with the object after you have created it -- you must put it into the ArrayList after creation so that it is accessible to the rest of the program:

public void addBranch(String branchName) {
   Branch newBranch= new Branch(branchName);
   branches.add(newBranch);
}

This is the programming equivalent of buying groceries at the store but forgetting to take them home. Don't do this and instead remember to take your objects home.

Hovercraft Full Of Eels
  • 276,051
  • 23
  • 238
  • 346
  • I understand and added that line. However the printout after the branch is created still shows empty where it should show it's name. I'm pretty sure object creation is not a problem, because printing a branch list still prints an empty line for each object created. If i create 2 branches and print a list of branches retreiving the items in the array list it prints 2 empty lines. What is even more strange is that I changed the argument in the Branch constructor to print the String name argument passed instead of this.branchName, and it also prints an empty space instead of the name passed. – pinkfloyd90 Nov 02 '19 at 02:27
  • @1990eam This certainly is a major problem in the code that you posted, but otherwise I can't possibly guess your current problem based on the incomplete picture given. In short, we need a more complete picture of your problem. You need to create and post a valid [mre] if this doesn't solve the problem. – Hovercraft Full Of Eels Nov 02 '19 at 03:18
  • Thanks, I added the rest of the code so you have the bigger picture. I realized a couple things like the arrayList being initialized twice and corrected it, but the error persists. If you could take a look I'd appreciate it. – pinkfloyd90 Nov 02 '19 at 05:48
  • @1990eam it is still missing the main method -- I cannot run the code. – Hovercraft Full Of Eels Nov 02 '19 at 12:06
  • There, I added it. I just call the class methods from there, so I don't think it's where the bug is. Let me know if you find anything, I'm very curious. And thanks. – pinkfloyd90 Nov 02 '19 at 13:31
  • @1990eam: Your *new* problem is in fact within the main method. Please look at [Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo() methods](http://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-nextint-or-other-nextfoo). Add a `scanner.nextLine()` after calling `scanner.nextInt()`, not after `scanner.nextLine()` – Hovercraft Full Of Eels Nov 02 '19 at 13:37
  • Thanks!!! What a stupid bug. I moved the scanner.nextLine before storing the empty line in the name variable and now it works. I knew I had to add that line somewhere to clean the buffer, but it never ocurred to me that was the bug. – pinkfloyd90 Nov 02 '19 at 14:07
  • @1990eam: Lesson to learn here: you stated that "I don't think it's where the bug is", when in fact the main method was the source of the bug. Never assume and always *test* – Hovercraft Full Of Eels Nov 02 '19 at 14:09