class Dog
{
private int amountOfDogs;
public int AmountOfDogs { get { return amountOfDogs; } set { amountOfDogs = value; } }
private bool dalmation, boxer, chihuahua ;
private int dalmationCost,boxerCost,chihuahuaCost;
private decimal totalCost;
public void DalmationChosen(bool dalmation)
{
if (dalmation)
{
dalmationCost = 50;
dalmation = true;
}
else
{
dalmationCost = 0;
dalmation = false;
}
}
public void BoxerChosen(bool boxer)
{
if (boxer)
{
boxerCost = 100;
boxer = true;
}
else
{
boxerCost = 0;
boxer = false;
}
}
public void chihuahuaChosen(bool chihuahua)
{
if (chihuahua)
{
chihuahuaCost = 200;
chihuahua = true;
}
else
{
chihuahuaCost = 0;
chihuahua = false;
}
}
public Dog( bool dalmation, bool boxer, bool chihuahua, int amountOfDogs )
{
this.amountOfDogs = amountOfDogs;
chihuahuaChosen(chihuahua);
BoxerChosen(boxer);
DalmationChosen(dalmation);
}
public decimal getTotalAmount()
{
if (boxer)
totalCost = boxerCost * amountOfDogs;
if (dalmation)
totalCost = dalmationCost * amountOfDogs;
if (chihuahua)
totalCost = chihuahuaCost * amountOfDogs;
return totalCost;
}
}
FORM CODE:
public partial class Form1 : Form
{
Dog dog;
public Form1()
{
InitializeComponent();
dog = new Dog(radioButton3.Checked, radioButton1.Checked, radioButton2.Checked, (int)numericUpDown1.Value) { };
}
private void DisplayCost()
{
textBox1.Text = dog.getTotalAmount().ToString();
}
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
dog.AmountOfDogs = (int)numericUpDown1.Value;
DisplayCost();
}
private void radioButton3_CheckedChanged(object sender, EventArgs e)
{
dog.DalmationChosen(radioButton3.Checked);
DisplayCost();
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
dog.BoxerChosen(radioButton1.Checked);
DisplayCost();
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
dog.chihuahuaChosen(radioButton2.Checked);
DisplayCost();
}
So why won't that work? It's just a random code that I made to test encapsulation. Before, instead of methods I just had
private bool dalmation, boxer, chihuahua ;
at public, and instead of putting ex.
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
dog.chihuahuaChosen(radioButton2.Checked);
DisplayCost();
}
I just had to put dog.dalmation = radiobutton2.Checked;
and at the gettotalamount method i just put like
if (dalmation)
totalCost = amountOfDogs * 200;
etc.
and it worked.
But I decided to do the method thing because I think the dog.dalmation = radiobutton2.Checked was hard coding, and it made it so the dalmation had to be public, so by putting methods you could change dalmation to true/false without it being public. So better encapsulation.
For some insight, this is the form
EDIT: FINALLY FIXED IT. I"M THE DUMBEST MOFO IN THE WORLD. FORGOT TO PUT FUCKING THIS ON THE THREE METHODS:
if (chihuahua)
{
chihuahuaCost = 200;
THIS.chihuahua = true;
}
else
{
chihuahuaCost = 0;
THIS. chihuahua = false;
}