Using lists within an if and foreach statement - c#

I am creating a console application that is using user input to determine the data within two lists. Each list has the same data objects. They are name, rollNo, course and stream.
When all of the data has been inputted by the user then they are asked which stream information they would like to see. I then want to have the data that correlates with the stream input be displayed. For example if they want to see stream 1 then the information for the trainer and students on that stream should appear in the console.
This is my code below.
Student studentOne = new Student();
Console.WriteLine("Enter the Name of Student:");
studentOne.name = Console.ReadLine();
Console.WriteLine("Enter the roll number of Student:");
studentOne.rollNo = int.Parse(Console.ReadLine());
Console.WriteLine("Enter the course of Student:");
studentOne.course = Console.ReadLine();
Console.WriteLine("Enter the Stream of Student:");
studentOne.stream = int.Parse(Console.ReadLine());
Student studentTwo = new Student();
Console.WriteLine("Enter the Name of Student:");
studentTwo.name = Console.ReadLine();
Console.WriteLine("Enter the roll number of Student:");
studentTwo.rollNo = int.Parse(Console.ReadLine());
Console.WriteLine("Enter the course of Student:");
studentTwo.course = Console.ReadLine();
Console.WriteLine("Enter the Stream of Student:");
studentTwo.stream = int.Parse(Console.ReadLine());
Student studentThree = new Student();
Console.WriteLine("Enter the Name of Student:");
studentThree.name = Console.ReadLine();
Console.WriteLine("Enter the roll number of Student:");
studentThree.rollNo = int.Parse(Console.ReadLine());
Console.WriteLine("Enter the course of Student:");
studentThree.course = Console.ReadLine();
Console.WriteLine("Enter the Stream of Student:");
studentThree.stream = int.Parse(Console.ReadLine());
var studentData = new List<Student>();
studentData.Add(studentOne);
studentData.Add(studentTwo);
studentData.Add(studentThree);
Trainer trainerOne = new Trainer();
Console.WriteLine("Enter the Name of the trainer:");
trainerOne.name = Console.ReadLine();
Console.WriteLine("Enter the roll number of the trainer:");
trainerOne.trainNo = int.Parse(Console.ReadLine());
Console.WriteLine("Enter the course of the trainer:");
trainerOne.course = Console.ReadLine();
Console.WriteLine("Enter the Stream of the trainer:");
trainerOne.stream = int.Parse(Console.ReadLine());
Trainer trainerTwo = new Trainer();
Console.WriteLine("Enter the Name of the trainer:");
trainerTwo.name = Console.ReadLine();
Console.WriteLine("Enter the roll number of the trainer:");
trainerTwo.trainNo = int.Parse(Console.ReadLine());
Console.WriteLine("Enter the course of the trainer:");
trainerTwo.course = Console.ReadLine();
Console.WriteLine("Enter the Stream of the trainer:");
trainerTwo.stream = int.Parse(Console.ReadLine());
Trainer trainerThree = new Trainer();
Console.WriteLine("Enter the Name of the trainer:");
trainerThree.name = Console.ReadLine();
Console.WriteLine("Enter the roll number of the trainer:");
trainerThree.trainNo = int.Parse(Console.ReadLine());
Console.WriteLine("Enter the course of the trainer:");
trainerThree.course = Console.ReadLine();
Console.WriteLine("Enter the Stream of the trainer:");
trainerThree.stream = int.Parse(Console.ReadLine());
List<Trainer> trainerData = new List<Trainer>();
trainerData.Add(trainerOne);
trainerData.Add(trainerTwo);
trainerData.Add(trainerThree);
Console.WriteLine("Which stream details do you want to view?");
var streamInfo = int.Parse(Console.ReadLine());
if (streamInfo == 1)
{
foreach (var stream in trainerData)
{
}
}
class Student
{
public string name { get; set; }
public int rollNo { get; set; }
public string course { get; set; }
public int stream { get; set; }
}
class Trainer
{
public string name { get; set; }
public int trainNo { get; set; }
public string course { get; set; }
public int stream { get; set; }
}
Any help is appreciated.
Edit: I didn't make clear what I'm asking for. I'm not sure how to have the correct data show depending on what the stream input is. I have tried using an if statement and foreach statement but I don't know if this is the best approach.

Let's decompose the solution, let's start from reading integers:
private static int ReadInteger(string title, Func<int, bool> isValid = null) {
if (isValid is null)
isValid = x => true;
while (true) {
if (!string.IsNullOrWhiteSpace(title))
Console.WriteLine(title);
if (int.TryParse(Console.ReadLine(), out int result) && isValid(result))
return result;
Console.WriteLine("Invalid value, please, try again.");
}
}
private static string ReadString(string title, Func<string, bool> isValid = null) {
if (isValid is null)
isValid = x => true;
while (true) {
if (!string.IsNullOrWhiteSpace(title))
Console.WriteLine(title);
string result = Console.ReadLine();
if (isValid(result))
return result;
Console.WriteLine("Invalid value, please, try again.");
}
}
Then we can implement ReadStudent and ReadTrainer:
public static Student ReadStudent() {
return new Student() {
name = ReadString("Enter the Name of Student:", x => !string.IsNullOrWhiteSpace(x)),
rollNo = ReadInteger("Enter the roll number of Student:", x => x >= 0),
course = ReadString("Enter the course of Student:", x => !string.IsNullOrWhiteSpace(x)),
stream = ReadInteger("Enter the Stream of Student:", x => x >= 0),
};
}
public static Trainer ReadTrainer() {
return new Trainer() {
name = ReadString("Enter the Name of trainer:", x => !string.IsNullOrWhiteSpace(x)),
rollNo = ReadInteger("Enter the roll number of trainer:", x => x >= 0),
course = ReadString("Enter the course of trainer:", x => !string.IsNullOrWhiteSpace(x)),
stream = ReadInteger("Enter the Stream of trainer:", x => x >= 0),
};
}
Finally, we can create as many students and trainers as we want (note for loop - what if we want to enter 100 students?):
int students = 3;
int trainers = 3;
var studentData = new List<Student>(students);
for (int i = 0; i < students; ++i)
studentData.Add(ReadStudent());
var trainerData = new List<Trainer>(trainers);
for (int i = 0; i < trainers; ++i)
trainerData.Add(ReadTrainer());
Finally, to query the list by stream you can use Linq or good old loop or Linq:
int stream = ReadInteger("Which stream details do you want to view?");
foreach (Student s in studentData)
if (s.stream == stream)
Console.WriteLine($"Student {s.name} Roll No {s.rollNo}");
foreach (Trainer t in trainerData)
if (t.stream == stream)
Console.WriteLine($"Trainer {t.name} Roll No {t.rollNo}");

Related

Adding multiple rows to a table in docx files with OpenXML

I am using TemplateEngine.Docx, Template engine for generating Word docx in Visual Studio for C#. Basically I am trying to add multiple rows to a table in an edited word document using a for loop. However when I loop through the if statement it will just rewrite the data that was just read. Here is my code:
if (fieldName == "examrubric")
{
for (; ; )
{
Console.WriteLine("To enter a row to the tabel type 'yes'\n");
string choice = Console.ReadLine();
if (choice == "yes")
{
Console.WriteLine("Enter a value for the question number:\n");
string qnum = Console.ReadLine();
Console.WriteLine("Enter a value for what the question is out of:\n");
string score = Console.ReadLine();
Console.WriteLine("Enter a value for how much was scored:\n");
string qscore = Console.ReadLine();
Console.WriteLine("Enter a value for score:\n");
string score2 = Console.ReadLine();
Console.WriteLine("Enter the total mark:\n");
string total = Console.ReadLine();
valuesToFill = new Content(
new TableContent("examrubric")
.AddRow(
new FieldContent("qnum", qnum),
new FieldContent("score", score),
new FieldContent("qscore", qscore),
new FieldContent("score2", score2),
new FieldContent("total", total)));
}
else
{
break;
}
}
}
You could try the following code to write data to the docx file in the loop to avoid the rewrite the data .
static void Main(string[] args)
{
File.Copy("test1.docx", "OutputDocument.docx");
Console.WriteLine("To enter a row to the tabel type 'yes'\n");
string choice = Console.ReadLine();
Content valuesToFill = new Content(new TableContent("Team Members Table"));
for (int i = 0; i < 2; i++)
{
if (choice == "yes")
{
Console.WriteLine("Enter a value for the Name:\n");
string name = Console.ReadLine();
Console.WriteLine("Enter a value for the Role:\n");
string role = Console.ReadLine();
valuesToFill.Tables.First().AddRow(
new FieldContent("Name", name),
new FieldContent("Role", role)) ;
}
}
using (var outputDocument = new TemplateProcessor("OutputDocument.docx").SetRemoveContentControls(true))
{
outputDocument.FillContent(valuesToFill);
outputDocument.SaveChanges();
}
}
Tested result:

C# Program to store multiple student record with name, grade1 - grade5, and average

I am trying to create a program that takes user input for the amount of students and then with that in mind prompt for 5 grades and an average per student.
An example of a student record would be as follows:
Student #...... Grade1..... Grade2..... Grade3..... Grade4..... Grade5..... Average
Code so far (Probably not correct in anyway):
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Question_Two
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Input the number of students: ");
int n = int.Parse(Console.ReadLine());
Student[] students = new Student[6];
string studentId = "Student 1"; //error here
students[0].addGrade(double.Parse(studentId)); // or error here
double value;
value = double.Parse(Console.ReadLine());
students[1].addGrade(value);
value = double.Parse(Console.ReadLine());
students[2].addGrade(value);
value = double.Parse(Console.ReadLine());
students[3].addGrade(value);
value = double.Parse(Console.ReadLine());
students[4].addGrade(value);
value = double.Parse(Console.ReadLine());
students[5].addGrade(value);
students[6].getAverage();
}
public class Student
{
private ArrayList grades;
public Student()
{
grades = new ArrayList();
}
public void addGrade(double val)
{
grades.Add(val);
}
public double getAverage()
{
double avg = 0;
for (int i = 0; i < grades.Count; i++)
{
avg += (double)grades[i];
}
avg /= grades.Count;
return avg;
}
}
}
}
I am not sure where to go from here as I have an error when storing the name into the array.
Creating an array with new does not create each individual element (except primitives like int). You need to call new for every item in the array:
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Input the number of students: ");
int n = int.Parse(Console.ReadLine());
// Create array of size n (instead of 6)
// Array is created but individual Students are not created yet!
Student[] students = new Student[n];
// Use a loop to get all student info
for (int s = 0; s < n; s++) {
string studentId = $"Student{s+1}";
// Important! Need to create a student object
students[s] = new Student(studentId);
// Use another loop to get grades
for (int g = 0; g < 5; g++) {
double grade = double.Parse(Console.ReadLine());
students[s].addGrade(grade);
}
// Print average
Console.WriteLine($"{students[s].id} average = {students[s].getAverage()}");
}
}
Also, modify the Student class to have an id
public class Student
{
private ArrayList grades;
public string id;
// Accepts one parameter : an id
public Student(string studentId)
{
id = studentId;
grades = new ArrayList();
}
public void addGrade(double val)
{
grades.Add(val);
}
public double getAverage()
{
double avg = 0;
for (int i = 0; i < grades.Count; i++)
{
avg += (double)grades[i];
}
avg /= grades.Count;
return avg;
}
}
}
You can not parse a string "Student 1" to double like in the following code:
string studentId = "Student 1"; //error here
students[0].addGrade(double.Parse(studentId));
I don't know what you want to the result to be. But make sure that the student id is a numeric value (can be in string form). You can use the following code to get what you want.
Console.WriteLine("Input the number of students: ");
int numberOfStudents = int.Parse(Console.ReadLine());
Student student = new Student();
string studentId = "1";
student.addGrade(double.Parse(studentId));
double value;
value = double.Parse(Console.ReadLine());
student.addGrade(value);
value = double.Parse(Console.ReadLine());
student.addGrade(value);
value = double.Parse(Console.ReadLine());
student.addGrade(value);
value = double.Parse(Console.ReadLine());
student.addGrade(value);
value = double.Parse(Console.ReadLine());
student.addGrade(value);
double avarageNumber = student.getAverage();

