private void checkButton_Click(object sender, EventArgs e) //get data from hash table according to ID { try //try catching errors { if (inputBox.Text == null || inputBox.TextLength == 0) //alert if input is empty { MessageBox.Show("ID cannot be empty."); } else if (int.TryParse(inputBox.Text, out int isNumeric)) //alert if input is not numeric { if (allAnimals.TryGetValue(isNumeric, out livestock)) //get data of ID from hash table { animalIDLabel.Text = livestock.getID().ToString(); //get id if (livestock.getIsJersy() == true) //display jersey or not jersey { animalTypeLabel.Text = livestock.getType() + " - Jersey"; } else { animalTypeLabel.Text = livestock.getType(); } amountOfWaterLabel.Text = livestock.getAmountOfWater().ToString(); //get amount of water dailyCostLabel.Text = livestock.getDailyCost().ToString(); //get daily cost weightLabel.Text = livestock.getWeight().ToString(); //get weight ageLabel.Text = livestock.getAge().ToString(); //get age colourLabel.Text = livestock.getColour().ToString(); //get colour if (livestock.getAmountOfMilk() == 0) //display n/a if not milk animal { amountOfMilkLabel.Text = "N/A"; } else { amountOfMilkLabel.Text = livestock.getAmountOfMilk().ToString(); } if (livestock.getAmountOfWool() == 0) //display n/a if not sheep { amountOfWoolLabel.Text = "N/A"; } else { amountOfWoolLabel.Text = livestock.getAmountOfWool().ToString(); } } else { MessageBox.Show("Animal not registered."); } //alert if ID not found } else { MessageBox.Show("Please enter ID as integer."); } } catch (Exception exception) { MessageBox.Show(exception.Message); //show error type } }
public String profitInOrder() //Retreive profit in desending order from binary tree { string str = "ID\t| Profit\r\n--------------------------\r\n"; profitInOrder(livestock); return(str); void profitInOrder(Livestock livestock) //recursive function { if (livestock == null) { return; } profitInOrder(livestock.right); //most right is the biggest number str += livestock.getID().ToString() + "\t| " + Math.Round(livestock.getProfit()).ToString() + "\r\n"; profitInOrder(livestock.left); //if right is null, get left } }