79.4k views
1 vote
Please note that the numbers in the square brackets [] before the task you are required to do is the number of points rewarded for the task if done correctly. The maximum number of points is 25 .

Create a new Console Application project called "ImportExportApp" (without quotes). In your Program.cs file, inside your ImportExportApp namespace and outside the Program class, create a new class called Course.
This class should have three public auto-implemented properties:
- [2] CourselD (string)
- [2] Name (string)
- [2] Duration (int)
[3] In the Course class, override the ToString() method and have it returning a string with all values from the Course properties.
In addition, the Course class must have two static methods:
[5] 1 - WriteFile: Saves one or more Course objects to a specified file (i.e. Courses.json) as JSON. This method receives two parameters:
- The first is a list of Course objects to be saved (Hint: You must use lists, not arrays),
- The second is a string that represents the mie name or the text files
This method "returns" void, and writes each course object serialized to JSON in an individual line in the file.
[5] 2 - ReadFile: Loads one or more Course objects from a specified text file (i.e. Courses.json), which comes in the same format as specified for method Write File. This method receives one parameter: A string representing the file name of the text file to be loaded.
This method returns a list of Course objects loaded from the targeted file (Hint: You must use lists, not arrays).
Once you are done, go back to the Main method in your Program.cs file, and write the test harness that fully demonstrates that your code works as per requirements. In other words:
[2] - You must invoke the WriteFile method to write a list of course objects to your hard disk. Use a declared constant to store the name of the file.
[2] - Then invoke the ReadFile method to load the data from the file you have just created. Use the same declared constant to store the name of the file.
[2] At the end, make sure to include a loop that iterates over the list of Course objects you have just loaded from the file, and display their information in the Console.
Make sure you completely adhere to all requirements. Do not change anything in terms of behavior, property names, method names, identifiers, etc. You are expected to follow all C\# naming conventions. Penalties apply if these guidelines are not followed.

User Lupa
by
7.9k points

2 Answers

5 votes

Final answer:

The task is to create a C# Console Application with a Course class capable of saving and loading its instances as JSON. It must feature auto-implemented properties, overridden ToString() method, and static methods for file I/O.

Step-by-step explanation:

The question asks to create a new Console Application project named ImportExportApp with a class called Course in C#. The Course class should contain three public auto-implemented properties: CourselD (string), Name (string), and Duration (int).

Furthermore, the class should override the ToString() method to return a string with property values, and include two static methods: WriteFile and ReadFile. WriteFile should save Course objects as JSON to a specified file, and ReadFile should load Course objects from a specified file.

A test harness in the Main method should demonstrate the functionality of these methods by writing to and reading from a file, and looping to display the loaded Course objects in the console.

Steps to Create Course Class

  1. Create the Course class with the required public properties.
  2. Override the ToString() method within the Course class.
  3. Implement the WriteFile method to serialize and save Course objects to a file as JSON.
  4. Implement the ReadFile method to deserialize and load Course objects from a file.

Testing in Main Method

  1. Invoke the WriteFile method to save a list of Course objects.
  2. Invoke the ReadFile method to load Course objects from the file.
  3. Loop over and display the loaded Course objects on the console.
User Piotr Sarnacki
by
8.6k points
2 votes

Final answer:

To create the "ImportExportApp" Console Application project and meet the requirements mentioned, follow these steps:

1. Open your preferred integrated development environment (IDE) for C#, such as Visual Studio.

2. Create a new Console Application project and name it "ImportExportApp".

3. Inside the "ImportExportApp" namespace in the Program. cs file, create a new class called "Course" with the following three public auto-implemented properties:

```csharp

public class Course

{

public string CourseID { get; set; }

public string Name { get; set; }

public int Duration { get; set; }

}

```

4. Override the ToString () method in the Course class to return a string representation of all the values in the Course properties:

```csharp

public override string ToString ()

{

return $"Course ID: {CourseID}, Name: {Name}, Duration: {Duration}";

}

```

5. Add two static methods to the Course class:

a. WriteFile method: This method saves one or more Course objects to a specified file (Courses. json) as JSON. It takes two parameters: a list of Course objects to be saved and a string representing the file name.

```csharp

public static void WriteFile (List<Course> courses, string fileName)

{

using (StreamWriter writer = new StreamWriter (fileName))

{

foreach (Course course in courses)

{

string json = JsonConvert.SerializeObject (course);

writer.WriteLine (json);

}

}

}

```

b. ReadFile method: This method loads one or more Course objects from a specified text file (Courses. json) that is formatted in JSON. It takes one parameter: a string representing the file name to be loaded.

```csharp

public static List<Course> ReadFile (string fileName)

{

List<Course> courses = new List<Course>();

using (StreamReader reader = new StreamReader (fileName))

{

string line;

while ((line = reader.ReadLine ()) != null)

{

Course course = JsonConvert. DeserializeObject<Course>(line);

courses.Add (course);

}

}

return courses;

}

```

6. In the Main method of the Program. cs file, write a test harness to demonstrate that your code works as per the requirements. Follow these steps:

```csharp

static void Main (string[] args)

{

const string fileName = "Courses. json";

// Create a list of Course objects

List<Course> courses = new List<Course>()

{

new Course() { CourseID = "C001", Name = "Mathematics", Duration = 60 },

new Course() { CourseID = "C002", Name = "Science", Duration = 45 },

new Course() { CourseID = "C003", Name = "English", Duration = 90 }

};

// Invoke the WriteFile method to save the list of Course objects to the file

Course.WriteFile(courses, fileName);

// Invoke the ReadFile method to load the data from the file

List<Course> loadedCourses = Course.ReadFile(fileName);

// Display the information of the loaded Course objects

foreach (Course course in loadedCourses)

{

Console.WriteLine (course.ToString ());

}

}

```

7. Run the program, and it will create the "Courses. json" file and load the data from it. It will then display the information of the loaded Course objects in the console.

Step-by-step explanation:

The subject of this question is Computers and Technology and the grade level is College. The question is asking to create a Console Application project called "ImportExportApp" that includes a class called Course with three public auto-implemented properties. It also requires the Course class to override the ToString () method and have two static methods, WriteFile and ReadFile. The main method should test the functionality of the code by invoking WriteFile and ReadFile methods and displaying the information of the Course objects loaded from the file.

Make sure to follow the guidelines mentioned, including adhering to C# naming conventions and not changing any behavior, property names, method names, or identifiers.

User Ulana
by
7.8k points