Click here to Skip to main content
15,868,164 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Create a C# library project that calculates the total area of all shapes in a collection.
Details
For simplicity, two types of shapes are required below
Circle
Rectangle
Support of adding different types of shapes in the future with minimum change to code is desired.
Each shape object contains two properties below.
Name
1. The name of a shape, e.g. ‘Red Circle’, ‘Blue Rectangle’, which is provided by callers or users
2. An identity for the type of a shape within a collection. That is, the name of a shape is unique for that type of the shape within a collection.

Creation Date
1. The date when a shape object is created, e.g. 12 Jun 2019
2. Circles are allowed to be created on weekdays, not allowed to be created on weekends.
3. Rectangles are allowed to be created on weekends, not allowed to be created on weekdays
Finally, calculate the total area of all shapes in a collection.

What I have tried:

Which all class have to be created and how to use dictionary in this context.
I would like to create a class shape with two properties as Name and Date and one dictionary with key as Shape name and value as either weekday or weekend.
Posted
Updated 24-Apr-19 10:21am

If you are waiting for permission,m then consider it given.

Forget dictionaries and other collections until you have the actual classes in place - you don't need to worry about totals or anything else until you have your shape classes sorted.

I would start with an abstract Shape class, which has an abstract Area property or GetArea method, and an abstract Name property, and a concrete CreateDate property.
Then derive your Circle and Rectangle classes from that, and implement the methods and properties for each, along with the appropriate constructors (which check if a class can be created on that date).

Think about it, and see how far you get.
 
Share this answer
 
Comments
Maciej Los 24-Apr-19 15:43pm    
The shortest tutorial i've seen till today!
In addition to OriginalGriff's solution...

You don't need to create "an identity for the type of a shape within a collection". All you need to do is to detect the type of custom shape.

For further details, please see:
Inheritance - C# Programming Guide | Microsoft Docs[^]
Polymorphism - C# Programming Guide | Microsoft Docs[^]
Abstract and Sealed Classes and Class Members - C# Programming Guide | Microsoft Docs[^]

Good luck!
 
Share this answer
 

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