2014年6月19日木曜日

開発環境

Head First C#―頭とからだで覚えるC#の基本 (Andrew Stellman (著)、Jennifer Green (著)、佐藤 嘉一 (監修)、木下 哲也 (翻訳)、オライリージャパン)の7章(ファイルの読み書き: バイト配列を保存し、世界を救う)、エクササイズ(p.302)を解いてみる。

エクササイズ(p.302)

コード

Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        Excuse currentExcuse = new Excuse();
        bool formChanged = false;
        Random random = new Random();
        string selectedFolder = "";

        public Form1()
        {
            InitializeComponent();
        }

        private void folder_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.SelectedPath = selectedFolder;
            DialogResult result = folderBrowserDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                selectedFolder = folderBrowserDialog1.SelectedPath;
                save.Enabled = true;
                open.Enabled = true;
                select.Enabled = true;
            }
        }
        private bool isChanged()
        {
            if (formChanged)
            {
                DialogResult result = MessageBox.Show("この口実は保存されません。よろしいですか?",
                    "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                if (result == DialogResult.No)
                {
                    return false;
                }
            }
            return true;
        }
        private void updateForm(bool changed)
        {
            if (!changed)
            {
                this.description.Text = currentExcuse.Description;
                this.results.Text = currentExcuse.Results;
                this.lastUsed.Value = currentExcuse.LastUsed;

                if(!String.IsNullOrEmpty(currentExcuse.ExcusePath))
                {
                    fileDate.Text = File.GetLastWriteTime(currentExcuse.ExcusePath).ToString();
                }
                this.Text = "口実マネージャ";
            }
            else
            {
                this.Text = "口実マネージャ*";
            }
            this.formChanged = changed;
        }

        private void save_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(description.Text) || String.IsNullOrEmpty(results.Text))
            {
                MessageBox.Show("口実とその結果を入力してください。", "保存できません");
            }
            else
            {
                saveFileDialog1.InitialDirectory = selectedFolder;
                saveFileDialog1.Filter = "テキストファイル (*.txt)|*.txt|すべてのファイル (*.*)|*.*";
                saveFileDialog1.FileName = description.Text + ".txt";
                DialogResult result = saveFileDialog1.ShowDialog();
                if (result == DialogResult.OK)
                {
                    currentExcuse.save(saveFileDialog1.FileName);
                    updateForm(false);
                    MessageBox.Show("口実を保存しました", "保存完了");
                }
            }
        }

        private void open_Click(object sender, EventArgs e)
        {
            if(isChanged())
            {
                openFileDialog1.InitialDirectory = selectedFolder;
                openFileDialog1.Filter = "テキストファイル (*.txt)|*.txt|すべてのファイル (*.*)|*.*";
                openFileDialog1.FileName = description.Text + ".txt";
                DialogResult result = openFileDialog1.ShowDialog();
                if (result == DialogResult.OK)
                {
                    currentExcuse = new Excuse(openFileDialog1.FileName);
                    updateForm(false);
                }
            }
        }

        private void select_Click(object sender, EventArgs e)
        {
            if(isChanged())
            {
                currentExcuse = new Excuse(random, selectedFolder);
                updateForm(false);
            }
        }

        private void description_TextChanged(object sender, EventArgs e)
        {
            currentExcuse.Description = description.Text;
            updateForm(true);
        }

        private void results_TextChanged(object sender, EventArgs e)
        {
            currentExcuse.Results = results.Text;
            updateForm(true);
        }

        private void lastUsed_ValueChanged(object sender, EventArgs e)
        {
            currentExcuse.LastUsed = lastUsed.Value;
            updateForm(true);
        }
    }
    class Excuse
    {
        private string description;
        private string results;
        private DateTime lastUsed;
        private string excusePath;

        public Excuse()
        {
            excusePath = "";
        }
        public Excuse(string name)
        {
            openFile(name);
        }
        public Excuse(Random random, string folder)
        {
            string[] files = Directory.GetFiles(folder, "*.txt");
            openFile(files[random.Next(files.Length)]);
        }
        public string Description
        {
            get { return description; }
            set { this.description = value; }
        }
        public string Results
        {
            get { return results; }
            set { this.results = value; }
        }
        public DateTime LastUsed
        {
            get { return lastUsed; }
            set { this.lastUsed = value; }
        }
        public string ExcusePath
        {
            get { return excusePath; }
            set { this.excusePath = value; }
        }

        public void openFile(string path)
        {
            this.excusePath = path;
            using (StreamReader sr = new StreamReader(path))
            {
                description = sr.ReadLine();
                results = sr.ReadLine();
                lastUsed = Convert.ToDateTime(sr.ReadLine());
            }
        }
        public void save(string path)
        {
            using (StreamWriter sw = new StreamWriter(path))
            {
                sw.WriteLine(description);
                sw.WriteLine(results);
                sw.WriteLine(lastUsed.ToString());
            }
        }
    }
}

0 コメント:

コメントを投稿