//****************************************************
        // Method: ReadFromFile
        //
        // Purpose: Reads Json into  CourseWork Instance
        //          Sets TextBoxes and ListViews to Approperate
        //          Values
        //****************************************************
        private void ReadFromFile(string filename)
        {
            //Reads Json into CourseWork Class
            StreamReader sr = new StreamReader(filename);

            reader  = new FileStream(filename, FileMode.Open, FileAccess.Read);
            jsonSer = new DataContractJsonSerializer(typeof(CourseWork));
            cw      = (CourseWork)jsonSer.ReadObject(reader);
            reader.Close();
            sr.Close();

            // Sets File Path, CourseName and Grade rounded to 2 decimal places
            textBoxFilePath.Text     = filename;
            textBoxCourseName.Text   = cw.CourseName;
            textBoxOverallGrade.Text = Math.Round(cw.CalculateGrade(), 2).ToString();

            //Reads Categories, Assignments, Submissions into their respective ListViews
            foreach (Category c in cw.Categories)
            {
                listViewCategories.Items.Add(c);
            }

            foreach (Assignment a in cw.Assignments)
            {
                listViewAssignment.Items.Add(a);
            }

            foreach (Submission s in cw.Submissions)
            {
                listViewSubmissions.Items.Add(s);
            }
        }
Exemplo n.º 2
0
        //*****************************************************************************
        // Method: OpenButton_Click
        //
        // Purpose: Opens the file dialog to allow the user to select a json file to open.
        //*****************************************************************************
        private void OpenButton_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();

            openFileDialog.InitialDirectory = Directory.GetCurrentDirectory();
            openFileDialog.Filter           = "JSON files (*.json)|*.json";
            if (openFileDialog.ShowDialog() == true)
            {
                string fileName = openFileDialog.FileName;

                // display file path name
                txtFilename.Text = fileName;

                // instantiate couseWork
                courseWork = new CourseWork();

                // try reading from file, if exception thrown, break
                try
                {
                    FileStream reader = new FileStream(fileName, FileMode.Open, FileAccess.Read);
                    DataContractJsonSerializer input;
                    input      = new DataContractJsonSerializer(typeof(CourseWork));
                    courseWork = (CourseWork)input.ReadObject(reader);
                    reader.Close();
                }
                catch (IOException)
                {
                    Console.WriteLine("Invalid file name.\n");
                    return;
                }

                // clear all submission textboxes
                txtAssignmentName.Clear();
                txtSubAssignment.Clear();
                txtSubCateogry.Clear();
                txtSubGrade.Clear();

                // display course name and overall grade
                txtCourseName.Text   = courseWork.CourseName;
                txtOverallGrade.Text = Math.Round(courseWork.CalculateGrade(), 2).ToString();

                // add to category listview
                for (int i = 0; i < courseWork.Categories.Count; ++i)
                {
                    listCategories.Items.Add(courseWork.Categories[i]);
                }

                // add to assignment listview
                for (int i = 0; i < courseWork.Assignments.Count; ++i)
                {
                    listAssignments.Items.Add(courseWork.Assignments[i]);
                }

                // add to submission listview
                for (int i = 0; i < courseWork.Submissions.Count; ++i)
                {
                    listSubmissions.Items.Add(courseWork.Submissions[i]);
                }
            }
        }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            string menuSelection;

            // instantiate object to use with serialization/deserialization
            CourseWork courseWork = new CourseWork();

            // file names
            string fileName;

            do
            {
                // display menu
                Console.WriteLine("Course Work Menu");
                Console.WriteLine("-------------------");
                Console.WriteLine("1 - Read course work from JSON file");
                Console.WriteLine("2 - Read course work from XML file");
                Console.WriteLine("3 - Write course work to JSON file");
                Console.WriteLine("4 - Write course work to XML file");
                Console.WriteLine("5 - Display all course work on screen");
                Console.WriteLine("6 - Find submission");
                Console.WriteLine("7 - Exit");
                Console.Write("Enter Choice: ");

                // read and store user's selection
                menuSelection = Console.ReadLine();
                Console.WriteLine();

                // perform unit testing based on user's input or exit the program
                switch (menuSelection)
                {
                case "1":     // Read course work from JSON file
                    // prompt user for file name
                    Console.Write("Enter a file name to read from: ");
                    fileName = Console.ReadLine();
                    // try reading from file, if exception thrown, break
                    try
                    {
                        FileStream   reader       = new FileStream(fileName, FileMode.Open, FileAccess.Read);
                        StreamReader streamReader = new StreamReader(reader, Encoding.UTF8);
                        string       jsonString   = streamReader.ReadToEnd();
                        byte[]       byteArray    = Encoding.UTF8.GetBytes(jsonString);
                        MemoryStream stream       = new MemoryStream(byteArray);
                        DataContractJsonSerializer input;
                        input      = new DataContractJsonSerializer(typeof(CourseWork));
                        courseWork = (CourseWork)input.ReadObject(stream);
                        reader.Close();
                    }
                    catch (IOException)
                    {
                        Console.WriteLine("Invalid file name.\n");
                    }
                    catch (SerializationException e) {
                        Console.WriteLine("Serialization exception.\n");
                        Console.WriteLine(e);
                    }
                    break;

                case "2":     // Read course work from XML file
                    // prompt user for file name
                    Console.Write("Enter a file name to read from: ");
                    fileName = Console.ReadLine();
                    // try reading from file, if exception thrown, break
                    try
                    {
                        FileStream             reader = new FileStream(fileName, FileMode.Open, FileAccess.Read);
                        DataContractSerializer input;
                        input      = new DataContractSerializer(typeof(CourseWork));
                        courseWork = (CourseWork)input.ReadObject(reader);
                        reader.Close();
                    }
                    catch (IOException)
                    {
                        Console.WriteLine("Invalid file name.\n");
                    }
                    break;

                case "3":     // Write course work to JSON file
                    // prompt user for file name
                    Console.Write("Enter a file name to write to: ");
                    fileName = Console.ReadLine();
                    // serialization code
                    FileStream jsonWriter = new FileStream(fileName, FileMode.Create, FileAccess.Write);
                    DataContractJsonSerializer jsonSer;
                    jsonSer = new DataContractJsonSerializer(typeof(CourseWork));
                    jsonSer.WriteObject(jsonWriter, courseWork);
                    jsonWriter.Close();
                    break;

                case "4":     // Write course work to XML file
                    // prompt user for file name
                    Console.Write("Enter a file name to write to: ");
                    fileName = Console.ReadLine();
                    // serialization code
                    FileStream             xmlWriter = new FileStream(fileName, FileMode.Create, FileAccess.Write);
                    DataContractSerializer xmlSer;
                    xmlSer = new DataContractSerializer(typeof(CourseWork));
                    xmlSer.WriteObject(xmlWriter, courseWork);
                    xmlWriter.Close();
                    break;

                case "5":     // Display course work data on screen
                    Console.WriteLine(courseWork);
                    Console.WriteLine("Overall grade: " + courseWork.CalculateGrade());
                    break;

                case "6":     // Find submission
                    Console.Write("Enter assignment name: ");
                    string     assignment = Console.ReadLine();
                    Submission s          = courseWork.FindSubmission(assignment);
                    if (s != null)
                    {
                        Console.WriteLine(s);
                    }
                    else
                    {
                        Console.WriteLine("Submission not found");
                    }
                    break;

                case "7":     // Exit
                    Console.WriteLine("Exiting program.\n");
                    break;

                default:
                    Console.WriteLine("Invalid input. Please enter a choice from the menu.\n");
                    break;
                }

                Console.WriteLine();
            } while (menuSelection != "7"); // display menu until user enters "16" to exit
        }
