Поиск по строке (с применением индексатора) - C#

Узнай цену своей работы

Формулировка задачи:

Добрый день, Хочу, помимо всего прочего реализовать поиск по названию модели с применением индексатора, но так чтобы получить все возможные вхождения. Пока получается найти только первое (328 строка).
using System;
 
abstract class Device
{
    protected Double price;
    protected String vendor;
    protected String category;
    protected DateTime productionDate;
    protected Byte warranty;
    protected String model;
 
    public Double Price
    {
        get
        {
            return price;
        }
        set
        {
            if (price > 0) price = value;
        }
    }
 
    public String Vendor
    {
        get
        {
            return vendor;
        }
        set
        {
            vendor = value;
        }
    }
 
    public String Category
    {
        get
        {
            return category;
        }
        set
        {
            category = value;
        }
    }
 
    public DateTime ProductionDate
    {
        get
        {
            return productionDate;
        }
        set
        {
            productionDate = value;
        }
    }
 
    public Byte Warranty
    {
        get
        {
            return warranty;
        }
        set
        {
            if (warranty > 0) warranty = value;
        }
    }
 
    public String Model
    {
        get
        {
            return model;
        }
        set
        {
            model = value;
        }
    }
}
 
class Laptop : Device
{
    public Laptop(Double price, String vendor, String category, DateTime productionDate, Byte warranty, String model)
    {
        this.price = price;
        this.vendor = vendor;
        this.category = category;
        this.productionDate = productionDate;
        this.warranty = warranty;
        this.model = model;
    }
 
    public override String ToString()
    {
        return "Vendor: " + vendor + ", model: " + model + ", price: $" + price + ", date of production: " 
            + productionDate.ToShortDateString() + ", warranty: " + warranty + " years.";
    }
}
 
class Tablet : Device
{
    public Tablet(Double price, String vendor, String category, DateTime productionDate, Byte warranty, String model)
    {
        this.price = price;
        this.vendor = vendor;
        this.category = category;
        this.productionDate = productionDate;
        this.warranty = warranty;
        this.model = model;
    }
 
    public override String ToString()
    {
        return "Vendor: " + vendor + ", model: " + model + ", price: $" + price + ", date of production: "
            + productionDate.ToShortDateString() + ", warranty: " + warranty + " years.";
    }
}
 
class Smartphone : Device
{
    public Smartphone(Double price, String vendor, String category, DateTime productionDate, Byte warranty, String model)
    {
        this.price = price;
        this.vendor = vendor;
        this.category = category;
        this.productionDate = productionDate;
        this.warranty = warranty;
        this.model = model;
    }
 
    public override String ToString()
    {
        return "Vendor: " + vendor + ", model: " + model + ", price: $" + price + ", date of production: "
            + productionDate.ToShortDateString() + ", warranty: " + warranty + " years.";
    }
}
 
class Charger : Device
{
    public Charger(Double price, String vendor, String category, DateTime productionDate, Byte warranty, String model)
    {
        this.price = price;
        this.vendor = vendor;
        this.category = category;
        this.productionDate = productionDate;
        this.warranty = warranty;
        this.model = model;
    }
 
    public override String ToString()
    {
        return "Vendor: " + vendor + ", model: " + model + ", price: $" + price + ", date of production: "
            + productionDate.ToShortDateString() + ", warranty: " + warranty + " years.";
    }
}
 
class Case : Device
{
    public Case(Double price, String vendor, String category, DateTime productionDate, Byte warranty, String model)
    {
        this.price = price;
        this.vendor = vendor;
        this.category = category;
        this.productionDate = productionDate;
        this.warranty = warranty;
        this.model = model;
    }
 
    public override String ToString()
    {
        return "Vendor: " + vendor + ", model: " + model + ", price: $" + price + ", date of production: "
            + productionDate.ToShortDateString() + ", warranty: " + warranty + " years.";
    }
}
 
//////////////////////////////////////////////////////////////////////////////////////
 
class Store
{
    private Device[] devices;
 
