Coding a Console App with dart language
A console app that prompts users to input their name, age, favorite color, and one unique trait; Hogwart Zitz App.
After the user had inputted the required information, it will print out the Hogwart house he/she belongs to.
Here is the step-by-step process for coding the console app.
(1). To take input from the console, I need to import a library called dart:io from the Dart libraries.
'import "dart:io";
(2). I typed in the entry gate of the program.
void main() {
(3). I used the List function to list out the color with the String name house color
List<String> housecolor = ['red', 'blue', 'yellow', 'green'];
(4). To take input from the user, you need to use the .readLineSync() function. So I apply it to ask for the user's name. And for the output to be displayed for the user to read, I used print instead of the standard output function. Though both are the same, I prefer the print function because the word will begin in another line, giving it space.
Here is it
// prompting user to insert name
print(" Welcome, kindly enter your name")
// user inputted name printed out
String name = stdin.readLineSync()?? 'no name inputted'
(5). The step above is applied to the other data except the age because it's an integer. So I apply the int.parse to convert the string into an integer.
//prompting user to input age
print('What is your age');
//user inputted age printed out
int age = int.parse(stdin.readLineSync()!);
//int.parse converts the string into an integer
(6). After the user had inputted all the required information, we need to print out the house he/she belongs to. To do that, I used the Switch and Case statement.
// Assigning Switch and Case statements to the color
switch(color) {
case'red': {print('You are in Gryffindor house');}
we need to use the break statement to indicate the end of this code.
break;
The same process is used for the remaining colors.
(7). The next process is to print out all the input. This is simple. I concatenate the datum.
// printing out all the input
print('Your name is $name; you are $age years; your favorite color is $color; your unique trait is $unitrait');
Below is the code snippet
import "dart:io";
void main() {
List<String> housecolor = ['red', 'yellow', 'blue', 'green];
//Prompting user to input name
print('Welcome, Kindly enter your name:');
//user inputted name printed out
String name = stdin.readLineSync()?? 'no name inputted';
//prompting user to insert age
print('What is your age?');
//user inputted age printed out
int age = int.parse(stdin.readLineSync()!);
//int.parse converts the string into an integer
//prompting user to input favorite color
print('What is your favorite color?');
//user inputted favorite color printed out
String color = stdin.readLineSync()?? 'no color inputted';
//prompting user to input a unique trait
print('Enter one unique trait you have:');
//user inputted unique trait printed out
String unitrait = stdin.readLineSync()?? 'no unique trait inputted';
//Assigning Switch and Case statements to the color
switch(color) {
case 'red': {print('You are in Gryffindor house');}
break;
switch(color) {
case 'blue': {print('You are in Slytherin house');}
break;
switch(color) {
case 'yellow': {print('You are in Ravenclaw house');}
break;
switch(color) {
case 'green': {print('You are in Hufflepuff house');}
break;
}
// Printing out all the input
print('Your name is $name; your age is $age years; your favorite color is $color; your unique trait is $unitrait');
}