locked
Help needed in Class related question RRS feed

  • Question

  • User1489758560 posted

    Hello Friends,

    i am trying to resolve a puzzle in my company and for the below the portion i have write some code. but got to know my code was wrong. can you please check the requirement and correct my code please. it would help me to understand and learn from the mistakes

    An enumerated type, representing the possible resource types in the program, containing the following values: Hardware, Firmware, Software, and .
    
    A class, entitled Resource, that contains two pieces of data. The first is an instance of the enumerated type above. The second is an integer that represents the number of units of the enumerated type specified in the first piece of data. The class should have a constructor or member function that accepts the enumerated type and the value of the units variable.
    
    A class, entitled Requirements, that contains two pieces of data. The first is an instance of the enumerated type above. The second is an integer that represents the required number of units of the enumerated type specified in the first piece of data. The class should have a constructor or member function that accepts the enumerated type and the value of the units variable.
    
    A class, entitled Provider, which contains a collection of zero to four instances of the Resource class. The class should have member functions that allows for addition and removal of Resources. No instance of the Resource class that is added to the Provider should be able to have the same value of the enumerated type as another instance currently present in the Provider.
    
    A class, entitled, Demands, which contains a collection of zero to four instances of the Requirements class. The class should have member functions that allow for addition and removal of Requirements. No instance of the Requirements class that is added to the Demands should be able to have the same value of the enumerated type as another instance currently present in the Demands.
    
    

    Code :

    using System;
    using System.Collections.Generic;
    
    namespace Test.ClassFiles
    {
        internal class AllClass
        {
        }
    
        public enum ResourceType
        {
            Hardware = 1,
            Firmware = 2,
            Software = 3,
            Other = 4
        }
    
        public class Resource
        {
            ResourceType resourceType = 0;
            int NoOfUnits = Enum.GetNames(typeof(Resource)).Length;
            public Resource(ResourceType enuResourceType,int unit)
            {
    
            }
    
        }
        public class Requirements
        {
            ResourceType resourceType = 0;
            int NoOfUnits = Enum.GetNames(typeof(Resource)).Length;
            public Requirements(ResourceType enuResourceType, int unit)
            {
    
            }
    
        }
        public class Provider
        {
            public List<Resource> lstInstance = new List<Resource>();
            public List<Resource> AddResource(List<Resource> lstRresources)
            {
                ResourceType resourceType = new ResourceType();
    
                   Resource resource = new Resource(resourceType,0);
                lstRresources.Add(resource);
                return lstRresources;
    
            }
        }
    
        public class Demands
        {
            public List<Requirements> lstInstance = new List<Requirements>();
            public List<Requirements> AddResource(List<Requirements> lstRresources)
            {
                ResourceType resourceType = new ResourceType();
    
                Requirements resource = new Requirements(resourceType, 0);
                lstRresources.Add(resource);
                return lstRresources;
    
            }
        }
    }

    Please do the needful.

    Thursday, May 23, 2019 1:46 AM

