//seralize and write fruit objects back into file in the new folder according to its status public static void writeFruit(Fruit fruitIn) { string fldr = @"..\..\StateTest\" + fruitIn.DeliveryState + "\\" + fruitIn.Name + ".txt"; string seralisedFruit = JsonConvert.SerializeObject(fruitIn); StreamWriter strWriter = new StreamWriter(fldr); strWriter.Write(seralisedFruit); strWriter.Close(); }
//end getter and setters public static void newStatus(Fruit fruitIn) { string fruitState; fruitIn.DeliveryState = fruitIn.deliveryState + 1; fruitState = Regex.Replace(fruitIn.deliveryState.ToString(), "(\\B[A-Z])", " $1"); Console.WriteLine(fruitIn.name + "\t " + "\t " + fruitState); FileManager.writeFruit(fruitIn); FileManager.processed(fruitIn); }
//copy file into processed folder of previous state public static void processed(Fruit fruitIn) { fruitIn.DeliveryState = fruitIn.DeliveryState - 1; string destFile = @"..\..\StateTest\" + fruitIn.DeliveryState + "\\Processed\\" + fruitIn.Name + ".txt"; string fldr = @"..\..\StateTest\" + fruitIn.DeliveryState + "\\" + fruitIn.Name + ".txt"; System.IO.File.Move(fldr, destFile); fruitIn.DeliveryState = fruitIn.DeliveryState + 1; //if fruit is in delivered state move into processed folder if (fruitIn.DeliveryState == DeliveryState.Delivered) { string destFileDelivered = @"..\..\StateTest\" + fruitIn.DeliveryState + "\\Processed\\" + fruitIn.Name + ".txt"; string fldrDelivered = @"..\..\StateTest\" + fruitIn.DeliveryState + "\\" + fruitIn.Name + ".txt"; System.IO.File.Move(fldrDelivered, destFileDelivered); }//end if }
//read all files in statetest folder and sub folders, deserialize them and add to list public static List<Fruit> readFile() { Fruit fruit1 = new Fruit(); string jsonObject; string fldr = @"..\..\StateTest\"; List<Fruit> fruitList = new List<Fruit>(); //search through all files in folder and suber folder with .txt extension and deseralize from JSON format foreach (string file in Directory.EnumerateFiles(fldr, "*.txt", SearchOption.AllDirectories)) { using (StreamReader strReader = new StreamReader(file)) { while ((jsonObject = strReader.ReadLine()) != null) { Fruit deserializedFruit = JsonConvert.DeserializeObject<Fruit>(jsonObject); fruitList.Add(deserializedFruit); }//end while }//end using }//end foreach return fruitList; }