Step-by-step Procedure in Building Hogwarts Console App Using Dart

Building this console app was a little challenging at first especially, since I am only a beginner. Firstly, I did some prayers where I asked God for assistance because 'the pressure was getting worser". After which, I reached out to some friends who had ideas about dart and general programming. Personally, I also did some research before and during the code to make sure I was on the right path or at least, close to it.

The Task

Build Hogwarts Zitz: A console app that prompts user to insert:

  • Name

  • Age

  • Favourite color

  • One Unique trait

  • Print these values to the user and tell them what Harry Potter house they belong to Write an article detailing your steps

Steps

  1. I imported dart:io by typing, import dart:io which is a library that allows you to work with files, directories, sockets, processes, HTTP servers and clients, and more. Many operations related to input and output are asynchronous and are handled using [Future]s or [Stream]s, both of which are defined in the dart:async library.

2. Inside the void main, I started with my code where I listed out the colors and wrapped them in List. The code should look like this; List<String> hogwartsHouseColor = ['Red', 'Green','blue', 'Yellow'];

3. Then I inputted stdout.write('Welcome, please enter your name'); The stdout means standard output and it organizes the line of code in the console, it is from the dart.io file.

4. To make this work, the stdin.readLineSync is used. Stdin means standard input and it works hand in hand with readLineSync to return a string. Hence, the code should look like this; String name = stdin.readLineSync() ?? ' No Name entered';

5. The same method should be applied in inputting age. However, instead of stdin.readLineSync, we use; int age = int.parse(stdin.readLineSync()!); int.parse is used to convert the string and it passes into an integer.

6. We continue the same process used in //inputting name, for both inputting favorite color and unique trait because they are string values.

7. After inputting name, age, color and unique trait, we use colors to assign the SWITCH and CASE statements to the different houses of Hogswart which are; Gryffindor, Slytherin, Ravenclaw and Hufflepuff.

8. Finally, use print ('Your name is $name; You are $age years; Your favorite color is $color; Your unique trait is $trait'); to print out the user values for name, age, favorite color and unique trait.

Below is the General Code Snippet;

import 'dart:io';
void main () {
List<String> hogwartsHouseColor = ['Red', 'Green','blue', 'Yellow'];
  // Prompting user to input name
stdout.write('Welcome, please enter your name\n');
String name = stdin.readLineSync() ?? ' No Name entered';
  // readLineSync returns a string


  // User input age
stdout.write('Please, enter your age\n');
int age = int.parse(stdin.readLineSync()!);
  // int.parse converts the string into an integer


  // User input color
stdout.write('Please, input your favorite color\n');
String color = stdin.readLineSync() ?? 'No color inputted';

// User input unique trait
stdout.write('What is your unique trait\n');
String trait = stdin.readLineSync() ?? 'No trait inputted';

  // Assigning SWITCH and CASE statements to each house
switch(color) {
  case 'red':
  print('You are in house Gryffindor');
  break;

  case 'blue':
  print('You are in house Slytherin');
  break;

  case 'green':
  print('You are in house Ravenclaw');
  break;

  case 'Yellow': 
  print('You are in house Hufflepuff');
  break;

  default: 
  print('You are not in any house, rest!');

}
// Printing all the Input
print('Your name is $name; You are $age years; Your favorite color is $color; Your unique trait is $trait');

Note: If the code shows error after inputting stdin.readLineSync, add !. This error is peculiar to dart 2.12 which is ensured for null safety.