Guest User

Untitled

a guest
Dec 18th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.52 KB | None | 0 0
  1. package m3.uf5.ac02;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Random;
  5. import java.util.Scanner;
  6. import java.util.Stack;
  7.  
  8. /**
  9. *
  10. * @author Alumno
  11. */
  12. public class M3UF5AC02 {
  13.  
  14. /**
  15. * @param args the command line arguments
  16. */
  17.  
  18.  
  19.  
  20.  
  21. static Stack <Alumno> alumnos = new Stack <Alumno> ();
  22. static Stack <Profesor> profesores = new Stack <Profesor> ();
  23. static Stack <Persona> profalum = new Stack <Persona> ();
  24.  
  25. public static void main(String[] args) {
  26. // TODO code application logic here
  27. Scanner Scan = new Scanner(System.in);
  28. ArrayList <Integer> l = new ArrayList<Integer>();
  29.  
  30.  
  31.  
  32. //Añadimos los datos a la arrayList
  33. l.add(1);
  34. l.add(2);
  35. l.add(2);
  36. l.add(4);
  37. l.add(5);
  38. l.add(2);
  39. l.add(7);
  40. l.add(8);
  41. l.add(9);
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48. String f;
  49.  
  50. System.out.println("1.- Eliminar Elemento de un ArrayList");
  51. System.out.println("2.- Datos a replazar en una pila");
  52. System.out.println("3.- Datos a replazar en una ArrayList");
  53. System.out.println("4.- Indica la posicion que quieres imprimir ");
  54. System.out.println("5.- Apilar los Profesor y los Alumnos en una misma pila");
  55. System.out.println("6.- Mostrar Productos y Sumar su Precio final");
  56. System.out.println("Indica la Intruccion a Realizar ");
  57. f = Scan.next();
  58.  
  59.  
  60.  
  61. switch(f){
  62.  
  63. case "1":
  64. borrarnum(l);
  65.  
  66. break;
  67.  
  68. case "2":
  69.  
  70. remplazarpilas();
  71.  
  72.  
  73. case "3":
  74. mezcla();
  75.  
  76.  
  77.  
  78.  
  79. case "4":
  80.  
  81.  
  82. imprimeenteros();
  83.  
  84.  
  85. case "5":
  86.  
  87. apilar();
  88.  
  89.  
  90.  
  91. case "6":
  92.  
  93. productos();
  94.  
  95. }
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105. }
  106.  
  107. public static void borrarnum(ArrayList <Integer> l){
  108. Scanner Scan = new Scanner(System.in);
  109. System.out.println("Indicame el Numero a Eliminar de el ArrayList");
  110. int n = Scan.nextInt();
  111.  
  112.  
  113.  
  114.  
  115. for(int i = 0; i<l.size(); i++){
  116. if(l.get(i)==n){
  117. l.remove(i);
  118. i--;
  119. System.out.println("Se ha eliminado");
  120.  
  121. }
  122.  
  123.  
  124. }
  125. System.out.println(l);
  126.  
  127.  
  128.  
  129. }
  130.  
  131.  
  132. public static void remplazarpilas(){
  133. Scanner Scan = new Scanner(System.in);
  134. Stack <Integer> pila1 = new Stack <Integer> ();
  135. Stack <Integer> pila2 = new Stack <Integer> ();
  136.  
  137. //Apilamos los datos a la pila
  138. pila1.push(1);
  139. pila1.push(2);
  140. pila1.push(3);
  141. pila1.push(4);
  142. pila1.push(5);
  143. pila1.push(6);
  144. pila1.push(7);
  145. pila1.push(8);
  146. pila1.push(9);
  147.  
  148. System.out.println(pila1);
  149.  
  150.  
  151.  
  152. System.out.println("Indicame el Dato a remplazar:");
  153. int DatoR = Scan.nextInt();
  154.  
  155. System.out.println("Indicame el nuevo dato:");
  156. int DatoN = Scan.nextInt();
  157.  
  158.  
  159.  
  160. while(!pila1.empty()){
  161. int dato = pila1.pop();
  162.  
  163. if(dato !=DatoR){
  164. pila2.push(dato);
  165. }else{
  166. pila1.push(DatoN);
  167.  
  168. }
  169. }
  170.  
  171. while(!pila2.empty()){
  172. pila1.push(pila2.pop());
  173.  
  174. }
  175.  
  176. System.out.println(pila1);
  177. }
  178.  
  179.  
  180. public static void mezcla(){
  181.  
  182. ArrayList <Persona> Lista1 = new ArrayList<Persona>();
  183. ArrayList <Persona> Lista2 = new ArrayList<Persona>();
  184. ArrayList <Persona> mostrarPersonas = new ArrayList<Persona>();
  185.  
  186.  
  187. //Añadimos los datos a la arrayList de Lista1
  188.  
  189. Persona p1= new Persona("pol","Aguilar");
  190. Lista1.add(p1);
  191.  
  192. Persona p2= new Persona("Roger","Carreras");
  193. Lista1.add(p2);
  194.  
  195. Persona p3= new Persona("Nona","Ruiz");
  196. Lista1.add(p3);
  197.  
  198. //Añadimos los datos a la arrayList de Lista2
  199. Persona p4 = new Persona("Juan" , "Carlos");
  200. Lista2.add(p4);
  201.  
  202. Persona p5 = new Persona("Aitor" , "Carreras");
  203. Lista2.add(p5);
  204.  
  205. Persona p6 = new Persona("Nuria" , "Gomez");
  206. Lista2.add(p6);
  207.  
  208.  
  209. for(int x=0; x<Lista1.size(); x++){
  210.  
  211. Persona per = Lista1.get(x);
  212. mostrarPersonas.add(per);
  213. }
  214.  
  215. for(int y=0; y<Lista1.size(); y++){
  216.  
  217. Persona per = Lista2.get(y);
  218. mostrarPersonas.add(per);
  219. }
  220.  
  221. for(int x=0; x<mostrarPersonas.size(); x++){
  222. System.out.println("---------------------------");
  223. System.out.println("Nombre: " + mostrarPersonas.get(x).getNombre());
  224. System.out.println("Apellido: " + mostrarPersonas.get(x).getApellidos());
  225. }
  226.  
  227.  
  228. }
  229.  
  230. public static void imprimeenteros(){
  231.  
  232. ArrayList <Integer> enteros = new ArrayList<Integer>();
  233. Scanner Scan = new Scanner(System.in);
  234.  
  235.  
  236. enteros.add(1);
  237. enteros.add(2);
  238. enteros.add(3);
  239. enteros.add(4);
  240. enteros.add(5);
  241.  
  242. int dato;
  243.  
  244.  
  245. System.out.println("Indicame la Posicion que quieres comenzar a imprimir:");
  246. dato = Scan.nextInt();
  247.  
  248.  
  249.  
  250.  
  251. for(int i=dato; i>=0; i--){
  252.  
  253. System.out.println(enteros.get(i));
  254.  
  255.  
  256.  
  257. }
  258.  
  259.  
  260.  
  261. }
  262.  
  263.  
  264. public static void apilar(){
  265. boolean salir = false;
  266. while(!salir){
  267. Scanner Scan = new Scanner(System.in);
  268. System.out.println("1.- Alumno");
  269. System.out.println("2.- Profesor");
  270. System.out.println("3.- Mostrar Alumnos y profesores");
  271.  
  272. int opcion = Scan.nextInt();
  273.  
  274. Alumno alum = new Alumno();
  275. Profesor prof = new Profesor();
  276.  
  277. switch(opcion){
  278.  
  279. case 1:
  280.  
  281. System.out.println("Nombre Alumno: ");
  282. String nombrealum = Scan.next() ;
  283. alum.setNombre(nombrealum);
  284.  
  285. System.out.println("Edad Alumno: ");
  286. int edadalum = Scan.nextInt();
  287. alum.setEdad(edadalum);
  288.  
  289.  
  290. alumnos.push(alum);
  291.  
  292. Alumno datosalumnos = (Alumno) alumnos.pop();
  293. profalum.push(datosalumnos);
  294.  
  295.  
  296. System.out.println("Se ha apilado correctamente.");
  297.  
  298. break;
  299.  
  300.  
  301.  
  302.  
  303. case 2:
  304.  
  305. System.out.println("Nombre Profesor:");
  306. String nombreprof = Scan.next();
  307. prof.setNombre(nombreprof);
  308.  
  309. System.out.println("Edad Profesor:");
  310. int edadprof = Scan.nextInt();
  311. prof.setEdad(edadprof);
  312.  
  313. profesores.push(prof);
  314.  
  315.  
  316. Profesor datosprofesores = (Profesor) profesores.pop();
  317. profalum.push(datosprofesores);
  318.  
  319.  
  320. System.out.println("Se ha apilado correctamente.");
  321.  
  322. break;
  323.  
  324.  
  325.  
  326. case 3:
  327.  
  328. for(int x = 0; x<profalum.size(); x++){
  329.  
  330. System.out.println("Nombre: " + profalum.get(x).getNombre());
  331. System.out.println("Edad: " + profalum.get(x).getEdad());
  332.  
  333.  
  334.  
  335. }
  336.  
  337. case 4:
  338.  
  339. salir = true;
  340.  
  341. break;
  342.  
  343. }
  344.  
  345.  
  346.  
  347.  
  348.  
  349.  
  350.  
  351.  
  352.  
  353. }
  354.  
  355. }
  356.  
  357. public static void productos(){
  358.  
  359. String espacio=" ";
  360. String espacio1 =" ";
  361. String espacio2 =" ";
  362.  
  363. Random ran = new Random();
  364.  
  365. ArrayList <Productos> p = new ArrayList<Productos>();
  366.  
  367.  
  368.  
  369. Productos Productos1 = new Productos("Producto1", 5, 2.6);
  370. p.add(Productos1);
  371.  
  372. Productos Productos2 = new Productos("Producto2",8,3.5);
  373. p.add(Productos2);
  374.  
  375. Productos Productos3 = new Productos("Producto3",2,5.4);
  376. p.add(Productos3);
  377.  
  378. Productos Productos4 = new Productos("Producto4",10,9.5);
  379. p.add(Productos4);
  380.  
  381. Productos Productos5 = new Productos("Producto5",3,4.5);
  382. p.add(Productos5);
  383.  
  384. Productos Productos6 = new Productos("Producto6",1,7.2);
  385. p.add(Productos6);
  386.  
  387. Productos Productos7 = new Productos("Producto7",7,20.5);
  388. p.add(Productos7);
  389.  
  390.  
  391. Productos Productos8 = new Productos("Producto8",10,8.5);
  392. p.add(Productos8);
  393.  
  394. double preciototal;
  395.  
  396. preciototal = Productos1.precioFinal() + Productos2.precioFinal() + Productos3.precioFinal() + Productos4.precioFinal() + Productos5.precioFinal() + Productos6.precioFinal() + Productos7.precioFinal() + Productos8.precioFinal();
  397.  
  398.  
  399.  
  400. for(int x=0; x<p.size(); x++) {
  401.  
  402. int randomIndex = ran.nextInt(p.size());
  403. System.out.println("Nombre" + "----" + "Cantidad" +"----" +"Precio" +"----" + "----" + "Total") ;
  404. System.out.println(p.get(x).getNombres()+ espacio + p.get(randomIndex).getCantidad() + espacio1+ p.get(randomIndex).getPrecio()+ espacio2 + p.get(x).precioFinal());
  405.  
  406.  
  407.  
  408. }
  409. System.out.println("Precio TOTAL: " + preciototal);
  410.  
  411. }
  412.  
  413.  
  414.  
  415.  
  416.  
  417. }
Add Comment
Please, Sign In to add comment