Exemplo n.º 1
0
        public static void SaveToText(Shelf shelf, string path)
        {
            FileStream      fileStream      = new FileStream(path, FileMode.Create);
            BinaryFormatter binaryFormatter = new BinaryFormatter();

            binaryFormatter.Serialize(fileStream, shelf);
            fileStream.Close();
            Console.WriteLine("File saved Successfully to " + path);
        }
Exemplo n.º 2
0
        public static void SaveToJSON(Shelf shelf, string path)
        {
            JsonSerializer serializer = new JsonSerializer();

            using (StreamWriter sw = new StreamWriter(path))
                using (JsonWriter writer = new JsonTextWriter(sw))
                {
                    serializer.Serialize(writer, shelf.shelf);
                }
        }
Exemplo n.º 3
0
        public static void LoadFromTxt(string path)
        {
            FileStream fileStream = new FileStream(path, FileMode.Open);

            if (fileStream.Length > 6)
            {
                BinaryFormatter binaryFormatter   = new BinaryFormatter();
                Shelf           deserializedShelf = (Shelf)binaryFormatter.Deserialize(fileStream);
                fileStream.Close();
                AddressBookMain.shelf = deserializedShelf;
                Console.WriteLine("File loaded Successfully from " + path);
            }
            else
            {
                Console.WriteLine("Load File Empty, creating new Shelf");
            }
        }
Exemplo n.º 4
0
 public static void LoadFromJSON(Shelf shelf, string path)
 {
     shelf.shelf = JsonConvert.DeserializeObject <Dictionary <string, AddressBook> >(File.ReadAllText(path));
 }
Exemplo n.º 5
0
        static void Main(string[] args)
        {
            Console.WriteLine(DateTime.Now);
            FileIO.LoadFromTxt(savePath);

            Logger nlog = LogManager.GetCurrentClassLogger();

            if (shelf == null)
            {
                shelf = new Shelf();
            }
            shelf.ListBooks();

            bool flag = true;
            int  choice;

            while (flag)
            {
                try
                {
                    Console.WriteLine("\n1. Create New Address Book \n2. Use Address Book\n3. Search Contact by City Name\n4. Search Contact by State Name" +
                                      "\n5. Save To JSON" + "\n6. Load from JSON" + "\n7. Exit");
                    choice = int.Parse(Console.ReadLine());
                    if (choice == 1)
                    {
                        AddressBook addressBook = new AddressBook();
                        Console.Write("\nEnter New Address Book's Name: ");
                        string addressBookName = Console.ReadLine();
                        shelf.AddNewAddressBook(addressBookName, addressBook);
                        Console.WriteLine("Successfully created " + addressBookName + "\tUsing Address Book " + addressBookName + "...");
                        UseAddressBook(addressBookName, addressBook);
                    }
                    else if (choice == 2)
                    {
                        shelf.ListBooks();
                        Console.Write("\nEnter Address Book's Name: ");
                        string      addressBookName = Console.ReadLine();
                        AddressBook addressBook     = shelf.GetAddressBook(addressBookName);
                        if (addressBook != null)
                        {
                            Console.WriteLine("Using Address Book " + addressBookName + "...");
                            UseAddressBook(addressBookName, addressBook);
                        }
                        else
                        {
                            Console.WriteLine("There is no Book with name " + addressBookName);
                        }
                    }
                    else if (choice == 3)
                    {
                        Console.Write("\nEnter City Name: ");
                        Info.GetPeopleByState(Console.ReadLine());
                    }
                    else if (choice == 4)
                    {
                        Console.Write("\nEnter State Name: ");
                        Info.GetPeopleByState(Console.ReadLine());
                    }
                    else if (choice == 5)
                    {
                        FileIO.SaveToJSON(shelf, jsonPath);
                        Console.WriteLine("Data saved to JSON file");
                    }
                    else if (choice == 6)
                    {
                        FileIO.LoadFromJSON(shelf, jsonPath);
                        Console.WriteLine("Data Loaded from JSON file");
                    }
                    else if (choice == 7)
                    {
                        FileIO.SaveToText(shelf, savePath);
                        nlog.Info("Exiting Program");
                        flag = false;
                    }
                    else
                    {
                        nlog.Warn("Invalid Input");
                        Console.WriteLine("Invalid Input");
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("Invalid data entered. Error: " + e.Message + "\n" + e.StackTrace);
                }
            }
        }