Está en la página 1de 8

RESOLUCIÓN DEL EJERCICIO 4

 
Bien, aquí lo tienes. ¿Cómo que es largo? Bueno, puede que sí, si lo comparas con lo
que hemos venido haciendo hasta ahora, pero no es nada comparado con un programa de
verdad...
 
using System;
using System.Collections;
 
namespace Facturas
{
    public class Factura
    {
        private string nombre="";
        private string ape1="";
        private string ape2="";
        private string direccion="";
        private string poblacion="";
        private string provincia="";
        private string codpostal="";
        private string nif="";
        private byte iva=0;
        private Detalles detalles=new Detalles();
 
        public string Nombre
        {
            get
            {
                return this.nombre;
            }
            set
            {
                this.nombre=value;
            }
        }
        public string Ape1
        {
            get
            {
                return this.ape1;
            }
            set
            {
                this.ape1=value;
            }
        }
        public string Ape2
        {
            get
            {
                return this.ape2;
            }
            set
            {
                this.ape2=value;
            }
        }
        public string Direccion
        {
            get
            {
                return this.direccion;
            }
            set
            {
                this.direccion=value;
            }
        }
        public string Poblacion
        {
            get
            {
                return this.poblacion;
            }
            set
            {
                this.poblacion=value;
            }
        }
        public string Provincia
        {
            get
            {
                return this.provincia;
            }
            set
            {
                this.provincia=value;
            }
        }
        public string CodPostal
        {
            get
            {
                return this.codpostal;
            }
            set
            {
                this.codpostal=value;
            }
        }
        public string NIF
        {
            get
            {
                return this.nif;
            }
            set
            {
                this.nif=value;
            }
        }
 
        public byte IVA
        {
            get
            {
                return this.iva;
            }
            set
            {
                this.iva=value;
            }
        }
 
        public Detalles Detalles
        {
            get
            {
                return this.detalles;
            }
        }
 
        public static Factura operator+(Factura f, Detalle d)
        {
            Factura ret=f.Clone();
 
            ret.detalles[ret.detalles.Count]=d;
 
            return ret;
        }
 
        public decimal BaseImponible
        {
            get
            {
                decimal ret=0;
 
                for (int i=0;i<this.detalles.Count;i++)
                    ret+=this.Detalles[i].Importe;
 
                return ret;
            }
        }
 
        public decimal Cuota
        {
            get
            {
                return this.BaseImponible*this.iva/100;
            }
        }
 
        public decimal Total
        {
            get
            {
                return this.BaseImponible+this.Cuota;
            }
        }
 
        public Factura Clone()
        {
            Factura ret=new Factura();
 
            ret.nombre=this.nombre;
            ret.ape1=this.ape1;
            ret.ape2=this.ape2;
            ret.direccion=this.direccion;
            ret.poblacion=this.poblacion;
            ret.provincia=this.provincia;
            ret.codpostal=this.codpostal;
            ret.nif=this.nif;
            ret.iva=this.iva;
 
            for (int i=0; i<this.detalles.Count;i++)
                ret.detalles[i]=this.detalles[i];
 
            return ret;
        }
 
        public override string ToString()
        {
            string ln="\n";
 
            return this.nombre+" "+this.ape1+" "+this.ape2+ln+
                this.detalles.ToString()+ln+"BASE IMPONIBLE: "+ln+
                this.BaseImponible.ToString()+ln+"CUOTA: "+
                this.Cuota.ToString()+ln+"TOTAL DE LA FACTURA: "
                +this.Total.ToString()+ln;
        }
    }
 
    public class Detalles
    {
        ArrayList det=new ArrayList();
 
        public int Count
        {
            get
            {
                return det.Count;
            }
        }
 
