Advertisement
damarijsilva

ejercicio2-tp4

Oct 13th, 2017
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.91 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace ejercicio2
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             Lista<Producto> lista = new Lista<Producto>();
  13.             int opc;
  14.             do
  15.             {
  16.                                
  17.                 Console.WriteLine("Agregar producto al principio de la lista..........[1]");
  18.                 Console.WriteLine("Agregar producto al final de la lista..............[2]");
  19.                 Console.WriteLine("Eliminar producto desde el principio de la lista...[3]");
  20.                 Console.WriteLine("Eliminar producto desde el final de la lista.......[4]");
  21.                 Console.WriteLine("Mostrar productos..................................[5]");
  22.                 Console.WriteLine("SALIR..............................................[6]");
  23.                 Console.Write("ESCOJA UNA OPCION:~ ");
  24.                 opc = Int32.Parse(Console.ReadLine());
  25.  
  26.                 switch (opc)
  27.                 {
  28.                     case 1: lista.AddToHead(NuevoProducto());
  29.                             Console.Clear();
  30.                             break;
  31.                     case 2: lista.AddToLast(NuevoProducto());
  32.                             Console.Clear();
  33.                             break;
  34.                     case 3: lista.RemoveFromHead();
  35.                             Console.Write("Eliminado");
  36.                             Console.ReadKey();
  37.                             Console.Clear();                            
  38.                             break;
  39.                     case 4: lista.RemoveFromTail();
  40.                             Console.Write("Eliminado");
  41.                             Console.ReadKey();
  42.                             Console.Clear();
  43.                             break;
  44.                     case 5: Console.Clear();                        
  45.                             lista.viewList();
  46.                             Console.ReadKey();
  47.                             Console.Clear();
  48.                             break;
  49.                     case 6: Console.Write("Bye Bye");
  50.                             break;
  51.                     default:Console.WriteLine("Ops, opcion incorrecta");
  52.                             Console.ReadKey();
  53.                             break;
  54.                 }                
  55.  
  56.             }while(opc!=6);
  57.         }
  58.  
  59.  
  60.         public static Producto NuevoProducto()
  61.         {
  62.             Producto P = new Producto();
  63.             Console.Write("Código: ");
  64.             P.Code = int.Parse(Console.ReadLine());
  65.             Console.Write("Descripción: ");
  66.             P.Description = Console.ReadLine();
  67.             Console.Write("Fecha de expiración (dd/mm/yyyy): ");
  68.             P.ExpirationDate = Convert.ToDateTime(Console.ReadLine());
  69.             Console.Write("Precio: $");
  70.             P.SalePrice = double.Parse(Console.ReadLine(), System.Globalization.CultureInfo.InvariantCulture);
  71.             return P;
  72.         }
  73.     }
  74. }
  75.  
  76.  
  77. /*-------------------------CLASE PRODUCTO--------------------------------------*/
  78. using System;
  79. using System.Collections.Generic;
  80. using System.Linq;
  81. using System.Text;
  82.  
  83. namespace ejercicio2
  84. {
  85.     class Producto
  86.     {
  87.         private int code;        
  88.         private string description;
  89.         private DateTime expirationDate;
  90.         private double salePrice;
  91.  
  92.         public Producto() { }
  93.  
  94.         public Producto(int code, DateTime expirationDate, string description, double saleprice)
  95.         {
  96.             this.Code = code;
  97.             this.ExpirationDate = expirationDate;
  98.             this.Description = description;
  99.             this.SalePrice = saleprice;
  100.         }
  101.  
  102.         public int Code
  103.         {
  104.             get
  105.             {
  106.                 return code;
  107.             }
  108.  
  109.             set
  110.             {
  111.                 code = value;
  112.             }
  113.         }
  114.  
  115.         public DateTime ExpirationDate
  116.         {
  117.             get
  118.             {
  119.                 return expirationDate;
  120.             }
  121.  
  122.             set
  123.             {
  124.                 expirationDate = value;
  125.             }
  126.         }
  127.  
  128.         public string Description
  129.         {
  130.             get
  131.             {
  132.                 return description;
  133.             }
  134.  
  135.             set
  136.             {
  137.                 description = value;
  138.             }
  139.         }
  140.  
  141.         public double SalePrice
  142.         {
  143.             get
  144.             {
  145.                 return salePrice;
  146.             }
  147.  
  148.             set
  149.             {
  150.                 salePrice = value;
  151.             }
  152.         }
  153.  
  154.         public override string ToString()
  155.         {
  156.             return "\nCodigo: " + this.code + "\nDescripcion: " + this.description + "\nFechaExpiracion: " + this.ExpirationDate.ToShortDateString() + "\nprecio: $" + this.salePrice;
  157.  
  158.         }
  159.    
  160.     }
  161. }
  162.  
  163.  
  164. /*-------------------------CLASE LISTA CON CLASE NODO PRIVADA--------------------------------------*/
  165.  
  166. using System;
  167. using System.Collections.Generic;
  168. using System.Linq;
  169. using System.Text;
  170.  
  171. namespace ejercicio2
  172. {
  173.     class Lista<E>
  174.     {
  175.         private Node head;
  176.         private int count;
  177.         private Node tail;
  178.  
  179.         public bool Empty
  180.         {
  181.             get { return this.count <= 0; }
  182.         }
  183.  
  184.         public Lista()
  185.         {
  186.             this.head = null;
  187.             this.count = 0;
  188.             this.tail = null;
  189.         }
  190.  
  191.         public void AddToHead(E item)
  192.         {
  193.             Node temp = new Node(item ,this.head);
  194.             if(Empty)
  195.             {
  196.                 this.tail = temp;
  197.             }
  198.             this.head = temp;
  199.             ++this.count;            
  200.            
  201.         }
  202.  
  203.         public void AddToLast(E item)
  204.         {
  205.             Node temp = new Node(item, null);
  206.             if (Empty)
  207.             {
  208.                 this.head = temp;
  209.             }
  210.             else
  211.             {
  212.                 this.tail.next = temp;
  213.             }
  214.             this.tail = temp;
  215.             ++this.count;          
  216.         }
  217.  
  218.         public E RemoveFromHead()
  219.         {
  220.             if (Empty)
  221.             {
  222.                 throw new  Exception("La lista esta vacia");
  223.             }
  224.             //toma el elemento que esta en el primer nodo
  225.  
  226.             E item = this.head.item;
  227.             //avanza el primer nodo al siguiente
  228.  
  229.             this.head = this.head.next;
  230.             //si no hay mas nodos
  231.             if (this.head == null)
  232.             {
  233.                 //vaciar la lista
  234.                 this.tail = null;
  235.             }
  236.             --this.count;
  237.             return item;
  238.         }
  239.  
  240.  
  241.         public E RemoveFromTail()
  242.         {
  243.             if (Empty)
  244.             {
  245.                 throw new Exception("La lista esta vacia");
  246.             }
  247.             E item = this.tail.item;
  248.  
  249.             //si es el unico nodo
  250.             if (this.head.next == null)
  251.             {
  252.                 //vacia la lista
  253.                 this.head = this.tail = null;
  254.             }
  255.             else
  256.             {
  257.                 Node skip = this.head;
  258.  
  259.                 //recorre la lista mientras haya dos o mas nodos
  260.                 for (; skip.Next.Next != null; skip = skip.next)
  261.                 { }
  262.  
  263.                 //skip es el penultimo nodo que ahora sera el ultimo
  264.                 this.tail = skip;
  265.  
  266.                 //anula la referencia del al siguiente nodo
  267.                 this.tail.next = null;
  268.             }
  269.             --this.count;
  270.             return item;
  271.  
  272.         }
  273.  
  274.  
  275.         public void viewList()
  276.         {
  277.             if (Empty)
  278.             {
  279.                 Console.WriteLine("Lista vacía...");
  280.             }
  281.             else
  282.             {
  283.                 Node aux = this.head;
  284.                 while (aux.Next != null)
  285.                 {
  286.                     Console.WriteLine(aux.Item);
  287.                     aux = aux.Next;
  288.                 }
  289.                 Console.WriteLine(aux.Item);
  290.  
  291.             }
  292.         }
  293.        
  294.      
  295. //-------------------------------------------------------------------------------------------------------------------------------------
  296.  
  297.         private class Node
  298.         {
  299.             public E item;
  300.             public Node next;
  301.  
  302.             public Node()
  303.             {
  304.             }
  305.  
  306.             public Node(E item)
  307.             {
  308.                 this.item = item;
  309.             }
  310.  
  311.             public Node(E item, Node next)
  312.             {
  313.                 this.item = item;
  314.                 this.next = next;
  315.             }
  316.  
  317.             public Node(Node next)
  318.             {
  319.                 this.next = next;
  320.             }
  321.  
  322.             public Node Next
  323.             {
  324.                 get { return this.next; }
  325.                 set { this.next = value;}
  326.             }
  327.  
  328.             public E Item
  329.             {
  330.                 get { return this.item; }
  331.                 set { this.item = value;}
  332.             }
  333.         }
  334.  
  335.  
  336.     }
  337. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement