Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.76 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Assignment3
  8. {
  9. public class Tweet
  10. {
  11.  
  12. private static int currentId = 0;
  13. public int ID;
  14. public string From;
  15. public string To;
  16. public string Message;
  17. public string Hashtag;
  18.  
  19. public override string ToString()
  20. {
  21. string str = "From: " + this.From + "nTo: " + this.To + "nMessage: " + this.Message.Substring(0, Math.Min(this.Message.Length, 10));
  22. return str;
  23. }
  24.  
  25. public static Tweet Parse(string line)
  26. {
  27. Tweet temp;
  28. try
  29. {
  30. string[] parameterList = line.Split('t');
  31. Console.WriteLine(parameterList[0]);
  32. temp = new Tweet(parameterList[0], parameterList[1], parameterList[2], parameterList[3]);
  33. }
  34. catch (IndexOutOfRangeException e)
  35. {
  36. temp = new Tweet("Invalid", "Invalid", "Invalid", "Invalid");
  37. Console.WriteLine("Exception caught: {0}", e);
  38. }
  39. return temp;
  40. }
  41.  
  42. public Tweet(string from, string to, string message, string hashtag)
  43. {
  44. this.From = from;
  45. this.To = to;
  46. this.Message = message;
  47. this.Hashtag = hashtag;
  48. this.ID = Tweet.currentId;
  49. Tweet.currentId++;
  50. }
  51.  
  52. }
  53. }
  54.  
  55. using System;
  56. using System.Collections.Generic;
  57. using System.IO;
  58. using System.Linq;
  59. using System.Text;
  60. using System.Threading.Tasks;
  61. using System.Web.Script.Serialization;
  62.  
  63. namespace Assignment3
  64. {
  65. class TweetManager
  66. {
  67.  
  68. private static Tweet[] tweets;
  69. private static string fileName;
  70.  
  71.  
  72. static TweetManager()
  73. {
  74. int length = tweets.Length;
  75. tweets = new Tweet[length];
  76.  
  77. try
  78. {
  79.  
  80.  
  81. StreamReader read = new StreamReader(@"D:tweets.txt");
  82. string line = read.ReadLine();
  83.  
  84. for (int i =0; line !=null; i++)
  85. {
  86. line = read.ReadLine();
  87. tweets[i] = Tweet.Parse(line);
  88. Console.WriteLine(line);
  89. }
  90. read.Close();
  91.  
  92. }
  93.  
  94. catch(Exception e)
  95. {
  96. Console.WriteLine("Exception: " + e.Message);
  97. }
  98. }
  99.  
  100. public static void ShowAll()
  101. {
  102. foreach (var item in tweets)
  103. {
  104. Console.WriteLine($"{item}");
  105. }
  106. }
  107.  
  108. public static void ShowAll(string hashtag)
  109. {
  110. var hash = hashtag;
  111. if (hash != hashtag)
  112. {
  113. Console.WriteLine("none of the tweets have this hashtag");
  114. }
  115. else
  116. {
  117. foreach (var item in tweets)
  118. {
  119. Console.WriteLine($"{item}");
  120. }
  121. }
  122.  
  123.  
  124. }
  125.  
  126. public static void ConvertToJson()
  127. {
  128. JavaScriptSerializer serializer = new JavaScriptSerializer();
  129.  
  130. FileStream fileStream = new FileStream("tweets.json", FileMode.OpenOrCreate, FileAccess.Write);
  131. StreamWriter streamWriter = new StreamWriter(fileStream);
  132.  
  133. var serialized = serializer.Serialize(tweets);
  134.  
  135. streamWriter.Close();
  136. fileStream.Close();
  137. }
  138.  
  139. }
  140. }
  141.  
  142. using System;
  143. using System.Collections.Generic;
  144. using System.Linq;
  145. using System.Text;
  146. using System.Threading.Tasks;
  147.  
  148. namespace Assignment3
  149. {
  150. class Program
  151. {
  152. static void Main(string[] args)
  153. {
  154. TweetManager.ShowAll();
  155.  
  156. Console.ReadKey();
  157. }
  158. }
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement