TINFO200/Cs3Apps/Elimination/Program.cs
2022-03-15 21:16:45 -07:00

163 lines
6.3 KiB
C#

/* Shaun Marquardt
* TINFO 200
* CS3: Elimination
* ****************************************************
* Change History
* Date Developer Description
* 2020-02-18 marqusa File creation and initial implementation, Microsoft
*
* REFERENCES
* https://docs.microsoft.com/en-us/dotnet/api/system.array.resize?view=netframework-4.8
*
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Elimination
{
class Program
{
/* This is a program that asks the user for five numbers
* between 1 and 100 inclusive to be input.
* It uses a one-dimensional array, and as the number is read,
* the program will display the number only if it's not a duplicate
* of a number already read.
*
* This program has the following requirements:
* 1) There must be 5 input values that are ultimately accepted
* by the program before it is halted.
* 2) To be accepted, the value must be between 10-100 inclusive.
* 3) To be accepted, the value must be unique having never been
* entered BEFORE that point in time.
* 4) As each value is accepted, the program displays the contents
* of the unique set of values gathered so far.
*/
static void Main(string[] args)
{
//Initialize variables.
int[] eliminationArray = new int[0];
int userInputNumber;
// user interface
// 1 - sell the user of the software to the user
// 2 - instructions (basic) on how to use the software
Console.WriteLine(@"
****************************************************
********** Welcome to the Elimination App **********
****************************************************
This program will ask you, the user, for five numbers
to be input and display only the unique values that
were provided. It can be used to deduplicate
PIN numbers, combination locks, or anywhere where
a sequence of 1 to 5 unique values is required.
You will be asked to provide five whole numbers between
1 and 100 inclusively, such as 12, 42, 39, 98, etc.
Please watch the printed warnings about input types
carefully so that you don't crash the program!
****************************************************
");
/* This for loop will prompt and ask the user for
* input a total of five times.
*
* Implements Requirement 1:
* There must be 5 input values that are ultimately accepted
* by the program before it is halted.
*/
for (int i = 0; i < 5; i++)
{
//INPUT
//Requirement 2 (10-100 inclusive)
//is implemented in GetNumber.
userInputNumber = GetNumber();
//PROCESSING
/* If-check to implement requirement 3:
* To be accepted, the value must be unique having never been
* entered BEFORE that point in time.
* So if the eliminationArray DOES NOT contain the
* input number, add it to the array.
*/
if (!eliminationArray.Contains(userInputNumber))
{
//Add one more element to the elimination array
//ensuring we use the smallest possible array for each
//run of the program.
//Ref: Microsoft
Array.Resize(ref eliminationArray, eliminationArray.Length + 1);
//Insert the unique number into the end of the array.
eliminationArray[eliminationArray.Length - 1] = userInputNumber;
}
//OUTPUT
/* Implement Requirement 4: As each value is accepted,
* the program displays the contents of the unique set
* of values gathered so far.
*/
Console.Write($"The current unique values are: ");
//Instead of writing a standard loop, I can write it
//as an expression-bodied lamda statement.
//Convert the eliminationArray ToList, and iterate using ForEach method.
//"item" goes to "console.write <current unique integer in the array>"
eliminationArray.ToList().ForEach(item => Console.Write($"{item.ToString()} "));
//Insert some space bewtween the output and our next prompt.
Console.WriteLine();
} //end for
//salutation
Console.WriteLine("\nThank you for using this program! Good bye!");
} //end main
/* Gets a number from the user on the console.
* Implements Requirement 2: To be accepted, the
* value must be between 10-100 inclusive.
*
* Preconditions: not yet
* Inputs: no args
* Outputs: integer
* Postconditions: not yet
* Ref: marqusa / cs2 barchart
*/
private static int GetNumber()
{
//PROCESSING SECTION
//setup variables
bool isValidNumber = false;
int userInputNumber;
//Loop to check for valid input between 10-100.
do
{
//Prompt the user for input.
Console.Write("Please enter a whole number from 10-100: ");
//Store the input as an integer in the userInputNumber variable.
userInputNumber = int.Parse(Console.ReadLine());
//If the number is between 10 and 100 inclusive
//the input is valid. Set the isValidNumber boolean value
//to true to break out of the while loop.
if (userInputNumber >= 10 && userInputNumber <= 100)
isValidNumber = true;
//If the number is not valid, inform the user and return to the start of do..while
if (!isValidNumber)
Console.WriteLine("Sorry, that's not a valid number.");
}
while (!isValidNumber);
//Return the valid input number, between 10 and 100 inclusive.
return userInputNumber;
} //END GetNumber
} // END class Program
}