Java - array list menú

 
Vista:
sin imagen de perfil
Val: 25
Ha aumentado su posición en 1146 puestos en Java (en relación al último mes)
Gráfica de Java

array list menú

Publicado por andre (15 intervenciones) el 17/06/2021 20:56:09
tengo este código necesito que cada método agregue un producto y la cantidad , elimine, muestre y ordene productos del arraylist

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
public class ShoppingList1 {
 
 
	public static void main(String[] args) {
		// Initialize variables
		Scanner sIn = new Scanner(System.in); // Input Scanner for String
		ArrayList<String> shoppingList = new ArrayList<String>(); // ArrayList for shoppingList
		String choice; // Holds user input of type String
		boolean done = false; // Keeps program running until user wants to quit
 
		// Keep running the program until the user quits
		do {
			// Print out the menu to the console
			System.out.println();
			System.out.println("1. agregar productos");
			System.out.println("2. eliminar productos");
			System.out.println("3. mostrar productos");
			System.out.println("4. ordenar productos");
			System.out.println("5. Exit");
			System.out.print("ingrese opcion ");
			choice = sIn.nextLine();
 
			// Call the appropriate method for the choice selected.
			switch (choice) {
			case "1": // Add items to the Shopping List
				System.out.println(addItems(sIn, shoppingList) + " productos se han agregado a la lista .");
				break;
			case "2": // Delete items from the Shopping List
				System.out
						.println(deleteItems(sIn, shoppingList) + " productos se han eliminado de la lista .");
				break;
			case "3": // Show the items in the Shopping List
				showItems(shoppingList);
				break;
			case "4": // Sort the items in the Shopping List
				sortItems(shoppingList);
				break;
			case "5": // Exit the program
				System.out.println();
				System.out.println("Goodbye");
				done = true;
				break;
			default: // Handles all input that is not between 1-5
				System.out.println("Invalido ingrese opciond e menu u (1-5).");
			} // End of switch (choice)
		} while (!done); // Keep running the program until the user quits
 
		sIn.close();
	}// end of main()
 
 
 
	public static int addItems(Scanner sIn, ArrayList<String> shoppingList) {
		// FIXME: implement the method
	sIn = new Scanner(System.in);
 
		boolean continuar = true;
		while(continuar) {
			System.out.println("Agrege un articulo a la lista   y la cantidad o simplemente presione Enter para finalizar ");
 
 
			String sn = sIn.nextLine();
			if (sn.isBlank())
				continuar = false;
			else {
 
					shoppingList.add((sn));
 
 
				}
 
			}
 
 
 
 
 
 
		return 0;
	}// end of method addItems(ArrayList<String>)
 
 
	public static int deleteItems(Scanner sIn, ArrayList<String> shoppingList) {
		// FIXME: implement the method
		return 0;
	}// end of method deleteItems(ArrayList<String>)
 
 
	public static void showItems(ArrayList<String> shoppingList) {
		// FIXME: implement the method
	}// end of method showItems(ArrayList<String>)
 
 
	public static void sortItems(ArrayList<String> shoppingList) {
		// FIXME: implement the method
	}// end of method sortItems(ArrayList<String>)
 
}//
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de Kabuto
Val: 3.428
Oro
Ha mantenido su posición en Java (en relación al último mes)
Gráfica de Java

array list menú

Publicado por Kabuto (1381 intervenciones) el 18/06/2021 11:33:44
En el método de añadir, parece que lo único que falta es que se retorne la cantidad de ítems que el usuario agrega en este proceso.
Es decir, habría que añadir un contador que se incremente cada vez que se ingresa un ítem y retornarlo al terminar el método.

El método de eliminar ítems, sería prácticamente copiar el método de añadir, pero esta vez los ítems que teclea el usuario no se añaden, si no que se eliminan del ArrayList (si es que existen). Como antes, hay que retornar un contador de ítems eliminados.

El método de mostrar ítems, no tiene ningún misterio. Recorrer el ArrayList con un bucle y mostrar en pantalla los ítems almacenados.

El método de ordenar, no se especifica en base a que se debe ordenar, supongo que por orden alfabético.
El ArrayList de ítems lo que contiene son objetos de clase String y esta clase ya sabe cómo ha de ordenarse alfabéticamente ella sola.
Y el ArrayList ya tiene un método para ordenar su contenido según le indique la clase de los objetos que contiene, así que para ordenar basta con esta orden:
1
shoppingList.sort(null);

Y listo, después de ordenar, ya decides si mostrarlos en pantalla o no.


Ahí está explicado lo que hay que conseguir en cada método. Inténtalo, si no lo consigues, te atascas o lo que sea... comparte aquí lo que hayas podido hacer y te ayudamos a corregir y completar.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
1
Comentar