//Removes an animal from the list of animals that the user wants to add private void pbxRemove_Click(object sender, EventArgs e) { txtSelectedAnimalAmount.Visible = false; pbxChange.Visible = false; pbxRemove.Visible = false; pbxAddNew.Visible = true; pnlStats.Visible = true; AnimalsSelected assd = (AnimalsSelected)lstAnimalsSelected.SelectedItem; animalsSelected.Remove(assd); //Refresh the List if (animalsSelected.Count > 0) { quickSortSelected(animalsSelected, 0, animalsSelected.Count - 1); } bs1.ResetBindings(false); //Remove option of animal already added animalSpecies.Add(assd.Animaal); //Refresh the combo box quickSortSpecies(animalSpecies, 0, animalSpecies.Count - 1); bs2.ResetBindings(false); txtAnimalAmount.Text = "0"; CagesNeeded = 0; foreach (AnimalsSelected item in animalsSelected) { double animalsDecimal = ((double)item.AnimalAmount) / 10; numcages = (int)Math.Ceiling(animalsDecimal); CagesNeeded += numcages; } txtCurrentCages.Text = CagesNeeded.ToString(); }
int splitSelected(List <AnimalsSelected> animalsSelected, int indexLow, int indexHigh) { AnimalsSelected pivot = animalsSelected[indexHigh]; // values are shifted around this value int i = (indexLow - 1); // Index of smaller element AnimalsSelected temp; for (int j = indexLow; j <= indexHigh - 1; j++) { // If current element is smaller than or equal to pivot if (animalsSelected[j].Animaal.AnimalName.CompareTo(pivot.Animaal.AnimalName) <= 0) { i++; // increment index of smaller element temp = animalsSelected[i]; animalsSelected[i] = animalsSelected[j]; animalsSelected[j] = temp; } } temp = animalsSelected[i + 1]; animalsSelected[i + 1] = animalsSelected[indexHigh]; animalsSelected[indexHigh] = temp; return(i + 1); }
//Allows the user to edit an animal already added to the list of animals to add to the farm private void listBox1_SelectedIndexChanged(object sender, EventArgs e) { try { if (!lstAnimalsSelected.SelectedItem.ToString().Equals("No animals yet")) { //Make the change part visible txtSelectedAnimalAmount.Visible = true; pbxChange.Visible = true; pbxRemove.Visible = true; pbxAddNew.Visible = false; pnlStats.Visible = false; //Change the amount of specific species txtSelectedAnimalAmount.Text = ((AnimalsSelected)lstAnimalsSelected.SelectedItem).AnimalAmount.ToString(); } } catch (NullReferenceException nulls) { AnimalsSelected selected = new AnimalsSelected(); selected.NullReferenceExpetion(nulls.Message.ToString()); } }
////Decreases the amount of animals //private void pbxPrevious_Click(object sender, EventArgs e) //{ //} //increases the amount of animals //private void pbxNext1_Click(object sender, EventArgs e) //{ //} //changes the quantity of an existing animal private void pbxChange_Click(object sender, EventArgs e) { txtSelectedAnimalAmount.Visible = false; pbxChange.Visible = false; pbxRemove.Visible = false; pbxAddNew.Visible = true; pnlStats.Visible = true; try { int newAmount = int.Parse(txtSelectedAnimalAmount.Text); //Make sure the new amount is still greater than 5 if (newAmount >= 5) { AnimalsSelected currentAnimal = ((AnimalsSelected)lstAnimalsSelected.SelectedItem); int counter = 0; foreach (AnimalsSelected item in animalsSelected) { if (item.Equals(currentAnimal)) { currentAnimal = item; currentAnimal.AnimalAmount = newAmount; animalsSelected.Add(currentAnimal); //Remove old instance of the animal animalsSelected.RemoveAt(counter); quickSortSelected(animalsSelected, 0, animalsSelected.Count - 1); //Reset List bs1.ResetBindings(false); quickSortSpecies(animalSpecies, 0, animalSpecies.Count - 1); return; } counter++; } } else { throw new AnimalCriteriaNotMeetException("At least 5 animals have to be selected"); } } catch (AnimalCriteriaNotMeetException ex) { MessageBox.Show(ex.Message); } catch (FormatException) { MessageBox.Show("Please ensure a number is entered!"); } finally { CagesNeeded = 0; foreach (AnimalsSelected item in animalsSelected) { double animalsDecimal = ((double)item.AnimalAmount) / 10; numcages = (int)Math.Ceiling(animalsDecimal); CagesNeeded += numcages; } txtCurrentCages.Text = CagesNeeded.ToString(); } }
//Adds an animal to the list of animals to add to farm private void pbxAdd_Click(object sender, EventArgs e) { //Make the change part not visible txtSelectedAnimalAmount.Visible = false; pbxChange.Visible = false; pbxRemove.Visible = false; pbxAddNew.Visible = true; pnlStats.Visible = true; try { AnimalsSelected selects = new AnimalsSelected(); //Checking if there are still animals in the combo box if (cbxAnimals.SelectedItem != null) { string animalname = cbxAnimals.SelectedItem.ToString(); int amount = int.Parse(txtAnimalAmount.Text); //Making sure there is more than 5 animals if (amount >= 5) { Species selectedSpecies = ((Species)cbxAnimals.SelectedItem); selects.Animaal = selectedSpecies; selects.AnimalAmount = amount; animalsSelected.Add(selects); //Refresh the List quickSortSelected(animalsSelected, 0, animalsSelected.Count - 1); lstAnimalsSelected.DataSource = bs1; bs1.ResetBindings(false); int currentIndex = cbxAnimals.SelectedIndex; //Remove option of animal already added animalSpecies.RemoveAt(currentIndex); //Refresh the combo box quickSortSpecies(animalSpecies, 0, animalSpecies.Count - 1); bs2.ResetBindings(false); } else { throw new AnimalCriteriaNotMeetException("At least 5 animals have to be selected"); } } } catch (AnimalCriteriaNotMeetException ex) { MessageBox.Show(ex.Message); } catch (FormatException) { MessageBox.Show("Please ensure a number is entered!"); } CagesNeeded = 0; foreach (AnimalsSelected item in animalsSelected) { double animalsDecimal = ((double)item.AnimalAmount) / 10; numcages = (int)Math.Ceiling(animalsDecimal); CagesNeeded += numcages; } txtCurrentCages.Text = CagesNeeded.ToString(); }
private void btnAddAnimalsSelected_Click(object sender, EventArgs e) { try { int amount = int.Parse(txtAnimalAmount.Text); Species specie = (Species)cbxSpecies.SelectedItem; int counter = animals.Where(ani => ani.Species.Equals(specie)).Count(); if (counter % 10 == 0) { AnimalsSelected assd = new AnimalsSelected(); assd.Animaal = specie; assd.AnimalAmount = amount; AddingAnimal adding = new AddingAnimal(new List <AnimalsSelected>() { assd }, ID); MessageObject message = new MessageObject(assd.BinarySerialization(), 4, 3, 2); } else { //Cages available var cageIds = from loc in locations where !(loc.Cage.Equals(null)) select loc; foreach (Location item in cageIds) { tempLoc.Add(item); } int counters = 0; Species[] space = new Species[tempLoc.Count]; foreach (Location item in tempLoc) { foreach (Animal item2 in animals) { if (item.ID == item2.LocationID) { space[counters] = item2.Species; } } counters++; } for (int i = 0; i < space.Length; i++) { if (space[i].Equals(specie)) { int animalAmount = 0; foreach (Animal item in animals) { if (item.LocationID == tempLoc[i].ID) { animalAmount++; } } for (int j = 0; j < amount + 1; j++) { if (animalAmount < 10) { //Insert Singular Animal Here ArrayList AnimalToAdd = new ArrayList(); AnimalToAdd.Add(specie.AnimalName); //Generate Gender Random rnd = new Random(); int genderChance = rnd.Next(0, 7); string gender = ""; if (genderChance <= 3) { gender = "Female"; AnimalToAdd.Add(gender); } else { gender = "Male"; AnimalToAdd.Add(gender); } int age = rnd.Next(0, 2556); string mate = ""; if ((gender.Equals("Male") || gender.Equals("Female")) && age > 474) { mate = "Ready"; } else { mate = "Not Ready"; } AnimalToAdd.Add(mate); AnimalToAdd.Add(age); double eatTime = 2; if (age < 365 || age > 2920) { eatTime += 1.5; } AnimalToAdd.Add(eatTime); Animal ani = new Animal(gender, mate, eatTime, specie, age, tempLoc[i].ID); animals.Add(ani); MessageObject message = new MessageObject(ani.BinarySerialization(), 6, 3, 2); co.SendData(message); animalAmount++; amount--; } } } } } } catch (Exception) { MessageBox.Show("Please enter a number for Amount!"); } }
public void ServerActions(MessageObject message, Socket client) { switch (message.FormIdentifier) { //From Loading Screen case 0: { switch (message.ObjectIdentifier) { //Farmer case 1: { switch (message.ActionIdentifier) { //Select case 1: { Farmer farmer = new Farmer(); Farmer[] AllFarmers = farmer.FarmerSelection(); message.Data = AllFarmers.BinarySerialization(); SendData(message, client); break; } default: break; } break; } default: break; } break; } //From Farmer Selection case 1: { switch (message.ObjectIdentifier) { //Farmer case 1: { switch (message.ActionIdentifier) { //Select case 1: { Farmer farmer = new Farmer(); Farmer[] AllFarmers = farmer.FarmerSelection(); message.Data = AllFarmers.BinarySerialization(); SendData(message, client); break; } case 5: { Farmer farmer = new Farmer(); Farmer[] AllFarmers = farmer.FarmerSelection(); message.Data = AllFarmers.BinarySerialization(); SendData(message, client); break; } //Delete case 3: { Farmer farmer = (Farmer)message.Data.BinaryDeserialization(); farmer.DeleteFarmer(); break; } default: break; } break; } default: break; } break; } //From Farmer Creation case 2: { switch (message.ObjectIdentifier) { //Farmer case 1: { switch (message.ActionIdentifier) { //Insert case 2: { Farmer farmer = (Farmer)message.Data.BinaryDeserialization(); farmer.InsertFarmer(); break; } default: break; } break; } default: break; } break; } //From Farm Creation case 3: { switch (message.ObjectIdentifier) { //Farm case 2: { switch (message.ActionIdentifier) { //Insert case 2: { Farm farm = (Farm)message.Data.BinaryDeserialization(); farm.insertFarm(); break; } default: break; } break; } default: break; } break; } //From Animal Selection case 4: { switch (message.ObjectIdentifier) { //Animal case 3: { switch (message.ActionIdentifier) { //Insert case 2: { lock (semePhore) { Animal animal = new Animal(); AddingAnimal animalstoadd = (AddingAnimal)message.Data.BinaryDeserialization(); // List<AnimalsSelected> animalsSelected = (List<AnimalsSelected>)message.Data.BinaryDeserialization(); animal.AddAnimal(animalstoadd.AnimalstoAdd, animalstoadd.FarmerId); } break; } default: break; } break; } //Species case 4: { switch (message.ActionIdentifier) { //Select case 1: { AnimalsSelected animalsSelected = new AnimalsSelected(); List <Species> species = animalsSelected.getAnimalName(); message.Data = species.BinarySerialization(); SendData(message, client); break; } default: break; } break; } default: break; } break; } //From Add Species case 5: { switch (message.ObjectIdentifier) { //Species case 4: { switch (message.ActionIdentifier) { //Insert case 2: { Species species = (Species)message.Data.BinaryDeserialization(); species.writeSpecies(); break; } default: break; } break; } default: break; } break; } //From Farm View case 6: { switch (message.ObjectIdentifier) { //Farmer case 1: { switch (message.ActionIdentifier) { //Update case 4: { //Update farmer details Farmer farmer = (Farmer)message.Data.BinaryDeserialization(); farmer.UpdateFarmer(); break; } default: break; } break; } //Farm, Animal and Location case 2: { switch (message.ActionIdentifier) { //Select case 1: { ArrayList farmViewData = new ArrayList(); int id = (int)message.Data.BinaryDeserialization(); Farm farm = new Farm(); List <Farm> farms = farm.selectFarm(id); farmViewData.Add(farms); Animal animal = new Animal(); List <Animal> animals = animal.selectAnimals(id); farmViewData.Add(animals); Location location = new Location(); List <Location> locations = location.selectLocation(id); farmViewData.Add(locations); message.Data = farmViewData.BinarySerialization(); SendData(message, client); break; } //Update case 4: { //Update farm details Farm farm = (Farm)message.Data.BinaryDeserialization(); farm.UpdateFarm(); break; } default: break; } break; } //Animal case 3: { switch (message.ActionIdentifier) { //Select case 1: { break; } //Insert case 2: { Animal animal = (Animal)message.Data.BinaryDeserialization(); Console.WriteLine("INSERT IS STILL COMMENTED OU!!!!!!!!!!!!"); animal.InsertAnimal(); break; } //Delete case 3: { Animal animal = (Animal)message.Data.BinaryDeserialization(); Console.WriteLine("DELETE IS STILL COMMENTED OU!!!!!!!!!!!!"); animal.DeleteAnimal(); break; } default: break; } break; } //Location case 5: { switch (message.ActionIdentifier) { //Select case 1: { break; } default: break; } break; } default: break; } break; } default: break; } }