Question
Please I want the second and third part of the assignment. thanks First, Create a C# Console project named YourInitialsAsn1 (such as hpAsn1 for a
Please I want the second and third part of the assignment. thanks
First,
Create a C# Console project named YourInitialsAsn1 (such as hpAsn1 for a student named Harry Potter).
Use the Solution Explorer to add a new folder named YourInitialClasses in the project (such as hpClasses for a student named Harry Potter).
In this assignment you are expected to create two C# classes named Listing and Department in the YourInitialsClasses folder using Console application. The Solution Explorer for my completed project appears as follows.
Part 1:
Part 1 Task 1: Create a C# class named Course (in the YourIntialsClasses folder). The Course class should have the following properties: Note: Name each property with the first letter in upper-case and the first letter of each successive concatenated words also in upper case, as shown below).
CourseID as int (let us treat as an auto-numbered primary key)
CourseCode as string (ideally, it would be an alternate key)
Title as string
CreditHour as int
DeptCode as string (Note: Had it been a database table, this would have been a foreign key to the Department
table)
Develop a single constructor and a method as described below:
Constructor: Provide only one constructor where the user will pass all property values while constructing a course object.
Override the inherited ToString() method and return a formatted string so that a statement like Console.WriteLine(c1.ToString()); displays a course objects information as shown below:
Hints, in the inherited ToString() method should return a string as follows:
// Override the ToString method:
public override string ToString()
{
string CourseDesc =
String.Format("\t{0} \t{1} \t{2} \t{3} \t{4}", CourseID,CourseCode,Title, CreditHour, DeptCode);
return CourseDesc;
}
Part 1 Task 2: Testing the Course class: When testing the Course class in the Main method, do the following:
First enter the following lines:
Console.WriteLine("Asn1 Part1 Task2: Testing the Course Class by Mesbah Ahmed ");
//Part 1 Task 2
//Create two course objects like c1, c2 using the Constructor of the Course class
//and then display the course object's information one at a time using the overridden
//ToString() method of the Course class.
Create two Course objects as shown below:
CourseID | CourseCode | Title | CreditHour | DeptCode |
1 | ACCT2000 | Intro to Accounting | 4 | ACCT |
2 | ACCT4000 | Cost Accounting | 5 | ACCT |
After you created above two course objects, go ahead and display the information for these objects shown in the following screen shot.
Part 2: Creating a List
Part 2 Task 1:
Continue working on the Main method of the Program.cs. Start entering new code below the code for previous step.
First enter the following code before you create and populate the list of Course objects
// =====================================================
// Part 2 starts here
// =====================================================
Console.WriteLine("===================================================================");
Console.WriteLine("Part 2 starts here");
// Part2 Task 1: Create and populate a Listof Course objects
Now create a List
CourseID | CourseCode | Title | CreditHour | DeptCode |
1 | ACCT2000 | Intro to Accounting | 4 | ACCT |
2 | ACCT4000 | Cost Accounting | 5 | ACCT |
3 | INFS3000 | Intro to Programming | 3 | INFS |
4 | INFS3700 | Database Systems | 4 | INFS |
5 | INFS4010 | Systems Analysis | 6 | INFS |
6 | MKTG1000 | Intro to Marketing | 3 | MKTG |
7 | MKTG2000 | Transportation Theory | 4 | MKTG |
Part 2 Task 2:
After you have entered the code for creating and populating the list, enter the following lines:
Console.WriteLine("Part2 Task2 By Mesbah Ahmed");
// Display all course objects in the List
Console.WriteLine("The courses in the List are: ");
Now use a foreach loop to display the information for all course objects in the list. Use the ToString() method of
a course object to display the courses. At this stage, your output will be as shown below:
Part 2 Task 3: Getting a sub list from an existing List
Before you start this task, enter the following lines in your Main function (below the existing code:
Console.WriteLine("==================================================================="); Console.WriteLine("Part2 Task3 By Mesbah Ahmed");
Console.WriteLine("Create a sublist of courses for DeptCode MKTG from Courses");
// Run a "LINQ Where" query for DeptCode "MKTG" on Courses and store the result in a List object.
// Name the sublist as MktgCourses
The objective of this task is to run a LINQ query with Where method for extracting the course objects contained in Courses list for all courses offered by DeptCode MKTG, and save the results in a sub list named MktgCourses. Do not assume that you already know the MKTG couses. You must run the appropriate LINQ query. Do not try to do it in some other ways.
After you have run the query and saved the result in another list (say, MktgCourses), enter the following code:
// Display the courses in the MktgCourses list:
Console.WriteLine("The courses in the MktgCourses are:");
Now use a foreach loop to display the courses in the MktgCourses.
The results after this step would be as follows:
Part 2 Task 4: LINQ query for Sorting a List and applying Take n
In this task you are expected to employ LINQ query to display the top two courses with most credit hours in the Courses List.
First enter the following lines:
// Using LINQ Order By and Take n to sort and display top 2 expensive listing
Console.WriteLine("===================================================================");
Console.WriteLine("Part2 Task4 by Mesbah Ahmed");
Console.WriteLine("Top two Courses with most credit hours are");
// Run the appropriate LINQ query and store the results in a List
Now develop the code (i.e. the LINQ query) to sort the course objects of Courses using OrderByDescending method for CreditHour and employ Take(2) and save the results in a List
List
Note: In my handout or in some googles examples you may observe using an un-typed variable using the var keyword to do this task. The var type is resolved during the runtime and not during the compilation time, and it is often used when we do not know exactly what type of object the variable will point to. Anyway, I prefer the strongly typed objects such as List
Now use a foreach loop to display the contents of Top2Courses (or whatever name you gave to the list)
At this stage the bottom part of your result would be as shown below:
Part 3:
Note: In this part of the assignment we will consume the Course class developed in Part1 to create and populate a List
Part 3 Task 1: Creating the Department class
Step 1: In your YourInitialsClasses folder, create a C# class named Department with the properties as shown in below:
DeptCode as string (suppose it would the primary key in case of a database table)
DeptName as string
Budget as decimal
Courses as a virtual List
Hints: You may declare the Courses property using the following syntax:
public virtual List
You may wonder about the virtual keyword here. In this exercise the virtual declaration is not needed; but we will need it when we develop similar classes in MVC Entity Framework later in our course. So, I recommend that you use the keyword "virtual" here.
Step 2: After declaring the properties, develop a constructor for the Department class:
This constructor should accept the values of all properties as parameters. Observe the Courses property. Here
the constructor will expect a list of Course objects.
Step 3: At the least, you need to develop two methods as follows:
An instance method named NumOfListings that will return the number of courses in a Department. This is very simple. Just return the Courses.Count(); inside this method. To be more specific you may also use the syntax this.Courses.Count(); so that you do not get confused between all courses and the courses contained in an instance method of a department object. Note: The word this, though optional, can only be used inside any instance methods of a class.
Create the ToString() method by overriding the inherited ToString() method from the Object class. The overridden ToString method should return a formatted string. Prepare the formatted string such a way so that when the consumer can use Console.WriteLine(aDepartmentObject.ToString()) method of a Department object to display the information as shown below. Hints: In the overridden ToString() method you may return a formatted string as follows:
public override string ToString()
{
string deptDesc = string.Format("DeptCode: {0} \tName: {1} \tBudget: {2:C} \t#OfCourses: {3}",
DeptCode, DeptName, Budget, NumCourses());
return deptDesc;
}
This will display a Department as shown below:
Note: Observe that our objective here is to keep the ToString() method reasonably simple. As such, we will only display the above four values; we will not display the list of courses contained in the department. If the consumer wants to display the list of courses too, he/she has to tackle the situation himself/herself. To make it easier for a consumer, we could have built a second method to display the list of courses in a department to be called by the above ToString method. Perhaps some of you can take the challenge and implement such a situation, of course for extra credit.
For Submission:
Copy the code from Department.cs file and paste it here. Make it clean, well indented and suprress the Words spell checker. Enter some comments at the top of your Department.cs. At the least there should be comment like // Department.cs by your name
Code Listing for Department.cs
Part3 Task2: Testing the List of List
Testing: In the Program.cs main method, do the following:
Step 1: First enter the following lines. Be sure to replace my name with your name:
// =====================================================
// Part 3 starts here
// =====================================================
Console.WriteLine("=============================================================");
Console.WriteLine("Part 3 starts here");
Console.WriteLine("=============================================================");
Console.WriteLine("Part3 Task2 By Mesbah Ahmed");
// Create and populate a List of Department, and name the list as Categories
Step 2: Create and populate a list of Department. Name the list as Categories. Each of the categories will have a list of Listing. Use the following data for populating the Department and its listings (Ignore the 3rd column of the following table.
DeptCode
| DeptName | Budget | Course ID
| CourseCode | Title | Credit Hour | DeptCode |
ACCT | Accounting | 1123456.00 |
|
|
|
|
|
| 1 | ACCT2000 | Intro to Accounting | 4 | ACCT | ||
2 | ACCT4000 | Cost Accounting | 5 | ACCT | |||
INFS | Information Systems | 1234567.00 |
|
|
|
|
|
| 3 | INFS3000 | Intro to Programming | 3 | INFS | ||
4 | INFS3700 | Database Systems | 4 | INFS | |||
5 | INFS4010 | Systems Analysis | 6 | INFS | |||
MKTG | Marketing | 1111111.11 |
|
|
|
|
|
| 6 | MKTG1000 | Intro to Marketing | 3 | MKTG | ||
7 | MKTG2000 | Transportation Theory | 4 | MKTG |
Hints: While creating and populating the lists, you may see the example in about page 46 of the CSharpHandout2. A better example is available in the ListOfListExample.docx (posted at Lecture Materials > 002B folder of our blackboard). Entering records in a nested list is a pain in the neck. Be extremely careful about each and every squiggle bracket, comma, open parenthesis, close parenthesis, and semicolon. Good news: When we do the same chore in MVC webpages, we do not need this pain-in-the-neck procedure, however there will be other pains in that scenario.
After you are done with populating the list, first enter the following line:
// For each Department in the Departments, first display the Department. Then display its courses.
Now you will need to display the Departments information and its courses information as shown below:
Screen Shot for Part 3 Task 2
Hints: we need to display:
The information of a Department in the Departments list, and
Then immediately followed by displaying of each listing of the Department, we need to display the courses offered by that department.
Thus, you may need a foreach loop inside another foreach loop (known as nested loop).
The first loop will display the info about a Department in the Departments list (suppose that the iterator is named d, as in foreach (Department d in Departments).Then inside this loop we will insert another foreach loop (known as inner loop to iterate each course of d.Courses like foreach (Course c in d.Courses)
For Submission: Replace my screen shot (above) with your screen shot.
Part3 Task3: Finding an Object for a user given DepartmentCode
The objectives of this task are:
Ask the user for a Department Code (i.e., a DeptCode)
Store the user given Department code in a string object named like deptToBeFound (or whatever you like)
These two steps can be done as:
Console.Write("Please enter the Department Code for a desired Department: ");
deptToBeFound = Console.ReadLine();
Here, the first line will display Please enter blah blah on the console and the cursor will stay in the same line
expecting a value from the user. The user will enter some value and hit the Enter key. Whatever value the user
enters will be read by the console and it will store the value in the variable at left hand side of the Console.Read
statement.
Use LINQ query to find the Department object from Departments list.
If the object is not found, display an error message Sorry you entered bad Department code.
If the object is found the system should display the information of the found Department in one line, then it should display the courses of the found Department.
Step 1:
Enter the following lines:
Console.WriteLine("=============================================================");
Console.WriteLine("Part3 Task3 By Mesbah Ahmed");
Console.WriteLine("Here we will ask the user to enter a Department Code");
// Ask the user for a Department Code.
Console.Write("Please enter the Department Code for a desired Department: ");
deptToBeFound = Console.ReadLine();
// find the Department object that has the Department Code of catgToBeFound in the Categories list
// Save the resulting object in a Department typed object.
Now apply the LINQ querys Find method to find the desired object using (x => x.DeptCode == deptToBeFound) syntax and store the result in a Department type object named like theDepartment (or any other name you prefer).
Check if the object has been found. If it is not found display an error message else display the information of the theDepartment and also display the information about its courses offered by the found department (i.e., of the theDepartment.Courses).
This can be done via a simple if statement as shown below:
if (theDepartment == null)
{
Console.WriteLine("Sorry you entered bad Department code");
}
else
{ Console.WriteLine("We found the Department you wanted: its information are: ");
Console.WriteLine(theDepartment.ToString());
// Now iterate through the Courses of theDepartment and display its courses.
foreach (Course x in theDepartment.Courses)
{
Console.WriteLine(x.ToString());
}
}
Run your code and give a bad Department code. It should display the following (I am showing only the bottom half of the console screen):
For a bad code: Here, the user has entered a bad code FINA
For a good code:
Finally,
A1: Take a complete screen shot of the console and paste it here for a bad deptcode (replace my screen shot with your screen shot).
Complete Output: (in case of a bad DeptCode entered by the user)
A2: Complete Output: (in case of a good DeptCode entered by the user)
Solution Explorer Search Solution Explorer (Ctrl+ Solution "muaAsn1' project) 4 ECH muaAsn1. D A Properties D References 4 mua Classes D ct Course, cs D c Department,cs 4 Test Codes D muaAsn1TestCode.txt Y App.config D c Program.cs Solution Explorer Search Solution Explorer (Ctrl+ Solution "muaAsn1' project) 4 ECH muaAsn1. D A Properties D References 4 mua Classes D ct Course, cs D c Department,cs 4 Test Codes D muaAsn1TestCode.txt Y App.config D c Program.cs
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started