Exemplo n.º 4
0
        private void OpenCW_Click(object sender, RoutedEventArgs e)
        {
            // Created a insteance of File Dialog window

            // WHY if i put this here the filter doesnt work?
            // DialogResult result = openFileDialog.ShowDialog();

            // Sets the default path to the dialog window to the current project directory
            openFileDialog.InitialDirectory = Directory.GetCurrentDirectory();

            // Sets the default filter of the dialog windows
            openFileDialog.Filter = "JSON files (*.json) | *.json";

            // If you have multiple filters is will select the the that is in the given index and the rest will be seen in the dropdown menu
            // Where you will need to manually select them
            openFileDialog.FilterIndex = 1;

            // Creates a dialog result obj based on openFileDialog
            DialogResult result = openFileDialog.ShowDialog();

            // If the dialog box OK is pressed
            if (result == System.Windows.Forms.DialogResult.OK)
            {
                // Clear the List Views
                categoryLV.Items.Clear();
                assignmentLV.Items.Clear();
                submissionLV.Items.Clear();

                // Set text box to the file name selected
                courseWorkTB.Text = openFileDialog.FileName;

                // Deserialize JSON file and save results in cw coursework variable
                cw = (CourseWork)ReadJsonFile(cw);

                // Set text box to the course name from the Json file
                courseNameTB.Text = cw.CourseName;

                // Sets the text box to the grade that is calculated in the Dll file.
                // Then converts it to a String
                overallGradeTB.Text = Convert.ToString(cw.CalculateGrade());

                // Adds iterated through Categories list
                // Adds the datat to the list view
                foreach (var i in cw.Categories)
                {
                    categoryLV.Items.Add(i);
                }

                // Adds iterated through Assignment list
                // Adds the datat to the list view
                foreach (var i in cw.Assignment)
                {
                    assignmentLV.Items.Add(i);
                }

                // Adds iterated through Submission list
                // Adds the datat to the list view
                foreach (var i in cw.Submission)
                {
                    submissionLV.Items.Add(i);
                }
                // Sets FindSubmission textbox's to Null when new file is loaded in
                targetAsgNameTB.Text  = null;
                assignmentNameTB.Text = null;
                categoriesNameTB.Text = null;
                gradeTB.Text          = null;
            }
            // System.Diagnostics.Process.Start(Directory.GetCurrentDirectory());
            // courseWorkTB.Text = Directory.GetCurrentDirectory();
        }