lazacos cigi

gay
 avatar
unknown
csharp
5 months ago
3.0 kB
2
Indexable
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

public class Plant
{
    public string Name { get; set; }
    public string Botanical { get; set; }
    public int Zone { get; set; }
    public string Light { get; set; }
    public decimal Price { get; set; }
    public string Availability { get; set; }

    // Static method to import plants from XML
    public static List<Plant> ImportFromXML(string xmlFilePath)
    {
        XDocument doc = XDocument.Load(xmlFilePath);
        var plants = doc.Descendants("PLANT")
                        .Select(p => new Plant
                        {
                            Name = p.Element("NAME").Value,
                            Botanical = p.Element("BOTANICAL").Value,
                            Zone = int.Parse(p.Element("ZONE").Value),
                            Light = p.Element("LIGHT").Value,
                            Price = decimal.Parse(p.Element("PRICE").Value.Replace("$", "")),
                            Availability = p.Element("AVAILABILITY").Value
                        })
                        .ToList();
        return plants;
    }
}

class Program
{
    static void Main(string[] args)
    {
        // Load plants from XML
        string xmlFilePath = "path/to/your/plant_catalog.xml";
        List<Plant> plants = Plant.ImportFromXML(xmlFilePath);

        // a) List all plants' names and prices, sorted by price
        var sortedByPrice = plants
            .OrderBy(p => p.Price)
            .Select(p => new { p.Name, p.Price });
        Console.WriteLine("Plants sorted by price:");
        foreach (var plant in sortedByPrice)
        {
            Console.WriteLine($"{plant.Name}: ${plant.Price}");
        }

        // b) Plants with "Full Sun" and price < 10
        var fullSunPlants = plants
            .Where(p => p.Light == "Full Sun" && p.Price < 10)
            .Select(p => new { p.Name, p.Price });
        Console.WriteLine("\nPlants with Full Sun and price < 10:");
        foreach (var plant in fullSunPlants)
        {
            Console.WriteLine($"{plant.Name}: ${plant.Price}");
        }

        // c) Group by zone and count distinct plants
        var plantsByZone = plants
            .GroupBy(p => p.Zone)
            .Select(g => new { Zone = g.Key, Count = g.Select(p => p.Name).Distinct().Count() });
        Console.WriteLine("\nNumber of distinct plants by zone:");
        foreach (var zoneGroup in plantsByZone)
        {
            Console.WriteLine($"Zone {zoneGroup.Zone}: {zoneGroup.Count} plants");
        }

        // d) Group by light and count the number of plants in each light category
        var plantsByLight = plants
            .GroupBy(p => p.Light)
            .Select(g => new { Light = g.Key, Count = g.Count() });
        Console.WriteLine("\nNumber of plants by light category:");
        foreach (var lightGroup in plantsByLight)
        {
            Console.WriteLine($"{lightGroup.Light}: {lightGroup.Count} plants");
        }
    }
}
Editor is loading...
Leave a Comment