Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 5

Inventory

package inventory;

import java.util.Scanner;

public class Inventory {

public static void main(String[] args) {


Scanner in = new Scanner(System.in);
int maxSize, menuChoice;

maxSize = getNumProducts(in);
if (maxSize == 0) {
System.out.println("No se requieren productos");
} else {
Product[] products = new Product[maxSize];
addToInventory(products, in);
do {
menuChoice = getMenuOption(in);
executeMenuChoice(menuChoice, products, in);
} while (menuChoice != 0);
}

in.close();
}

public static void displayInventory(Product[] products) {


for (Product product : products) {
System.out.println(product.toString());
System.out.println("---------------------------------------------");
}
}

public static void agregarInventario(Product[] products, Scanner in) {


for (Product product : products) {
System.out.println("Cuantos productos desea agregar al inventario para
" + product.getNombreDeProducto() + "?");
int cantidad = in.nextInt();
product.addToInventory(cantidad);
}
}

public static void restarInventario(Product[] products, Scanner in) {


for (Product product : products) {
System.out.println("Cuantos productos desea restar del inventario para
" + product.getNombreDeProducto() + "?");
int cantidad = in.nextInt();
product.deductFromInventory(cantidad);
}
}

public static void discontinueInventory(Product[] products, Scanner in) {


int productChoice = getProductNumber(products, in);
if (productChoice >= 0 && productChoice < products.length) {
products[productChoice].setEstatus(false);
System.out.println("El producto " +
products[productChoice].getNombreDeProducto() + " ha sido descatalogado\n");
} else {
System.out.println("Opcion de producto invalida");
}
}

public static int getMenuOption(Scanner in) {


int menuChoice;
do {
System.out.println("Seleccione una opcion del menu:");
System.out.println("1. Ver inventario");
System.out.println("2. Agregar stock");
System.out.println("3. Restar stock");
System.out.println("4. Descatalogar producto");
System.out.println("0. Salir");
menuChoice = in.nextInt();
if (menuChoice < 0 || menuChoice > 4) {
System.out.println("Opcion invalida. Intentelo nuevamente");
}
} while (menuChoice < 0 || menuChoice > 4);
return menuChoice;
}

public static int getProductNumber(Product[] products, Scanner in) {


int productChoice = -1;
do {
System.out.println("Seleccione el numero de producto:");
for (int i = 0; i < products.length; i++) {
System.out.println((i + 1) + ". " +
products[i].getNombreDeProducto());
}
productChoice = in.nextInt();
if (productChoice < 0 || productChoice >= products.length) {
System.out.println("Numero de producto invalido. Intentelo
nuevamente.");
}
} while (productChoice < 0 || productChoice >= products.length);
return productChoice;
}

public static void addToInventory(Product[] products, Scanner in) {


for (int i = 0; i < products.length; i++) {
in.nextLine(); // Limpiar el buffer de entrada

System.out.println("Ingrese el numero del elemento " + (i + 1));


int tempNumeroDeElemento = in.nextInt();

System.out.println("Ingrese el nombre del producto " + (i + 1));


String tempNombreDeProducto = in.next();

System.out.println("Ingrese el numero de unidades del elemento " + (i +


1));
int tempNumeroDeUnidades = in.nextInt();

System.out.println("Ingrese el precio del producto " + (i + 1));


double tempPrecioDeProducto = in.nextDouble();

System.out.println("---------------------------------------------");

products[i] = new Product(tempNumeroDeElemento, tempNombreDeProducto,


tempNumeroDeUnidades, tempPrecioDeProducto);
}

public static int getNumProducts(Scanner in) {


int maxSize;
do {
try {
System.out.println("Ingrese el numero de productos que desea
agregar:");
System.out.println("Ingrese 0 si no desea agregar productos:");
maxSize = in.nextInt();

if (maxSize < 0) {
System.out.println("Valor incorrecto introducido\n");
}
} catch (Exception e) {
System.out.println("El tipo de dato introducido es incorrecto\n");
in.nextLine(); // Limpiar el buffer de entrada
maxSize = -1;
}
} while (maxSize < 0);
return maxSize;
}

public static void executeMenuChoice(int menuChoice, Product[] products,


Scanner in) {
switch (menuChoice) {
case 1:
displayInventory(products);
break;
case 2:
agregarInventario(products, in);
break;
case 3:
restarInventario(products, in);
break;
case 4:
discontinueInventory(products, in);
break;
case 0:
System.out.println("Saliendo del programa. Hasta luego");
break;
}
}
}

Product

package inventory;
public class Product {

private int numeroDeElemento;


private String nombreDeProducto;
private int numeroDeUnidades;
private double precioDeProducto;
private boolean estatus = true;

public Product() {

public Product(int numeroDeElemento, String nombreDeProducto, int


numeroDeUnidades, double precioDeProducto) {
this.numeroDeElemento = numeroDeElemento;
this.nombreDeProducto = nombreDeProducto;
this.numeroDeUnidades = numeroDeUnidades;
this.precioDeProducto = precioDeProducto;
}

public void setNumeroDeElemento(int numeroDeElemento) {


this.numeroDeElemento = numeroDeElemento;
}

public void setNombreDeProducto(String nombreDeProducto) {


this.nombreDeProducto = nombreDeProducto;
}

public void setNumeroDeUnidades(int numeroDeUnidades) {


this.numeroDeUnidades = numeroDeUnidades;
}

public void setPrecioDeProducto(double precioDeProducto) {


this.precioDeProducto = precioDeProducto;
}

public void setEstatus(boolean estatus) {


this.estatus = estatus;
}

public int getNumeroDeElemento() {


return numeroDeElemento;
}

public String getNombreDeProducto() {


return nombreDeProducto;
}

public int getNumeroDeUnidades() {


return numeroDeUnidades;
}

public double getPrecioDeProducto() {


return precioDeProducto;
}

public boolean getEstatus() {


return estatus;
}
@Override
public String toString() {
return "Numero del producto: " + getNumeroDeElemento() + "\nNombre del
producto: " + getNombreDeProducto() + "\nCantidad en inventario: " +
getNumeroDeUnidades() + "\nPrecio del producto: " + getPrecioDeProducto() + "\
nValor del inventario: " + getValorDeInventario() + "\nEstatus del producto: " +
(estatus ? "Activo" : "Inactivo") + "\n";
}

private double getValorDeInventario() {


return precioDeProducto * numeroDeUnidades;
}

public void addToInventory(int cantidad) {


numeroDeUnidades += cantidad;
}

public void deductFromInventory(int cantidad) {


numeroDeUnidades -= cantidad;
}
}

You might also like