Projekt

Obecné

Profil

Stáhnout (4.44 KB) Statistiky
| Větev: | Tag: | Revize:
1
using System;
2
using System.Collections.Generic;
3
using System.Globalization;
4
using System.IO;
5

    
6
namespace ServerApp.Parser.InputData
7
{
8
    /// <summary>
9
    /// Class responsible for loading data from file
10
    /// </summary>
11
    /// <author>Alex Konig</author>
12
    class CsvDataLoader : DataLoader
13
    {
14
        /// <summary> Culture info for parsing numbers </summary>
15
        static CultureInfo cultureInfo = new CultureInfo("de-DE");
16

    
17
        /// <summary>
18
        /// Load csv file into array
19
        /// </summary>
20
        /// <param name="name">Path to file</param>
21
        /// <returns>Array with loaded data</returns>
22
        private string[] LoadCsv(string name)
23
        {
24
            string[] lines = File.ReadAllLines(name);
25
            return lines;
26
        }
27

    
28
        /// <summary>
29
        /// Load jis data from file
30
        /// </summary>
31
        /// <param name="pathToFile">Path to file</param>
32
        /// <returns></returns>
33
        public List<JisInstance> LoadJisFile(string pathToFile)
34
        {
35
            var lines = LoadCsv(pathToFile);
36
            List<JisInstance> list = new List<JisInstance>();
37

    
38
            for (int i = 0; i < lines.Length; i++)
39
            {
40
                var data = lines[i].Split(';');
41
                data[0] = data[0].Substring(1, data[0].Length - 2);
42
                data[1] = data[1].Substring(1, data[1].Length - 2);
43
                
44
                var dateTime = DateTime.ParseExact(data[1], "G", cultureInfo);
45

    
46
                int amount = 0;
47
                int.TryParse(data[2], out amount);
48

    
49
                JisInstance inst = new JisInstance(data[0], dateTime, amount);
50
                list.Add(inst);
51
            }
52

    
53
            return list;
54
        }
55

    
56
        /// <summary>
57
        /// Load login data from file
58
        /// </summary>
59
        /// <param name="pathToFile">Path to file</param>
60
        /// <returns></returns>
61
        public List<LogInInstance> LoadLoginFile(string pathToFile)
62
        {
63
            var lines = LoadCsv(pathToFile);
64
            List<LogInInstance> list = new List<LogInInstance>();
65

    
66
            for (int i = 0; i < lines.Length; i++)
67
            {
68
                var data = lines[i].Split(';');
69
                data[0] = data[0].Substring(1, data[0].Length - 2);
70
                data[3] = data[3].Substring(1, data[3].Length - 2);
71
                data[4] = data[4].Substring(1, data[4].Length - 2);
72
                data[5] = data[5].Substring(1, data[5].Length - 2);
73
                data[6] = data[6].Substring(1, data[6].Length - 2);
74
                data[7] = data[7].Substring(1, data[7].Length - 2);
75
                data[8] = data[8].Substring(1, data[8].Length - 2);
76

    
77
                var dateTime = DateTime.ParseExact(data[0], "G", cultureInfo);
78
                var startTime = DateTime.ParseExact(data[3], "t", cultureInfo);
79
                var endTime = DateTime.ParseExact(data[4], "t", cultureInfo);
80

    
81
                int count = 0;
82
                int.TryParse(data[1], out count);
83

    
84
                int lesson = 0;
85
                int.TryParse(data[2], out lesson);
86

    
87
                LogInInstance inst = new LogInInstance(dateTime, count, lesson, startTime, endTime, data[5], data[6], data[7], data[8]);
88
                list.Add(inst);
89
            }
90

    
91
            return list;
92
        }
93

    
94
        /// <summary>
95
        /// Load weather data from file
96
        /// </summary>
97
        /// <param name="pathToFile">Path to file</param>
98
        /// <returns></returns>
99
        public List<WeatherInstance> LoadWeatherFile(string pathToFile)
100
        {
101
            var lines = LoadCsv(pathToFile);
102
            
103
            List<WeatherInstance> list = new List<WeatherInstance>();
104
            for (int i = 0; i < lines.Length; i++)
105
            {
106
                var data = lines[i].Split(';');
107
                data[0] = data[0].Substring(1, data[0].Length - 2);
108
                
109
                var dateTime = DateTime.ParseExact(data[0], "G", cultureInfo);
110
                
111
                double temp = 0;
112
                double.TryParse(data[1], out temp);
113
                
114
                double wind = 0;
115
                double.TryParse(data[2], out wind);
116
                
117
                int rain = 0;
118
                int.TryParse(data[3], out rain);
119
                
120
                double lum = 0;
121
                double.TryParse(data[4], out lum);
122

    
123
                WeatherInstance inst = new WeatherInstance(dateTime, temp, wind, rain, lum);
124
                list.Add(inst);
125
            }
126

    
127
            return list;
128
        }
129

    
130
    }
131
}
(1-1/5)