2016-06-01 4 views
-5

私はC#で基本的なデータストレージプログラムを作成しています。私はかなり新しいです、私に簡単に行ってください。私はこれを2つのクラスに分割して、他の人が自分のメインメソッドからそれを実行できるようにしたいと思います。私の問題は、どこから始めたらいいのか分かりません。私はメソッドの別の.csファイルを追加しようとしましたが、配列への参照などでプログラムのエラーが発生します。ここに私が持っているものがあります。プログラムを2つのクラスに分割する

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.IO; 

namespace Basic_Item_Entry 
{ 
    public class Program 
    { 
     static void Main(string[] args) 
     { 
      Console.WriteLine("This program is designed to take input and hold   data for 10 items"); 
      //make array for item #'s and add 10 values 
      Console.WriteLine("Enter 10 item numbers"); 
      int[] itemNums = new int[10]; 
      for(int i = 0; i <itemNums.Length; i++) 
      { 

       itemNums[i] = Convert.ToInt32(Console.ReadLine()); 

      } 
      //make array for item descriptions 
      string[] itemDesc = new string[10]; 
      for(int i = 0; i < itemNums.Length; i++) 
      { 
       Console.WriteLine("Enter the description for item number: " +  itemNums[i]); 
       itemDesc[i] = Console.ReadLine(); 
      } 
      //add contents of arrays to a file @"C:\temp\DataEntry.txt" 
      using (System.IO.StreamWriter file = new System.IO.StreamWriter(
       @"C:\temp\DataEntry.txt")) 
      { 
       file.WriteLine("Item Data:"); 
       for (int i = 0; i < itemNums.Length; i++) 
       { 
        file.WriteLine("Item number " + itemNums[i] + " Description: " + itemDesc[i]); 

       } 
       file.Close(); 
      } 
      //finish and maybe print contents from file 
      Console.WriteLine("Data has been recorded to a file. Would you like      to view the the contents? y/n"); 
      //print array data from previously written to file     @"C:\temp\DataEntry.txt" 
      try 
      { 
       if (Console.ReadLine().Equals("y")) 
       { 
        using (StreamReader stringRead = new  StreamReader(@"C:\temp\DataEntry.txt")) 
        { 
         String DataEntryTXT = stringRead.ReadToEnd(); 
         Console.WriteLine(DataEntryTXT); 

        } 
       } 
       //dont print anything, just exit (Still creates the file) 
       else 
       { 
        System.Environment.Exit(1); 
       } 

      } 
      catch(Exception ex) 
      { 
       Console.WriteLine("File not found"); 
       Console.WriteLine(ex.Message); 
      } 


      Console.ReadLine(); 
     } 
    } 
} 
+0

私は何ができるか見てみましょうにそれを読むことを説明したロジックが含まれています。私に数分を与えてください。 –

+1

あなたのコードを整理しただけでは何も得られません。代わりに、これを試してください。どこにいても「ここで私はやっているよ」と感じる。それは新しい考えであることを意味する。それを方法に分解してください。使用しているvarsを渡し、使用する結果値を返します。 *このクラスのインスタンスを作成していないため、静的メソッド名の前に置きます。 – FirebladeDan

+0

ダンにありがとう、私は間違いなく答えを求めていません。私はちょうどどこから始めるべきかわからない、助けに感謝する。 – mattp341

答えて

0

アイテムオブジェクトは - アイテムのインスタンス(番号、説明)

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace BasicDataStorageApp 
{ 
    public class Item 
    { 
     private int _number; 

     public int Number 
     { 
      get { return _number; } 
      set { _number = value; } 
     } 

     private string _description; 

     public string Description 
     { 
      get { return _description; } 
      set { _description = value; } 
     } 

     public Item(int number, string description) 
      : this() 
     { 
      _number = number; 
      _description = description; 
     } 

     public Item() 
     { 
     } 

     public override string ToString() 
     { 
      return string.Format("Item number {0} Description {1}", _number, _description); 
     } 
    } 
} 

モデルオブジェクトを格納します - アイテムのコレクションを格納し、ファイルの読み書きするためのメソッドが含まれています。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace BasicDataStorageApp 
{ 
    public class Model 
    { 
     private Item[] _items; 

     public Item[] Items 
     { 
      get { return _items; } 
      set { _items = value; } 
     } 

     public bool WriteItems(string filename, bool append) 
     { 
      if (_items != null) 
      { 
       for (int i = 0; i < _items.Length; i++) 
       { 
        string str = _items[i].ToString(); 
        FileHelper.WriteLine(str, filename, append); 
       } 

       return true; 
      } 

      return false; 
     } 

     public IEnumerable<string> ReadItems(string filename) 
     { 
      return FileHelper.ReadLines(filename); 
     } 
    } 
} 

FileHelper - 読み書きIO静的メソッドを提供します。

using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace BasicDataStorageApp 
{ 
    public static class FileHelper 
    { 
     public static bool WriteLines(IEnumerable<string> lines, string filename, bool append) 
     { 
      try 
      { 
       using (StreamWriter writer = new StreamWriter(filename, append)) 
       { 
        foreach (var line in lines) 
        { 
         writer.WriteLine(line); 
        } 
       } 

       return true; 
      } 
      catch (Exception ex) 
      { 
       throw ex; 
      } 
     } 

     public static bool WriteLine(string line, string filename, bool append) 
     { 
      try 
      { 
       using (StreamWriter writer = new StreamWriter(filename, append)) 
       { 
        writer.WriteLine(line); 
       } 

       return true; 
      } 
      catch (Exception ex) 
      { 
       throw ex; 
      } 
     } 

     public static IEnumerable<string> ReadLines(string filename) 
     { 
      try 
      { 
       var lines = new List<string>(); 

       using (StreamReader reader = new StreamReader(filename)) 
       { 
        string line = null; 
        while ((line = reader.ReadLine()) != null) 
        { 
         lines.Add(line); 
        } 
       } 

       return lines; 
      } 
      catch (Exception ex) 
      { 
       throw ex; 
      } 
     } 
    } 
} 

プログラムは - 、ユーザー入力を取得し、それをファイルに書き込み、バックユーザー

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace BasicDataStorageApp 
{ 
    class Program 
    { 
     static Model _model; 
     const int _totalInput = 10; 
     const string _filename = @"C:\temp\DataEntry.txt"; 

     static void Main(string[] args) 
     { 
      _model = new Model(); 
      _model.Items = new Item[_totalInput]; 

      Console.WriteLine("This program is designed to take input and hold data for 10 items"); 

      int i = 0; 
      while (i < _totalInput) 
      { 
       int number = -1; 

       Console.WriteLine("\nEnter number: "); 
       string numberValue = Console.ReadLine(); 

       if (Int32.TryParse(numberValue, out number)) 
       { 
        _model.Items[i] = new Item(number, null); 

        Console.WriteLine("\nEnter description: "); 
        string descriptionValue = Console.ReadLine(); 

        _model.Items[i].Description = descriptionValue; 

        i++; 
       } 
      } 

      _model.WriteItems(_filename, true); 

      var itemStrings = _model.ReadItems(_filename); 
      foreach (var s in itemStrings) 
      { 
       Console.WriteLine(s); 
      } 

      Console.ReadLine(); 
     } 
    } 
} 
+0

これは素晴らしいことです。あなたの時間をありがとう! – mattp341

関連する問題