Drop links or images here to add them to the editor.

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.

Cookies

Make

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.

Success Criteria

Remember to add a comment before a subprogram, selection or iteration statement to explain its purpose.

  1. Opens a file called preferences.txt for reading data. You can assume this file exists.
  2. Reads the current theme from the file.
  3. Returns the theme. There is no requirement to split theme = from the data.

Complete the subprogram called change_theme that:

  1. Takes a parameter that is the theme.
  2. If the theme is dark it is swapped to light and vice versa.
  3. Calls the write_cookie subprogram to write the new theme to the file.
  1. Takes a parameter that is the theme.
  2. Opens a file called preferences.txt for writing data.
  3. Writes the theme to the file.

Complete the main program so that:

  1. The read_cookie function is called to return the current theme.
  2. Outputs the current theme.
  3. Reads the user’s choice (y/Y to change, anything else to keep).
  4. If the user answers y or Y, the change_theme subprogram is called.

Typical inputs and outputs from the program would be:

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().

🆘 If you're really stuck, use this Parsons code sorting exercise
Complete program
// 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);
---
        }
---
    }
---
}