Пример #1
0
        // Method definition for claclulating and displaying the averge weight of the pets by type
        public static void AvgWeightByType(Pet[] myPets)
        {
            string currentType  = myPets[0].GetType();
            int    currentCount = 1;
            double totalWeight  = myPets[0].GetWeight();

            StreamWriter outFile = new StreamWriter("avg_weight_by_type.txt");

            for (int i = 1; i < Pet.GetCount(); i++)
            {
                if (currentType == myPets[i].GetType())
                {
                    currentCount++;
                    totalWeight += myPets[i].GetWeight();
                }
                else
                {
                    Console.WriteLine("The average weight of "
                                      + currentType + " is: " + (totalWeight / currentCount));
                    outFile.WriteLine("The average weight of "
                                      + currentType + " is: " + (totalWeight / currentCount));

                    currentType  = myPets[i].GetType();
                    currentCount = 1;
                    totalWeight  = myPets[i].GetWeight();
                }
            }
            Console.WriteLine("The average weight of "
                              + currentType + " is: " + (totalWeight / currentCount));
            outFile.WriteLine("The average weight of "
                              + currentType + " is: " + (totalWeight / currentCount));
            outFile.Close();
        }
        public static Pet[] ReadPetFile()
        {
            Pet[] myPets = new Pet[100];

            if (File.Exists("input.txt"))
            {
                StreamReader inFile = new StreamReader("input.txt");
                string       line   = inFile.ReadLine();
                while (line != null)
                {
                    string[] tempArray = line.Split('#');
                    myPets[Pet.GetCount()] = new Pet(int.Parse(tempArray[0]), tempArray[1], tempArray[2], double.Parse(tempArray[3]));

                    Pet.SetCount(Pet.GetCount() + 1);
                    line = inFile.ReadLine();
                }

                inFile.Close();
            }
            else
            {
                Console.WriteLine("File not found.");
            }

            return(myPets);
        }
        public static void WritepetFile(Pet[] myPets)
        {
            StreamWriter outFile = new StreamWriter("output.txt");

            for (int i = 0; i < Pet.GetCount(); i++)
            {
                outFile.WriteLine(myPets[i].ToFile());
            }

            outFile.Close();
        }
Пример #4
0
        public static void CombinedWeightOver50(Pet[] myPets)
        {
            Console.WriteLine();

            for (int i = 0; i < Pet.GetCount() - 1; i++)
            {
                for (int j = i + 1; j < Pet.GetCount(); j++)
                {
                    if (myPets[i].GetWeight() + myPets[j].GetWeight() > 50)
                    {
                        Console.WriteLine(myPets[i].GetId() + " " + myPets[i].GetName() + ":" + myPets[i].GetWeight() + "lbs"
                                          + "\t" + myPets[j].GetId() + " " + myPets[i].GetName() + ": " + myPets[j].GetWeight() + "lbs");
                    }
                }
            }
        }
Пример #5
0
        public static void AverageWeight(Pet[] myPets)
        {
            Console.WriteLine();

            StreamWriter outFile = new StreamWriter("average_weight.txt");

            double total = 0;

            for (int i = 0; i < Pet.GetCount(); i++)
            {
                total += myPets[i].GetWeight();
            }

            Console.WriteLine("The average weight of the pets is: " + Math.Round(total / Pet.GetCount(), 2) + " lbs");
            outFile.WriteLine("The average weight of the pets is: " + Math.Round(total / Pet.GetCount(), 2) + " lbs");

            outFile.Close();
        }
Пример #6
0
        public static void SortByType(Pet[] myPets)
        {
            int minIndex;

            for (int i = 0; i < Pet.GetCount() - 1; i++)
            {
                minIndex = i;

                for (int j = i + 1; j < GetCount(); j++)
                {
                    if (myPets[j].GetType().CompareTo(myPets[minIndex].GetType()) < 0)
                    {
                        minIndex = j;
                    }
                }
                if (minIndex != i)
                {
                    Swap(myPets, i, minIndex);
                }
            }
        }