Advertisement
Guest User

Trabajo N°4 Ejercicio 2

a guest
Oct 12th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.67 KB | None | 0 0
  1. // Clase Lista
  2. public class List<T> {
  3.    
  4.     private Node<T> head;
  5.     private Node<T> tail;
  6.  
  7.     public List() {
  8.  
  9.         this.head = null;
  10.         this.tail = null;
  11.     }
  12.  
  13.     public boolean getIsEmpty() {
  14.         return this.head==null;
  15.     }
  16.  
  17.     public void AddToHead (T item) {
  18.        
  19.         if (getIsEmpty()) {
  20.            
  21.             this.head = this.tail = new Node<T>(item,null);
  22.            
  23.         }
  24.         else {
  25.            
  26.             Node<T> temp = new Node<T>(item,null);
  27.             temp.setNext(this.head);
  28.             this.head = temp;
  29.            
  30.         }
  31.     }
  32.    
  33.     public void AddToTail(T item) {
  34.        
  35.         if (getIsEmpty()){
  36.            
  37.             this.head = this.tail = new Node<T>(item, null);
  38.            
  39.         }else{
  40.            
  41.             Node<T> temp = new Node<T>(item, null);
  42.             this.tail.setNext(temp);
  43.             this.tail = temp;
  44.            
  45.         }
  46.        
  47.     }
  48.    
  49.     public T RemoveFromHead() {
  50.         if(getIsEmpty()){
  51.             throw new RuntimeException("La lista esta vacia...");
  52.         }
  53.        
  54.         T item=this.head.getItem();//toma el elemento que esta en el primer nodo
  55.         this.head=this.head.getNext();//avanza el primer nodo al siguiente
  56.        
  57.         if(this.head==null){//si no hay mas nodos
  58.             this.tail=null;//vaciar la lista
  59.         }
  60.         return item;
  61.     }
  62.    
  63.     public T RemoveFromTail() {
  64.         if(getIsEmpty()){
  65.             throw new RuntimeException("La lista esta vacia");
  66.         }
  67.         T item = this.tail.getItem();
  68.        
  69.         if(this.head.getNext()==null){//hay un unico nodo
  70.            
  71.             this.head = this.tail =null;//vacía la lista
  72.            
  73.         }else{
  74.            
  75.             Node<T> skip = new Node<T>();
  76.             skip = this.head;
  77.             for(; skip.getNext().getNext() != null; skip = skip.getNext()){}//recorre la lista mientras haya dos nodos mas
  78.             this.tail = skip;//skip es el penultimo nodo que ahora sera el ultimo
  79.             this.tail.setNext(null);
  80.            
  81.         }
  82.         return item;//regresa el elemento
  83.     }
  84.    
  85.     public boolean Contains(T item){
  86.         for(Node<T> skip=this.head; skip != null; skip = skip.getNext()){
  87.             if (item == skip.getItem()) {
  88.                 return true;
  89.             }
  90.         }
  91.         return false;
  92.     }
  93.    
  94.     public void Mostrar(){
  95.         for(Node<T> skip=this.head; skip != null; skip = skip.getNext()){
  96.             System.out.printf("%s ", skip.item.toString());
  97.             System.out.println("\n---------------------------------------");
  98.         }
  99.     }
  100. }
  101.  
  102. // Clase Nodo
  103. public class Node<T> {
  104.    
  105.     public T item;
  106.     private Node<T> next;
  107.  
  108.     public Node() {
  109.        
  110.         this.item = null;
  111.         this.next = null;
  112.        
  113.     }
  114.    
  115.     public Node(T item) {
  116.        
  117.         this.item = item;
  118.         this.next = null;
  119.        
  120.     }
  121.    
  122.     public Node(T item, Node<T> next) {
  123.        
  124.         this.item = item;
  125.         this.next = next;
  126.        
  127.     }
  128.  
  129.     public T getItem() {
  130.         return item;
  131.     }
  132.  
  133.     public void setItem(T item) {
  134.         this.item = item;
  135.     }
  136.  
  137.     public Node<T> getNext() {
  138.         return next;
  139.     }
  140.  
  141.     public void setNext(Node<T> next) {
  142.         this.next = next;
  143.     }
  144.  
  145. }
  146.  
  147. // Clase Producto
  148. public class Product {
  149.  
  150.     private int code;
  151.     private String description;
  152.     private String expirationDate;
  153.     private double salePrice;
  154.    
  155.  
  156.     public Product(int code, String description, double salePrice, String expirationDate) {
  157.         this.code = code;
  158.         this.description = description;
  159.         this.salePrice = salePrice;
  160.         this.expirationDate= expirationDate;
  161.     }
  162.  
  163.     public int getCode() {
  164.         return code;
  165.     }
  166.  
  167.     public void setCode(int code) {
  168.         this.code = code;
  169.     }
  170.  
  171.     public String getDescription() {
  172.         return description;
  173.     }
  174.  
  175.     public void setDescription(String description) {
  176.         this.description = description;
  177.     }
  178.  
  179.     public String getExpirationDate() {
  180.         return expirationDate;
  181.     }
  182.  
  183.     public void setExpirationDate(String expirationDate) {
  184.         this.expirationDate = expirationDate;
  185.     }
  186.  
  187.     public double getSalePrice() {
  188.         return salePrice;
  189.     }
  190.  
  191.     public void setSalePrice(float salePrice) {
  192.         this.salePrice = salePrice;
  193.     }
  194.  
  195.     @Override
  196.     public String toString() {
  197.         return "\nCodigo: "+this.code +" \nDescripcion: "+this.description+" \nPrecio: $"+((Double)this.salePrice).toString()+
  198.                 "\nFecha de Expiración: "+ this.expirationDate;
  199.     }
  200.  
  201. }
  202.  
  203. // Main
  204. import java.util.Scanner;
  205. import java.util.Random;
  206.  
  207. public class TP04E2 {
  208.  
  209.     public static void main(String[] args) {
  210.         TP04E2 programa = new TP04E2();
  211.         programa. execute();
  212.     }
  213.    
  214.     private void execute() {
  215.         List<Product> productos = new List<>();
  216.         Scanner t = new Scanner(System.in);
  217.         Random rand = new Random();
  218.         int op,cant;
  219.        
  220.         do{
  221.             System.out.println("************************MENU************************");
  222.             System.out.println("1-Agregar Productos al principio de la lista");
  223.             System.out.println("2-Agregar Productos al final de la lista");
  224.             System.out.println("3-Eliminar Producto desde el principio de la lista");
  225.             System.out.println("4-Eliminar Producto desde el final de la lista");
  226.             System.out.println("5-Mostrar la lista de Productos");
  227.             System.out.println("0-Salir");
  228.             System.out.print("Eliga una opcion: ");
  229.             op=t.nextInt();
  230.            
  231.             switch (op){
  232.             case 1:
  233.                 System.out.print("Ingrese cantidad de productos a agregar: ");
  234.                 cant = t.nextInt();
  235.                 for (int i=0; i<cant;i++){
  236.                     productos.AddToHead(new Product(Entero(10000,20000),Descripcion(),(rand.nextDouble()+0.1)*40,FechaExpiracion()));
  237.                 }
  238.                 break;
  239.                
  240.             case 2:
  241.                 System.out.print("Ingrese cantidad de productos a agregar: ");
  242.                 cant = t.nextInt();
  243.                 for (int i=0; i<cant;i++){
  244.                     productos.AddToTail(new Product(Entero(10000,20000),Descripcion(),Entero(20,40),FechaExpiracion()));
  245.                    
  246.                 }
  247.                 break;
  248.                
  249.             case 3:
  250.                 System.out.println("\n---------------------------------");
  251.                 System.out.println("Producto eliminado del principio: "+ "\n" + productos.RemoveFromHead());
  252.                 System.out.println("\n---------------------------------");
  253.                 break;
  254.                
  255.             case 4:
  256.                 System.out.println("\n---------------------------------");
  257.                 System.out.println("Producto eliminado del final: "+ "\n" + productos.RemoveFromTail());
  258.                 System.out.println("\n---------------------------------");
  259.                 break;
  260.                
  261.             case 5:
  262.                 System.out.println("\n*************************\nListado de Productos: ");
  263.                 productos.Mostrar();
  264.                 System.out.println();
  265.  
  266.                 break;
  267.            
  268.             case 6:
  269.                 System.out.println("Fin del Programa...");
  270.                 break;
  271.           }
  272.            
  273.         }while (op!=0);
  274.        
  275.         t.close();
  276.     }
  277.    
  278.    
  279.    
  280.     private int Entero(int minimo, int maximo) {
  281.         int num=(int)Math.floor(Math.random()*(minimo-(maximo+1))+(maximo+1));
  282.         return num;
  283.     }
  284.    
  285.     private String Descripcion() {
  286.         int al = Entero(1,4);
  287.         switch (al) {
  288.         case 1:
  289.             return "Gaseosa";
  290.         case 2:
  291.             return "Galleta";
  292.         case 3:
  293.             return "Leche";
  294.         case 4:
  295.             return "Mayonesa";
  296.         }
  297.         return "";
  298.     }
  299.    
  300.     public String FechaExpiracion() {
  301.         int year = Entero(1990,2020);
  302.         int month = Entero(1,12);
  303.         int day;
  304.         switch (month){
  305.         case 2: day = Entero(1,28);
  306.                 break;
  307.         default:  day = Entero(1,30);
  308.                 break;
  309.         }
  310.         return day+"/"+month+"/"+year;
  311.     }
  312.  
  313. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement