Esempio n. 1
0
 public void AddStorage(DataStorage storage) //Method for adding Data Storage
 {
     _storages.Add(storage);
     Console.WriteLine($"Storage {storage.Name} was added");
 }
Esempio n. 2
0
        public void Change(int index, string value, DataStorage storage) //Method for changing some property of some Data Storage
        {
            if (_storages.Count == 0)
            {
                Console.WriteLine("There are no items!");
                return;
            }
            if (!(_storages.Contains(storage)))
            {
                Console.WriteLine("Item doesn't belong to list");
                return;
            }
            else
            {
                switch (index)
                {
                case 0:     //if we want to change Name
                    storage.Name = value;
                    break;

                case 1:     //if we want to change Producer
                    storage.Producer = value;
                    break;

                case 2:     //if we want to change Model
                    storage.Model = value;
                    break;

                case 3:     //if we want to change Quantity
                    int quantity;
                    try
                    {
                        quantity = Int32.Parse(value);
                    }
                    catch (Exception)
                    {
                        Console.WriteLine("Invalid value!");
                        break;
                    }
                    storage.Quantity = quantity;
                    break;

                case 4:     //if we want to change price
                    decimal price;
                    try
                    {
                        price = Decimal.Parse(value);
                    }
                    catch (Exception)
                    {
                        Console.WriteLine("Invalid value!");
                        break;
                    }
                    storage.Price = price;
                    break;

                case 5:                 //if we want to change the first unique class property
                    if (storage is Dvd) //if object is Dvd we changing the Writing Speed
                    {
                        double writingSpeed;
                        try
                        {
                            writingSpeed = Double.Parse(value);
                        }
                        catch (Exception)
                        {
                            Console.WriteLine("Invalid value!");
                            break;
                        }
                        Dvd dvd = storage as Dvd;
                        dvd.WritingSpeed = writingSpeed;
                    }
                    else if (storage is Hdd)    //if object is Hdd we changing the Disk Size
                    {
                        int diskSize;
                        try
                        {
                            diskSize = Int32.Parse(value);
                        }
                        catch (Exception)
                        {
                            Console.WriteLine("Invalid value!");
                            break;
                        }
                        Hdd hdd = storage as Hdd;
                        hdd.DiskSize = diskSize;
                    }
                    else     //if object is Flash Drive we changing the Memory Amount
                    {
                        int memoryAmount;
                        try
                        {
                            memoryAmount = Int32.Parse(value);
                        }
                        catch (Exception)
                        {
                            Console.WriteLine("Invalid value!");
                            break;
                        }
                        FlashDrive flashDrive = storage as FlashDrive;
                        flashDrive.MemoryAmount = memoryAmount;
                    }
                    break;

                case 6:         //if we want to change the second unique class property
                    double num; //one variable, cause second unqie class property anyway has type double
                    try
                    {
                        num = double.Parse(value);
                    }
                    catch (Exception)
                    {
                        Console.WriteLine("Invalid value!");
                        break;
                    }
                    if (storage is Dvd)    //if object is Dvd we changing the Reading Speed
                    {
                        Dvd dvd = storage as Dvd;
                        dvd.ReadingSpeed = num;
                    }
                    else if (storage is Hdd)    //if object is Hdd we changing the Speed
                    {
                        Hdd hdd = storage as Hdd;
                        hdd.Speed = num;
                    }
                    else     //if object is Flash Drive we changing the Speed
                    {
                        FlashDrive flashDrive = storage as FlashDrive;
                        flashDrive.Speed = num;
                    }
                    break;

                default:     //if index is invalid
                    Console.WriteLine("Invalid index!");
                    break;
                }
            }
        }