Está en la página 1de 5

José Orlando Durán Pérez

Universidad Autónoma de Querétaro

Facultad de Informática

Materia: Programación Orientada a Objetos

Práctica 13

Instrucciones: Elabore un proyecto en Java llamado Cerrajeria. El proyecto debe constar de una
clase llamada Herramienta.java basada en el siguiente diagrama de clase:

Herramienta
+clave : int
+nombre :
String
+cantidad : int
+precio : double
+getClave()
+setClave()
+getNombre()
+setNombre()
+getCantidad()
+setCantidad()
+getPrecio()
+setPrecio()

En el archivo Cerrajeria.java, el programa debe solicitar al usuario los datos necesarios para crear
n cantidad de objetos del tipo Herramienta por medio del teclado y las almacene en un ArrayList.
Las Herramientas para crear son:

CLAVE PRODUCTO CANTIDAD PRECIO

12345 Martillo 2 $60

24273 Destornillador 1 $50

54321 Clavo 10 $2

84629 Tuerca 50 $1

57201 Tornillo 8 $5

Después de agregar las Herramientas anteriores, el programa debe imprimir el contenido del
ArrayList con los datos de las Herramientas mediante el uso de los métodos get() de cada una de
ellas.
José Orlando Durán Pérez

import java.io.*;
import java.util.ArrayList;

public class Cerrajeria {


public static BufferedReader teclado = new BufferedReader(new InputStreamReader(System.in));

public static void main(String[] args) throws IOException


{

//OBJETO(ArrayList) tipo Herramienta


ArrayList<Herramienta> cerrajeria = new ArrayList<>();

System.out.println("¿Cuantas Herramientas quiere crear?");


int numeroHerramientas = Integer.parseInt(teclado.readLine());

for(int i = 0; i < numeroHerramientas; i++){

System.out.println("Ingrese la clave");
int clave = Integer.parseInt(teclado.readLine());
System.out.println("Ingrese producto");
String producto = teclado.readLine();
System.out.println("Ingrese la cantidad");
int cantidad = Integer.parseInt(teclado.readLine());
System.out.println("Ingrese el precio");
double precio = Double.parseDouble(teclado.readLine());

Herramienta objetoHerramienta = new Herramienta(clave, producto, cantidad, precio);


cerrajeria.add(objetoHerramienta);
}

for(int j = 0; j < numeroHerramientas; j++){


System.out.println("Herramienta (" + j+1 + ")");
System.out.println("Clave: " + cerrajeria.get(j).getClave());
System.out.println("Nombre: " + cerrajeria.get(j).getNombre());
System.out.println("Cantidad: " + cerrajeria.get(j).getCantidad());
System.out.println("Precio: $" + cerrajeria.get(j).getPrecio());
System.out.println();
}

}
}
José Orlando Durán Pérez

public class Herramienta {

//ATRIBUTOS
private int clave;
private String nombre;
private int cantidad;
private double precio;

//CONSTRUCTOR
public Herramienta(int clave, String nombre, int cantidad, double precio) {
this.clave = clave;
this.nombre = nombre;
this.cantidad = cantidad;
this.precio = precio;
}

//METODOS SET & GET

public int getClave() {


return clave;
}

public void setClave(int clave) {


this.clave = clave;
}

public String getNombre() {


return nombre;
}

public void setNombre(String nombre) {


this.nombre = nombre;
}

public int getCantidad() {


return cantidad;
}

public void setCantidad(int cantidad) {


this.cantidad = cantidad;
}

public double getPrecio() {


return precio;
}

public void setPrecio(double precio) {


this.precio = precio;
}
}
José Orlando Durán Pérez
José Orlando Durán Pérez

También podría gustarte