Saving website preferences.
★☆☆Websites save user preferences in small files called cookies. This allows the user to customise their experience on the website with preferences saved for when they return to the site in the future.
Write a program that reads a text file stored on the computer and outputs the contents. This will either be: theme = dark or theme = light. The program then asks the user if they wish to change the theme, and if they do writes this new data to the file, overwriting what was previously stored.
Remember to add a comment before a subprogram, selection or iteration statement to explain its purpose.
read_cookie that:preferences.txt for reading data. You can assume this file exists.theme = from the data.change_theme that:write_cookie subprogram to write the new theme to the file.write_cookie that:preferences.txt for writing data.read_cookie function is called to return the current theme.y/Y to change, anything else to keep).y or Y, the change_theme subprogram is called.The current theme = dark
n
The current theme = dark
y
The structure of the preferences.txt file is:
theme = dark
or…
theme = light
The test runner provides the y/n choice on stdin without any prompt, so the program reads directly with
Console.ReadLine().
// Cookies program
using System;
using System.IO;
class Submission
{
---
// -------------------------
// Subprograms
// -------------------------
// Read the light/dark theme from the cookie file
---
static string read_cookie()
{
---
string file_name = "preferences.txt";
string theme = "";
StreamReader file = new StreamReader(file_name);
theme = file.ReadLine().Trim();
file.Close();
return theme;
---
}
---
// Write the new theme to the cookie file
static void write_cookie(string theme)
{
---
StreamWriter file = new StreamWriter("preferences.txt");
file.WriteLine(theme);
file.Close();
---
}
---
// Swap the theme
static void change_theme(string theme)
{
---
// Dark to light or light to dark
if (theme == "theme = dark")
{
---
theme = "theme = light";
---
}
---
else
{
---
theme = "theme = dark";
---
}
---
write_cookie(theme);
---
}
---
// -------------------------
// Main program
// -------------------------
public static void Main(string[] args)
{
---
string theme = read_cookie();
---
Console.WriteLine($"The current {theme}");
---
string change = Console.ReadLine();
---
// Swap the theme if the user enters y/Y
if (change.ToLower() == "y")
{
---
change_theme(theme);
---
}
---
}
---
}