Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Clase Lista
- public class List<T> {
- private Node<T> head;
- private Node<T> tail;
- public List() {
- this.head = null;
- this.tail = null;
- }
- public boolean getIsEmpty() {
- return this.head==null;
- }
- public void AddToHead (T item) {
- if (getIsEmpty()) {
- this.head = this.tail = new Node<T>(item,null);
- }
- else {
- Node<T> temp = new Node<T>(item,null);
- temp.setNext(this.head);
- this.head = temp;
- }
- }
- public void AddToTail(T item) {
- if (getIsEmpty()){
- this.head = this.tail = new Node<T>(item, null);
- }else{
- Node<T> temp = new Node<T>(item, null);
- this.tail.setNext(temp);
- this.tail = temp;
- }
- }
- public T RemoveFromHead() {
- if(getIsEmpty()){
- throw new RuntimeException("La lista esta vacia...");
- }
- T item=this.head.getItem();//toma el elemento que esta en el primer nodo
- this.head=this.head.getNext();//avanza el primer nodo al siguiente
- if(this.head==null){//si no hay mas nodos
- this.tail=null;//vaciar la lista
- }
- return item;
- }
- public T RemoveFromTail() {
- if(getIsEmpty()){
- throw new RuntimeException("La lista esta vacia");
- }
- T item = this.tail.getItem();
- if(this.head.getNext()==null){//hay un unico nodo
- this.head = this.tail =null;//vacía la lista
- }else{
- Node<T> skip = new Node<T>();
- skip = this.head;
- for(; skip.getNext().getNext() != null; skip = skip.getNext()){}//recorre la lista mientras haya dos nodos mas
- this.tail = skip;//skip es el penultimo nodo que ahora sera el ultimo
- this.tail.setNext(null);
- }
- return item;//regresa el elemento
- }
- public boolean Contains(T item){
- for(Node<T> skip=this.head; skip != null; skip = skip.getNext()){
- if (item == skip.getItem()) {
- return true;
- }
- }
- return false;
- }
- public void Mostrar(){
- for(Node<T> skip=this.head; skip != null; skip = skip.getNext()){
- System.out.printf("%s ", skip.item.toString());
- System.out.println("\n---------------------------------------");
- }
- }
- }
- // Clase Nodo
- public class Node<T> {
- public T item;
- private Node<T> next;
- public Node() {
- this.item = null;
- this.next = null;
- }
- public Node(T item) {
- this.item = item;
- this.next = null;
- }
- public Node(T item, Node<T> next) {
- this.item = item;
- this.next = next;
- }
- public T getItem() {
- return item;
- }
- public void setItem(T item) {
- this.item = item;
- }
- public Node<T> getNext() {
- return next;
- }
- public void setNext(Node<T> next) {
- this.next = next;
- }
- }
- // Clase Producto
- public class Product {
- private int code;
- private String description;
- private String expirationDate;
- private double salePrice;
- public Product(int code, String description, double salePrice, String expirationDate) {
- this.code = code;
- this.description = description;
- this.salePrice = salePrice;
- this.expirationDate= expirationDate;
- }
- public int getCode() {
- return code;
- }
- public void setCode(int code) {
- this.code = code;
- }
- public String getDescription() {
- return description;
- }
- public void setDescription(String description) {
- this.description = description;
- }
- public String getExpirationDate() {
- return expirationDate;
- }
- public void setExpirationDate(String expirationDate) {
- this.expirationDate = expirationDate;
- }
- public double getSalePrice() {
- return salePrice;
- }
- public void setSalePrice(float salePrice) {
- this.salePrice = salePrice;
- }
- @Override
- public String toString() {
- return "\nCodigo: "+this.code +" \nDescripcion: "+this.description+" \nPrecio: $"+((Double)this.salePrice).toString()+
- "\nFecha de Expiración: "+ this.expirationDate;
- }
- }
- // Main
- import java.util.Scanner;
- import java.util.Random;
- public class TP04E2 {
- public static void main(String[] args) {
- TP04E2 programa = new TP04E2();
- programa. execute();
- }
- private void execute() {
- List<Product> productos = new List<>();
- Scanner t = new Scanner(System.in);
- Random rand = new Random();
- int op,cant;
- do{
- System.out.println("************************MENU************************");
- System.out.println("1-Agregar Productos al principio de la lista");
- System.out.println("2-Agregar Productos al final de la lista");
- System.out.println("3-Eliminar Producto desde el principio de la lista");
- System.out.println("4-Eliminar Producto desde el final de la lista");
- System.out.println("5-Mostrar la lista de Productos");
- System.out.println("0-Salir");
- System.out.print("Eliga una opcion: ");
- op=t.nextInt();
- switch (op){
- case 1:
- System.out.print("Ingrese cantidad de productos a agregar: ");
- cant = t.nextInt();
- for (int i=0; i<cant;i++){
- productos.AddToHead(new Product(Entero(10000,20000),Descripcion(),(rand.nextDouble()+0.1)*40,FechaExpiracion()));
- }
- break;
- case 2:
- System.out.print("Ingrese cantidad de productos a agregar: ");
- cant = t.nextInt();
- for (int i=0; i<cant;i++){
- productos.AddToTail(new Product(Entero(10000,20000),Descripcion(),Entero(20,40),FechaExpiracion()));
- }
- break;
- case 3:
- System.out.println("\n---------------------------------");
- System.out.println("Producto eliminado del principio: "+ "\n" + productos.RemoveFromHead());
- System.out.println("\n---------------------------------");
- break;
- case 4:
- System.out.println("\n---------------------------------");
- System.out.println("Producto eliminado del final: "+ "\n" + productos.RemoveFromTail());
- System.out.println("\n---------------------------------");
- break;
- case 5:
- System.out.println("\n*************************\nListado de Productos: ");
- productos.Mostrar();
- System.out.println();
- break;
- case 6:
- System.out.println("Fin del Programa...");
- break;
- }
- }while (op!=0);
- t.close();
- }
- private int Entero(int minimo, int maximo) {
- int num=(int)Math.floor(Math.random()*(minimo-(maximo+1))+(maximo+1));
- return num;
- }
- private String Descripcion() {
- int al = Entero(1,4);
- switch (al) {
- case 1:
- return "Gaseosa";
- case 2:
- return "Galleta";
- case 3:
- return "Leche";
- case 4:
- return "Mayonesa";
- }
- return "";
- }
- public String FechaExpiracion() {
- int year = Entero(1990,2020);
- int month = Entero(1,12);
- int day;
- switch (month){
- case 2: day = Entero(1,28);
- break;
- default: day = Entero(1,30);
- break;
- }
- return day+"/"+month+"/"+year;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement