2
Answers

Please tell me does this code is object-oriented or not?

Stojanco Vellkov

Stojanco Vellkov

11y
1.2k
1
using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.Threading.Tasks;
using System.Net;

namespace GrillMaster
{
/// <summary>
///  This class is main class.
/// </summary>
public class GrillMaster
{
    int GrillCapacity;
    string MenuNbr;
    int k;
    int[] BestFit;
    List<int> LstData; // Dynamic list to store the item value
    Hashtable hTable; // Hashtable to use the store item name, itemValue pairs
    /// <summary>
    /// Constructor method
    /// </summary>
    /// <param name="Capacity"></param>
    /// <param name="MenuNbr"></param>
    public GrillMaster(int Capacity, string MenuNbr)
    {
        this.GrillCapacity = Capacity;
        this.MenuNbr = MenuNbr;
        this.LstData = new List<int>(); // Dynamic list to store the item value
        this.BestFit = new int[100];
        this.hTable = new Hashtable(); // Hashtable to use the store item name, itemValue pairs

    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="NbrItm"></param>
    /// <param name="ItmVal"></param>
    /// <param name="ItmName"></param>
    public void FillList(int NbrItm, int ItmVal, string ItmName)
    {
        for (int i = 1; i <= NbrItm; i++)
        {
            LstData.Add(ItmVal);  // fill in the list data with the item value
        }
        if (!(hTable.ContainsKey(ItmName))) 
        {
            hTable.Add(ItmName,ItmVal); // if HashTable not contains the current item then add it.
        }
    }
    /// <summary>
    /// 
    /// </summary>
    public void ClearList()
    {
        this.LstData.Clear();
    }
    public void NumberOfRoundsBestFit()
    {
        int m = 0;
        bool bOK = true;
        k = 0;
        LstData.Sort();  //Sort the data
        LstData.Reverse(); // Sort the data in descending order

        BestFit[k] = GrillCapacity;

        foreach (var elm in LstData)
        {
            bOK = true;
            for (int m1 = 0; m1 <= m; m1++)
            {
                if (elm <= BestFit[m1])
                {
                    BestFit[m1] -= elm;
                    bOK = false; //if find correct empty Grill set variable to false
                    break;
                }
            }
            if (bOK)
            {
                    k++; //increase the rounds
                    BestFit[k] = GrillCapacity;  // set the initial capacity of the grill
                    BestFit[k] -= elm; //Decrease the capacity of the grill
            }
            m = k;
        }
    }
    /// <summary>
    /// 
    /// </summary>
    public void NumberOfRounds()
    {
        int Capacity = GrillCapacity; // save the capacity of Grill
        k = 0;
        LstData.Sort();  //Sort the data
        LstData.Reverse(); // Sort the data in descending order
        foreach (var elm in LstData)
        {
            if (elm <= Capacity && elm <= GrillCapacity)
                Capacity -= elm;  //Decrease the capacity of the grill
            else
            {
                k++; //increase the rounds 
                Capacity = GrillCapacity; // set the initial capacity of the grill
                Capacity -= elm; //Decrease the capacity of the grill
            }
        }
 
        foreach (DictionaryEntry entry in hTable)
   {
            Console.WriteLine("{0}, {1} ", entry.Key, entry.Value); //display on the console key and value of the HashTable
   }
 }
    /// <summary>
    /// 
    /// </summary>
public void PrintData()
    {
    Console.WriteLine("Menu "+ this.MenuNbr + ": "+ (k + 1) + " rounds.");
    }
}
    /// <summary>
    /// 
    /// </summary>
class Program
{
     static void Main(string[] args)
     
    {
        int GrilDim = 600; //Der Grill misst 20cm x 30cm
        string MenuName = "";
        int[] Items = new int[] { 100, 200, 300, 400, 200, 100 };
        string[] ItemsName = new string[] { "Fleisch1", "Fleisch2", "Fleisch3", "Fleisch4", "Fleisch2", "Fleisch1" };
  /*      Uri serviceUri = new Uri("http://grillassessment.cloudapp.net/GrillMenuService.svc");
        var serviceCreds = new NetworkCredential("[email protected]", "pasw"); var cache = new CredentialCache(); cache.Add(serviceUri, "Basic", serviceCreds);
        var service = new GrillMenu.GrillMenuContext(serviceUri)
        { Credentials = cache }; 
        foreach (var grillMenu in service.GrillMenus.Expand(g => g.GrillMenuItemQuantity))
        {
            Console.WriteLine("Menu: {0}", grillMenu.Name);
            MenuName = grillMenu.Name;
            GrillMaster TestGrill = new GrillMaster(GrilDim, MenuName);
 
            foreach (var grillMenuItemQuantity in grillMenu.GrillMenuItemQuantity) 
            { service.LoadProperty(grillMenuItemQuantity, "GrillMenuItem");

                TestGrill.FillList(1, grillMenuItemQuantity.Quantity, grillMenuItemQuantity.GrillMenuItem.Name);

                Console.WriteLine("{0} x {1}", grillMenuItemQuantity.Quantity, grillMenuItemQuantity.GrillMenuItem.Name); 
            }
   */
        
         GrillMaster TestGrill = new GrillMaster(GrilDim, MenuName);
        for(int i =0; i < Items.Length; i++)
        { 
            TestGrill.FillList(1, Items[i],ItemsName[i]);
        }
            TestGrill.NumberOfRounds();
            TestGrill.PrintData();
            TestGrill.ClearList();  // Clear the list , and prepare for the next menu!!!
            Console.WriteLine();
        //}
        Console.ReadLine();
    }
    }
}

Answers (2)