Photo by James Harrison on Unsplash
Console App in Dart that Shows Students' Data Using Maps (and its methods)
As usual, in the course of my internship with the ADA Software development program specifically during the fifth week, we were asked to create a student database for JSS1 using input from the Registrar of the school. As a beginner who had no idea about maps, I had to start my research on maps in dart for two days then, I proceeded to coding.
Task Overview
The principal of Evergreen was highly impressed by your previous solution and now requests that you build a new solution that will make the work of the registrar easier. But starting with jss1 students.
These are the requirements:
- The registrar should be able to upload all the students names, date of birth, height, state of origin
- They should be able to view this data in the console.
Task Objective:
- Improve your logic skills
- Help you experiment with maps in dart and it's methods.
- Solidify the basics you've been practicing with.
Steps In Achieving this Code
- I imported
dart:io
anddart:core
which we will use to input student data and create map that is needed respectively. 2. I created an empty list and called itJSS1
then, created a functionaddStudent()
to store the different data inputs from the registrar. 3. The data inputs included three strings and two integers hence, I created variables for the different data; name, date of birth, age, height, state of origin and used stdin.readLineSync() which can be found indart:io
, to pass either as a string or converting to aninteger
. To convert integer inputs usingstdin.readLineSync
, I usedint.parse(stdin.readLineSync)
. - I created a map with keys and values and made sure to call the variables created above into the values.
- I finally used the .add method in map to add each Student data created from the input into our empty list
[]
just like this,JSS1.add(Student)
. - In line 51, I created another function called
printStudents()
and created a nestedfor...in
loop that will take the data in 5 above and print out each keys and values. - In the main function (line 59), I called the
addStudent()
function then created a while loop that always return true. I want this loop to ask the registrar to press 1 to continue adding student's name (i.eaddstudent
function) or press 0 to view the data of the student(s) he had already inputted (printStudent
function)which will come out as a map because of what I created in 6 above. - To enable this loop, I introduced the if/else statement.
Code
import 'dart:io';
import 'dart:core';
// creating an empty list
final JSS1 = [];
// A function that prompts registrar to input student data.
void addStudent() {
print(
"Welcome, this is the JSS1 students database, please enter the student's name.");
final String name = stdin.readLineSync()!;
print("Enter student's age.");
final int age = int.parse(stdin.readLineSync()!);
print("Enter student's date of birth.");
final String dateOfBirth = stdin.readLineSync()!;
print("Enter height in centimeters.");
final int height = int.parse(stdin.readLineSync()!);
print("Enter student's state of origin.");
final String stateOfOrigin = stdin.readLineSync()!;
// A map that accepts the above student's data
var Student = {
"name": name,
"age": age,
"dateOfBirth": dateOfBirth,
"height": height,
"stateOfOrigin": stateOfOrigin,
};
JSS1.add(Student);
}
// function that can be called in the main function to print student name(s).
void printStudents() {
for (var Student in JSS1) {
for(var singleStudent in Student.entries) {
print('${singleStudent.key}: ${singleStudent.value}');
}
}
}
void main() {
// call the addStudent function so it will be the first thing to show on the console.
addStudent();
// A while loop that always returns true
while(true) {
print("Enter 1 to add another student's name or enter 0 to view list of students already in database.");
final int userInput = int.parse(stdin.readLineSync()!);
if(userInput == 1){
addStudent();
}else if(userInput == 0){
printStudents();
break;
}else{
print('Invalid input, try again.');
}
}
}
Challenges Faced
In Nigeria, we would rephrase this heading as "Shege seen successfully" because it was not easy to understand the task at first. I kept asking our facilitator and chief distributor of this "shege", Ms. Sophia Ahuoyiza questions so that I could understand what she wanted us to achieve. My developer friends were not left behind regardless of their tracks and some of them were eager to help as well. Finally, I achieved this with the help of God, my friends, my facilitator, and track colleagues.
Remember that Giving up is not an Option!**