Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Exam
- {
- public class Fridge
- {
- private Product head;
- private Product tail;
- private int count;
- private class Product
- {
- private string name;
- private Product next;
- public Product(string name)
- {
- this.name = name;
- }
- public string Name
- {
- get { return name; }
- set { name = value; }
- }
- public Product Next
- {
- get { return next; }
- set { next = value; }
- }
- public override string ToString()
- {
- return "Product " + name;
- }
- }
- public Fridge()
- {
- }
- public int Count
- {
- get { return count; }
- set { count = value; }
- }
- public void AddProduct(string ProductName)
- {
- Product Product = new Product(ProductName);
- if (this.head == null)
- {
- this.head = Product;
- this.tail = Product;
- }
- else
- {
- Product currentLast = this.tail;
- currentLast.Next = Product;
- this.tail = Product;
- }
- this.Count++;
- }
- public string[] CookMeal(int start, int end)
- {
- int count = 0;
- List<string> usedProducts = new List<string>();
- Product current = this.head;
- while (current != null && count < end)
- {
- if (count >= start)
- {
- usedProducts.Add(current.Name);
- }
- current = current.Next;
- count++;
- }
- return usedProducts.ToArray();
- }
- public string RemoveProductByIndex(int index)
- {
- int count = 0;
- Product current = this.head;
- while (current != null)
- {
- if (index == 0)
- {
- this.head = current.Next;
- this.Count--;
- return current.Name;
- }
- if (count + 1 == index)
- {
- String name = current.Next.Name;
- if(current.Next.Next == null)
- {
- this.tail = current;
- this.Count--;
- current.Next = null;
- return name;
- }
- current.Next = current.Next.Next;
- this.Count--;
- return name;
- }
- current = current.Next;
- count++;
- }
- return null;
- }
- public string RemoveProductByName(string name)
- {
- Product current = this.head;
- while (current != null)
- {
- if (current.Name == name)
- {
- this.head = current.Next;
- this.Count--;
- return current.Name;
- }
- if (current.Next.Name == name)
- {
- if (current.Next.Next == null)
- {
- this.tail = current;
- this.Count--;
- current.Next = null;
- return name;
- }
- current.Next = current.Next.Next;
- this.Count--;
- return name;
- }
- current = current.Next;
- }
- return null;
- }
- public bool CheckProductIsInStock(string name)
- {
- Product current = this.head;
- while (current != null)
- {
- if (current.Name == name)
- {
- return true;
- }
- current = current.Next;
- }
- return false;
- }
- public string[] ProvideInformationAboutAllProducts()
- {
- String[] info = new string[this.Count];
- int index = 0;
- Product current = this.head;
- while (current != null)
- {
- info[index++] = current.ToString();
- current = current.Next;
- }
- return info;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement