Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I can't seem to figure out why my JSON code, will not print new objects in the new directory when it is made. Everything seems

I can't seem to figure out why my JSON code, will not print new objects in the new directory when it is made. Everything seems to work besides the serialization. Code: using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Json;
public class TvShow
{
public int Id { get; set; }
public string BackdropPath { get; set; }
public string Name { get; set; }
public string OriginCountry { get; set; }
public string OriginalLanguage { get; set; }
public string OriginalName { get; set; }
public string Overview { get; set; }
public double Popularity { get; set; }
public string PosterPath { get; set; }
public double VoteAverage { get; set; }
public int VoteCount { get; set; }
}
public class TvShowWriter
{
public string BaseDirPath { get; set; }
public string WriteDirPath { get; set; }
public TvShowWriter(string baseDirectory, string writeDirectoryPath)
{
BaseDirPath = baseDirectory;
WriteDirPath = writeDirectoryPath;
// Create WriteDirPath directory if it doesn't exist
if (!Directory.Exists(WriteDirPath))
{
Directory.CreateDirectory(WriteDirPath);
}
}
public void MoveToBaseDir()
{
Directory.SetCurrentDirectory(BaseDirPath);
}
public void Write(TvShow tvShow)
{
// Write TvShow details to a text file
string filePath = Path.Combine(WriteDirPath, $"{tvShow.Name}.txt");
using (StreamWriter writer = File.CreateText(filePath))
{
writer.WriteLine($"Name: {tvShow.Name}");
writer.WriteLine($"Origin Country: {tvShow.OriginCountry}");
// Write other properties as needed
}
}
public void WriteToJson(TvShow tvShow)
{
// Serialize TvShow object to JSON
string json = JsonSerializer.Serialize(tvShow);
// Write JSON to a file
string filePath = Path.Combine(WriteDirPath, $"{tvShow.Name}.json");
File.WriteAllText(filePath, json);
}
public int CreateCountryDirectories(List tvShows, string countryDirName, bool returnToBasePath = true)
{
int count =0;
string writePathDir = Path.Combine(WriteDirPath, countryDirName);
// Create directory if it doesn't exist
if (!Directory.Exists(writePathDir))
{
Directory.CreateDirectory(writePathDir);
count++;
}
// Get unique countries from tvShows
var uniqueCountries = tvShows.Select(t => t.OriginCountry).Distinct();
// Create directory for each country
foreach (var country in uniqueCountries)
{
string countryPath = Path.Combine(writePathDir, country);
if (!Directory.Exists(countryPath))
{
Directory.CreateDirectory(countryPath);
count++;
}
}
if (returnToBasePath)
{
MoveToBaseDir();
}
return count;
}
public void WriteShowsByCountry(List tvShows, string countryDirName, bool returnToBasePath = true)
{
string countryWritePathDir = Path.Combine(WriteDirPath, countryDirName);
// Create directory if it doesn't exist
if (!Directory.Exists(countryWritePathDir))
{
Directory.CreateDirectory(countryWritePathDir);
}
// Get unique countries from tvShows
var uniqueCountries = tvShows.Select(t => t.OriginCountry).Distinct();
foreach (var country in uniqueCountries)
{
string countryPath = Path.Combine(countryWritePathDir, country);
// Create directory if it doesn't exist
if (!Directory.Exists(countryPath))
{
Directory.CreateDirectory(countryPath);
}
// Write TvShows corresponding to each country
var showsInCountry = tvShows.Where(t => t.OriginCountry == country);
foreach (var show in showsInCountry)
{
string filePath = Path.Combine(countryPath, $"{show.Name}.txt");
using (StreamWriter writer = File.CreateText(filePath))
{
writer.WriteLine($"Name: {show.Name}");
writer.WriteLine($"Origin Country: {show.OriginCountry}");
// Write other properties as needed
}
}
}
if (returnToBasePath)
{
MoveToBaseDir();
}
}
}
class Program
{
static void Main(string[] args)
{
// Prompt 1: Initialize TvShowWriter
string baseDir = AppDomain.CurrentDomain.BaseDirectory;
string writeDir = @"TvShows";
TvShowWriter writer = new TvShowWriter(baseDir, writeDir);

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

1. What are the pros and cons of diversity for an organisation?

Answered: 1 week ago

Question

1. Explain the concept of diversity and equality in the workplace.

Answered: 1 week ago