Exemplo n.º 1
0
        public void TransactionAgent(Transaction transaction)
        {
            BlockOfShares sellerBlockOfShare = new BlockOfShares
            {
                ClientID = transaction.SellerID,
                ShareID  = transaction.ShareID,
                Amount   = (-1) * transaction.Amount // subtract stock from seller
            };

            BlockOfShares buyerBlockOfShare = new BlockOfShares
            {
                ClientID = transaction.BuyerID,
                ShareID  = transaction.ShareID,
                Amount   = transaction.Amount
            };

            if (!this.blockOfSharesRepository.IsClientHasShareType(transaction.BuyerID, transaction.ShareID))
            {
                this.blockOfSharesRepository.Insert(buyerBlockOfShare);
            }
            else
            {
                this.blockOfSharesRepository.ChangeShareAmountForClient(buyerBlockOfShare);
            }

            this.blockOfSharesRepository.ChangeShareAmountForClient(sellerBlockOfShare);

            decimal sharePrice = this.sharesRepository.GetSharePrice(transaction.ShareID);

            this.clientManager.ChangeBalance(transaction.BuyerID, -1 * sharePrice * transaction.Amount);
            this.clientManager.ChangeBalance(transaction.SellerID, sharePrice * transaction.Amount);
        }
Exemplo n.º 2
0
        public static void AddShares(GameEntity company, GameEntity investor, int shares)
        {
            int investorId = investor.shareholder.Id;
            var block      = new BlockOfShares
            {
                amount = shares,

                InvestorType       = investor.shareholder.InvestorType,
                shareholderLoyalty = 100,

                Investments = new List <Investment>()
            };

            var shareholders = GetShareholders(company);

            BlockOfShares b1;

            if (IsInvestsInCompany(company, investor))
            {
                // increase shares
                b1         = shareholders[investorId];
                b1.amount += block.amount;
            }
            else
            {
                // set shares
                b1 = block;
            }

            shareholders[investorId] = b1;

            AddOwning(investor, company.company.Id);
            ReplaceShareholders(company, shareholders);
        }
Exemplo n.º 3
0
        public static void DecreaseShares(GameContext gameContext, GameEntity company, GameEntity shareholder, int shares)
        {
            var shareholders = company.shareholders.Shareholders;
            int investorId   = shareholder.shareholder.Id;

            var prev = shareholders[investorId];

            if (shares >= prev.amount)
            {
                // will lose all shares
                // needs to be deleted
                RemoveShareholder(company, gameContext, shareholder);

                return;
            }

            // decrease shares otherwise
            shareholders[investorId] = new BlockOfShares
            {
                amount             = prev.amount - shares,
                InvestorType       = prev.InvestorType,
                shareholderLoyalty = prev.shareholderLoyalty
            };

            ReplaceShareholders(company, shareholders);
        }
Exemplo n.º 4
0
        public static void AddShares(GameContext gameContext, GameEntity company, int investorId, int amountOfShares)
        {
            var shareholders = company.shareholders.Shareholders;
            var shareholder  = Investments.GetInvestorById(gameContext, investorId).shareholder;

            if (IsInvestsInCompany(company, investorId))
            {
                var prev = shareholders[investorId];

                shareholders[investorId] = new BlockOfShares
                {
                    amount             = prev.amount + amountOfShares,
                    InvestorType       = prev.InvestorType,
                    shareholderLoyalty = prev.shareholderLoyalty,
                };
            }
            else
            {
                // new investor
                shareholders[investorId] = new BlockOfShares
                {
                    amount             = amountOfShares,
                    InvestorType       = shareholder.InvestorType,
                    shareholderLoyalty = 100,
                };
            }

            ReplaceShareholders(company, shareholders);
        }
Exemplo n.º 5
0
        public void ShouldDeleteBlockOfShares()
        {
            // Arrange
            var blockOfSharesRepositoryMock = Substitute.For <IBlockOfSharesRepository>();
            var clientRepositoryMock        = Substitute.For <IClientRepository>();
            var sharesRepositoryMock        = Substitute.For <ISharesRepository>();

            var sut = new BlockOfSharesManager(
                blockOfSharesRepositoryMock,
                clientRepositoryMock,
                sharesRepositoryMock);

            int clientID             = 1;
            int shareID              = 3;
            int amount               = 13;
            var blockOfShareToUpdate =
                new BlockOfShares
            {
                ClientID = clientID,
                ShareID  = shareID,
                Amount   = amount
            };

            // Act
            sut.Delete(blockOfShareToUpdate);

            // Asserts
            blockOfSharesRepositoryMock
            .Received(1)
            .Remove(blockOfShareToUpdate);
        }