    public Store(int size)
    {
        devices = new Device[size];
    }
 
    public int Length
    {
        get
        {
            return devices.Length;
        }
    }
 
    public Device this[int index]
    {
        get
        {
            if (index < 0 || index >= devices.Length)
            {
                throw new IndexOutOfRangeException();
            }
            else
            {
                return devices[index];
            }
        }
        set
        {
            devices[index] = value;
        }
    }
 
    public Device this[Double price]
    {
        get
        {
            if (price <= 0.0)
            {
                throw new IndexOutOfRangeException();
            }
 
            return this[FindByPrice(price)];
        }
    }
 
    public Device this[String name]
    {
        get
        {
            if (name.Length == 0)
            {
                //return null;
                throw new IndexOutOfRangeException();
            }
 
            return this[FindByName(name)];
        }
    }
 
    private Int32 FindByName(String name)
    {
        Int32 result = -1;
 
        for (int i = 0; i < devices.Length; i++)
        {
            if (devices[i].Vendor == name)
            {
                result = i;
                break;
            }
        }
 
        return result;
    }
 
    public Int32 FindByPrice(Double price)
    {
        Int32 result = -1;
 
        for (Int32 i = 0; i < devices.Length; i++)
        {
            if (devices[i].Price == price)
            {
                result = i;
                break;
            }
        }
 
        return result;
    }
 
    public Int32[] FindByPrice(Double priceFrom, Double priceTo)
    {
        Int32 count = 0;
 
        for (Int32 i = 0; i < devices.Length; i++)
        {
            if (devices[i].Price >= priceFrom && devices[i].Price <= priceTo)
            {
                count++;
            }
        }
        Int32[] result = new Int32[count];
 
        for (Int32 i = 0, y = 0; i < devices.Length; i++)
        {
            if (devices[i].Price >= priceFrom && devices[i].Price <= priceTo)
            {
                result[y] = i;
                y++;
            }
        }
 
        return result;
    }
}
 
class Program
{
    static void Main(string[] args)
    {
        Store s = new Store(6);
        s[0] = new Laptop(5700, "Samsung", "Laptops", DateTime.Parse("2015.11.23"), 2, "AAA");
        s[1] = new Tablet(4700.25, "Asus", "Tablets", DateTime.Parse("2016.01.01"), 1, "BBB");
        s[2] = new Smartphone(3700, "HTC", "Smartphones", DateTime.Parse("2015.10.23"), 1, "CCC");
        s[3] = new Charger(150.55, "Hiawei", "Chargers", DateTime.Parse("2016.03.17"), 1, "DDD");
        s[4] = new Case(80, "no name", "Cases", DateTime.Parse("2015.08.08"), 0, "EEE");
        s[5] = new Laptop(6700, "Samsung", "Laptops", DateTime.Parse("2016.06.23"), 2, "FFF");
 
        /*
        try
        {
            foreach(int el in s.FindByPrice(4000, 6000))
            {
                Console.WriteLine(s[el]);
            }
        }
        catch
        {
            Console.WriteLine("Ничего не найдено!");
        }*/
 
        try
        {
            for (int i = 0; i < s.Length; i++)
            {
                if (s[i] == s["Samsung"]) 
                    Console.WriteLine(s[i]);
            }
        }
        catch
        {
            Console.WriteLine("Ничего не найдено!");
        }
 
        //Console.WriteLine(s["Samsung"]);
        //Console.WriteLine(s[4700.25]);
    }
}

Решение задачи: «Поиск по строке (с применением индексатора)»

textual
Листинг программы
public IEnumerable<Device> this[String name]
    {
        get
        {
            if (name.Length == 0)
            {
                //return null;
                throw new IndexOutOfRangeException();
            }
 
            //return devices.Where(x => x.Vendor == name);
            return FindByName(name);
        }
    }
 
    private IEnumerable<Device> FindByName(String name)
    {
        foreach (Device dev in devices) {
            if (dev.Vendor == name) {
                yield return dev;
            }
        }
    }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

7   голосов , оценка 4 из 5