Table of contents
Building this console app was so challenging to me, especially, since I am just a beginner, who knows little or nothing about Darts or how to build a Console App. I watched lots of videos on YouTube about Darts for beginners to understand what I was meant to do and also reached out to my friend who is also learning with me so we could share ideas together on how to go about, it wasn't an easy feat but I was glad that at the end I was able to get my desired result.
The Assignment
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's house they belong to.
Write an article detailing your steps
Procedure:
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.Inside the void main, I started with my code where I listed out the colors I'm using and I wrapped them in a List. The code should look like this;
List<String> houseColor = ['green', 'red', 'blue', 'Yellow'];
Then I inputted stdout.
write(kindly, enter your name here/n');
The stdout means standard output and it organizes the line of code in the console, it is from thedart.io
file.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()!);
I used same method in inputting that of age. But , instead of stdin.readLineSync, I use;
int age = int.parse(stdin.readLineSync()!);
int.parse is used to convert the string and it passes into an integer.I continued the same process I used in inputting name, for both inputting favorite color and unique trait because both are string values.
After inputting name, age, color and unique trait, I used colors to assign the SWITCH and CASE statements to the different houses of Hogswart which are; Ravenclaw, Slytherin, Hufflepuff, Gryffindor .
At the end i 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.I also added the default print('invalid house') incase someone puts a color that is not among the four colors used when writing this code it will come back as an invalid house.
Below is the code
Import 'dart: io';
Void main ( ) {
// Asking the user to input their name
List<String> houseColor = [ 'green', 'red', 'blue', 'yellow'];
ardour write ( 'kindly, enter your name here/n');
String name =stdin.readLineSync()!;
// Asking for their age
stdout.write(' how old are you?/n');
int age = int.parse(stdin.readLineSync()!);
// Asking for their favourite color
stdout.write(' what is your favourite color /n');
String color =stdin.readLineSync()!;
// Asking for their unique trait
stdout.write(' what is your unique trait /n');
String trait =stdin.readLineSync()!;
// Assigning the switch and case statement to each of the house
switch (color) {
case 'green' :
print('you are in Ravenclaw house');
break;
case 'red' :
print('you are in Slytherin house');
break;
case 'blue' :
print ('you are in Hufflepuff house');
case 'yellow' :
print ('you are in Gryffindor');
default:
print ('invalid house');
}
print('Your name is $name; You are $age years; Your favourite color is $color; Your unique trait is $trait');
}