Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package examples;
- import java.util.Scanner;
- /**
- *
- * @author din32
- */
- public class RoperoEjemplo
- {
- //un ropero con 5 cajones que admite hasta tres prendes por cajón.
- static String[][] ropero = new String[2][2];
- static int posCol = 0;//actualCol
- static int posFil = 0;//actualFil
- static boolean changed = false;
- static boolean validar(String ropa)
- {
- return (ropa != null);
- }
- static String showRopa()
- {
- String acum = "";
- for(String[] cajon : ropero)
- {
- acum+="{";
- for(String ropa : cajon)
- {
- if(ropa != null)
- {
- acum += ropa+" - ";
- }
- }
- acum+="}";
- }
- return !(acum.equals("")) ? acum : "";
- }
- public static void main(String[] args)
- {
- Scanner scan = new Scanner(System.in);
- int ropCol = ropero.length;//tamaño de indexs reales
- int ropFil = ropero[0].length; //tamaño de indexs reales
- do
- {
- System.out.println("Para agregar ropa escriba: ropa\nPara buscar ropa escriba: buscar");
- String ropaLista = showRopa();
- System.out.println((ropaLista.length() > 4) ? ropaLista : "");
- String resp = scan.nextLine();
- switch(resp)
- {
- case "ropa":
- System.out.println("Escriba el nombre:");
- String nombre = scan.nextLine();
- if(!changed)
- {
- ropero[0][0] = nombre;//ingresamos el primer valor de la columna 1 y el primer valor de la fila 1
- changed = true;
- }
- else
- {
- System.out.println("Tamaño de columnas:"+ropero.length+" - tamaño filas: "+ ropero[0].length);
- if(posCol == ropCol && posFil == ropFil)
- {
- System.out.println("El ropero está lleno, ya no puedes ingresar más!");
- break;
- }
- int sumFil = posFil + 1;//es decir si la posición es 1 y aumenta a 2 -> debería saltar de columna y rellenar la que sigue
- int sumCol = posCol + 1;
- //if(validar(ropero[posCol][posFil]))//no hay ropa ocupando la columna x y la fila x
- //{
- if(sumFil >= ropFil)//tamaño de indexs reales utilizables
- {
- if(sumCol < ropCol)//si el nuevo movimiento de indexs no excede la cantidad de columnas existenes entonces hará algo
- {
- posFil=0;//se pone en 0 para luego permitir guardar más en las filas siguientes.
- posCol++;
- ropero[posCol][posFil] = nombre;
- }
- else
- {
- System.out.println("El ropero está lleno, ya no puedes ingresar más!");
- break;
- }
- }
- else
- {
- posFil++;
- ropero[posCol][posFil] = nombre;
- }
- // }
- }
- break;
- case "buscar":
- String buscar = scan.nextLine();
- int findX=0;
- int findY=0;
- boolean encontrado=false;
- for(int i = 0; i < ropero.length; i++)
- {
- for(int j = 0; j < ropero[i].length; j++)
- {
- if(ropero[i][j].contains(buscar))
- {
- encontrado = true;
- findX = i;
- findY = j;
- }
- }
- }
- if(encontrado)
- {
- System.out.println("Hemos encontrado la prenda "+ buscar);
- System.out.println("Se encuentra en la columna "+ findX + " y en la fila "+ findY);
- }
- break;
- }
- System.out.println("Desea continuar? s/n");
- }
- while(!scan.nextLine().equals("n"));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement