Skip to content

Instantly share code, notes, and snippets.

@Dyrits
Forked from codecademydev/IDisplayable.cs
Last active May 10, 2023 08:08
Show Gist options
  • Save Dyrits/139a6490f4597145d2ef4619e8c1cf8c to your computer and use it in GitHub Desktop.
Save Dyrits/139a6490f4597145d2ef4619e8c1cf8c to your computer and use it in GitHub Desktop.
App Interfaces

App Interfaces

The team at Computron Computing has asked you to join their product team to develop the hottest new Computron computer. You’ll be responsible for building some of the standard apps on this new machine, specifically the to-do list and the password manager.

At this point in development you have two classes started: TodoList representing the to-do list application and PasswordManager representing the password manager. In order to work within the Computron system, every app must have a display and reset feature. In other words, each class will need to implement the IDisplayable and IResetable interfaces.

Classes can implement multiple interfaces using the colon and comma syntax:

class TodoList : IDisplayable, IResetable
{}

Let’s get started! Make sure to save every file and test your code in the console with the command:

dotnet run
// Define IDisplayable in this file
using System;
namespace SavingInterface
{
interface IDisplayable
{
void Display();
}
}
// Define IResetable in this file
using System;
namespace SavingInterface
{
interface IResetable
{
void Reset();
}
}
using System;
namespace SavingInterface
{
class PasswordManager: IDisplayable, IResetable
{
private string Password
{ get; set; }
public bool Hidden
{ get; private set; }
public PasswordManager(string password, bool hidden)
{
Password = password;
Hidden = hidden;
}
public void Display()
{
Console.WriteLine("Password");
Console.WriteLine("----------");
Console.WriteLine(Hidden ? "***" : Password);
Console.WriteLine(String.Empty);
}
public void Reset()
{
Password = String.Empty;
Hidden = false;
}
}
}
using System;
namespace SavingInterface
{
class Program
{
static void Main(string[] args)
{
TodoList tdl = new TodoList();
tdl.Add("Invite friends");
tdl.Add("Buy decorations");
tdl.Add("Party");
tdl.Display();
tdl.Reset();
tdl.Display();
PasswordManager pm = new PasswordManager("iluvpie", true);
pm.Display();
pm.Reset();
pm.Display();
}
}
}
using System;
namespace SavingInterface
{
class TodoList: IDisplayable, IResetable
{
public string[] Todos
{ get; private set; }
private int nextOpenIndex;
public TodoList()
{
Todos = new string[5];
nextOpenIndex = 0;
}
public void Add(string todo)
{
Todos[nextOpenIndex] = todo;
nextOpenIndex++;
}
public void Display()
{
Console.WriteLine("Todos");
Console.WriteLine("----------");
foreach (string todo in Todos)
{
Console.WriteLine(todo != null ? todo : "[Add a todo~]");
}
Console.WriteLine(String.Empty);
}
public void Reset()
{
Todos = new string[5];
nextOpenIndex = 0;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment