In: Computer Science
Objectives:
1. To get familiar with C# programming language
2. To get familiar with Visual Studio development environment
3. To practice on writing a C# program
Task 1: Create documentation for the following program which
includes the following:
a. Software Requirement Specification (SRS)
b. Use Case
Task 2: Write a syntactically and semantically correct C# program
that models telephones. Your program has to be
a C# Console Application. You will not implement classes in this
program other than the class which runs the main
method.
The main() method
Use a do while loop to prompt for telephone data by calling an
inputPhone( ) method which you will implement and
is explained below. Please do not change the signature of the
method!
You will need parallel arrays to telephone data.
Once done with data entry, output the telephone data stored in
arrays by invoking an outputPhones( ) method as
described below. Again, do not change the signature of the
method!
The inputPhone() method
The method signature is
static void inputPhone(ref string manufacturer, ref string
model,
ref bool hasCord, ref double price)
It prompts the user for the telephone manufacturer’s name, phone
model, price and whether it has a cord (refer to
the sample run output below). Store the entered values in the
parameters. You may use Convert.ToDouble( ) to
convert a string to a double number.
Have you noticed that all parameters are reference
parameters?
The outputPhones() method
The method signature is
static void outputPhones(string [] manufacturers, string []
models,
bool [] hasCords, double [] prices, int numberOfPhones)
The last parameter contains the number of the telephones to be
displayed.
You code need to output the heading, and use a for loop to output
the telephone data stored in the arrays passed to
the method. The format of the output is shown in the sample run
output.
Refer to the sample run output for details.
Note: Please follow the coding style of C#
Deliverables:
Sample Output:
Enter the Phone Manufacturer: VTech
Enter the Phone Model: V3399
Is it cordless? [Y or N]: y
Enter the Phone Price: $49.99
Would like to process another phone? [Y or N]: Y
Enter the Phone Manufacturer: AT&T
Enter the Phone Model: T-9898
Is it cordless? [Y or N]: N
Enter the Phone Price: $29.98
Would like to process another phone? [Y or N]: y
Enter the Phone Manufacturer: Panasonic
Enter the Phone Model: P 4321
Is it cordless? [Y or N]: Y
Enter the Phone Price: $98.97
Would like to process another phone? [Y or N]: n
Output of Telephones
=== Phone #1 ===
Manufacturer: VTech
Model: V3399
Cordless: Yes
Price: $49.99
=== Phone #2 ===
Manufacturer: AT&T
Model: T-9898
Cordless: No
Price: $29.98
=== Phone #3 ===
Manufacturer: Panasonic
Model: P 4321
Cordless: Yes
Price: $98.97
A total of 3 telephones
Press any key to continue . . .
Screenshot
Program
using System;
using System.Collections.Generic;
namespace TelephoneModels
{
class Program
{
//Main method
static void
Main(string[] args)
{
//Variables for user input
char ch=' ';
string manufacturer="", model="";
double price=0;
bool hasCord=false;
//Storage arrays
string[] manufactureDetails, modelDetails;
double[] priceDetails;
bool[] hascordDetails;
//List use for getting dynamic arrays
List<string> man = new List<string>(), mod = new
List<string>();
List<double> pr=new List<double>();
List<bool> cord=new List<bool>();
//Loop until user wishes
do
{
//Call function to get input from user
inputPhone(ref manufacturer,ref model,ref hasCord,ref price);
Console.Write("Would like to process another phone?[Y or N]:
");
man.Add(manufacturer);
mod.Add(model);
pr.Add(price);
cord.Add(hasCord);
ch = Console.ReadKey().KeyChar;
Console.WriteLine();
} while (ch == 'y' || ch == 'Y');
//Convert into array
manufactureDetails = man.ToArray();
modelDetails = mod.ToArray();
priceDetails = pr.ToArray();
hascordDetails = cord.ToArray();
//Call method to display output
outputPhones(manufactureDetails, modelDetails, hascordDetails,
priceDetails, manufactureDetails.Length);
}
//Method to get input of
phone details from user
//Prompt for each
details and add correspoding values into reference variables
static void
inputPhone(ref string manufacturer, ref string model, ref bool
hasCord, ref double price)
{
Console.Write("Enter the Phone Manufacturer: ");
manufacturer = Console.ReadLine();
Console.Write("Enter the Phone Model: ");
model = Console.ReadLine();
Console.Write("Is it cordless? [Y or N]: ");
char isCord = Console.ReadKey().KeyChar;
if (isCord == 'Y' || isCord == 'y')
{
hasCord = true;
}
else
{
hasCord = false;
}
Console.Write("\nEnter the Phone Price: $");
price = Convert.ToDouble(Console.ReadLine());
}
//Function to display
phone etails
static void
outputPhones(string[] manufacturers, string[] models,bool[]
hasCords, double[] prices, int numberOfPhones)
{
int cntr = 1;
Console.WriteLine("Output of Telephones");
for(int i = 0; i < numberOfPhones; i++)
{
Console.WriteLine("=== Phone #" + cntr + " ===");
Console.WriteLine("Manufacturer: " + manufacturers[i]);
Console.WriteLine("Model: " + models[i]);
if (hasCords[i])
{
Console.WriteLine("Cordless: Yes");
}
else
{
Console.WriteLine("Cordless: No");
}
Console.WriteLine("Price: $" + prices[i]);
cntr++;
}
Console.WriteLine("A total of " + numberOfPhones + "
telephones");
}
}
}
---------------------------------------------
Output
Enter the Phone Manufacturer: VTech
Enter the Phone Model: V3399
Is it cordless? [Y or N]: y
Enter the Phone Price: $49.99
Would like to process another phone?[Y or N]: y
Enter the Phone Manufacturer: At&T
Enter the Phone Model: T-9898
Is it cordless? [Y or N]: n
Enter the Phone Price: $29.98
Would like to process another phone?[Y or N]: y
Enter the Phone Manufacturer: Panasonic
Enter the Phone Model: P 4321
Is it cordless? [Y or N]: y
Enter the Phone Price: $98.97
Would like to process another phone?[Y or N]: n
Output of Telephones
=== Phone #1 ===
Manufacturer: VTech
Model: V3399
Cordless: Yes
Price: $49.99
=== Phone #2 ===
Manufacturer: At&T
Model: T-9898
Cordless: No
Price: $29.98
=== Phone #3 ===
Manufacturer: Panasonic
Model: P 4321
Cordless: Yes
Price: $98.97
A total of 3 telephones
Press any key to continue . . .