Exemplo n.º 6
0
 public void Insert(BlockOfShares blockOfShares)
 {
     using (var db = new TradingContext())
     {
         db.BlockOfSharesTable.Add(blockOfShares);
         db.SaveChanges();
     }
 }
        public void Execute()
        {
            string url = "http://localhost/shares/update";

            this.outputDevice.WriteLine("Write Share ID:");
            int shareID = 0;

            while (true)
            {
                if (int.TryParse(this.inputDevice.ReadLine(), out shareID))
                {
                    break;
                }
                else
                {
                    this.outputDevice.WriteLine("Please enter valid share ID");
                }
            }


            this.outputDevice.WriteLine("Write Client ID:");
            int clientID = 0;

            while (true)
            {
                if (int.TryParse(this.inputDevice.ReadLine(), out clientID))
                {
                    break;
                }
                else
                {
                    this.outputDevice.WriteLine("Please enter valid client ID");
                }
            }

            this.outputDevice.WriteLine("Write stocks amount:");
            int amount = 0;

            while (true)
            {
                if (int.TryParse(this.inputDevice.ReadLine(), out amount))
                {
                    break;
                }
                else
                {
                    this.outputDevice.WriteLine("Please enter valid amount.");
                }
            }

            var blockOfShare = new BlockOfShares {
                ClientID = clientID, ShareID = shareID, Amount = amount
            };
            HttpContent contentString = new StringContent(JsonConvert.SerializeObject(blockOfShare), Encoding.UTF8, "application/json");
            var         result        = this.httpRequestManager.PostAsync(url, contentString);

            this.outputDevice.WriteLine(result.ToString());
        }
Exemplo n.º 8
0
    public void SetEntity(int shareholderId, BlockOfShares shares)
    {
        company = SelectedCompany;

        int totalShares = Companies.GetTotalShares(company.shareholders.Shareholders);

        shareholder = Companies.GetInvestorById(Q, shareholderId);

        Render(shareholder.shareholder.Name, shares, totalShares, shareholderId);
    }
    public void SetEntity(int shareholderId, BlockOfShares shares)
    {
        ShareholderId = shareholderId;
        Shares        = shares;

        TotalShares       = Companies.GetTotalShares(SelectedCompany.shareholders.Shareholders);
        ShareholderEntity = Companies.GetInvestorById(Q, shareholderId);

        Render();
    }
Exemplo n.º 10
0
 public void UpdateClientShares(BlockOfShares blockOfShares)
 {
     if (this.IsClientHasStockType(blockOfShares.ClientID, blockOfShares.ShareID))
     {
         this.ChangeShareAmountForClient(blockOfShares);
     }
     else
     {
         this.AddShare(blockOfShares);
     }
 }
Exemplo n.º 11
0
        public void ManualAddNewBlockOfShare()
        {
            int shareID;

            while (true)
            {
                this.outputDevice.WriteLine("Write Stock Type:");
                string stockNameInput = this.inputDevice.ReadLine();
                shareID = this.shareManager.GetShareID(stockNameInput);
                if (shareID != 0)
                {
                    break;
                }

                this.outputDevice.WriteLine("Please enter valid Stock Type.");
            }

            int clientID;

            while (true)
            {
                this.outputDevice.WriteLine("Write client name:");
                string clientNameInput = this.inputDevice.ReadLine();
                clientID = this.clientManager.GetClientID(clientNameInput);
                if (clientID != 0)
                {
                    break;
                }

                this.outputDevice.WriteLine("Please enter valid Client name.");
            }

            this.outputDevice.WriteLine("Write stocks amount:");
            int amount = 0;

            while (true)
            {
                if (int.TryParse(this.inputDevice.ReadLine(), out amount))
                {
                    break;
                }
                else
                {
                    this.outputDevice.WriteLine("Please enter valid amount.");
                }
            }

            var blockOfShare = new BlockOfShares {
                ClientID = clientID, ShareID = shareID, Amount = amount
            };

            this.blockOfSharesManager.AddShare(blockOfShare);
        }
