Docsity

Practica programacion, Ejercicios de Programación Javascript

Toggle sidebar
bg1
CLASE BASE:
public class Producto {
public String nombre;
public double precio;
public int stock;
public Producto(String nombre, double precio, int stock) {
this.nombre = nombre;
this.precio = precio;
this.stock = stock;
}
public String getStock(){
return "En stock hay: "+this.stock;
}
public String agregarStock(int cantidad){
this.stock+=cantidad;
return "Ahora hay en stock: "+this.stock;
}
public void vender(int cantidadV) throws StockInsuficienteException{
if (cantidadV > this.stock)
throw new StockInsuficienteException(
"Cantidad insuficiente en stock, hay en stock: "+this.stock
);
this.stock-=cantidadV;
System.out.println("Venta de mercancia, ahora hay en stock: "+this.stock);
}
public void mostrarProducto(){
System.out.println("Nombre: "+this.nombre);
System.out.println("Precio: $"+this.precio);
System.out.println("Cantidad en stock: "+this.stock);
}
}
Excepcion:
public class StockInsuficienteException extends Exception{
public StockInsuficienteException(String message) {
super(message);
}
}
pf3

Vista previa parcial del texto

¡Descarga Practica programacion y más Ejercicios en PDF de Programación Javascript solo en Docsity!

CLASE BASE:

public class Producto { public String nombre; public double precio; public int stock; public Producto(String nombre, double precio, int stock) { this.nombre = nombre; this.precio = precio; this.stock = stock; } public String getStock(){ return "En stock hay: "+this.stock; } public String agregarStock(int cantidad){ this.stock+=cantidad; return "Ahora hay en stock: "+this.stock; } public void vender(int cantidadV) throws StockInsuficienteException{ if (cantidadV > this.stock) throw new StockInsuficienteException( "Cantidad insuficiente en stock, hay en stock: "+this.stock ); this.stock-=cantidadV; System.out.println("Venta de mercancia, ahora hay en stock: "+this.stock); } public void mostrarProducto(){ System.out.println("Nombre: "+this.nombre); System.out.println("Precio: $"+this.precio); System.out.println("Cantidad en stock: "+this.stock); } }

Excepcion:

public class StockInsuficienteException extends Exception{ public StockInsuficienteException(String message) { super(message); } }

appMain:

public class appMain { public static void main(String[] args) { Scanner cap = new Scanner(System.in); Producto celular = new Producto("Samsung s24",12500.00,35); int cantidad, cantidadV, opcion; do { System.out.println("\n===MENU TIENDA==="); System.out.println("1. Ver stock"); System.out.println("2. Agregar stock"); System.out.println("3. Vender producto"); System.out.println("4. Mostrar datos del producto"); System.out.println("5. Salir"); System.out.print("Seleccione una opcion: "); opcion = cap.nextInt(); switch (opcion) { case 1: System.out.println("Stock disponible: " + celular.getStock()); break; case 2: System.out.print("Ingrese la cantidad de producto a agregar: "); cantidad = cap.nextInt(); celular.agregarStock(cantidad); break; case 3: System.out.print("Ingrese cantidad a vender: "); cantidadV = cap.nextInt(); try { celular.vender(cantidadV); } catch (StockInsuficienteException e) { System.out.println("Error: " + e.getMessage()); } break; case 4: celular.mostrarProducto(); break; case 5: System.out.println("Saliendo del menu....."); break; default: System.out.println("Opcion no valida"); }