checking integers in String

I'm checking a string whether it has integers or anything else in function Parse().
Here is my Code
static public int input()
{
Console.WriteLine("Enter The Number Of Student You Want to get Record");
int x;
string inputString = Console.ReadLine();
if (int.TryParse(inputString, out x))
{
Console.WriteLine(inputString + " Is Integer");
return x= Convert.ToInt32(inputString);
}
else
{
input();
}
return x;
}
And full code is:
static void Main(string[] args)
{
int num = 0;
string[] names = new string[] { };
long[] fee = new long[] { };
string[] className = new string[] { };
do
{
Console.WriteLine("Enter Option You Want: \nA:Enter Student Record\nB:Display Student Record\nQ:Exit");
string option =null;
option =Console.ReadLine();
switch (option)
{
case "A":
case "a":
{
num = input();
names = new string[num];
fee = new long[num];
className = new string[num];
for (int i = 0; i < num; i++)
{
Console.WriteLine("Enter Name Of Student:{0}",i);
Console.Write("Enter Student Name: "); names[i] = Console.ReadLine();
Console.Write("Enter Student Fee: "); fee[i] = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter Student Class Name: "); className[i] = Console.ReadLine();
}
break;
}
case "B":
case "b":
{
for (int i = 0; i < names.Length; i++)
{
Console.WriteLine("Record of Student: {0}",i);
Console.WriteLine("Name: "+names[i]+ "\nFee: " + fee[i]+ "\nClass Name: " + className[i]);
//Console.WriteLine("Name: {0}\n Class Name: {1}\n Fee: {3}\n",names[i],className[i],fee[i]);
}
break;
}
case "Q":
case "q":
{
Environment.Exit(1);
break;
}
default:
{
Console.WriteLine("Invalid Option");
break;
}
}
} while (true);
}
But The problem is when I enters char instead of int and it works fine and calls itself again but if 2nd time or after 2nd time I input int then does not take input of students and instead it repeats the LOOP again.
So what's the problem, is problem in Input Function????
You could use a regular expression to find the INTs. Also you should call
return input();
instead of
input();
new method:
static public int input(){
Console.WriteLine("Enter The Number Of Student You Want to get Record");
string input = Console.ReadLine();
if (Regex.IsMatch(input, #"\d+"))
{
return int.Parse(Regex.Match(input, #"\d+").Value);
}
else
{
return input();
}
}
I'm assuming you're a student. I started out with C# doing the same stuff. Which I wouldn't do anymore but since you are doing it. I'd recommend using goto, making this method recursive is a no no.
static public int input()
{
Prompt:
Console.WriteLine("Enter The Number Of Student You Want to get Record");
int x;
string inputString = Console.ReadLine();
if (int.TryParse(inputString, out x))
{
Console.WriteLine(inputString + " Is Integer");
return x;
}
else
{
goto Prompt;
}
}

Two instances of a class: only sort 1

I have a basic class that has four attributes (Patient). In Main() I have reserved the memory for the array and before I actually create the instance, I ask the user for the account number to ensure it doesn't already exist within the array. So BinarySearch requires it to be sorted but as soon as I sort it the ability to do the for loop is lost.
//Variables
int intMaxNum = 5; //set max number of patients to 5
int intInputValue;
int intResult;
string strTempName;
int intTempAge;
double dblTempTotal;
Patient[] objectPatient = new Patient[intMaxNum]; //create an array of references
for (int x = 0; x < objectPatient.Length; ++x)
{
//attempt to create a 'shadow' class to search through and keep integrity of main class (objectPatient)
Patient[] tempobjectPatient = new Patient[intMaxNum];
tempobjectPatient = objectPatient;
if (x > 0)
{
Console.Write("\n***Next Patient***");
Array.Sort(tempobjectPatient); //this will sort both objects even though I send the temporary class only - interface impact I'm sure
}
//ask for the Patient Account number
Console.Write("\nEnter Patient Account Number: ");
ReadTheAccountNumber:
intInputValue = Convert.ToInt32(Console.ReadLine());
//create temporary class for comparison
Patient SeekPatient = new Patient();
SeekPatient.PatientNumber=intInputValue; // reset the default info with the input Pateint Account Number
//verify the Patient Account number doesn't already exist
intResult = Array.BinarySearch(tempobjectPatient, SeekPatient);
//intResult = Array.BinarySearch(objectPatient, SeekPatient);
//if (objectPatient.Equals(SeekPatient)) //Can not get the .Equals to work at all...
if (intResult >= 0)
{
Console.Write("\nSorry, Patient Account Number {0} is a duplicate.", intInputValue);
Console.Write("\nPlease re-enter the Patient Account Number: ");
goto ReadTheAccountNumber;
}
else //no match found, get the rest of the data and create the object
{
if (x > 0) { Console.Write("***Patient Account Number unique and accepted***\n"); } //looks silly to display this if entering the first record
Console.Write("Enter the Patient Name: ");
strTempName = Console.ReadLine();
Console.Write("Enter the Patient Age: ");
intTempAge = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter the total annual Patient amount due: ");
dblTempTotal = Convert.ToDouble(Console.ReadLine());
objectPatient[x] = new Patient(intInputValue, strTempName, intTempAge, dblTempTotal);
}
}
Here is the class:
class Patient : IComparable
{
//Data fields
private int patientNumber;
private string patientName;
private int patientAge;
private double patientAmountDue;
//Constructors
public Patient(): this(9,"ZZZ",0,0.00)
{
}
public Patient(int _patientNumber, string _patientName, int _patientAge, double _patientAmountDue)
{
PatientNumber = _patientNumber;
PatientName = _patientName;
PatientAge = _patientAge;
PatientAmountDue = _patientAmountDue;
}
//Properties
public int PatientNumber
{
get { return patientNumber; }
set { patientNumber = value; }
}
public string PatientName
{
get { return patientName; }
set { patientName = value; }
}
public int PatientAge
{
get { return patientAge; }
set { patientAge = value; }
}
public double PatientAmountDue
{
get { return patientAmountDue; }
set { patientAmountDue = value; }
}
//Interfaces
int IComparable.CompareTo(Object o)
{
int returnVal; //temporary value container
Patient temp = (Patient)o; //create temp instance of the class
if (this.PatientNumber > temp.PatientNumber)
returnVal = 1;
else
if (this.PatientNumber < temp.PatientNumber)
returnVal = -1;
else
returnVal = 0; //exact match
return returnVal;
}
}
You can put all the patient numbers into HashSet<int> and test via Contains() if a number is allocated one:
class Patient : IComparable {
...
// Simplest, not thread safe
private static HashSet<int> s_AllocatedPatientNumbers = new HashSet<int>();
public static Boolean IsNumberAllocated(int patientNumber) {
return s_AllocatedPatientNumbers.Contains(patientNumber);
}
public int PatientNumber {
get {
return patientNumber;
}
set {
s_AllocatedPatientNumbers.Remove(patientNumber);
patientNumber = value;
s_AllocatedPatientNumbers.Add(patientNumber);
}
}
}
So whenever you need to test if the number has been allocated you have no need to create a temporal patient, sort the array etc. just one simple call:
if (Patient.IsNumberAllocated(intInputValue)) {
...
}
This
Patient[] tempobjectPatient = new Patient[intMaxNum];
creates a new array and assigns it to tempobjectPatient. But this new array is never used, because here
tempobjectPatient = objectPatient;
you immediately assign the old one to tempobjectPatient. So after this, you don't have two array instances. Both tempobjectPatient and objectPatient refer to the same instance.
You probably want:
Patient[] tempobjectPatient = (Patient[])objectPatient.Clone();
Replace the Array with a Dictionary, it is desigend to be used with unique keys:
Dictionary<int,Patient> patients = new Dictionary<int,Patient>();
while (true)
{
Console.Write("\nEnter Patient Account Number: ");
int number = Convert.ToInt32(Console.ReadLine());
if (patients.ContainsKey(number))
{
Console.Write("\nSorry, Patient Account Number {0} is a duplicate.", number);
Console.Write("\nPlease re-enter the Patient Account Number: ");
continue;
}
Console.Write("***Patient Account Number unique and accepted***\n");
Console.Write("Enter the Patient Name: ");
string name = Console.ReadLine();
Console.Write("Enter the Patient Age: ");
int age = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter the total annual Patient amount due: ");
double amountDue = Convert.ToDouble(Console.ReadLine());
patients.Add(number, new Patient(number, name, age, amountDue));
}
The loop is now missing an exit condition.
EDIT:
While this does not answer the question of the title i think it is what the OP was looking for.

C# problems with a for loop

Can someone tell me why this doesnt work. When I enter the loop it prints everything instead of one line and get the users input. It prints Enter the integer the account numberEnter the integer the account balanceEnter the account holder lastname
Got it working thanks everyone, but now the searchaccounts doesnt work
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class accounts
{
private int[] accountnum = new int[5]; //private accountnum array of five integer account numbers
private int[] accountbal = new int[5];//private balance array of five balance amounts
private string[] accountname = new string[5];//private accntname array to hold five last names
public void fillAccounts()
{
int bal;
int accountnumber;
string name;
for (int x = 0; x < 5; ++x)
{
Console.Write("Enter the integer the account number");
accountnumber = Console.Read();
Console.Write("Enter the integer the account balance");
bal = Console.Read();
Console.Write("Enter the account holder lastname");
name = Console.ReadLine();
accountnum[x] = accountnumber;
accountbal[x] = bal;
accountname[x] = name;
}
}
public void searchAccounts()
{
Console.WriteLine("Enter the account number");
int acctnum = Console.Read();
for (int x = 0; x < 6; ++x)
{
if (x < 5)
{
if (accountnum[x] == acctnum)
{
Console.WriteLine("Account #{0} has a balance of {1} for customer {2}", acctnum, accountbal[x].ToString("C"), accountname[x]);
break;
}
}
else
{
Console.WriteLine("You entered invalid account number");
}
}
}
public void averageAccounts()
{
int sum = 0;
int avg;
for (int x = 0; x < 5; ++x)
{
sum = accountbal[x] + sum;
}
avg = sum / 5;
Console.WriteLine("The average dollar amount is {0}", avg.ToString("c"));
}
}
class assignment3_alt
{
static void Main(string[] args)
{
accounts myclass = new accounts();
string userin;
myclass.fillAccounts();
int i = 0;
while (i != 1)
{//use the following menu:
Console.WriteLine("*****************************************");
Console.WriteLine("enter an a or A to search account numbers");
Console.WriteLine("enter a b or B to average the accounts");
Console.WriteLine("enter an x or X to exit program");
Console.WriteLine("*****************************************");
Console.Write("Enter option-->");
userin = Console.ReadLine();
if (userin == "a" || userin == "A")
{
myclass.searchAccounts();
}
else if (userin == "b" || userin == "B")
{
myclass.averageAccounts();
}
else if (userin == "x" || userin == "X")
{
break;
}
else
{
Console.WriteLine("You entered an invalid option");
}
}
}
}
}
Console.Read only reads a single character. You need to use Console.ReadLine.
Console.Write("Enter the integer the account number");
accountnumber = int.Parse(Console.ReadLine());
Console.Write("Enter the integer the account balance");
bal = int.Parse(Console.ReadLine());
Console.Write("Enter the account holder lastname");
name = Console.ReadLine();
You might also want to consider using int.TryParse instead of int.Parse so that you can better handle invalid input.
For your new question it is the same error. Just replace:
int acctnum = Console.Read();
with
int acctnum = int.Parse(Console.ReadLine());
or (preferably)
int acctnum;
if (!int.TryParse(Console.ReadLine(), out acctnum))
{
Console.WriteLine("You need to enter a number");
return;
}
The first will fail if the user doesn't enter a valid integer the second will print out a nice error message and return to the loop.
This is not an answer, as other have already put up some good ones. These are just a few tips about your code.
Instead of looping with while (i != 1), never changing the value of i and terminating the loop with break, it would be better to use a do-while loop, like this:
do
{
// blah blah
} while(userin != "x" || userin != "X")
It is less confusing than having a variable with no use at all.

Categories