Exemplo n.º 12
0
 public void ChangeShareAmountForClient(BlockOfShares blockOfShares)
 {
     using (var db = new TradingContext())
     {
         var entry = db.BlockOfSharesTable
                     .Where(b => (b.ClientID == blockOfShares.ClientID &&
                                  b.ShareID == blockOfShares.ShareID))
                     .FirstOrDefault();
         entry.Amount += blockOfShares.Amount;
         db.SaveChanges();
     }
 }
 public void Remove(BlockOfShares blockOfShare)
 {
     using (var db = new TradingContext())
     {
         var blockToDelete = db.BlockOfSharesTable
                             .Where(b => b.ShareID == blockOfShare.ShareID &&
                                    b.ClientID == blockOfShare.ClientID)
                             .FirstOrDefault();
         db.BlockOfSharesTable.Remove(blockToDelete);
         db.SaveChanges();
     }
 }
Exemplo n.º 14
0
        public static void AddShareholder(GameContext context, int companyId, int investorId, int shares)
        {
            var b = new BlockOfShares
            {
                amount = shares,

                InvestorType       = GetInvestorById(context, investorId).shareholder.InvestorType,
                shareholderLoyalty = 100
            };

            AddShareholder(context, companyId, investorId, b);
        }
Exemplo n.º 15
0
    void Render(string name, BlockOfShares shares, int totalShares, int investorId)
    {
        Name.text = name;
        AddLinkToInvestorIfPossible(shares.InvestorType);

        Type.text = Investments.GetFormattedInvestorType(shareholder.shareholder.InvestorType);

        Share.text = Companies.GetShareSize(Q, company.company.Id, investorId) + "%";

        //BuyShares.gameObject.SetActive(investorId != MyGroupEntity?.shareholder?.Id);

        //BuyShares.gameObject.GetComponent<LinkToBuyShares>().SetInvestorId(investorId);
    }
        public void AddShareTest()
        {
            // Arrange
            var blockOfSharesRepositoryMock = Substitute.For <IBlockOfSharesRepository>();

            var sut = new BlockOfSharesManager(blockOfSharesRepositoryMock);

            BlockOfShares blockOfShares = new BlockOfShares();

            // Act
            sut.AddShare(blockOfShares);

            // Asserts
            blockOfSharesRepositoryMock.Received(1).Insert(Arg.Is <BlockOfShares>(blockOfShares));
        }
        public void ChangeShareAmountForClientTest()
        {
            // Arrange
            var blockOfSharesRepositoryMock = Substitute.For <IBlockOfSharesRepository>();

            var sut = new BlockOfSharesManager(blockOfSharesRepositoryMock);

            BlockOfShares blockOfShares = new BlockOfShares {
                ClientID = 3, ShareID = 6, Amount = 10
            };

            // Act
            sut.ChangeShareAmountForClient(blockOfShares);

            // Asserts
            blockOfSharesRepositoryMock.Received(1).ChangeShareAmountForClient(Arg.Is <BlockOfShares>(blockOfShares));
        }
Exemplo n.º 18
0
        public void CreateRandomShare()
        {
            const int MaxAmountOfShares = 10;
            int       clientID          = this.random.Next(1, this.clientManager.GetNumberOfClients());
            int       shareID           = this.random.Next(1, this.shareManager.GetNumberOfShares());
            int       sharesAmount      = this.random.Next(1, MaxAmountOfShares);
            var       block             = new BlockOfShares
            {
                ClientID = clientID,
                ShareID  = shareID,
                Amount   = sharesAmount
            };

            if (!this.blockOfSharesManager.IsClientHasStockType(clientID, shareID))
            {
                this.blockOfSharesManager.AddShare(block);
            }

            this.blockOfSharesManager.ChangeShareAmountForClient(block);
        }
Exemplo n.º 19
0
        public static void AddShareholder(GameContext context, int companyId, int investorId, BlockOfShares block)
        {
            var c = Get(context, companyId);

            var shareholders = c.shareholders.Shareholders;

            BlockOfShares b;

            if (IsInvestsInCompany(c, investorId))
            {
                b         = shareholders[investorId];
                b.amount += block.amount;
            }
            else
            {
                b = block;
            }

            shareholders[investorId] = b;

            ReplaceShareholders(c, shareholders);
        }
