Exemplo n.º 1
0
        /// <summary>
        /// Odczytuje stan systemu z pliku o podanej ścieżce.
        /// UWAGA: Obecny stan systemu zostanie utracony na rzecz nowego stanu
        /// </summary>
        /// <param name="filePath">Ścieżka do pliku do odczytania</param>
        /// <returns>Zwraca true, jeżeli udało się poprawnie załadować stan systemu, a false, jeżeli wystąpił błąd</returns>
        internal bool LoadSystemState(string filePath)
        {
            Stream fileStream = FileHandling.GetReadingStream(filePath);

            if (fileStream == null)
            {
                throw new NoSuchFileException("Nie znaleziono podanego pliku do czytania", filePath);
            }

            // Czyszczenie listy banków
            bankList.Clear();

            // XMLParser (reader)
            using (XmlReader reader = XmlReader.Create(fileStream))
            {
                string currentBankName = null, currentBankId = null;
                while (reader.Read())
                {
                    if (reader.NodeType == XmlNodeType.Element && reader.Name == "bank")
                    {
                        currentBankName = reader.GetAttribute("name");
                        currentBankId   = reader.GetAttribute("id");
                        CreateBankIfNotExists(currentBankName, currentBankId);
                    }

                    if (reader.NodeType == XmlNodeType.Element && reader.Name == "card")
                    {
                        if (reader.HasAttributes)
                        {
                            string     clientName, clientNumber, cardNumber, cardLimitString;
                            decimal    cardLimit = 0, balance = 0;
                            bool       isActive = false;
                            ClientType clientType;
                            CardType   cardType;
                            //TODO: dodać wczytywanie isActive i balance

                            clientName      = reader.GetAttribute("ownerName");
                            clientNumber    = reader.GetAttribute("ownerNumber");
                            clientType      = (ClientType)int.Parse(reader.GetAttribute("ownerType"));
                            cardNumber      = reader.GetAttribute("number");
                            cardType        = (CardType)int.Parse(reader.GetAttribute("cardType"));
                            cardLimitString = reader.GetAttribute("cardLimit");
                            isActive        = bool.Parse(reader.GetAttribute("isActive"));
                            balance         = decimal.Parse(reader.GetAttribute("balance"));

                            if (cardLimitString != null)
                            {
                                cardLimit = decimal.Parse(cardLimitString);
                            }

                            Client owner;

                            if (clientType == ClientType.NaturalPerson)
                            {
                                owner = new NaturalPerson(clientName, clientNumber);
                            }
                            else if (clientType == ClientType.ServiceCompany)
                            {
                                owner = new ServiceCompany(clientName, clientNumber);
                            }
                            else if (clientType == ClientType.Shop)
                            {
                                owner = new Shop(clientName, clientNumber);
                            }
                            else if (clientType == ClientType.ServiceCompany)
                            {
                                owner = new ServiceCompany(clientName, clientNumber);
                            }
                            else
                            {
                                continue;
                            }

                            Card newCard;

                            if (cardType == CardType.CreditCard)
                            {
                                newCard = new CreditCard(cardNumber, owner, isActive, balance, cardLimit);
                            }
                            else if (cardType == CardType.DebitCard)
                            {
                                newCard = new DebitCard(cardNumber, owner, isActive, balance);
                            }
                            else if (cardType == CardType.ATMCard)
                            {
                                newCard = new ATMCard(cardNumber, owner, isActive, balance);
                            }
                            else
                            {
                                continue;
                            }

                            CreateBankIfNotExists(currentBankName, currentBankId);

                            AddNewCardRequest(newCard, int.Parse(currentBankId));
                        }
                    }

                    if (reader.NodeType == XmlNodeType.EndElement && reader.Name == "bank")
                    {
                        currentBankName = null;
                        currentBankId   = null;
                    }
                }
            }
            // ....

            return(true);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Zapisuje stan systemu w pliku w formacie XML
        /// </summary>
        /// <param name="filePath">Ścieżka do pliku do zapisania</param>
        /// <returns>Zwraca true, jeżeli udało się zapisać bez problemu, a false jeżeli sie nie udało</returns>
        internal bool SaveSystemState(string filePath)
        {
            StringBuilder xmlContent = new StringBuilder();

            XmlWriterSettings settings = new XmlWriterSettings
            {
                Indent             = true,
                Encoding           = new UTF8Encoding(false),
                OmitXmlDeclaration = true
            };

            using (XmlWriter writer = XmlWriter.Create(xmlContent, settings))
            {
                writer.WriteStartElement("system");
                //writer.WriteStartDocument();
                // zapisywanie stanu banków
                foreach (Bank bank in bankList)
                {
                    string bankName = bank.Name;
                    int    bankId   = bank.Id;
                    if (bankName == null || bankId < 0)
                    {
                        continue;
                    }
                    writer.WriteStartElement("bank");
                    writer.WriteAttributeString("name", bankName);
                    writer.WriteAttributeString("id", bankId.ToString());

                    foreach (Card card in bank.Cards)
                    {
                        string cardNumber = card.Number;
                        Client cardOwner  = card.Owner;
                        if (cardNumber == null || cardOwner == null)
                        {
                            continue;
                        }
                        CardType cardType;
                        if (card is CreditCard)
                        {
                            cardType = CardType.CreditCard;
                        }
                        else if (card is DebitCard)
                        {
                            cardType = CardType.DebitCard;
                        }
                        else if (card is ATMCard)
                        {
                            cardType = CardType.ATMCard;
                        }
                        else
                        {
                            continue;
                        }

                        writer.WriteStartElement("card");
                        writer.WriteAttributeString("number", cardNumber);
                        writer.WriteAttributeString("ownerName", cardOwner.Name);
                        writer.WriteAttributeString("ownerNumber", cardOwner.Number.ToString());
                        writer.WriteAttributeString("ownerType", cardOwner.ClientType.ToString("d"));
                        writer.WriteAttributeString("cardType", cardType.ToString("d"));
                        writer.WriteAttributeString("isActive", card.IsActive.ToString());
                        if (card is CreditCard)
                        {
                            writer.WriteAttributeString("cardLimit", ((CreditCard)card).CreditLimit.ToString());
                        }
                        writer.WriteAttributeString("balance", card.Balance.ToString());

                        writer.WriteEndElement();
                    }

                    writer.WriteEndElement();
                }
                writer.WriteEndElement();
                // zapisywanie stanu archiwum
                // .........
                // .........
            }

            return(FileHandling.WriteFile(filePath, xmlContent.ToString(), true));
        }