ASSIGNMENT 2 FRONT SHEET Qualification BTEC Level 5 HND Diploma in Computing Unit number and title Unit 20: Advanced Programming Submission date 26/12/2023 Date Received 1st submission 26/12/2023 Re-submission Date Date Received 2nd submission Student Name Tran Duc Long Student ID GCH210562 Class GCH1106 Assessor name Le Viet Bach Student declaration I certify that the assignment submission is entirely my own work and I fully understand the consequences of plagiarism. I understand that making a false declaration is a form of malpractice. Student’s signature Grading grid P3 P4 M3 M4 D3 D4 Summative Feedback: Resubmission Feedback: 2.1 Grade: Assessor Signature: Date: Lecturer Signature: Table of Contents 1. Main menu Code.
Plant menu Code. Harvest menu Code. Growth menu Code. Main program Code.
Flow 1: Plants and growth trees. Flow 2: Plants and harvest trees. Range of similar patterns. Usage of pattern.
54 Table of Figures Figure 1: Tree Class Diagram image. 8 Figure 2: Class Library Structure image. 10 Figure 3: Console App structure image. 11 Figure 4: Tree class code image.
12 Figure 5: Tree class constructor image. 12 Figure 6: CountTime function image. 13 Figure 7: Growing tree methods images. 13 Figure 8: GetTreeStatus method image.
14 Figure 9: Product abstract class image. 14 Figure 10: Fruit class image. 15 Figure 11: Medicine class image. 16 Figure 12: Wood class image.
17 Figure 13: IHarvestStrategy interface image. 18 Figure 14: FruitHarvestStrategy class image. 19 Figure 15: MedicineHarvestStrategy class image. 20 Figure 16: WoodHarvestStrategy class image.
21 Figure 17: MainMenu class image. 22 Figure 18: MainMenu class methods image 1. 23 Figure 19: MainMenu class methods image 2. 24 Figure 20: PlantMenu class image.
25 Figure 21: PlantMenu methods image. 26 Figure 22: HarvestMenu methods class image. 27 Figure 23: HarvestMenu methods class image. 28 Figure 24: GrowthMenu class image.
29 Figure 25: GrowthMenu print menu method image. 30 Figure 26: Validate class image. 31 Figure 27: InputInterger method image. 32 Figure 28: InputDouble method image.
33 Figure 29: ValidateHarvestAmount method image. 34 Figure 30: Program class image. 34 Figure 31: Running program Flow 1 image 1. 36 Figure 32: Running program Flow 1 image 2.
36 Figure 33: Running program Flow 1 image 3. 37 Figure 34: Running program Flow 1 image 4. 38 Figure 35: Running program Flow 1 image 5. 38 Figure 36: Running program Flow 1 image 6.
39 Figure 37: Running program Flow 1 image 7. 40 Figure 38: Running program Flow 1 image 8. 41 Figure 39: Running program Flow 1 image 9. 42 Figure 40: Running program Flow 2 image 1.
43 Figure 41: Running program Flow 2 image 2. 44 Figure 42: Running program Flow 2 image 3. 45 Figure 43: Running program Flow 2 image 4. 46 Figure 44: Running program Flow 2 image 5.
47 Figure 45: Running program Flow 2 image 6. 48 Figure 46: Running program Flow 2 image 7. 49 Figure 47: Running program Flow 2 image 8. Introduction This Assignment represents a follow-up to our ongoing investigation of Object-Oriented Programming (OOP) and Design Patterns.
Our objective is to showcase the utility and efficiency of these concepts in software development by selecting a real-life scenario, creating a class based on a relevant class diagram, and exploring various Design Patterns. This process is geared towards equipping us with valuable knowledge and skills that will undoubtedly influence our future pursuits in the realm of software engineering. Scenario A farming family in a small village is preparing for the planting season. They have been growing trees for many years, but they have not had a way to take care of and come up with harvesting methods for the trees they grow.
They heard that there was software on the playstore developed by the development team from GWSoft that could help them monitor their crops, plan irrigation and fertilization, and monitor yields and supply. The family farmer decided to use garden management software provided by GWSoft. They look for a software that can meet their needs, such as tracking multiple crops from which to harvest products from the crops. The farmer's family was very satisfied with the software after using it.
It helps them monitor their crops easily. They can irrigate and fertilize more effectively. And they can track their productivity in more detail. Most importantly, the application offers harvesting methods including: Fruit harvesting method, medicinal leaf harvesting method and wood harvesting method.
With the fruit harvesting method, the software will check and harvest the number of fruits on the tree based on the user's wishes. When the number of fruits on the branches is not enough for the user to harvest, the software will suggest ways to help the tree grow and grow more fruits. The method of harvesting tobacco leaves will be similar to the method of harvesting fruits, however the number of leaves harvested will not exceed a certain threshold because it will make it difficult for the plant to grow if harvested too much. At the same time, the software will give suggestions on how to care for the tree so that it has more leaves, to help farmers harvest later.
With the wood harvesting method, the software will provide methods to harvest wood with the best yield. When the tree is not tall enough to harvest the correct amount of wood the user desires, the software will also provide care instructions to help the tree grow in height and have more wood. Using the above software has not only helped farmers in managing the plants in their gardens, but it has also helped them harvest products from their crops more effectively. Diagram My team used the Strategy pattern to implement this scenario.
Below is the class diagram of the project: Figure 1: Tree Class Diagram image Class Tree: This is a basic class with attributes such as fruits, leaves, name, Weight, and HealthStatus. In this class there are methods such as GetTreeHealth() to get health information, Fertilizing() to fertilize, UpdateTreeStatus() to update the current state of the tree, absorbCO2() to absorb CO2, and Watering() to Sprinklers. Class Fruit, Wood, Medicine classes are considered products of the tree. They inherit from an abstract class Product and have methods like use() to use the product.
Specifically: • Class Fruit: Has attributes such as taste). • Class Wood: Has properties such as hardness, color, and makeFurniture() method to create wood furniture. • Class Medicine: Has attributes such as usage, medicineType, and makeProduct() method to produce medicine. Interface IHarvestStrategy: This is part of the Strategy design pattern, allowing my project to define different harvesting strategies for product types.
There are three specific strategies: • FruitHarvestStrategy: Fruit harvesting strategy. • WoodHarvestStrategy: Wood harvesting strategy. • MedicineHarvestStrategy: Medicine Harvesting Strategy (Tobacco Leaves) Each strategy has a Harvest(Tree, amount) method to harvest corresponding to the type of product from the tree. A tree can harvest both fruit, wood and leaves.
Enum HealthStatus: This is an enumeration that defines the possible health states of the plant such as Good, LackFertilize, LackWater, LackCO2, and Bad. The diagram also has solid arrows from product classes to Product indicating inheritance relationships, and dashed arrows from HarvestStrategy to Product indicating that the harvest strategy uses products. Code Based on the giving class diagram above, I have created all classes that necessary for the project as shown in the image below: Figure 2: Class Library Structure image In the diagram given above, the main class that the program will interact with most is the Tree class. On top of that, the class diagram also contains product abstract classes which are implemented by the Fruit class, Medicine class, Wood class.
Furthermore, the image also shows the harvest strategy interface, which also be implemented by the Fruit harvest strategy, Medicine harvest strategy and Wood harvest strategy. The classes have been created in the image based on the attributes and names contained in the diagram class. Furthermore, to interact with all the class as in the class library above we use console app which has structure as illustrated in the image. Figure 3: Console App structure image I separate the program's main menu into four menus, in which the main menu will play the most important role.
It contains vital information about plants and products and is a bridge linking menus together. Each menu has a specific function that serves the purpose corresponding to that menu's name. Specifically, the tree growing menu will provide methods to grow trees, more specifically, the method of initializing tree objects. For tree growing methods, it will contain functions that help increase the value of the tree.
And the harvest menu will contain strategies to help users harvest products from previously created trees. To prevent input data from being entered incorrectly by the user, we have created a validate class that contains validation methods to prevent the program from encountering errors during use. Last but not least is the program class where it has the main function of the program. Tree Code The image below illustrates the properties of Tree class, along with the enum HealthStatus as described in the class diagram above.
Figure 4: Tree class code image To begin with, the HealthStatus describes multiple health states a tree can have. Furthermore, private fields that a tree can store include its name, weight (which can be used to collect wood weight), health status (which indicates how healthy the tree is), and fruit count (which indicates how many fruits the tree has). The Name property has a private setter to guarantee that it is set only during object initialization, and it has public properties to access these attributes. Furthermore, the image also shows the basic constructor of tree class with no required parameters.
Figure 5: Tree class constructor image Furthermore, the image above describes a constructor of tree class with some parameters such as fruits, leafs, name, height and healthstatus to assign given data to a newly created tree. Figure 6: CountTime function image On top of that, the function above as shown by the image describes the CountTime function, which is used as a time limit to increase the practicality of the method since each tree development will take some time in real life. Figure 7: Growing tree methods images Additionally, the images above are methods simulating growth actions of a tree. They update the trees attributes based on the passage of time and print the current tree status using the GetTreeStatus method.
Figure 8: GetTreeStatus method image Another method illustrated by the image above is GetTreeStatus, which returns formatted string outlining the characteristics and condition of the tree along with all of its data. Based on predetermined criteria, the UpdateTreeStatus method assesses the tree's properties and establishes its health state. The relevant HealthStatus enum value is returned. Every time it is called, this procedure updates the tree status automatically.
Product Code Figure 9: Product abstract class image The image above describes the abstract class Product, which is implemented by fruit, medicine and wood class. The Product class serves as a blueprint for other classes to inherit from, it contains an abstract method named “Use". The “Use" abstract function is part of the Product class, which acts as a model for other classes to follow. The "Use" method needs to be implemented by every class that derives from the product class.
Although all the subclasses use the same function, each one has a unique implementation that encourages customization and flexibility. Fruit Code Figure 10: Fruit class image The image above describes the Fruit class with its property, constructor and some functions similar to the class diagram.