无法从另一个类创建类对象

时间:2019-11-02 00:36:30

标签: java class object

我正在尝试使用addBranch方法从Bank类创建一个分支对象,但是似乎未创建该对象,因为Branch构造函数中的printOut打印类似

“创建分支空”。使用银行类中的相应方法打印分支机构列表可以确认这一点,因为该列表为空。

怎么了?

//主要

    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();

    }


}

//银行类

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));
    }
}

//分支类别

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;
    }
}

//客户

    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);
    }
}

1 个答案:

答案 0 :(得分:3)

您错了-您正在使用以下方法创建对象:

Branch newBranch= new Branch(branchName);

因此创建对象不是问题。相反,问题在于创建对象后您什么都不做-创建对象后必须将其放入ArrayList中,以便程序的其余部分可以访问它:

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

在编程上,这相当于在商店购买杂货,但忘记带回家。不要这样做,而要记住将物品带回家。