0% found this document useful (0 votes)
35 views

Proyecto 7 Parte 1 Java

The document defines an Inventory class with methods to manage an inventory of Products. It includes: 1. A main method that initializes the inventory by getting product details, displays menus to view, add, remove, or discontinue products, and exits when the user selects option 0. 2. Methods to display the full inventory, add or remove quantities of products, and discontinue products. 3. Supporting methods to get menu selections, product numbers, and initialize the inventory from user input. 4. A Product class to represent each product with details like name, quantity, price, and status.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Proyecto 7 Parte 1 Java

The document defines an Inventory class with methods to manage an inventory of Products. It includes: 1. A main method that initializes the inventory by getting product details, displays menus to view, add, remove, or discontinue products, and exits when the user selects option 0. 2. Methods to display the full inventory, add or remove quantities of products, and discontinue products. 3. Supporting methods to get menu selections, product numbers, and initialize the inventory from user input. 4. A Product class to represent each product with details like name, quantity, price, and status.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 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