All replies

  • User303363814 posted

    my code was wrong.
    Can you tell us what is wrong?  What problem do you have?  What does the code do that you don't want it to do?  What does the code not do that you want it to do?  Do you have an error?  Or an Exception?

    I am not sure what 'the needful' is

    Thursday, May 23, 2019 8:17 AM
  • User1489758560 posted

    Hello Paul,

    Below the full requirement.

    Write a program in C#/.NET that contains the following:
    An enumerated type, representing the possible resource types in the program, containing the following values: Hardware, Firmware, Software, and Other.
    A class, entitled Resource, that contains two pieces of data. The first is an instance of the enumerated type above. The second is an integer that represents the number of units of the enumerated type specified in the first piece of data. The class should have a constructor or member function that accepts the enumerated type and the value of the units variable.
    A class, entitled Requirements, that contains two pieces of data. The first is an instance of the enumerated type above. The second is an integer that represents the required number of units of the enumerated type specified in the first piece of data. The class should have a constructor or member function that accepts the enumerated type and the value of the units variable.
    A class, entitled Provider, which contains a collection of zero to four instances of the Resource class. The class should have member functions that allows for addition and removal of Resources. No instance of the Resource class that is added to the Provider should be able to have the same value of the enumerated type as another instance currently present in the Provider.
    A class, entitled, Demands, which contains a collection of zero to four instances of the Requirements class. The class should have member functions that allow for addition and removal of Requirements. No instance of the Requirements class that is added to the Demands should be able to have the same value of the enumerated type as another instance currently present in the Demands.
    A method which takes an instance of a Provider, and an instance of a Demands, and returns how many whole multiples of the set of Requirements inside the Demands instance are present in the set of Resources inside the Provider instance.
    One or more test methods which:
    
    1.	Create an instance of a Provider with different numbers and combinations of Resources;
    2.	Create an instance of a Demands with different numbers and combinations of Requirements;
    3.	Invoke the previous method using the Provider and Demands from (1) and (2) above;
    4.	Verify that the value returned from the method is correct.
    

    Here is my try.

    public class Resource
            {
               public ResourceType resourceType = new ResourceType();
               public int NoOfUnits = Enum.GetNames(typeof(ResourceType)).Length;
                public Resource(ResourceType enuResourceType, int unit)
                {
    
                }
                public Resource()
                {
    
                }
            }
            public class Requirements
            {
               public ResourceType resourceType = new ResourceType();
               public int NoOfUnits = Enum.GetNames(typeof(ResourceType)).Length;
                public Requirements(ResourceType enuResourceType, int unit)
                {
    
                }
                public Requirements()
                {
    
                }
    
            }
            public class Provider
            {
                public List<Resource> lstInstance = new List<Resource>();
                public List<Resource> AddResource(List<Resource> lstRresources)
                {
                    int count = 1;
                    foreach (ResourceType item in Enum.GetValues(typeof(ResourceType)))
                    {
                        Resource resource = new Resource(item, count);
                        lstRresources.Add(resource);
                        count++;
                    }
                    return lstRresources;
                }
    
                public List<Resource> DeleteResource(ResourceType resourceType,int m)
                {
                    Resource resource = new Resource(resourceType, m);
                    lstInstance.Remove(resource);
                    return lstInstance;
                }
            }
    
            public class Demands
            {
                public List<Requirements> lst = new List<Requirements>();
                public List<Requirements> AddResource(List<Requirements> lstRresources)
                {
                    int count = 1;
                    foreach (ResourceType item in Enum.GetValues(typeof(ResourceType)))
                    {
                        Requirements requirements = new Requirements(item, count);
                        lstRresources.Add(requirements);
                        count++;
                    }
                    return lstRresources;
    
                }
                public List<Requirements> DeleteResource(ResourceType resourceType, int m)
                {
                    Requirements requirements = new Requirements(resourceType, m);
                    lst.Remove(requirements);
                    return lst;
                }
            }

    But not sure how to move forward to achieve the below portion

    A class, entitled, Demands, which contains a collection of zero to four instances of the Requirements class. The class should have member functions that allow for addition and removal of Requirements. No instance of the Requirements class that is added to the Demands should be able to have the same value of the enumerated type as another instance currently present in the Demands.
    A method which takes an instance of a Provider, and an instance of a Demands, and returns how many whole multiples of the set of Requirements inside the Demands instance are present in the set of Resources inside the Provider instance.
    One or more test methods which:
    
    1.	Create an instance of a Provider with different numbers and combinations of Resources;
    2.	Create an instance of a Demands with different numbers and combinations of Requirements;
    3.	Invoke the previous method using the Provider and Demands from (1) and (2) above;
    4.	Verify that the value returned from the method is correct.
    

    i have coded to achieve the other portions in the requirement. Please refer the full requirement. but not sure how to move forward to achieve the full requirement. any help will be highly appreciated.

    Thursday, May 23, 2019 10:49 AM
  • User288213138 posted

    Hi  born2win,
     

    I couldn’t understand what do you want your code to do? The error I tested was 

     int NoOfUnits = Enum.GetNames(typeof(Resource)).Length;

    the arguments inside typeof () should be enumerated types rather than classes.

    There is also a constructor, which is mainly used to create an object and assign an initial value when the object is created. You only declared it, but it didn't initialize. Does it make sense?

    The last thing is about list<T>, regardless of whether the previous one is correct, you initialized an lstInstance, why should you put the value of add into the parameter, is there any special purpose? Although this is grammatically no problem.

    So please post more details information about your requirement.

     
    Best Regard,
    Sam

    Thursday, May 23, 2019 11:39 AM
  • User1489758560 posted

    Hi Sam,

    Thanks for the reply and here is my requirement.  i know this is long requirement and i struggling to move forward. i know this is little big talk. i did try what i know. but not sure how to make classes for this requirement and write test methods as asked in the requirement. i tried to create a class based on the explanation given in the requirement. but not sure it's correct. could you please go through my requirement and help me in this writing class and test methods as per. Thanks in advance.

    Write a program in C#/.NET that contains the following:
    An enumerated type, representing the possible resource types in the program, containing the following values: Hardware, Firmware, Software, and .
    
    A class, entitled Resource, that contains two pieces of data. The first is an instance of the enumerated type above. The second is an integer that represents the number of units of the enumerated type specified in the first piece of data. The class should have a constructor or member function that accepts the enumerated type and the value of the units variable.
    
    A class, entitled Requirements, that contains two pieces of data. The first is an instance of the enumerated type above. The second is an integer that represents the required number of units of the enumerated type specified in the first piece of data. The class should have a constructor or member function that accepts the enumerated type and the value of the units variable.
    
    A class, entitled Provider, which contains a collection of zero to four instances of the Resource class. The class should have member functions that allows for addition and removal of Resources. No instance of the Resource class that is added to the Provider should be able to have the same value of the enumerated type as another instance currently present in the Provider.
    
    A class, entitled, Demands, which contains a collection of zero to four instances of the Requirements class. The class should have member functions that allow for addition and removal of Requirements. No instance of the Requirements class that is added to the Demands should be able to have the same value of the enumerated type as another instance currently present in the Demands.
    
    A method which takes an instance of a Provider, and an instance of a Demands, and returns how many whole multiples of the set of Requirements inside the Demands instance are present in the set of Resources inside the Provider instance.
    One or more test methods which:
    
    Create an instance of a Provider with different numbers and combinations of Resources;
    Create an instance of a Demands with different numbers and combinations of Requirements;
    Invoke the previous method using the Provider and Demands from (1) and (2) above;
    Verify that the value returned from the method is correct.
    

    Thursday, May 23, 2019 2:43 PM
  • User303363814 posted

    Instead of just copying and pasting from your assignment could you please do a little formatting so that all the readers do not have to scroll horizontally backwards and forwards all the time.  Help us to help you!

    The class should have member functions that allow for addition and removal of Requirements.

    I would interpret this as meaning that a single Requirement can be added or removed at a time (not a list of them as you have coded).  The AddResource method (it should be called AddRequirement since that is what is being added) has to do two things

    1) Check that there are currently fewer than four Requirements.  The specification does not tell you what to do if there are already four Requirements and you are asked to add a fifth.  I would suggest throwing an Exception.  But maybe the AddRequirement method is supposed to return a value which represents the success or otherwise of the action.  (Also, what should you do if there are already four Requirements and you are asked to add one which is already present?  Is that an error?  Should it be ignored and success returned?  Should it be ignored and failure returned?  Ask your teacher or make your own decision)

    2) See if the Requirement being added specifies a Resource already present in the set of Requirements.  If it already exists then do not add and decide on an appropriate return status/value.  You seem to have assumed that the Add method should return the existing list.  If the new Requirement does not exist in the current set then add it.

    A general comment is that exposing the internal detail called 'lst' is a bad idea.  Since you have exposed this any external code could modify it (add/remove elements) and not obey the design specification (no more than four Requirements and all Requirements to specify distinct Resources).  Your current code gives external access total access to violate the restrictions that you specification requires.  Do not make 'lst public and do not return 'lst' from and public method.

    As for the Delete operation … Again it should take a single Requirement as a parameter.  My guess is that the specification wants you to remove any existing Requirement which has the same Resource as specified by the parameter but this is not very well specified.  Do you think that this assignment is meant to be an exercise in programming or an exercise in interpreting loose specifications?  What sort of course are you doing?

    Friday, May 24, 2019 1:43 AM