Exemplo n.º 20
0
        public static void DecreaseShares(GameContext gameContext, GameEntity company, int investorId, int amountOfShares)
        {
            var shareholders = company.shareholders.Shareholders;
            var shareholder  = Investments.GetInvestorById(gameContext, investorId).shareholder;

            var prev = shareholders[investorId];

            if (amountOfShares >= prev.amount)
            {
                // needs to be deleted
                RemoveShareholder(company, investorId);
                return;
            }

            shareholders[investorId] = new BlockOfShares
            {
                amount             = prev.amount - amountOfShares,
                InvestorType       = prev.InvestorType,
                shareholderLoyalty = prev.shareholderLoyalty
            };

            ReplaceShareholders(company, shareholders);
        }
Exemplo n.º 21
0
        public void ShouldUpdateClientShares()
        {
            // Arrange
            var blockOfSharesRepositoryMock = Substitute.For <IBlockOfSharesRepository>();
            var clientRepositoryMock        = Substitute.For <IClientRepository>();
            var sharesRepositoryMock        = Substitute.For <ISharesRepository>();

            var sut = new BlockOfSharesManager(
                blockOfSharesRepositoryMock,
                clientRepositoryMock,
                sharesRepositoryMock);

            int clientID             = 1;
            int shareID              = 3;
            int amount               = 13;
            var blockOfShareToUpdate =
                new BlockOfShares
            {
                ClientID = clientID,
                ShareID  = shareID,
                Amount   = amount
            };

            blockOfSharesRepositoryMock
            .IsClientHasShareType(
                blockOfShareToUpdate.ClientID,
                blockOfShareToUpdate.ShareID)
            .Returns(true);

            // Act
            sut.UpdateClientShares(blockOfShareToUpdate);

            // Asserts
            blockOfSharesRepositoryMock
            .Received(1)
            .ChangeShareAmountForClient(blockOfShareToUpdate);
        }
Exemplo n.º 22
0
        public void ShouldCreateClientSharesInsteadOfUpdatingClientSharesThatDidntExist()
        {
            // Arrange
            var blockOfSharesRepositoryMock = Substitute.For <IBlockOfSharesRepository>();
            var clientRepositoryMock        = Substitute.For <IClientRepository>();
            var sharesRepositoryMock        = Substitute.For <ISharesRepository>();

            var sut = new BlockOfSharesManager(
                blockOfSharesRepositoryMock,
                clientRepositoryMock,
                sharesRepositoryMock);

            int clientID             = 1;
            int shareID              = 3;
            int amount               = 13;
            var blockOfShareToUpdate =
                new BlockOfShares
            {
                ClientID = clientID,
                ShareID  = shareID,
                Amount   = amount
            };

            blockOfSharesRepositoryMock
            .IsClientHasShareType(
                blockOfShareToUpdate.ClientID,
                blockOfShareToUpdate.ShareID)
            .Returns(false);

            // Act
            sut.UpdateClientShares(blockOfShareToUpdate);

            // Asserts
            blockOfSharesRepositoryMock
            .Received(1)
            .Insert(blockOfShareToUpdate);
        }
Exemplo n.º 23
0
 public void ChangeShareAmountForClient(BlockOfShares blockOfShares)
 {
     this.blockOfSharesRepository.ChangeShareAmountForClient(blockOfShares);
 }
Exemplo n.º 24
0
 public static string GetInvestorGoalDescription(BlockOfShares shares)
 {
     return(shares.InvestorType.ToString());
     //return GetFormattedInvestorGoal(shares.InvestorType);
 }
Exemplo n.º 25
0
 public ActionResult <string> DeleteClientShares([FromBody] BlockOfShares blockOfShare)
 {
     this.blockOfSharesManager.Delete(blockOfShare);
     return(Ok($"Share ID {blockOfShare.ShareID} deleted from client ID {blockOfShare.ClientID}"));
 }
Exemplo n.º 26
0
 public ActionResult <string> UpdateClientShares([FromBody] BlockOfShares blockOfShare)
 {
     blockOfSharesManager.UpdateClientShares(blockOfShare);
     return(Ok($"BlockOfShares updated"));
 }
Exemplo n.º 27
0
 public ActionResult <string> AddClientShares([FromBody] BlockOfShares blockOfShare)
 {
     blockOfSharesManager.AddShare(blockOfShare);
     return(Ok($"Share ID {blockOfShare.ShareID} added to client ID {blockOfShare.ClientID}"));
 }
Exemplo n.º 28
0
 public void Delete(BlockOfShares blockOfShare)
 {
     this.blockOfSharesRepository.Remove(blockOfShare);
 }
Exemplo n.º 29
0
 public void AddShare(BlockOfShares blockOfShares)
 {
     this.blockOfSharesRepository.Insert(blockOfShares);
 }