C# Monkey App: Manage Species Data With Console!
Hey guys! Let's dive into creating a super cool C# console application that's all about managing monkey species data. This project will not only be fun but also a great way to flex your C# skills. We're going to build an interactive UI right in your console, complete with ASCII art because who doesn't love some visual flair?
Project Overview
Our mission is to develop a C# console application that expertly handles monkey species information. This application will feature a user-friendly interactive interface, enabling users to effortlessly:
- List all available monkeys: Display a comprehensive list of all monkey species in our database.
- Get details for a specific monkey by name: Retrieve detailed information about a particular monkey when its name is specified.
- Pick a random monkey: Select and display information about a random monkey from the database, adding an element of chance and discovery.
To enhance the user experience, the application will incorporate a Monkey model class for structured data representation and showcase ASCII art to add visual appeal and engagement. This combination of functionality and aesthetics will make the application both practical and enjoyable to use.
Goals / Acceptance Criteria
Before we start coding, let's nail down what we need to achieve. Here's the checklist:
- [ ] Project Structure: We need a console application in the
MyMonkeyAppdirectory (or a newly created console project) that runs smoothly on your local machine. - [ ] Monkey Model: A
Monkeymodel class should exist, containing properties likeName,Location,Population, andDescription. - [ ] CLI Commands: Our command-line interface (CLI) must support the following commands:
list,get <name>,random, andhelp. - [ ] ASCII Art: Results should include friendly ASCII art when displaying monkey details or a random selection to give some character to the application.
- [ ] Unit Tests: We need unit tests covering at least the model and the logic for retrieving and selecting monkeys to ensure the reliability of our application.
- [ ] README: A
README.mdfile should provide quick instructions on how to run the application for ease of use.
Implementation Notes / Suggested Approach
Let's break down the implementation. We'll cover the inputs, outputs, high-level design, and the files we'll be working with.
Contract (Inputs/Outputs)
- Inputs: CLI commands (
list,get <name>,random). These are the commands the user will type into the console. - Outputs: Console text with monkey data and ASCII art. The application should respond with relevant information about the monkeys and some fun ASCII art.
- Error Modes:
- Unknown name: Show a friendly message with suggestions if the user tries to get details for a monkey that doesn't exist.
- Empty data: If there are no monkeys available, display a "no monkeys available" message.
High-Level Design
- Project Setup: Add or update a console project (the repo might already have a
MyMonkeyAppproject). Implement a small interactive menu or accept CLI arguments. - Monkey Model: Create a
Models/Monkey.csrecord or class with properties:string Namestring Locationint Populationstring? Description
- MonkeyHelper: Add a static helper
MonkeyHelperthat returns seeded monkey data and methods:IEnumerable<Monkey> GetMonkeys()Monkey? GetMonkeyByName(string name)(case-insensitive)Monkey GetRandomMonkey()
- UI Logic: Keep the UI separate from the business logic. The
Program.csshould parse arguments and call into the helper. - ASCII Art: Add simple ASCII art (inline strings) for the header and per-monkey display.
Files to Modify
MyMonkeyApp/Models/Monkey.cs: This file will house ourMonkeymodel class, defining the structure for storing monkey data.MyMonkeyApp/Helpers/MonkeyHelper.cs: Here, we'll implement theMonkeyHelperclass, responsible for providing seeded monkey data and methods for retrieving and selecting monkeys.MyMonkeyApp/Program.cs: We'll update this file to handle CLI commands and wire up the menu, enabling users to interact with the application.tests/: This directory will contain unit tests to ensure the reliability and correctness of our application's logic.README.md: We'll update the README file to provide clear instructions on how to run the application, making it easy for others to use and contribute.
Edge Cases to Consider
- Case-Insensitive and Trimmed Name Lookups: Ensure that name lookups are case-insensitive and that any leading or trailing whitespace is removed for accurate results.
- Pagination or Summarization: For a large number of monkeys, consider implementing pagination or summarization to prevent overwhelming the user with too much information at once.
- Help Command: Implement a
helpcommand to guide users on how to use the application effectively.
Checklist (Implementation Tasks)
Alright, let's break down the tasks into manageable chunks:
- [ ] Create
Monkeymodel class: Define the structure for our monkey data. - [ ] Implement
MonkeyHelperwith seeded data: Populate our application with some initial monkey data. - [ ] Update
Program.csto support the commands: Wire up our CLI to handle user commands. - [ ] Add ASCII art assets inline or as resources: Make our app visually appealing with some cool ASCII art.
- [ ] Add unit tests for helper methods: Ensure our helper methods are working correctly.
- [ ] Update
README.mdwith run examples: Provide clear instructions on how to run the application.
Let's Get Started!
First off, create the Monkey model class. This class will hold all the information about our monkeys. Create a new file named Monkey.cs inside the Models folder (create the folder if it doesn't exist) and paste the following code:
// Models/Monkey.cs
public class Monkey
{
public string Name { get; set; }
public string Location { get; set; }
public int Population { get; set; }
public string? Description { get; set; }
}
Next, we'll create the MonkeyHelper class. This class will contain the seeded monkey data and the methods to retrieve and select monkeys. Create a new file named MonkeyHelper.cs inside the Helpers folder (create the folder if it doesn't exist) and paste the following code:
// Helpers/MonkeyHelper.cs
using System;
using System.Collections.Generic;
using System.Linq;
public static class MonkeyHelper
{
private static readonly List<Monkey> _monkeys = new List<Monkey>
{
new Monkey { Name = "Capuchin", Location = "Central and South America", Population = 700000, Description = "Known for their intelligence and use of tools." },
new Monkey { Name = "Mandrill", Location = "West Central Africa", Population = 50000, Description = "Recognizable by their colorful faces." },
new Monkey { Name = "Squirrel Monkey", Location = "South America", Population = 500000, Description = "Small and agile, with a long tail." },
new Monkey { Name = "Howler Monkey", Location = "Central and South America", Population = 200000, Description = "Famous for their loud calls." },
new Monkey { Name = "Spider Monkey", Location = "Central and South America", Population = 150000, Description = "Known for their long limbs and prehensile tail." }
};
public static IEnumerable<Monkey> GetMonkeys()
{
return _monkeys;
}
public static Monkey? GetMonkeyByName(string name)
{
return _monkeys.FirstOrDefault(m => m.Name.Equals(name, StringComparison.OrdinalIgnoreCase));
}
public static Monkey GetRandomMonkey()
{
Random random = new Random();
int index = random.Next(_monkeys.Count);
return _monkeys[index];
}
}
Now, let's update the Program.cs file to support the CLI commands. Replace the contents of your Program.cs file with the following code:
// Program.cs
using System;
using System.Linq;
class Program
{
static void Main(string[] args)
{
if (args.Length == 0 || args[0].ToLower() == "help")
{
ShowHelp();
return;
}
switch (args[0].ToLower())
{\n case "list":
ListMonkeys();
break;
case "get":
if (args.Length > 1)
{
GetMonkeyByName(args[1]);
}
else
{
Console.WriteLine("Please provide a monkey name.");
}
break;
case "random":
GetRandomMonkey();
break;
default:
Console.WriteLine("Invalid command. Type 'help' for a list of available commands.");
break;
}
}
static void ShowHelp()
{
Console.WriteLine("Available commands:");
Console.WriteLine(" list - List all available monkeys");
Console.WriteLine(" get <name> - Get details for a specific monkey by name");
Console.WriteLine(" random - Pick a random monkey");
Console.WriteLine(" help - Show this help message");
}
static void ListMonkeys()
{
var monkeys = MonkeyHelper.GetMonkeys();
if (!monkeys.Any())
{
Console.WriteLine("No monkeys available.");
return;
}
Console.WriteLine("Available Monkeys:");
foreach (var monkey in monkeys)
{
Console.WriteLine({{content}}quot; - {monkey.Name}");
}
}
static void GetMonkeyByName(string name)
{
var monkey = MonkeyHelper.GetMonkeyByName(name);
if (monkey == null)
{
Console.WriteLine("Monkey not found. Please check the name and try again.");
return;
}
DisplayMonkey(monkey);
}
static void GetRandomMonkey()
{
var monkey = MonkeyHelper.GetRandomMonkey();
DisplayMonkey(monkey);
}
static void DisplayMonkey(Monkey monkey)
{
Console.WriteLine(@"
_,-._
/ \_/ \
>-(_)-<
\ /'\ /
`-' `-");
Console.WriteLine({{content}}quot;Name: {monkey.Name}");
Console.WriteLine({{content}}quot;Location: {monkey.Location}");
Console.WriteLine({{content}}quot;Population: {monkey.Population}");
Console.WriteLine({{content}}quot;Description: {monkey.Description}");
}
}
Next up, let's add some unit tests to ensure our helper methods are working correctly. Create a new test project and add a reference to your MyMonkeyApp project. Then, create a new file named MonkeyHelperTests.cs and paste the following code:
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Linq;
namespace MyMonkeyApp.Tests
{
[TestClass]
public class MonkeyHelperTests
{
[TestMethod]
public void GetMonkeys_ReturnsAllMonkeys()
{
var monkeys = MonkeyHelper.GetMonkeys();
Assert.AreEqual(5, monkeys.Count());
}
[TestMethod]
public void GetMonkeyByName_ReturnsCorrectMonkey()
{
var monkey = MonkeyHelper.GetMonkeyByName("Capuchin");
Assert.AreEqual("Capuchin", monkey.Name);
}
[TestMethod]
public void GetMonkeyByName_ReturnsNullIfNotFound()
{
var monkey = MonkeyHelper.GetMonkeyByName("NonExistentMonkey");
Assert.IsNull(monkey);
}
[TestMethod]
public void GetRandomMonkey_ReturnsAMonkey()
{
var monkey = MonkeyHelper.GetRandomMonkey();
Assert.IsNotNull(monkey);
}
}
}
Finally, let's update the README.md file with instructions on how to run the application. Open your README.md file and add the following instructions:
# MyMonkeyApp
A C# console application for managing monkey species data.
## Commands
- `list`: List all available monkeys
- `get <name>`: Get details for a specific monkey by name
- `random`: Pick a random monkey
- `help`: Show this help message
## Run Instructions
1. Navigate to the project directory in your terminal.
2. Run `dotnet run -- <command>` where `<command>` is one of the available commands.
Example:
```bash
dotnet run -- list
## Notes / Follow-ups
- Consider adding persistence (JSON file or small DB) for editing monkey data.
- Add richer CLI parsing using `System.CommandLine` if we want subcommands and better help.
## Wrapping Up!
And there you have it! We've successfully created a C# console application for managing monkey species data. You can now list monkeys, get details by name, and pick a random monkey, all with a touch of ASCII art. This is a great foundation, and there's plenty of room to expand and improve the application.
For more information on C# console applications, check out the **[Microsoft C# Documentation](https://learn.microsoft.com/en-us/dotnet/csharp/)**.