        public Detalle this[int idx]
        {
            get
            {
                if (this.det.Count==0 || idx>=this.det.Count || idx<0)
                    throw new Exception("No se encuentra en la
lista");
                else
                    return (Detalle) this.det[idx];
            }
            set
            {
                if (idx>this.det.Count || idx<0)
                    throw new Exception("No se puede asignar a este
elemento");
                else
                {
                    if (value==null)
                        this.det.Remove(this.det[idx]);
                    else
                        this.det.Add(value);
                }
            }
        }
 
        public override string ToString()
        {
            string ret="";
            string ln="\n";
            
            for (int i=0;i<this.Count;i++)
                ret+=this[i].ToString()+ln;
 
            return ret;
        }
    }
 
    public class Detalle
    {
        private int cantidad;
        private string descripcion;
        private decimal precio;
 
   
public Detalle(int cantidad, string descripcion, decimal precio)
        {
            this.cantidad=cantidad;
            this.descripcion=descripcion;
            this.precio=precio;
        }
 
        public int Cantidad
        {
            get
            {
                return this.cantidad;
            }
            set
            {
                this.cantidad=value;
            }
        }
 
        public string Descripcion
        {
            get
            {
                return this.descripcion;
            }
            set
            {
                this.descripcion=value;
            }
        }
 
        public decimal Precio
        {
            get
            {
                return this.precio;
            }
            set
            {
                this.precio=value;
            }
        }
 
        public decimal Importe
        {
            get
            {
                return (decimal) this.cantidad * this.precio;
            }
        }
 
        public override string ToString()
        {
            return this.cantidad.ToString()+" "+
                this.descripcion+" "+this.precio.ToString()+" "+
                this.Importe.ToString();
        }
    }
 
    public class FacturasApp
    {
        static void Main(string[] args)
        {
            byte opcion=0;
            Detalle d;
            int idx;
 
            Factura f=new Factura();
 
            Console.Write("Dame el nombre: ");
            f.Nombre=Console.ReadLine();
 
            Console.Write("Dame el primer apellido: ");
            f.Ape1=Console.ReadLine();
 
            Console.Write("Dame el segundo apellido: ");
            f.Ape2=Console.ReadLine();
 
            Console.Write("Dame la dirección: ");
            f.Direccion=Console.ReadLine();
            
            Console.Write("Dame la población: ");
            f.Poblacion=Console.ReadLine();
 
            Console.Write("Dame la provincia: ");
            f.Provincia=Console.ReadLine();
 
            Console.Write("Dame el código postal: ");
            f.CodPostal=Console.ReadLine();
 
            Console.Write("Dame el NIF/CIF: ");
            f.NIF=Console.ReadLine();
 
            do
            {
                try
                {
                    Console.Write("Dame el IVA: ");
                    f.IVA=Byte.Parse(Console.ReadLine());
                }
                catch
                {
                    continue;
                }
            } while (f.IVA<=0);
 
            while (opcion!=4)
            {
                opcion=Menu(f);
 
                switch (opcion)
                {
                    case 1:
                        Console.WriteLine();
                        d=NuevoDetalle();
                        f+=d;
                        Console.WriteLine();
                        break;
                    case 2:
                        Console.WriteLine();
                        if (f.Detalles.Count==0) continue;
 
                        Console.Write("Dame el índice a eliminar: ");
                        idx=Int32.Parse(Console.ReadLine());
 
                        f.Detalles[idx]=null;
                        Console.WriteLine();
                        break;
                    case 3:
                        Console.WriteLine();
                        Console.WriteLine(f.ToString());
                        Console.WriteLine();
                        break;
                }
            }
        }
 
        static byte Menu(Factura f)
        {
            byte opcion=0;
 
            Console.WriteLine("La factura consta de {0} líneas de
detalle",f.Detalles.Count);
            Console.WriteLine("1. Añadir una línea de detalle");
            Console.WriteLine("2. Borrar una línea de detalle");
            Console.WriteLine("3. Ver factura actual");
            Console.WriteLine("4. Finalizar");
            Console.Write("Elige una opción: ");
 
            do
            {
                try
                {
                    opcion=Byte.Parse(Console.ReadLine());
                }
                catch
                {
                    continue;
                }
            } while (opcion<=0 || opcion >4);
 
            return opcion;
        }
 
        static Detalle NuevoDetalle()
        {
            Console.Write("Dame la descripción: ");
            string descripcion=Console.ReadLine();
 
            Console.Write("Dame la cantidad: ");
            int cantidad=Int32.Parse(Console.ReadLine());
 
            Console.Write("Dame el precio: ");
            decimal precio=Decimal.Parse(Console.ReadLine());
 
            Detalle d=new Detalle(cantidad,descripcion,precio);
 
            return d;
        }
    }
}

··> Ver todos los cursos

También podría gustarte