import java.util.Scanner;
public class Caixa {
static private Conta minhaConta;
static private Scanner entrada
= new Scanner
(System.
in); public static void main
(String[] args
) { int opcao = -1;
do {
System.
out.
println("\t Escolha a opção desejada"); if (minhaConta != null) {
System.
out.
println("1 - Consultar Saldo"); System.
out.
println("2 - Sacar"); System.
out.
println("3 - Depositar"); }
System.
out.
println("4 - Abrir Conta"); System.
out.
println("0 - Sair\n"); opcao = lerInt();
if (minhaConta == null && opcao != 0) opcao = 4;
switch (opcao) {
case 1:
System.
out.
println("Nome: " + minhaConta.
getNome()); System.
out.
println("Número da conta: " + minhaConta.
getConta()); System.
out.
printf("Saldo atual: %.2f\n", minhaConta.
getSaldo()); System.
out.
println("Saques realizados: " + minhaConta.
getSaques() + "\n"); break;
case 2:
if (minhaConta.podeSacar()) {
System.
out.
print("Quanto deseja sacar: "); int ok = minhaConta.sacar(lerInt());
if (ok >= 0) {
System.
out.
println("Sacado: " + ok
); System.
out.
println("Novo saldo: " + minhaConta.
getSaldo() + "\n"); } else if (ok
== -1) System.
out.
println("Não pode ser negativo\n"); else if (ok
== -2) System.
out.
println("Saldo insuficiente. Faça um depósito\n"); else if (ok
== -3) System.
out.
println("Limite de saques atingidos.\n"); } else System.
out.
println("Limite de saques atingidos.\n"); break;
case 3:
System.
out.
print("Quanto deseja depositar: "); int ok = minhaConta.depositar(lerInt());
if (ok >= 0) {
System.
out.
println("Depositado: " + ok
); System.
out.
println("Novo saldo: " + minhaConta.
getSaldo() + "\n"); } else System.
out.
print("Não pode ser negativo"); break;
case 4:
System.
out.
println("Cadastrando novo cliente."); System.
out.
print("Ente com seu nome: "); System.
out.
print("Entre com o valor inicial depositado na conta: "); minhaConta = new Conta(entrada.nextLine(), lerInt());
break;
case 0: break;
default:
System.
out.
println("Opção inválida"); }
} while (opcao != 0);
}
private static int lerInt() {
while (true) {
String lido
= entrada.
nextLine().
trim(); try {
System.
out.
println("Desculpe, mas " + lido
+ " não é um número inteiro. Tente novamente."); }
}
}
}
class Conta {
static private int ultimaConta;
private int conta, saques;
private double saldo;
public String getNome
() { return nome
; } public double getConta() { return conta; }
public double getSaldo() { return saldo; }
public int getSaques() { return saques; }
public boolean podeSacar() { return saques < 3; }
public Conta
(String nome,
int saldoInicial
) { this.nome = nome;
conta = ++ultimaConta;
saldo = saldoInicial < 0 ? 0 : saldoInicial;
}
public int sacar(int valor) {
if (valor < 1) return -1;
if (saldo < valor) return -2;
if (!podeSacar()) return -3;
saldo -= valor;
saques++;
return valor;
}
public int depositar(int valor) {
if (valor < 1) return -1;
saldo += valor;
return valor;
}
}
//https://pt.stackoverflow.com/q/262849/101
aW1wb3J0IGphdmEudXRpbC5TY2FubmVyOwoKcHVibGljIGNsYXNzIENhaXhhIHsKICAgCXN0YXRpYyBwcml2YXRlIENvbnRhIG1pbmhhQ29udGE7CiAgICBzdGF0aWMgcHJpdmF0ZSBTY2FubmVyIGVudHJhZGEgPSBuZXcgU2Nhbm5lcihTeXN0ZW0uaW4pOwogICAgcHVibGljIHN0YXRpYyB2b2lkIG1haW4oU3RyaW5nW10gYXJncykgewogICAgICAgIGludCBvcGNhbyA9IC0xOwogICAgICAgIGRvIHsKCSAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKCJcdCBFc2NvbGhhIGEgb3DDp8OjbyBkZXNlamFkYSIpOwoJICAgICAgICBpZiAobWluaGFDb250YSAhPSBudWxsKSB7CgkJICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oIjEgLSBDb25zdWx0YXIgU2FsZG8iKTsKCQkgICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbigiMiAtIFNhY2FyIik7CgkJICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oIjMgLSBEZXBvc2l0YXIiKTsKCSAgICAgICAgfQoJICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oIjQgLSBBYnJpciBDb250YSIpOwoJICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oIjAgLSBTYWlyXG4iKTsKCSAgICAgICAgU3lzdGVtLm91dC5wcmludCgiT3DDp8OjbzogIik7CiAgICAgICAgICAgIG9wY2FvID0gbGVySW50KCk7CiAgICAgICAgICAgIGlmIChtaW5oYUNvbnRhID09IG51bGwgJiYgb3BjYW8gIT0gMCkgb3BjYW8gPSA0OwoJICAgICAgICBzd2l0Y2ggKG9wY2FvKSB7CiAgICAgICAgICAgIGNhc2UgMToKICAgICAgICAgICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbigiU0FMRE8iKTsKCQkgICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbigiTm9tZTogIiArIG1pbmhhQ29udGEuZ2V0Tm9tZSgpKTsKCQkgICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbigiTsO6bWVybyBkYSBjb250YTogIiArIG1pbmhhQ29udGEuZ2V0Q29udGEoKSk7CgkJICAgICAgICBTeXN0ZW0ub3V0LnByaW50ZigiU2FsZG8gYXR1YWw6ICUuMmZcbiIsIG1pbmhhQ29udGEuZ2V0U2FsZG8oKSk7CgkJICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oIlNhcXVlcyByZWFsaXphZG9zOiAiICsgbWluaGFDb250YS5nZXRTYXF1ZXMoKSArICJcbiIpOwogICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgIGNhc2UgMjoKICAgICAgICAgICAgICAgIGlmIChtaW5oYUNvbnRhLnBvZGVTYWNhcigpKSB7CiAgICAgICAgICAgICAgICAgICAgU3lzdGVtLm91dC5wcmludCgiUXVhbnRvIGRlc2VqYSBzYWNhcjogIik7CiAgICAgICAgICAgICAgICAgICAgaW50IG9rID0gbWluaGFDb250YS5zYWNhcihsZXJJbnQoKSk7CiAgICAgICAgICAgICAgICAgICAgaWYgKG9rID49IDApIHsKICAgICAgICAgICAgICAgICAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKCJTYWNhZG86ICIgKyBvayk7CgkJCQkgICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbigiTm92byBzYWxkbzogIiArIG1pbmhhQ29udGEuZ2V0U2FsZG8oKSArICJcbiIpOwogICAgICAgICAgICAgICAgICAgIH0gZWxzZSBpZiAob2sgPT0gLTEpIFN5c3RlbS5vdXQucHJpbnRsbigiTsOjbyBwb2RlIHNlciBuZWdhdGl2b1xuIik7CiAgICAgICAgICAgICAgICAgICAgZWxzZSBpZiAob2sgPT0gLTIpIFN5c3RlbS5vdXQucHJpbnRsbigiU2FsZG8gaW5zdWZpY2llbnRlLiBGYcOnYSB1bSBkZXDDs3NpdG9cbiIpOwogICAgICAgICAgICAgICAgICAgICBlbHNlIGlmIChvayA9PSAtMykgU3lzdGVtLm91dC5wcmludGxuKCJMaW1pdGUgZGUgc2FxdWVzIGF0aW5naWRvcy5cbiIpOwogICAgICAgICAgICAgICAgfSBlbHNlIFN5c3RlbS5vdXQucHJpbnRsbigiTGltaXRlIGRlIHNhcXVlcyBhdGluZ2lkb3MuXG4iKTsKICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICBjYXNlIDM6CiAgICAgICAgICAgICAgICBTeXN0ZW0ub3V0LnByaW50KCJRdWFudG8gZGVzZWphIGRlcG9zaXRhcjogIik7CiAgICAgICAgICAgICAgICBpbnQgb2sgPSBtaW5oYUNvbnRhLmRlcG9zaXRhcihsZXJJbnQoKSk7CiAgICAgICAgICAgICAgICBpZiAob2sgPj0gMCkgewogICAgICAgICAgICAgICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbigiRGVwb3NpdGFkbzogIiArIG9rKTsKCQkJICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oIk5vdm8gc2FsZG86ICIgKyBtaW5oYUNvbnRhLmdldFNhbGRvKCkgKyAiXG4iKTsKICAgICAgICAgICAgICAgIH0gZWxzZSBTeXN0ZW0ub3V0LnByaW50KCJOw6NvIHBvZGUgc2VyIG5lZ2F0aXZvIik7CiAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgY2FzZSA0OiAKCQkgICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbigiQ2FkYXN0cmFuZG8gbm92byBjbGllbnRlLiIpOwoJCSAgICAgICAgU3lzdGVtLm91dC5wcmludCgiRW50ZSBjb20gc2V1IG5vbWU6ICIpOwoJCSAgICAgICAgU3lzdGVtLm91dC5wcmludCgiRW50cmUgY29tIG8gdmFsb3IgaW5pY2lhbCBkZXBvc2l0YWRvIG5hIGNvbnRhOiAiKTsKCQkgICAgICAgIG1pbmhhQ29udGEgPSBuZXcgQ29udGEoZW50cmFkYS5uZXh0TGluZSgpLCBsZXJJbnQoKSk7CiAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgY2FzZSAwOiBicmVhazsKICAgICAgICAgICAgZGVmYXVsdDoKICAgICAgICAgICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbigiT3DDp8OjbyBpbnbDoWxpZGEiKTsKCSAgICAgICAgfQogICAgICAgIH0gd2hpbGUgKG9wY2FvICE9IDApOwogICAgfQogICAgcHJpdmF0ZSBzdGF0aWMgaW50IGxlckludCgpIHsKICAgICAgICB3aGlsZSAodHJ1ZSkgewogICAgICAgICAgICBTdHJpbmcgbGlkbyA9IGVudHJhZGEubmV4dExpbmUoKS50cmltKCk7CiAgICAgICAgICAgIHRyeSB7CiAgICAgICAgICAgICAgICByZXR1cm4gSW50ZWdlci5wYXJzZUludChsaWRvKTsKICAgICAgICAgICAgfSBjYXRjaCAoTnVtYmVyRm9ybWF0RXhjZXB0aW9uIGUpIHsKICAgICAgICAgICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbigiRGVzY3VscGUsIG1hcyAiICsgbGlkbyArICIgbsOjbyDDqSB1bSBuw7ptZXJvIGludGVpcm8uIFRlbnRlIG5vdmFtZW50ZS4iKTsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KfQoKY2xhc3MgQ29udGEgewoJc3RhdGljIHByaXZhdGUgaW50IHVsdGltYUNvbnRhOwogICAgcHJpdmF0ZSBTdHJpbmcgbm9tZTsKICAgIHByaXZhdGUgaW50IGNvbnRh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