티스토리 뷰

AT2 for c sharp programing

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;
using System.Xml.Serialization;
//Date: 15 October 2017
//This program will create a database of games.
//all games will be saved to an WML file.
//If the file does not exist then a default file will be ceated.
namespace AT2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        List<Game> mygames = new List<Game>();
        
        private void btnAdd_Click(object sender, EventArgs e)
        {
            bool hasData = true;
            Game m = new Game();

            m.GameName = textBoxName.Text;
            if (String.IsNullOrEmpty(textBoxName.Text))
            {
                MessageBox.Show("Please enter Game Name");
                hasData = false;
                return;
            }
            m.GamePrice = textBoxPrice.Text;
            if (String.IsNullOrEmpty(textBoxPrice.Text))
            {
                MessageBox.Show("Please enter Game Price");
                hasData = false;
                return;
            }
            m.DeveloperName = textBoxDeveloper.Text;
            if (String.IsNullOrEmpty(textBoxDeveloper.Text))
            {
                MessageBox.Show("Please enter Developer Name");
                hasData = false;
                return;
            }
            m.GamePlatform = comboBoxPlatfom.Text;
            if (String.IsNullOrEmpty(textBoxDeveloper.Text))
            {
                MessageBox.Show("Please enter Game Platform");
                hasData = false;
                return;
            }
            m.GameGenre = comboBoxGenre.Text;
            if (String.IsNullOrEmpty(textBoxDeveloper.Text))
            {
                MessageBox.Show("Please enter Game Genre");
                hasData = false;
                return;
            }

            if(radioButtonG.Checked == true)
            {
                m.GameRating = "General (G)";
            }
            else if (radioButtonPG.Checked == true)
            {
                m.GameRating = "Parental Guidance (PG)";
            }
            else if(radioButtonM.Checked == true)
            {
                m.GameRating = "Mature (M)";
            }
            else if (radioButtonMA.Checked == true)
            {
                m.GameRating = "Mature Audiences (MA)";
            }
            else if(radioButtonR.Checked == true)
            {
                m.GameRating = "Restricted (R)";
            }
            else if (radioButtonX.Checked == true)
            {
                m.GameRating = "Restricted (X)";
            }
            else
            {
                MessageBox.Show("Please enter Game Rating");
                hasData = false;
                return;
            }
            
            bool duplicateFound = mygames.Exists(x => x.GameName == textBoxName.Text);
            if (hasData && !duplicateFound)
            {
                mygames.Add(m); //add all fields to List<>
                ClearFields(); //clear all fields for next contact
                ShowList();
            }
            else
            {
                MessageBox.Show("Duplicate Game Name found");
            }

            ShowList();
        } //end of method btnAdd_Click
        
        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (listGames.SelectedIndex == -1)
            {
                MessageBox.Show("Please select a record from the List Box");
            }
            else
            {
                string curItem = listGames.SelectedItem.ToString();
                int index = listGames.FindString(curItem);
                mygames.RemoveAt(index);
                ClearFields();
                ShowList();
            }
        }//End of btnDelete_Click

        private void btnDisplay_Click(object sender, EventArgs e)
        {
            if (listGames.SelectedIndex == -1)
            {
                MessageBox.Show("Item is not available in Games list");
            }
            else
            {
                string curItem = listGames.SelectedItem.ToString();
                int index = listGames.FindString(curItem);
                listGames.SetSelected(index, true);
                textBoxName.Text = mygames[index].GameName;
                textBoxPrice.Text = mygames[index].GamePrice;
                textBoxDeveloper.Text = mygames[index].DeveloperName;
                comboBoxPlatfom.Text = mygames[index].GamePlatform;
                comboBoxGenre.Text = mygames[index].GameGenre;
                if (mygames[index].GameRating == "General (G)")
                {
                    radioButtonG.Checked = true;
                    return;
                }
                else if (mygames[index].GameRating == "Parental Guidance (PG)")
                {
                    radioButtonPG.Checked = true;
                    return;
                }
                else if (mygames[index].GameRating == "Mature (M)")
                {
                    radioButtonM.Checked = true;
                    return;
                }
                else if (mygames[index].GameRating == "Mature Audiences (MA)")
                {
                    radioButtonMA.Checked = true;
                    return;
                }
                else if (mygames[index].GameRating == "Restricted (R)")
                {
                    radioButtonR.Checked = true;
                    return;
                }
                else if (mygames[index].GameRating == "Restricted (X)")
                {
                    radioButtonX.Checked = true;
                    return;
                }
                else
                {
                    return;
                }
            }
        }//End of btnDisplay_Click

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            bool hasData = true;
            Game updateGame = new Game();

            updateGame.GameName = textBoxName.Text;
            if (String.IsNullOrEmpty(textBoxName.Text))
            {
                MessageBox.Show("Please enter Game Name");
                hasData = false;
                return;
            }
            updateGame.GamePrice = textBoxPrice.Text;
            if (String.IsNullOrEmpty(textBoxPrice.Text))
            {
                MessageBox.Show("Please enter Game Price");
                hasData = false;
                return;
            }
            updateGame.DeveloperName = textBoxDeveloper.Text;
            if (String.IsNullOrEmpty(textBoxDeveloper.Text))
            {
                MessageBox.Show("Please enter Developer Name");
                hasData = false;
                return;
            }
            updateGame.GamePlatform = comboBoxPlatfom.Text;
            if (String.IsNullOrEmpty(textBoxDeveloper.Text))
            {
                MessageBox.Show("Please enter Game Platform");
                hasData = false;
                return;
            }
            updateGame.GameGenre = comboBoxGenre.Text;
            if (String.IsNullOrEmpty(textBoxDeveloper.Text))
            {
                MessageBox.Show("Please enter Game Genre");
                hasData = false;
                return;
            }

            if (radioButtonG.Checked == true)
            {
                updateGame.GameRating = "General (G)";
            }
            else if (radioButtonPG.Checked == true)
            {
                updateGame.GameRating = "Parental Guidance (PG)";
            }
            else if (radioButtonM.Checked == true)
            {
                updateGame.GameRating = "Mature (M)";
            }
            else if (radioButtonMA.Checked == true)
            {
                updateGame.GameRating = "Mature Audiences (MA)";
            }
            else if (radioButtonR.Checked == true)
            {
                updateGame.GameRating = "Restricted (R)";
            }
            else if (radioButtonX.Checked == true)
            {
                updateGame.GameRating = "Restricted (X)";
            }
            else
            {
                MessageBox.Show("Please enter Game Rating");
                hasData = false;
                return;
            }



            if (listGames.SelectedIndex == -1)
            {
                MessageBox.Show("Select a record from the List Box.");
            }
            else
            {
                string curItem = listGames.SelectedItem.ToString();
                int index = listGames.FindString(curItem);

                mygames[index].GameName = updateGame.GameName;
                mygames[index].GamePrice = updateGame.GamePrice;
                mygames[index].DeveloperName = updateGame.DeveloperName;
                mygames[index].GamePlatform = updateGame.GamePlatform;
                mygames[index].GameGenre = updateGame.GameGenre;

                if (radioButtonG.Checked == true)
                {
                    mygames[index].GameRating = "General (G)";
                }
                else if (radioButtonPG.Checked == true)
                {
                    mygames[index].GameRating = "Parental Guidance (PG)";
                }
                else if (radioButtonM.Checked == true)
                {
                    mygames[index].GameRating = "Mature (M)";
                }
                else if (radioButtonMA.Checked == true)
                {
                    mygames[index].GameRating = "Mature Audiences (MA)";
                }
                else if (radioButtonR.Checked == true)
                {
                    mygames[index].GameRating = "Restricted (R)";
                }
                else if (radioButtonX.Checked == true)
                {
                    mygames[index].GameRating = "Restricted (X)";
                }
                else
                {
                    return;
                }

                ClearFields();
                ShowList();
            }

        }//end of method btnUpdate_Click
        
        private void btnLoad_Click(object sender, EventArgs e)
        {
            string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);

            //Create a mygames.xml in c:\\temp
            if (!Directory.Exists("C:\\Temp"))
                Directory.CreateDirectory("C:\\Temp");
            if (!File.Exists("C:\\Temp\\mygames.xml"))
                File.Create("C:\\Temp\\mygames.xml").Close();

            //Call method to read data from XML to List<>
            mygames = ReadFromXmlFile<List<Game>>("C:\\Temp\\mygames.xml");
            ShowList();
        }//End of method btnLoad_Click

        private void btnSave_Click(object sender, EventArgs e)
        {
            WriteToXmlFile<List<Game>>("C:\\Temp\\mygames.xml", mygames);
            MessageBox.Show("All games Saved", "Saving games", MessageBoxButtons.OK);
        }//end of method btnSave_Click

        private void ClearFields()
        {
            textBoxName.Text = "";
            textBoxPrice.Text = "";
            textBoxDeveloper.Text = "";
            comboBoxPlatfom.Text = "";
            comboBoxGenre.Text = "";
            radioButtonG.Checked = false;
            radioButtonPG.Checked = false;
            radioButtonM.Checked = false;
            radioButtonMA.Checked = false;
            radioButtonR.Checked = false;
            radioButtonX.Checked = false;
            
        }//End of method ClearFields

        public void ShowList()
        {
            listGames.Items.Clear();

            mygames.Sort(delegate (Game game1, Game game2) {
                return game1.GameName.CompareTo(game2.GameName);
            });

            foreach (var x in mygames)
            {
                listGames.Items.Add(x.GameName + " : " + x.GamePlatform);
            }
            
        }//End of method ShowList
        
        public static void WriteToXmlFile<T>(string filePath, T objectToWrite, bool append = false) where T : new()
        {
            TextWriter writer = null;
            try
            {
                var serializer = new XmlSerializer(typeof(T));
                writer = new StreamWriter (filePath, append);
                serializer.Serialize(writer, objectToWrite);
            }

            finally
            {
                if (writer != null)
                {
                    writer.Close();
                }
            }


        }//end of method WriteToXmlFile<T>

        public static T ReadFromXmlFile<T>(string filePath) where T : new()
        {
            TextReader reader = null;
            try
            {
                var serializer = new XmlSerializer(typeof(T));
                reader = new StreamReader(filePath);
                return (T)serializer.Deserialize(reader);
            }//try
            finally
            {
                if (reader != null)
                    reader.Close();
            }//finally
        }//end of method ReadFromXmlFile<T>
        
    }

    public class Game
    {
        [XmlElement("GameName")]
        public string GameName
        {
            get;
            set;
        }
        [XmlElement("Gameprice")]
        public string GamePrice
        {
            get;
            set;
        }
        [XmlElement("DeveloperName")]
        public string DeveloperName
        {
            get;
            set;
        }
        [XmlElement("GamePlatform")]
        public string GamePlatform
        {
            get;
            set;
        }
        [XmlElement("GameGenre")]
        public string GameGenre
        {
            get;
            set;
        }
        [XmlElement("GameRating")]
        public string GameRating
        {
            get;
            set;
        }


    }//end of class Game

}//end of namespace



'AU Study > TAFE Assessment' 카테고리의 다른 글

C sharp programming AT 1.1  (0) 2017.10.25
C sharp programming AT 1.2  (0) 2017.10.25
C sharp programming AT 1.4  (0) 2017.10.25
Javascript Portfolio  (0) 2017.10.24
C sharp programming AT 1.3  (0) 2017.09.29
댓글
Announcement
Recent Posts
Recent Comments
Total
Today
Yesterday
Link
TAG
more
«   2024/12   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
Search by month