Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Clase Main
- package maipackete;
- import java.io.IOException;
- public class Tp2 {
- public static void main(String[] args) throws IOException {
- Maquina original = new Maquina();
- while (true) {
- original.iniciarMaquina();
- }
- }
- }
- // Clase Cuenta
- /*Aunque no lo pedían, agregue un sistema de ID de cuenta y un sistema de claves. Aunque el sistema de ID debería implementarse pensando en el total de cuentas y no crearse a partir de un numero random*/
- package maipackete;
- import java.lang.Math;
- public class Cuenta {
- private double saldo;
- private int idCuenta;
- private int clave;
- public boolean esIgualClave(int numero) {
- if (clave == numero) {
- return true;
- }
- return false;
- }
- public Cuenta(int saldoInic) {
- saldo = saldoInic;
- idCuenta = (int)Math.floor(Math.random()*1000000);
- System.out.println("Su numero de cuenta es: "+idCuenta);
- obtenerClave();
- }
- public Cuenta() {
- idCuenta = (int)Math.floor(Math.random()*1000000);
- System.out.println("Su numero de cuenta es: "+idCuenta);
- saldo = 0;
- obtenerClave();
- }
- private void obtenerClave() {
- System.out.println("Ingrese una clave de 4 dígitos");
- clave = Maquina.entrada.nextInt();
- }
- public boolean esIgual(int numero) {
- if (idCuenta == numero) {
- return true;
- }
- else
- {
- return false;
- }
- }
- public void ingresare(float c) {
- saldo += c;
- }
- public void extraere(float c) {
- if (c > saldo) {
- Maquina.error(1);
- }
- else {
- saldo -= c;
- }
- }
- public float getSaldo() {
- return (float) saldo;
- }
- }
- /*Clase Maquina, aqui están todas las cuentas guardadas en un ArrayList que se maneja de forma estatica (osea, que existe en todas las maquinas ) */
- package maipackete;
- import java.io.IOException;
- import java.util.Scanner;
- import java.util.ArrayList;
- public class Maquina {
- public static Scanner entrada = new Scanner (System.in);
- public static ArrayList<Cuenta> cuentas = new ArrayList<Cuenta>();
- public static void error(int i) {
- switch (i) {
- case 1:
- System.out.println("Error, saldo insuficiente");
- break;
- case 2:
- System.out.println("Error, numero de cuenta no existente");
- break;
- case 3:
- System.out.println("Error, clave ingresada no es correcta");
- break;
- }
- }
- public void iniciarMaquina() throws IOException {
- System.out.println("Presiona una tecla para iniciar");
- if (System.in.read() != -1) {
- Maquina.menu();
- Maquina.seleccionarOpcion();
- }
- }
- private static void menu () {
- System.out.println("Seleccione una opcion con los numeros y luego presione ENTER");
- System.out.println("1. Crear cuenta vacia");
- System.out.println("2. Crear cuenta saldo inicial");
- System.out.println("3. Ingresar diner");
- System.out.println("4. Sacar dinero");
- System.out.println("5. Ver saldo");
- System.out.println("6. Salir");
- }
- private static int buscarCuenta (int NdeCuenta) {
- for (int i = 0; i<cuentas.size();i++) {
- if (cuentas.get(i).esIgual(NdeCuenta)) {
- return i;
- }
- }
- return -1;
- }
- public static void seleccionarOpcion () {
- int opc = entrada.nextInt();
- switch (opc) {
- case 1:
- Maquina.cuentas.add(new Cuenta());
- break;
- case 2:
- System.out.println("Ingrese el saldo inicial");
- Maquina.cuentas.add(new Cuenta(entrada.nextInt()));
- break;
- case 3:
- System.out.println("Ingrese su cuenta");
- int aux = buscarCuenta(entrada.nextInt());
- System.out.println("Ingrese su clave");
- cuentas.get(aux).esIgualClave(entrada.nextInt());
- System.out.println("Ingrese su saldo inicial");
- cuentas.get(aux).ingresare(entrada.nextFloat());
- break;
- case 4:
- System.out.println("Ingrese su cuenta");
- int aux1 = buscarCuenta(entrada.nextInt());
- System.out.println("Ingrese su clave");
- cuentas.get(aux1).esIgualClave(entrada.nextInt());
- System.out.println("Ingrese saldo a retirar");
- cuentas.get(aux1).extraere(entrada.nextFloat());
- break;
- case 5:
- System.out.println("Ingrese su cuenta");
- int aux11 = buscarCuenta(entrada.nextInt());
- System.out.println("Ingrese su clave");
- cuentas.get(aux11).esIgualClave(entrada.nextInt());
- System.out.println("Ingrese su saldo inicial");
- System.out.println("Saldo " +cuentas.get(aux11).getSaldo());
- break;
- default:
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement