1
resposta

[Projeto] TicketDiscountCalculation

namespace TicketDiscountCalculation
{
    public class TicketSales()
    {
        //set the base ticket price (constant)
        private const decimal BaseTicketPrice = 20m;
        //set the minimun and maximum age for the benefit (constant)
        private const int MinDiscountAge = 18;
        private const int MaxDiscountAge = 65;

        public void RunSalesProcess()
        {
            //variable used to calculate today's year
            int currentYear = DateTime.Now.Year;

            DisplayHeader();

            //try to get a valid number of tickets
            decimal ticketQuantity = GetTicketQuantity();
            if (ticketQuantity <= 0)
            {
                Console.WriteLine("\nOperation canceled. Enter a valid number of tickets..");
                return;
            }

            //ask about the half price benefit
            bool hasHalfPriceBenefit = AskForHalfPriceBenefit();

            //if the answer is NO, calculate the total and exit
            if (!hasHalfPriceBenefit)
            {
                decimal total = ticketQuantity * BaseTicketPrice;
                Console.WriteLine($"\n{ticketQuantity} Tickets Selected. Total to pay: ${total:F2}.");
                return;
            }

            //if the answer is YES, check the condition by age rules
            int birthYear = GetBirthYear();
            int age = currentYear - birthYear;

            //variables for the final calculation
            decimal finalPrice;
            string message;

            if (age < MinDiscountAge || age >= MaxDiscountAge) //under 18 or over 65
            {
                finalPrice = ticketQuantity * (BaseTicketPrice / 2m);
                message = "Eligible age for Half-Price Tickets.";
            }
            else
            {
                finalPrice = ticketQuantity * BaseTicketPrice;
                message = "Age not eligible for Half-Price Tickets.";
            }

            //show the resume
            Console.WriteLine($"\n--- PURCHASE SUMMARY ---");
            Console.WriteLine($"Age: {age} Years.");
            Console.WriteLine($"{message}");
            Console.WriteLine($"Tickets total: {ticketQuantity}. Final value ${finalPrice:F2}.");
        }

        private static void DisplayHeader()
        {
            Console.WriteLine("***************************************");
            Console.WriteLine("*****WELCOME TO THE MOVIE THEATHER*****");
            Console.WriteLine("***************************************");
            Console.WriteLine("\n**TODAY'S MOVIE IS STAR WARS - EP. IV**");
            Console.WriteLine($"*THE PRICE OF THE MOVIE TODAY IS ${BaseTicketPrice:F2}");
        }
        private static decimal GetTicketQuantity()
        {
            decimal quantity;
            Console.WriteLine("\n*****HOW MANY TICKETS DO YOU WANT?*****");

            //loop to ensure input is a valid, positive number
            while (!decimal.TryParse(Console.ReadLine(), out quantity) || quantity <= 0)
            {
                Console.WriteLine("Invalid input.Please enter an integer (ex. 2):");
            }
            return quantity;
        }

        private static bool AskForHalfPriceBenefit()
        {
            string answer;
            Console.WriteLine("\n*DO YOU HAVE HALF PRICE BENEFIT? (Y/N)*");

            //loop to ensure a valid answer ('Y' or 'N')
            while (true)
            {
                answer = (Console.ReadLine() ?? "").Trim().ToUpper();
                if (answer == "Y") return true;
                if (answer == "N") return false;

                Console.WriteLine("Invalid answer. Please enter 'Y' for Yes or 'N' for No:");
            }
        }

        private static int GetBirthYear()
        {
            int year;
            Console.WriteLine("What is your birth year? (ex. 1990).");

            //loop to ensure a valid year of birth
            while (!int.TryParse(Console.ReadLine(), out year) || year < 1900 || year > DateTime.Today.Year)
            {
                Console.WriteLine($"Invalid input. Enter a valid year between 1900 and {DateTime.Today.Year}");
            }
            return year;
        }
    }
}

Matricule-se agora e aproveite até 50% OFF

O maior desconto do ano para você evoluir com a maior escola de tecnologia

QUERO APROVEITAR
1 resposta

Oi, Rafael! Como vai?

Agradeço por compartilhar suas reflexões e aprendizados com a comunidade Alura.

Gostei muito da sua implementação no projeto TicketDiscountCalculation. A estrutura está clara e o uso de constantes para o preço base e faixas etárias mostra uma boa organização e legibilidade do código. A lógica para verificar o benefício de meia-entrada ficou bem construída e com validações adequadas.

Continue praticando e testando suas condições com diferentes idades e quantidades de ingressos.

Alura Conte com o apoio da comunidade Alura na sua jornada. Abraços e bons estudos!