Object array in JAVA

Asked

Viewed 1,504 times

-1

You guys talk beauty ? So, I’m a beginner in java development and my first cruel question that I faced in a question in the course was the following.

"Register data from a current account or savings account. Ask the user to enter each information requested. After entering the data, print the value of each attribute of the instantiated class.

Also create Withdraw, Deposit, Print Data, Apply Yield methods.

Attributes:

Current Account has: Agency, Account Number, Balance, Name of Holder, Limit;

Savings: Agencia, Nr_conta, Saldo, Nome do Titular,;

Create a Main class with Void Main(),

Make a menu for:

Menu

1 - Register Current Account

2 - Register Savings

3 - Current Account Withdrawal

4 - Withdrawal of Savings

5 - Current Account Deposit

6 - Savings Deposit

7 - Current Account Balance

8 - Savings Balance

9 - Application of Income to Savings

10- to get out

Make class diagram and program.

Leave class attributes as private, create gets and sets methods.

Do the other methods, according to the menu."

So far so good, I did everything correctly, and the teacher already corrected, then he asked me the following.

"Ask the question above and store each object in an array.

Tip: as we have a vector that is a set of objects, before performing any operation you must locate, so perform the task. Also create a variable to control the index of the last stored object.

Also make the method inform the amount of current account or savings registered.

Limit the vector to 20 position for each object type." package aula_21desafio;

import java.util.Scanner;

public class Program {

public static void main(String[] args) {

    Scanner read = new Scanner(System.in);

    int opt;

    ContaCorrente[] cc = new ContaCorrente[20];
    ContaPoupança[] cp = new ContaPoupança[20];

    do {

        System.out.println("\tMenu\n");
        System.out.println("Escolha abaixo qual opção desejada:\n");
        System.out.println("1 - Cadastrar Conta Corrente");
        System.out.println("2 - Cadastrar Conta Poupança");
        System.out.println("3 - Sacar Conta Poupança");
        System.out.println("4 - Sacar Conta Corrente");
        System.out.println("5 - Depósito Conta Poupança");
        System.out.println("6 - Depósito Conta Corrente");
        System.out.println("7 - Saldo Conta Poupança");
        System.out.println("8 - Saldo Conta Corrente");
        System.out.println("9 - Aplicação de Rendimentos na Poupança");
        System.out.println("10 - Sair");
        System.out.print("Opção: ");

        opt = Integer.parseInt(read.nextLine());

        switch (opt) {

        case 1:
            System.out.println("Você escolheu conta corrente");

            System.out.println("Qual o seu nome? ");
            String nomeDoTitular = read.nextLine().trim();

            System.out.println("Digite o número da agência: ");
            String agencia = read.nextLine();

            System.out.println("Digite o número da conta: ");
            String numeroDaConta = read.nextLine();

            System.out.println("Qual o limite desejado? ");
            double limite = Double.parseDouble(read.nextLine().replace(",", "."));

            System.out.println("Digite um valor inicial a ser depositado: ");
            double saldo = Double.parseDouble(read.nextLine().replace(",", "."));

            for(int i =0;i<=cc.length;i++) {
            cc[i]= new ContaCorrente(nomeDoTitular, agencia, numeroDaConta, limite, saldo);         
            cc[i].exibirInfos();
            break;
            }
            break;

When I try to access any Dice other than 0, I get Nullpointerexception, because it does not save each account in each Dice ?

2 answers

0

You have a break; inside the loop, this causes the loop to be stopped in the first iteration and added only cc[0], remove this break to add to all array contents

But why fill an array with the same data? It would no longer make sense to add just one and, when the user requests, add another

Note: The Java nomenclature standard does not use accents, i.e., instead of ContaPoupança place ContaPoupanca

  • Exactly this was my doubt, except break, it creates an equal account in all indices. Thanks for the tip of "Savings account" !

  • I don’t remember much of Java, I haven’t used it for a long time. But I think you need an Arraylist, which has the method add to add items, from one studied in arrays

-2

I believe your for is only adding cc at 0 because of the break in. However, several equal accounts will be created in the array.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.