Level 1 : Project 5
On the occasion of its twentieth anniversary and to encourage its customers, a company of telecommunications, whose numbers start with 65 and 66, grants a top-up bonus for the winning customers.
A customer is a winner if the amount of his communications is greater than or equal to the average consumption of N customers of the company.
To automate this task, we propose to write a Dart program which allows to:
-
Fill 2 tables t and c respectively, by the numbers and the telephone consumption of n customers, with 7 ≤ n ≤ 100, knowing that:
-
Each number is unique and consists of eight digits necessarily starting with 65 or 66.
-
For each telephone number t[i], we match a consumption amount c[i] expressed in Dollar.
-
-
View winning phone numbers and bonuses, obtained by multiplying by 20 the sum of the last six digits of each winning number.
Example:
For N = 7 and the following tables T and C:
The program displays:
The average amount consumed is: 156,285$
The number 66456789 won 780$
The number 65980765 won 700$
The number 66123321 won 240$
The number 65347743 won 560$
Solution:
import 'dart:io';
int inputNumber() {
var n;
do {
print("enter the number of customers ( 7 ≤ n ≤ 100)");
n = int.tryParse(stdin.readLineSync());
if (n != null) {
if (n > 100 || n < 7) n = null;
}
} while (n == null);
return n;
}
List inputCustomerNumers(int n) {
var customers = [];
for (int i = 0; i < n; i++) {
String number;
int testnumeric;
do {
print("enter the phone number of customer ${i + 1}");
number = stdin.readLineSync();
testnumeric = int.tryParse(number);
if (testnumeric != null) {
if ((number.length != 8) ||
(number[0] != "6") ||
((number[1] != "5") && (number[1] != "6"))) testnumeric = null;
}
} while (testnumeric == null);
customers.add(number);
}
return customers;
}
List inputCustomerConsumption(int n) {
var consumptions = [];
for (int i = 0; i < n; i++) {
int cons;
do {
print("enter the consumption of customer ${i + 1}");
cons = int.tryParse(stdin.readLineSync());
if (cons != null) if (cons < 0) cons = null;
} while (cons == null);
consumptions.add(cons);
}
return consumptions;
}
double average(List l) {
int a = 0;
for (int i = 0; i < l.length; i++) a += l[i];
return a / l.length;
}
void viewWinners(List t, List c, double av) {
for (int i = 0; i < t.length; i++) {
if (c[i] >= av) {
int sum = 0;
for (int j = 2; j < 8; j++) sum += int.parse(t[i][j]);
print("The number ${t[i]} won ${sum * 20}\$");
}
}
}
void main() {
int n = inputNumber();
var t = inputCustomerNumers(n);
var c = inputCustomerConsumption(n);
double averageOfConsumption = average(c);
print("The average amount consumed is: $averageOfConsumption\$");
viewWinners(t, c, averageOfConsumption);
}
7
enter the phone number of customer 1
66456789
enter the phone number of customer 2
66127721
enter the phone number of customer 3
65980765
enter the phone number of customer 4
66123321
enter the phone number of customer 5
66285585
enter the phone number of customer 6
66285582
enter the phone number of customer 7
65347743
enter the consumption of customer 1
200
enter the consumption of customer 2
20
enter the consumption of customer 3
300
enter the consumption of customer 4
250
enter the consumption of customer 5
45
enter the consumption of customer 6
99
enter the consumption of customer 7
180
The average amount consumed is: 156.28571428571428$
The number 66456789 won 780$
The number 65980765 won 700$
The number 66123321 won 240$
The number 65347743 won 560$