One method taking information from another method

Asked

Viewed 71 times

0

Hello, I would like a help. I’m redoing the object-oriented java workbook from Hidden Object and there came a question that I would like a help.

In the booklet you ask to create a simple system of a bank. Going straight to the point, an account class was created that has attributes like the client name, limit balance these things. It has the method sacar (has return of type boolean, as checks the balance of the customer at the time of drawing), deposits and transfer.

I created a method called extrato in which it shows the name of the account owner, the balance and the operations he has made. I would like him to take a boolean of the method saca and print a message based on that information.

Follow the code below:

public class Conta{
  int numero;
  String dono;
  double saldo;
  double limite;

  //métodos
  boolean saca(double valor) {
        if (this.saldo < valor) {          
            return false;
    } else {
        this.saldo = this.saldo - valor;        
        return true;
    }
  }
    void deposita(double quantidade) {
        this.saldo += quantidade;
 }
 boolean transfere(Conta destino, double valor) {
   boolean retirou = this.saca(valor);
   if (retirou == false) {
     // não deu pra sacar!
     return false;
   } else {
     destino.deposita(valor);
     return true;
   }
}

 void extrato(){
   System.out.println("Nome do Cliente: " +this.dono);
   if(this.saca(valor) == true){
    System.out.println("Saque de "+this.dono+" realizado com Sucesso!!!");
   } else{
    System.out.println("Saldo de "+this.dono+" está Insuficiente");
   }

   System.out.println("Saldo atual: " +this.saldo);
 }
}
public class Programa {
    public static void main(String[] args) {
    Conta minhaConta = new Conta();
    Conta minhaConta2 = new Conta();
    minhaConta.dono = "Daniel Araújo";
    minhaConta2.dono = "Maria Araújo";
    minhaConta.saldo = 1000.0;
    minhaConta2.saldo = 1500.0;
    // saca 200 reais
    minhaConta.saca(2000);
    minhaConta2.saca(300);
    // deposita 500 reais
    minhaConta.deposita(500);
    minhaConta2.deposita(100);
    //transfere
    minhaConta.transfere(minhaConta2, 250);
    minhaConta.extrato();
    minhaConta2.extrato();
    }
}

Small project github link

  • 1

    Your code has several language flaws (poor identation, == true and == false, forgot the modifier private in the fields and public in methods, etc.) but his structure is plausible to what he proposes. However, it has not become clear to me one thing: What is your question anyway? What is it that you’re asking? What kind of help do you want with this code?

  • I know you’re full of mistakes, but it was a simple question that a friend here answered. This was not a serious project, it was just a question that arose in my studies.

1 answer

1


import java.util.ArrayList;
import java.util.List;

public class Conta {

    int numero;
    String dono;
    double saldo;
    double limite;
    List<String> operacoesRealizadas = new ArrayList<String>();

    public static void main(String[] args) {
        Conta minhaConta = new Conta();
        Conta minhaConta2 = new Conta();
        minhaConta.dono = "Daniel Araújo";
        minhaConta2.dono = "Maria Araújo";
        minhaConta.saldo = 1000.0;
        minhaConta2.saldo = 1500.0;
        // saca 200 reais
        minhaConta.saca(2000);
        minhaConta2.saca(300);
        // deposita 500 reais
        minhaConta.deposita(500);
        minhaConta2.deposita(100);
        //transfere
        minhaConta.transfere(minhaConta2, 250);
        minhaConta.extrato();
        minhaConta2.extrato();
    }

    //métodos
    boolean saca(double valor) {
        if (this.saldo < valor) {
            return false;
        } else {
            this.saldo = this.saldo - valor;
            operacoesRealizadas.add("Saque de R$: " + valor + " Realizado");
            return true;
        }
    }

    void deposita(double quantidade) {
        this.saldo += quantidade;
        operacoesRealizadas.add("Deposito de R$: " + quantidade + " Realizado");
    }

    boolean transfere(Conta destino, double valor) {
        boolean retirou = this.saca(valor);
        if (retirou == false) {
            // não deu pra sacar!
            return false;
        } else {
            destino.deposita(valor);
            operacoesRealizadas.add("Transferencia de R$: " + valor + " Realizado");
            return true;
        }
    }

    void extrato() {
        System.out.println("Nome do Cliente: " + this.dono);
        for (int i = 0; i < operacoesRealizadas.size(); i++) {
            System.out.println("" + operacoesRealizadas.get(i));
        }
        System.out.println("Saldo atual: " + this.saldo);
    }
}

Browser other questions tagged

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