Chapter 9.

Write a program that contains a new class called Film. This class should encapsulate the film title and rating as in the previous chapter.

Remember to include accessor methods to set and get the title and rating.

In the Main section you should ask the user for multiple film titles and ratings and place this data in a Film object. Then write this class to a binary file. Once the user has finished entering films display the contents of the binary file to the console.

using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters;
using System.Runtime.Serialization.Formatters.Binary;

namespace Films2
{

class Class1
{

[Serializable]
public class film
{

private string filmTitle;
private int filmRating;

public string GetTitle()
{

return filmTitle;

}

public int GetRating()
{

return filmRating;

}

public void SetTitle(string str)
{

filmTitle = str;

}

public void SetRating(int num)
{

filmRating = num;

}

}

[STAThread]
static void Main(string[] args)
{

BinaryWriter bw = new BinaryWriter (File.Open("Film.dat", FileMode.Create));
string ans;

do
{

film film1 = new film();
Console.WriteLine("Enter the title of a film: ");
string str = Console.ReadLine();
film1.SetTitle(str);

Console.WriteLine("Enter the rating for the film: ");
int num = int.Parse(Console.ReadLine());
film1.SetRating(num);

BinaryFormatter fm = new BinaryFormatter();
fm.Serialize(bw.BaseStream, film1);
Console.Write("Enter another film? (Y/N): ");
ans = Console.ReadLine();

}while (ans != "N");

bw.Close();

BinaryReader br = new BinaryReader(File.Open("Film.dat", FileMode.Open));
film film2 = new film();
BinaryFormatter bm = new BinaryFormatter();

while (br.PeekChar() != -1)
{

film2 = (film)bm.Deserialize(br.BaseStream);
Console.WriteLine(film2.GetTitle());
Console.WriteLine(film2.GetRating());

}

br.Close();

}

}

}