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

Resposta - Exercício 5

Classe ElectronicProducts:

namespace exercise73.Models;

internal class ElectronicProducts
{
    public string Brand { get; set; }
    public string Model { get; set; }
    public double Price { get; set; }

    public ElectronicProducts(string brand, string model, double price)
    {
        Brand = brand;
        Model = model;
        Price = price;
    }

    public virtual string DisplayInformation()
    {
        return $"Brand: {Brand}, Model: {Model}, Price: {Price:F2}";
    }
}

Classe Smartphone:

namespace exercise73.Models;

internal class Smartphone : ElectronicProducts
{

    public string OperatingSystem { get; set; }

    public Smartphone(string brand, string model, double price, string operatingSystem)
        : base(brand, model, price)
    {
        OperatingSystem = operatingSystem;
    }

    public override string DisplayInformation()
    {
        return $"{base.DisplayInformation()}, Operating System: {OperatingSystem}.";
    }
}

Classe Tablet:

namespace exercise73.Models;

internal class Tablet : ElectronicProducts
{
    public string ScreenType { get; set; }

    public Tablet(string brand, string model, double price, string screenType)
        : base(brand, model, price)
    {
        ScreenType = screenType;
    }

    public override string DisplayInformation()
    {
        return $"{base.DisplayInformation()}, Screen Type: {ScreenType}.";
    }
}

Classe Laptop:

namespace exercise73.Models;

internal class Laptop : ElectronicProducts
{
    public string OperatingSystem { get; set; }

    public Laptop(string brand, string model, double price, string operatingSystem)
        : base(brand, model, price)
    {
        OperatingSystem = operatingSystem;
    }

    public override string DisplayInformation()
    {
        return $"{base.DisplayInformation()}, Operating System: {OperatingSystem}.";
    }
}

Instâncias:

using exercise73.Models;

Smartphone smartphone = new Smartphone("Samsung", "A15", 1500, "Android");
Console.WriteLine(smartphone.DisplayInformation());
Console.WriteLine();

Laptop laptop = new Laptop("Dell", "Inspiron 15", 4000.00, "Windows");
Console.WriteLine(laptop.DisplayInformation());
Console.WriteLine();

Tablet tablet = new Tablet("Samsung", "Galaxy Tab S10", 3000.00, "Immersive");
Console.WriteLine(tablet.DisplayInformation());
Console.WriteLine();
2 respostas
solução!

Oi, Carlos! Como vai?

Agradeço por compartilhar seu código com a comunidade Alura.

Ficou claro como estruturou bem a herança entre os tipos de produtos. O uso do virtual e override deixa cada classe com sua própria identidade sem perder o comportamento base, o que chamou bastante atenção.

Uma dica interessante para o futuro é usar ToString() quando quiser customizar a exibição de um objeto. Veja este exemplo:


public override string ToString() {
    return $"Produto: {Brand} - {Model}";
}

Este código redefine a representação textual do objeto, facilitando exibições diretas no console.

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

Obrigado pela dica Rafaela, vou utiliza-la no futuro quando tiver um desafio semalhante.