Created
June 28, 2019 14:06
-
-
Save celiosouza2013/de525732df9f34d30bc27704aa13f8cf to your computer and use it in GitHub Desktop.
Encapsulamento - testes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Globalization; | |
namespace Course | |
{ | |
class Produto | |
{ | |
private string _nome; | |
private double _preco; | |
private int _quantidade; | |
public Produto() | |
{ | |
} | |
public Produto(string nome, double preco, int quantidade) | |
{ | |
_nome = nome; | |
_preco = preco; | |
_quantidade = quantidade; | |
} | |
public string GetNome() | |
{ | |
return _nome; | |
} | |
public void SetNome(string nome) | |
{ | |
if (nome != null && nome.Length > 1) | |
{ | |
_nome = nome; | |
} | |
} | |
public double GetPreco() | |
{ | |
return _preco; | |
} | |
public int GetQuantidade() | |
{ | |
return _quantidade; | |
} | |
public double ValorTotalEmEstoque() | |
{ | |
return _preco * _quantidade; | |
} | |
public void AdicionarProdutos(int quantidade) | |
{ | |
_quantidade += quantidade; | |
} | |
public void RemoverProdutos(int quantidade) | |
{ | |
_quantidade -= quantidade; | |
} | |
public override string ToString() | |
{ | |
return _nome | |
+ ", $ " | |
+ _preco.ToString("F2", CultureInfo.InvariantCulture) | |
+ ", " | |
+ _quantidade | |
+ " unidades, Total: $ " | |
+ ValorTotalEmEstoque().ToString("F2", CultureInfo.InvariantCulture); | |
} | |
} | |
} | |
// testando get, set - Encapsulamento | |
using System; | |
using System.Globalization; | |
namespace Course | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Produto p = new Produto("TV", 500.00, 10); | |
p.SetNome("TV 4k"); | |
Console.WriteLine(p.GetNome()); | |
Console.WriteLine(p.GetPreco()); | |
Console.WriteLine(p.GetQuantidade()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment