Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Assignment3
- {
- public class Tweet
- {
- private static int currentId = 0;
- public int ID;
- public string From;
- public string To;
- public string Message;
- public string Hashtag;
- public override string ToString()
- {
- string str = "From: " + this.From + "nTo: " + this.To + "nMessage: " + this.Message.Substring(0, Math.Min(this.Message.Length, 10));
- return str;
- }
- public static Tweet Parse(string line)
- {
- Tweet temp;
- try
- {
- string[] parameterList = line.Split('t');
- Console.WriteLine(parameterList[0]);
- temp = new Tweet(parameterList[0], parameterList[1], parameterList[2], parameterList[3]);
- }
- catch (IndexOutOfRangeException e)
- {
- temp = new Tweet("Invalid", "Invalid", "Invalid", "Invalid");
- Console.WriteLine("Exception caught: {0}", e);
- }
- return temp;
- }
- public Tweet(string from, string to, string message, string hashtag)
- {
- this.From = from;
- this.To = to;
- this.Message = message;
- this.Hashtag = hashtag;
- this.ID = Tweet.currentId;
- Tweet.currentId++;
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Web.Script.Serialization;
- namespace Assignment3
- {
- class TweetManager
- {
- private static Tweet[] tweets;
- private static string fileName;
- static TweetManager()
- {
- int length = tweets.Length;
- tweets = new Tweet[length];
- try
- {
- StreamReader read = new StreamReader(@"D:tweets.txt");
- string line = read.ReadLine();
- for (int i =0; line !=null; i++)
- {
- line = read.ReadLine();
- tweets[i] = Tweet.Parse(line);
- Console.WriteLine(line);
- }
- read.Close();
- }
- catch(Exception e)
- {
- Console.WriteLine("Exception: " + e.Message);
- }
- }
- public static void ShowAll()
- {
- foreach (var item in tweets)
- {
- Console.WriteLine($"{item}");
- }
- }
- public static void ShowAll(string hashtag)
- {
- var hash = hashtag;
- if (hash != hashtag)
- {
- Console.WriteLine("none of the tweets have this hashtag");
- }
- else
- {
- foreach (var item in tweets)
- {
- Console.WriteLine($"{item}");
- }
- }
- }
- public static void ConvertToJson()
- {
- JavaScriptSerializer serializer = new JavaScriptSerializer();
- FileStream fileStream = new FileStream("tweets.json", FileMode.OpenOrCreate, FileAccess.Write);
- StreamWriter streamWriter = new StreamWriter(fileStream);
- var serialized = serializer.Serialize(tweets);
- streamWriter.Close();
- fileStream.Close();
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Assignment3
- {
- class Program
- {
- static void Main(string[] args)
- {
- TweetManager.ShowAll();
- Console.ReadKey();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement