Advertisement
Guest User

Untitled

a guest
Sep 18th, 2019
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.04 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package examples;
  7.  
  8. import java.util.Scanner;
  9.  
  10. /**
  11.  *
  12.  * @author din32
  13.  */
  14. public class RoperoEjemplo
  15. {
  16.     //un ropero con 5 cajones que admite hasta tres prendes por cajón.
  17.     static String[][] ropero = new String[2][2];
  18.     static int posCol = 0;//actualCol
  19.     static int posFil = 0;//actualFil
  20.     static boolean changed = false;
  21.    
  22.    
  23.     static boolean validar(String ropa)
  24.     {
  25.         return (ropa != null);
  26.     }
  27.    
  28.     static String showRopa()
  29.     {
  30.         String acum = "";
  31.         for(String[] cajon : ropero)
  32.         {
  33.             acum+="{";
  34.             for(String ropa : cajon)
  35.             {
  36.                 if(ropa != null)
  37.                 {
  38.                     acum += ropa+" - ";
  39.                 }
  40.             }
  41.             acum+="}";
  42.         }
  43.         return !(acum.equals("")) ? acum : "";
  44.     }
  45.    
  46.     public static void main(String[] args)
  47.     {
  48.         Scanner scan = new Scanner(System.in);
  49.         int ropCol = ropero.length;//tamaño de indexs reales
  50.         int ropFil = ropero[0].length; //tamaño de indexs reales
  51.         do
  52.         {
  53.             System.out.println("Para agregar ropa escriba: ropa\nPara buscar ropa escriba: buscar");
  54.             String ropaLista = showRopa();
  55.             System.out.println((ropaLista.length() > 4) ? ropaLista : "");
  56.             String resp = scan.nextLine();
  57.            
  58.             switch(resp)
  59.             {
  60.                 case "ropa":
  61.                     System.out.println("Escriba el nombre:");
  62.                     String nombre = scan.nextLine();
  63.                     if(!changed)
  64.                     {
  65.                         ropero[0][0] = nombre;//ingresamos el primer valor de la columna 1 y el primer valor de la fila 1
  66.                         changed = true;
  67.                     }
  68.                     else
  69.                     {
  70.                         System.out.println("Tamaño de columnas:"+ropero.length+" - tamaño filas: "+ ropero[0].length);
  71.                         if(posCol == ropCol && posFil == ropFil)
  72.                         {
  73.                             System.out.println("El ropero está lleno, ya no puedes ingresar más!");
  74.                             break;
  75.                         }
  76.                         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
  77.                         int sumCol = posCol + 1;
  78.                         //if(validar(ropero[posCol][posFil]))//no hay ropa ocupando la columna x y la fila x
  79.                         //{
  80.                             if(sumFil >= ropFil)//tamaño de indexs reales utilizables
  81.                             {
  82.                                 if(sumCol < ropCol)//si el nuevo movimiento de indexs no excede la cantidad de columnas existenes entonces hará algo
  83.                                 {
  84.                                     posFil=0;//se pone en 0 para luego permitir guardar más en las filas siguientes.
  85.                                     posCol++;
  86.                                     ropero[posCol][posFil] = nombre;
  87.                                 }
  88.                                 else
  89.                                 {
  90.                                     System.out.println("El ropero está lleno, ya no puedes ingresar más!");
  91.                                     break;
  92.                                 }
  93.                             }
  94.                             else
  95.                             {
  96.                                 posFil++;
  97.                                 ropero[posCol][posFil] = nombre;
  98.                             }
  99.                        // }
  100.                        
  101.                        
  102.                     }
  103.                     break;
  104.                 case "buscar":
  105.                     String buscar = scan.nextLine();
  106.                     int findX=0;
  107.                     int findY=0;
  108.                     boolean encontrado=false;
  109.                     for(int i = 0; i < ropero.length; i++)
  110.                     {
  111.                         for(int j = 0; j < ropero[i].length; j++)
  112.                         {
  113.                             if(ropero[i][j].contains(buscar))
  114.                             {
  115.                                 encontrado = true;
  116.                                 findX = i;
  117.                                 findY = j;
  118.                             }
  119.                         }
  120.                     }
  121.                     if(encontrado)
  122.                     {
  123.                         System.out.println("Hemos encontrado la prenda "+ buscar);
  124.                         System.out.println("Se encuentra en la columna "+ findX + " y en la fila "+ findY);
  125.                     }
  126.                     break;
  127.             }
  128.            
  129.             System.out.println("Desea continuar? s/n");
  130.         }
  131.         while(!scan.nextLine().equals("n"));
  132.        
  133.     }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement