Solucionado (ver solução)
Solucionado
(ver solução)
2
respostas

Resposta - Exercícios

Exercício 1:

public class BankAccount
{
    public int AccountNumber { get; set; }
    public string Holder { get; set; }
    public double Balance { get; set; }
    public string Password { get; set; }

    public BankAccount(int accountNumber, string holder, double balance, string password)
    {
        AccountNumber = accountNumber;
        Holder = holder;
        Balance = balance;
        Password = new string('*', 3) + password[3..^3] + new string('*', 3);
    }

    public void ShowDetails()
    {
        Console.WriteLine($"Account Number: {AccountNumber}");
        Console.WriteLine($"Holder: {Holder}");
        Console.WriteLine($"Balance: {Balance:F2}");
        Console.WriteLine($"Password: {Password}");
    }
}

Exercício 2:

public class Car
{
    public string Manufacturer { get; set; }
    public string Model { get; set; }
    public int Year { get; set; }
    public int Speed { get; set; } = 0;
    public string DetailedDescription => $"Car model: {this.Manufacturer} {this.Model} {this.Year}";

    public Car(string manufacturer, string model, int year)
    {
        Manufacturer = manufacturer;
        Model = model;
        Year = year;
    }

    public void Accelerate()
    {
        Console.WriteLine("Accelerating...");
        do
        {
            Speed += 5;
            Console.WriteLine($"Speed {Speed} km/h.");

            if (Speed >= 100)
                break;

        } while (true);
    }

    public void Brake()
    {
        Console.WriteLine("Braking...");
        do
        {
            Speed -= 5;
            Console.WriteLine($"Speed {Speed} km/h.");

            if (Speed <= 0)
                break;

        } while (true);
    }

    public static void Honk()
    {
        Console.WriteLine("Beep Beep");
    }
}

Exercício 3:

public class Car
{
    private int _year;
    public string Manufacturer { get; set; }
    public string Model { get; set; }
    public int Year
    {
        get => _year;
        set
        {
            if (value < 1960 || value > 2023)
                throw new ArgumentOutOfRangeException(nameof(Year));
            _year = value;
        }
    }
    public int Speed { get; set; } = 0;
    public string DetailedDescription => $"Car model: {this.Manufacturer} {this.Model} {this.Year}";

    public Car(string manufacturer, string model, int year)
    {
        Manufacturer = manufacturer;
        Model = model;
        Year = year;
    }

    public void Accelerate()
    {
        Console.WriteLine("Accelerating...");
        do
        {
            Speed += 5;
            Console.WriteLine($"Speed {Speed} km/h.");

            if (Speed >= 100)
                break;

        } while (true);
    }

    public void Brake()
    {
        Console.WriteLine("Braking...");
        do
        {
            Speed -= 5;
            Console.WriteLine($"Speed {Speed} km/h.");

            if (Speed <= 0)
                break;

        } while (true);
    }

    public static void Honk()
    {
        Console.WriteLine("Beep Beep");
    }
}

Exercício 4:

public class Product
{
    private double _price;
    private int _stock;
    public string Name { get; set; }
    public string Brand { get; set; }
    public double Price
    {
        get => _price;
        set
        {
            if (value < 0)
                throw new ArgumentOutOfRangeException(nameof(Price), "Price cannot be negative.");
            _price = value;
        }
    }
    public int Stock
    {
        get => _stock;
        set
        {
            if (value < 0)
                throw new ArgumentOutOfRangeException(nameof(Stock), "Stock cannot be negative.");
            _stock = value;
        }
    }
    public string DetailedDescription => $"Product: {this.Name} - Brand: {this.Brand} - Stock: {this.Stock} - Price: {this.Price:F2}";

    public Product(string name, string brand, double price, int stock)
    {
        Name = name;
        Brand = brand;
        Price = price;
        Stock = stock;
    }
}
2 respostas
solução!

Olá, Carlos, como vai?

Seus exercícios estão bem organizados e demonstram domínio das propriedades e validações nas classes. As soluções mantêm coerência entre si e atendem ao que foi proposto, com encapsulamento adequado nos pontos em que há validação.

Parabéns por compartilhar seu trabalho. Qualquer nova dúvida, o fórum está à disposição.

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

Obrigado pelo suporte, Mike.