In: Computer Science
Programming for business applications -
(New to programming. Just learned arrays, for/if/ifelse/else, and tryParse method. Please put comments so I am able to follow along with ease while doing this project.) C# is the language
Visual Studio 2019. Create a C# Console app (using .NET Framework)
Create a credit card console app. Each card has a Name, FICA score, and Credit Balance.
NAMES FICA BALANCE
Ashley Bentley 700 $200.00
Berk Fields 850 $16,250.00
(the FICA scores and Balances are right aligned and the names are left aligned.)
Code:
Type1:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
static class Main
{
public static void Main(string[] args)
{
Console.WriteLine("Enter the number of cards");
int n = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Number= {0}",n);
string[] Names= new string[n];
int[] FicaScore = new int[n];
int[] Balance = new int[n];
for (int i = 0; i < n; i++)
{
Console.WriteLine("Enter user name");
string name=Console.ReadLine();
if(string.IsNullOrEmpty(name))
{
while (false)
{
Console.WriteLine("Please enter valid name");
Console.Beep();
}
}
else
{
Names[i] = name;
}
Console.WriteLine("Enter user FicaScore");
int score=Convert.ToInt32(Console.ReadLine());
if (score <350 || score >850)
{
Console.WriteLine("Please enter valid score between 350 and
850");
}
else
{
FicaScore[i] = score;
}
Console.WriteLine("Enter user Balance");
Balance[i] = Convert.ToInt32(Console.ReadLine());
}
Console.WriteLine("Name\t FicaScore\t Balance");
for(int j=0;j<n;j++)
{
Console.WriteLine("{0}\t {1}\t
{2}\t",Names[j],FicaScore[j],Balance[j]);
}
}
}
Expalantion:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
static class ConsoleApp
{
public static void Main(string[] args)
{
Console.WriteLine("Enter the number of cards");
#asking input from number of cards and storing in n
int n = Convert.ToInt32(Console.ReadLine());
#Printing number of cards(you can ignore this if you don't want
to display
Console.WriteLine("Number= {0}",n);
#initializing array with size n(i.e, n=number of cards)
string[] Names= new string[n];
int[] FicaScore = new int[n];
int[] Balance = new int[n];
#asking for input, and validating conditions for which we use for
loop to perform operation for n number of times
for (int i = 0; i < n; i++)
{
Console.WriteLine("Enter user name");
string name=Console.ReadLine();
#IsNullOrEmoty is a function which accepts a string and returns
true only if string not null or empty
if(string.IsNullOrEmpty(name))
{
#while loop is run untill valid name is given
while (false)
{
Console.WriteLine("Please enter valid name");
Console.Beep();
}
}
#if valid name is given then name is stored in array
Names[i]
else
{
Names[i] = name;
}
Console.WriteLine("Enter user FicaScore");
#In c# input is read in string form so we are converting input
to int using Convert.ToInt32
int score=Convert.ToInt32(Console.ReadLine());
#validating the score condition
if (score <350 || score >850)
{
Console.WriteLine("Please enter valid score between 350 and
850");
}
#else score is stored in array FicaScore[i]
else
{
FicaScore[i] = score;
}
Console.WriteLine("Enter user Balance");
#In c# input is read in string form so we are converting input
to int using Convert.ToInt32
Balance[i] = Convert.ToInt32(Console.ReadLine());
}
Console.WriteLine("Name\t FicaScore\t Balance");
#Now we have to display the input, but the input is stored in
arrays, so to traverse through array we take for loop
for(int j=0;j<n;j++)
{
#\t gives a tab space between each value
Console.WriteLine("{0}\t {1}\t
{2}\t",Names[j],FicaScore[j],Balance[j]);
}
}
}
Snapshot of the input and the output obtained:
Input:
Output:
Type2:
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
namespace ConsoleApplication
{
static class Program
{
public static void Main(string[] args)
{
int n;
Console.WriteLine("Enter the number of cards in this group");
if (Console.ReadLine()==null)
{
Console.WriteLine("Entergreater then zero");
}
else {
n = Int32.Parse(Console.ReadLine());
Method1(n);
}
}
public static void Method1(int n)
{
string[] Names = new string[n];
int[] FicaScores = new int[n];
int[] Balance = new int[n];
int input;
for (int i = 0; i < n; i++)
{
Console.WriteLine("Enter user name");
if (Console.ReadLine() == null) //Verify that input is not
null
{
Console.WriteLine("Please enter user name.");
continue;
}
else
{
Names[i] = Console.ReadLine();
}
Console.WriteLine("Enter user FicaScore");
if (Int32.TryParse(Console.ReadLine(), out input) == true)
{
if (input < 350 && input > 850)
Console.WriteLine("Please enter score between 350 and 850");
}
else
{
FicaScores[i] = input;
}
Console.WriteLine("Enter user Balance");
Balance[i] = Int32.Parse((Console.ReadLine()));
Console.WriteLine("Name{i} \n FicaScore{i} \n Balance{i}
\n",Names[i], FicaScores[i],
Balance[i]);
}
}
}
}