How can I interact with a class that is behind two?

Asked

Viewed 82 times

0

Good night!

I will explain the problem since the title is not suggestive of it. I have to develop an online shopping system, able to manage the purchases of each user. If you think about it, physically there is a mall, or more, and there are several shopping carts. For this last case, that of the carts I am implementing a List.

We need to bear in mind that I am programming taking into account the concept of layered programming, and the class Carrinhos, as well as Produto are in the part concerning the OR (Buisness Object)

Sell a little my class Strollers in the OR we got:

  1. A list List<Produto> listaProdutos; of Produtos;
  2. Builders and properties (mandatory)
  3. Essential methods for handling the existing list, such as

        public bool AddProduto(Produto p)
        {
             if (!listaProdutos.Contains(p))
             {
                    listaProdutos.Add(p);
                    return true;
             }
            else
                    throw new ImpossivelExistirProdutosIguaisException();
    
    
        }
    
        public bool RemoveProduto(Produto p)
        {
            if(listaProdutos.Contains(p))//Se está lá um produto
            {
                listaProdutos.Remove(p);
                return true;
            }
            else
                throw new ProdutoNaoExisteNoCarrinho();
    
        }
    

From the DL onwards, (Data Layer) i enter this class and add, or remove carts to a particular supermarket (Class: Shopping). In the Data Layer i work with a class where I implement a Dictionary of Shoppings in which the name of the mall is the key, static Dictionary<string,Shopping> sistemaDeCompras = new Dictionary<string, Shopping>();

Schematically what goes on is as follows:

inserir a descrição da imagem aqui

That is, how can I check if a particular shopping mall exists, if a particular shopping cart exists and if both exist, how can I add products?

I leave here also my DL for analysis:

  static Dictionary<string,Shopping> sistemaDeCompras = new Dictionary<string, Shopping>();


        public static bool CriarShopping(Shopping sh, string nome)
        {
            if (!sistemaDeCompras.ContainsKey(nome))
            {
                sistemaDeCompras.Add(nome, sh);
                return true;
            }
            return false;   
        }

        public static bool RemoveShopping(string nome)
        {

            if (sistemaDeCompras.ContainsKey(nome))
            {
                sistemaDeCompras.Remove(nome);
                return true;
            }

            return false;
        }
        public static bool InsereCarrinho(Carrinho c, string nomeShopping)
        {
            if (sistemaDeCompras.ContainsKey(nomeShopping)) 
            {

                return sistemaDeCompras[nomeShopping].AddCarrinho(c);
                //GravaCarrosEstacionados("Park.f");

            }
            else
                return false;
        }

Thanks in advance!

1 answer

0

I don’t know if that’s exactly what you’re looking for, because from what I understand it’s pretty easy.

// Se você quer analisar se determinado shopping e carrinho está contido num dicionário.
public static bool Analise (Carrinho c, string shopping) {
    if (sistemaDeCompras.ContainsKey(nome)){
        return (sistemaDeCompras["nome"].Contains(c)) ? true : false;
    }
    else {
        return false;
    }
}

public static void Adicionar (){
    Carrinho meuCarrinho = new Carrinho();
    if (Analise(meuCarrinho, "Algum shopping que você queira")){
        meuCarrinho.AddProduto("Algum produto que você queira");
    }
    else {
        throw new CarrinhoOuShoppingNaoExistente();
    }
}

I think that’s what you want, but if it’s not, I think you’ve given some idea.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.