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 } }
} //Government tax getter public void insertToTree(Livestock newLivestock) //To insert livestock into binary tree { Livestock pointer = livestock; //set livestock to pointer if (pointer == null) { livestock = newLivestock; //if tree is empty, newLivestock will become the root. return; } while (true) { if (newLivestock.getProfit() < pointer.getProfit()) //if inserted profit is < pointer { if (pointer.left == null) //insert to left if pointer.left is empty { pointer.left = newLivestock; break; } else { pointer = pointer.left; } //reset pointer.left to pointer } else //if inserted data is > pointer { if (pointer.right == null) //insert to right if pointer.right is empty { pointer.right = newLivestock; break; } else { pointer = pointer.right; } //reset pointer.right to pointer } } }
private void Form1_Load(object sender, EventArgs e) //auto load { readData(); //read data double totalProfit = 0; //Report 2: Display the total profitability/loose of the farm per day double totalTax = 0; //Report 3: Display the total tax paid to the governement per month double totalMilk = 0; //Report 4: Display the total amount of milk per day for goats and cows double totalAge = 0; //Report 5: Display the average age of all animal farms (DOG excluded) int excludeDogCount = 0; //Report 5 & 8 double totalMilkProfit = 0; //Report 6: Display the average profitability of “Goats and Cow” Vs. Sheep double totalWoolProfit = 0; //Report 6 int milkAnimalCount = 0; //Report 6 int woolAnimalCount = 0; //Report 6 double dogCost = 0; //Report 7: Display the ratio of Dogs’ cost compared to the total cost double animalCost = 0; //Report 7 double redCount = 0; //Report 9: Display the ratio of livestock with the color red double taxPaid = 0; //Report 10: Display the total tax paid for Jersey Cows double profit = 0; //Report 12: Display the total profitability of all Jersey Cows. foreach (var animal in allAnimals) { if (allAnimals.TryGetValue(animal.Key, out livestock)) { totalProfit += livestock.getProfit(); //Report 2 totalTax += livestock.getGovTax(); //Report 3 totalMilk += livestock.getAmountOfMilk(); //Report 4 switch (livestock.getType()) { case "Dogs": dogCost += livestock.getDailyCost(); //Report 7 break; case "Sheep": totalAge += livestock.getAge(); //Report 5 totalWoolProfit += livestock.getProfit(); //Report 6 animalCost += livestock.getDailyCost(); //Report 7 excludeDogCount++; //Report 5 & 8 woolAnimalCount++; //Report 6 break; default: totalAge += livestock.getAge(); //Report 5 totalMilkProfit += livestock.getProfit(); //Report 6 animalCost += livestock.getDailyCost(); //Report 7 excludeDogCount++; //Report 5 & 8 milkAnimalCount++; //Report 6 break; } switch (livestock.getColour().ToLower()) { case "red": redCount++; //Report 9 break; } if (livestock.getIsJersy() == true) { taxPaid += (livestock.getAmountOfMilk() * RatesAndPrices.jersyCowTax); //Report 10 profit += livestock.getProfit(); //Report 12 } } } farmProfitLabel.Text = totalProfit.ToString(); //Report 2 taxPaidLabel.Text = totalTax.ToString(); //Report 3 totalMilkLabel.Text = totalMilk.ToString(); //Report 4 aveAgeLabel.Text = Math.Round((totalAge / excludeDogCount), 2).ToString(); //Report 5 aveProfitRLabel.Text = Math.Round(((totalMilkProfit / milkAnimalCount) / (totalWoolProfit / woolAnimalCount)), 2).ToString(); //Report 6 dogCostRLabel.Text = Math.Round((dogCost / (animalCost + dogCost)), 2).ToString(); //Report 7 redLSRLabel.Text = Math.Round((redCount / allAnimals.Count), 2).ToString(); //Report 9 totalTaxJerLabel.Text = Math.Round((taxPaid * 30), 2).ToString(); //Report 10 totalProfitJerLabel.Text = Math.Round((profit * 30), 2).ToString(); //Report 12 /* Report 8: Generate a file that contains the ID of all animal ordered by their * profitability(You are not allowed to use built -in sorting algorithm – Your * code must do the sorting). Dogs are excluded. */ livestockTree = bst(); }