Click here to Skip to main content
15,897,519 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Ive got this started but im really struggling, I have to modify the class Purse to implement the interface ICloneable . create a main program that demonstrates that the Purse method Clone works.

C#
using System;
using System.Collections;

namespace TestingProject
{
   /// <summary>
   /// A coin with a monetary value.
   /// </summary>
   public class Coin   {

       ///   Constructs a coin.
       ///   @param aValue the monetary value of the coin
       ///   @param aName the name of the coin
       public Coin(double aValue, String aName)
       {
           value = aValue;
           name = aName;
       }

       /// Gets the coin value.
       /// @return the value
       public double GetValue()   
       {
           return value;
       }

       ///   Gets the coin name.
       /// @return the name
       public String GetName()
       {
           return name;
       }
      
       public override bool Equals(Object otherObject)
       {
           Coin other = (Coin)otherObject;
           return name==other.name
               && value == other.value;
       }

       // C# requirement:
       // since we override Equals, MUST also override GetHashCode ( !! )
       public override int GetHashCode()
       {
           return base.GetHashCode ();
       }


      

       private double value;
       private string name;
   }
}

using System;
using System.Collections;

namespace TestingProject
{
   /// <summary>
   /// A purse holds a collection of coins.
   /// </summary>
   public class Purse : ICloneable
   {
       /// Constructs an empty purse.
       public Purse()
       {
           coins = new ArrayList();
           Purse MyPurse = (Purse)MemberwiseClone();
       }

       /// Add a coin to the purse.
       /// @param aCoin the coin to add
       public void Add(Coin aCoin)
       {
           coins.Add(aCoin);
       }

       /// Get the total value of the coins in the purse.
       /// @return the sum of all coin values
       public double GetTotal()
       {
          
           double total = 0;
           for (int i = 0; i < coins.Count; i++)
           {
               Coin aCoin = (Coin)coins[i];
               total = total + aCoin.GetValue();
           }
           return total;
       }

       public Object Clone()
       {
           return MemberwiseClone();
       }
      

       private ArrayList coins;
   }

}


What I have tried:

Ive started the program but im just stuck and confused
Posted
Updated 25-Jul-20 15:46pm
Comments
Richard Deeming 24-Jul-20 6:46am    
Your question title says you're trying to implement IComparable, but the question body says you're trying to implement ICloneable. Which is it?

Reference: ICloneable Interface (System) | Microsoft Docs[^]
Quote:
The ICloneable interface simply requires that your implementation of the Clone() method return a copy of the current object instance. It does not specify whether the cloning operation performs a deep copy, a shallow copy, or something in between. Nor does it require all property values of the original instance to be copied to the new instance.


Sample Implementation:
C#
public class Person : ICloneable
{
        public string FirstName { get; set; }
        public string LastName { get; set; }

        public Person(){}

        public Person(Person p)
        {
            this.FirstName = p.FirstName;
            this.LastName = p.LastName;
        }

        public object Clone()
        {
            return new Person(this);
        }
}

Usage:
C#
static void Main(string[] args)
{
            Person p1 = new Person { FirstName = "Sandeep", LastName = "Mewara" };
            Console.Write("Name in p1:- " + p1.FirstName + "\n");
            Person p2 =(Person) p1.Clone();
            Console.Write("Name in p2:- " + p2.FirstName);
            Console.ReadLine();
}
 
Share this answer
 
Comments
Maciej Los 24-Jul-20 8:50am    
5ed!
Sandeep Mewara 24-Jul-20 9:14am    
Thanks!
Quote:
but im just stuck and confused
Yes, you are, and there is nothing wrong with that; everyone here was once a beginner, and C#, and the Framework, are complex.

What concerns me is the level of misuse of the language shown in your code suggests you have not mastered the basics that you need to move on to higher level concepts.

Consider:

1 in the constructor of Purse: Purse MyPurse = (Purse)MemberwiseClone();

you create a copy in a private variable that cannot be accessed outside the constructor

2 the class Coins does not inherit from IEquatable<Coin> : the overrides you define will never be used

3 note that 'ArrayList is an old, deprecated, structure; use a List<Coin> ... using a strongly typed List means you don't have to cast from Object to Coin.

What to do: search CodeProject for recommended books on C#, and find resources like this: [^], [^], [^]
«... thank the gods that they have made you superior to those events which they have not placed within your own control, rendered you accountable for that only which is within you own control For what, then, have they made you responsible? For that which is alone in your own power—a right use of things as they appear.» Discourses of Epictetus Book I:12
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900