elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: AIO elhacker.NET 2021 Compilación herramientas análisis y desinfección malware


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  Java
| | | |-+  HashTable
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: HashTable  (Leído 3,261 veces)
winnipu

Desconectado Desconectado

Mensajes: 91


Ver Perfil
HashTable
« en: 7 Junio 2014, 09:51 am »

Vereis se me pide hacer el siguiente ejercicio:

Desarrollar un sistema de gestión de partes de un taller mecánico.

La información de los partes es: código, descripción e importe. Un parte irá asociado a la matrícula de un coche de manera que podamos añadir, mostrar y eliminar un parte a partir de la matrícula de coche al que está asociado.

La clase Main mostrará un menú de opciones por consola: crear, listar, mostrar y eliminar partes. También habrá una opción de salir. Utilizar alguna de las estructuras de datos dinámicos.


He estado pensando en realizarlo con HashTable, usando la matricula del vehiculo como parametro ID, y me gustaria que codigo, descripcion e importe sean el parametro VALOR de dicho ID. No se si es posible hacer eso porque nunca he trabajado con hashtable pero por lo que veo los parametros de su constructor son dos, el correspondiente a ID y el correspondiente a su VALOR asociado. Imagino que puedo crear una clase llamada Partes con un constructor en el que se introduzcan los tres valores (codigo, descripcion e importe) y luego crear un metodo tipo string que muestre los tres valores juntos. Y despues desde otra clase hashtable llamar a dicho metodo. Es esto posible o me estoy haciendo un lio?


En línea

winnipu

Desconectado Desconectado

Mensajes: 91


Ver Perfil
Re: HashTable
« Respuesta #1 en: 18 Octubre 2014, 22:24 pm »

Me autorespondo por si a alguien interesa:

Código:
public class Parte {

private String matricula;
private String codigo;
private String descripcion;
private double importe;

public Parte (String matricula,String codigo, String descripcion, double importe){
this.matricula = matricula;
this.codigo = codigo;
this.descripcion = descripcion;
this.importe = importe;
}

public void setMatricula(String matricula){
this.matricula = matricula;
}

public void setCodigo(String codigo){
this.codigo = codigo;
}

public void setDescripcion (String descripcion){
this.descripcion = descripcion;
}

public void setImporte(double importe){
this.importe = importe;
}

public String getMatricula(){
return matricula;
}

public String getCodigo(){
return codigo;
}

public String getDescripcion(){
return descripcion;
}

public double getImporte(){
return importe;
}

}

Código:
import java.util.*;

public class GestorPartes {
Hashtable <String, ArrayList<Parte>> ht = new Hashtable<String, ArrayList<Parte>>();
Parte p;
public void crearParte(){


@SuppressWarnings("resource")
Scanner teclado = new Scanner(System.in);
System.out.println("Introduce la matricula del vehiculo: ");
String matricula = teclado.nextLine();
System.out.println("Introduce el codigo de la averia: ");
String codigo = teclado.nextLine();
System.out.println("Introduce la descripcion de la averia: ");
String descripcion = teclado.nextLine();
System.out.println("Introduce el importe de la averia: ");
double importe = teclado.nextDouble();
p = new Parte (matricula,codigo,descripcion,importe);
if (!ht.containsKey(p.getMatricula())){
ArrayList<Parte> ArrPartes = new ArrayList<Parte>();
ht.put(p.getMatricula(), ArrPartes);
ht.get(p.getMatricula()).add(p);
} else
ht.get(p.getMatricula()).add(p);
}

public void mostrar(){

if(ht.containsKey(p.getMatricula())){
ArrayList<Parte> ArrPartes = ht.get(p.getMatricula());
Iterator<Parte> itPart = ArrPartes.iterator();
while (itPart.hasNext()){
Parte p = itPart.next();
System.out.println("========");
System.out.println("Matricula: " + p.getMatricula()+ "\n" +
"Codigo: " + p.getCodigo() + "\n" +
"Descripción: " + p.getDescripcion() + "\n" +
"Importe: " + p.getImporte() + "\n");
}
} else
System.out.println("Objeto no localizado");
}

public void listar(){
Set<String> keysmatri = ht.keySet();
Iterator<String> itMatri = keysmatri.iterator();
while (itMatri.hasNext()){
itMatri.hasNext();
String matricula = itMatri.next();
ArrayList<Parte> ArrPartes = ht.get(matricula);
Iterator<Parte> itPart = ArrPartes.iterator();
while(itPart.hasNext()){
Parte p = itPart.next();
System.out.println("========");
System.out.println("Matricula: " + p.getMatricula()+ "\n" +
"Codigo: " + p.getCodigo() + "\n" +
"Descripción: " + p.getDescripcion() + "\n" +
"Importe: " + p.getImporte() + "\n");
}
}
}

public void eliminar(){
if(ht.containsKey(p.getMatricula())){
ht.get(p.getMatricula()).remove(p);
}
}
}

Código:
import java.util.*;

public class Principal {

private static GestorPartes gp = new GestorPartes();

public static void main(String[] args) {
int opcion = 0;
do{
@SuppressWarnings("resource")
Scanner teclado = new Scanner(System.in);
System.out.println("SELECCIONA UNA OPCION......");
System.out.println("[1] CREAR PARTE");
System.out.println("[2] LISTAR PARTE");
System.out.println("[3] ELIMINAR PARTE");
System.out.println("[4] LISTAR TODOS LOS PARTES");
System.out.println("[5] SALIR");
opcion = teclado.nextInt();
switch(opcion){
case 1: gp.crearParte(); break;
case 2: gp.mostrar(); break;
case 3: gp.eliminar(); break;
case 4: gp.listar();
case 5: System.out.println("Fin del Programa"); System.exit(0);
default: System.out.println("Teclea una opcion valida"); break;
}
}while (opcion != 5);

}

}


En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
problema The type System.Collections.Hashtable is not supported en socket en PHP
PHP
OpenBSD 0 2,215 Último mensaje 15 Febrero 2010, 12:29 pm
por OpenBSD
Recorrer HashTable
Java
leogtz 1 7,257 Último mensaje 6 Octubre 2011, 15:52 pm
por Valkyr
Recuperar un valor de un hashtable que tiene diferentes values
Desarrollo Web
jabedoya 0 1,822 Último mensaje 23 Octubre 2014, 20:58 pm
por jabedoya
HashMap o HashTable - Para una tarea
Java
asalarco 1 1,674 Último mensaje 31 Julio 2016, 21:02 pm
por Once
hashmap o hashtable que es mejor?
Java
rikaardobm 3 2,266 Último mensaje 24 Agosto 2016, 22:15 pm
por Chuidiang
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines