2017-12-01 3 views
-3

ちょっと私はBIT(情報技術学士)をやっていて、私は20行のテキストファイルから読みました。それは '、'によって分割されています私は何をしようとしている取得すると私はID番号で検索を行っているスイッチメニューで最も牛乳を持っている牛を見つけることを試みているが、私は頭を得ることができませんそのテキストファイルからほとんどの牛乳を生産する牛テキストファイルから読み込み、それが牛で最高のcostpermonthであるかどうかを調べて画面に出力してください。

class Program 
{ 
    static void Main(string[] args) 
    { 
     Livestock[] animals = new Livestock[20]; 
     int counter = 0; 
     string myLine; 
     string[] words; 
     TextReader tr = new StreamReader("S:/BIT694/livestock.txt"); 

     while ((myLine = tr.ReadLine()) != null) 
     { 
      words = myLine.Split(','); 
      int ID = int.Parse(words[0]); 
      string LivestockType = words[1]; 
      int YearBorn = int.Parse(words[2]); 
      double CostPerMonth = double.Parse(words[3]); 
      double CostOfVaccination = double.Parse(words[4]); 
      double AmountMilk = double.Parse(words[5]); 

      if (LivestockType == "Cow") 
      { 
       Cow newCow = new Cow(ID, "Cow", YearBorn, CostPerMonth, CostOfVaccination, AmountMilk); 
       animals[counter] = newCow; 
      } 
      else if (LivestockType == "Goat") 
      { 
       Goat newGoat = new Goat(ID, "Goat", YearBorn, CostPerMonth, CostOfVaccination, AmountMilk); 
       animals[counter] = newGoat; 
      } 
      counter++; 
     } 
     int choice; 

     for (;;) 
     { 
      do 
      { 
       Console.WriteLine("--------Menu--------"); // The menue Title 
       Console.WriteLine(); 
       Console.WriteLine("1) Display livestock information by ID"); // Display the livestock by ID number 
       Console.WriteLine("2) Display cow that produced the most milk"); // Displays the cow that porduces the most milk 
       Console.WriteLine("3) Display goat that produced the least amount of milk"); // Displays the goat that produces the least amount of milk 
       Console.WriteLine("4) Calculate farm profit"); // Calculates the farm profit 
       Console.WriteLine("5) Display unprofitable livestock"); // Displays the unprofitable livestock 
       Console.WriteLine("0) Exit"); // Exits the program 
       Console.WriteLine(); 
       Console.Write("Enter an option: "); 

       choice = int.Parse(Console.ReadLine()); 
      } while (choice < 0 || choice > 5); 

      if (choice == 0) break; 

      Console.WriteLine("\n"); 

      switch(choice) 
      { 
       case 1: 
        Console.Write("Enter livestock ID: "); 
        int input = int.Parse(Console.ReadLine()); 

        // Find animal by id 
        Livestock livestock = null; 
        for(int i = 0; i < 20; i++) 
        { 
         if(animals[i].iD == input) 
         { 
          livestock = animals[i];  // Get the animal 
         } 
        } 

        if(livestock != null) 
        { 
         livestock.displayInfo(); 
        } else 
        { 
         Console.WriteLine("ID not found"); // Invaild ID 
        } 

        break; 
       case 2: 
        Console.WriteLine("Cow that produced the most Milk:"); 

        break; 
       case 3: 
        Console.WriteLine("Goat that produced the least amount of milk:"); 
        break; 
       case 4: 
        Console.WriteLine("Calculateion of farm profit:"); 
        break; 
       case 5: 
        Console.WriteLine("Livestock that are not profitable:"); 
        break; 
       case 0: 
        Environment.Exit(0); 
        break; 
       default: 
        Console.WriteLine("The Option that you have entered is invalid please try again"); 

        break; 
      } 
      Console.ReadLine(); 
     } 
    } 
} 

public class Livestock 
{ 
    private int ID; //ID Number of livestock 
    private string LivestockType; //Value is either "Cow" or "Goat" 
    private int YearBorn; //Year of birth with format YYYY (i.e. 2014) 
    private double CostPerMonth; //The cost per month 
    private double CostOfVaccination; //Annual vaccination cost 
    private double AmountMilk; //The amount of milk produced per day in liters 

    public int iD { get { return ID; } } 
    public string livestockType { get { return LivestockType; } } 
    public double costPerMonth { get { return CostPerMonth; } } 

    public Livestock(int ID, string LivestockType,int YearBorn,double CostPerMonth,double CostOfVaccination,double AmountMilk) 
    { 
     this.ID = ID; 
     this.LivestockType = LivestockType; 
     this.YearBorn = YearBorn; 
     this.CostPerMonth = CostPerMonth; 
     this.CostOfVaccination = CostOfVaccination; 
     this.AmountMilk = AmountMilk; 

    } 

    public void displayInfo() 
    { 
     Console.WriteLine(); 
     Console.WriteLine(LivestockType); 
     Console.WriteLine("ID:\t\t\t {0}",iD); 
     Console.WriteLine("Year Born:\t\t {0}",YearBorn); 
     Console.WriteLine("Cost Per Month\t\t ${0}",CostPerMonth); 
     Console.WriteLine("Cost Of Vaccination:\t ${0}",CostOfVaccination); 
     Console.WriteLine("Milk Per Day:\t\t {0}liters",AmountMilk); 
     return; 

    } 


} 

class Cow : Livestock 
{ 
    public Cow(int ID, string LivestockType, int YearBorn, double CostPerMonth, double CostOfVaccination, double AmountMilk) : base(ID, LivestockType, YearBorn, CostPerMonth, CostOfVaccination, AmountMilk) 
    {  

    } 
} 

あなたの周りには、ケース1は、あなたが "を提供していませんでしたので、私はちょうどケース2

+2

これは、コードプロバイダのサイトではありませんか? – MethodMan

答えて

0

AmountMilkにはprivateのアクセス指定子が指定されていますが、ゲッターはありません。あなたがC#3以上を使用しているあなたは、コンパイラは専用のプロパティのgetを介してアクセスすることができるプライベート、匿名バッキングフィールドを作成し

public string SomeProperty { get; set; } 

のようにプロパティを定義することができるように、あなたは、automatic propertiesを使用することができると仮定すると、

アクセッサーはsetです。

Livestock maxCow = animals.Where(animal => animal.LivestockType == "Cow").OrderByDescending(animal => animal.AmountMilk).FirstOrDefault(); 

上記のコードは、LINQの式を使用して、

animals.Where(animal => animal.LivestockType == "Cow" 

は、我々はAmountMilk特性に基づいてソート降順とFirstOrDefaultんどのLiveStockType "Cow"のすべての動物を取得ソートの最初の要素を返しますそのような要素がない場合は、nullを返します。

フルコード:あなたは、なぜあなたは `ケース2 'コーディングすることができない' 1 'のケースをコーディングすることができました場合

 static void Main(string[] args) 
     { 
      Livestock[] animals = new Livestock[20]; 
      int counter = 0; 
      string myLine; 
      string[] words; 
      TextReader tr = new StreamReader("S:/BIT694/livestock.txt"); 

      while ((myLine = tr.ReadLine()) != null) 
      { 
       words = myLine.Split(','); 
       int ID = int.Parse(words[0]); 
       string LivestockType = words[1]; 
       int YearBorn = int.Parse(words[2]); 
       double CostPerMonth = double.Parse(words[3]); 
       double CostOfVaccination = double.Parse(words[4]); 
       double AmountMilk = double.Parse(words[5]); 

       if (LivestockType == "Cow") 
       { 
        Cow newCow = new Cow(ID, "Cow", YearBorn, CostPerMonth, CostOfVaccination, AmountMilk); 
        animals[counter] = newCow; 
       } 
       else if (LivestockType == "Goat") 
       { 
        Goat newGoat = new Goat(ID, "Goat", YearBorn, CostPerMonth, CostOfVaccination, AmountMilk); 
        animals[counter] = newGoat; 
       } 
       counter++; 
      } 
      int choice; 

      for (;;) 
      { 
       do 
       { 
        Console.WriteLine("--------Menu--------"); // The menue Title 
        Console.WriteLine(); 
        Console.WriteLine("1) Display livestock information by ID"); // Display the livestock by ID number 
        Console.WriteLine("2) Display cow that produced the most milk"); // Displays the cow that porduces the most milk 
        Console.WriteLine("3) Display goat that produced the least amount of milk"); // Displays the goat that produces the least amount of milk 
        Console.WriteLine("4) Calculate farm profit"); // Calculates the farm profit 
        Console.WriteLine("5) Display unprofitable livestock"); // Displays the unprofitable livestock 
        Console.WriteLine("0) Exit"); // Exits the program 
        Console.WriteLine(); 
        Console.Write("Enter an option: "); 

        choice = int.Parse(Console.ReadLine()); 
       } while (choice < 0 || choice > 5); 

       if (choice == 0) break; 

       Console.WriteLine("\n"); 

       switch (choice) 
       { 
        case 1: 
         Console.Write("Enter livestock ID: "); 
         int input = int.Parse(Console.ReadLine()); 
         Livestock livestock = animals.Where(animal => animal.ID == input).ToList().FirstOrDefault(); 
         if (livestock != null) 
         { 
          livestock.displayInfo(); 
         } 
         else 
         { 
          Console.WriteLine("ID not found"); // Invaild ID 
         } 

         break; 
        case 2: 
         Console.WriteLine("Cow that produced the most Milk:"); 
         Livestock maxCow = animals.Where(animal => animal.LivestockType == "Cow").OrderByDescending(animal => animal.AmountMilk).FirstOrDefault(); 
         if (maxCow != null) 
          maxCow.displayInfo(); 
         else 
          Console.WriteLine("No cow"); 
         break; 
        case 3: 
         Console.WriteLine("Goat that produced the least amount of milk:"); 
         break; 
        case 4: 
         Console.WriteLine("Calculateion of farm profit:"); 
         break; 
        case 5: 
         Console.WriteLine("Livestock that are not profitable:"); 
         break; 
        case 0: 
         Environment.Exit(0); 
         break; 
        default: 
         Console.WriteLine("The Option that you have entered is invalid please try again"); 

         break; 
       } 
       Console.ReadLine(); 
      } 
     } 
    } 

    public class Livestock 
    { 
     public int ID { get; set; } //ID Number of livestock 
     public string LivestockType {get; set;} //Value is either "Cow" or "Goat" 
     public int YearBorn { get; set; } //Year of birth with format YYYY (i.e. 2014) 
     public double CostPerMonth { get; set; }//The cost per month 
     public double CostOfVaccination { get; set; } //Annual vaccination cost 
     public double AmountMilk { get; set; } //The amount of milk produced per day in liters 

     public Livestock(int ID, string LivestockType, int YearBorn, double CostPerMonth, double CostOfVaccination, double AmountMilk) 
     { 
      this.ID = ID; 
      this.LivestockType = LivestockType; 
      this.YearBorn = YearBorn; 
      this.CostPerMonth = CostPerMonth; 
      this.CostOfVaccination = CostOfVaccination; 
      this.AmountMilk = AmountMilk; 

     } 

     public void displayInfo() 
     { 
      Console.WriteLine(); 
      Console.WriteLine(LivestockType); 
      Console.WriteLine("ID:\t\t\t {0}", iD); 
      Console.WriteLine("Year Born:\t\t {0}", YearBorn); 
      Console.WriteLine("Cost Per Month\t\t ${0}", CostPerMonth); 
      Console.WriteLine("Cost Of Vaccination:\t ${0}", CostOfVaccination); 
      Console.WriteLine("Milk Per Day:\t\t {0}liters", AmountMilk); 
      return; 
     } 
    } 

LINQ reference

+0

私はそれを試して、それは働いていませんでした私のコードを更新しました –

+0

はヤギの同じものです=> to =

+0

@ P.Hohipa上記のコードでは、 'は[ラムダ式](https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/statements-expressions-operators/lambda-expressions)を宣言するための構文の一部ですが、そうではありませんそれ以上の演算子( '> =')と混同します。 – cyberpirate92

0

のために同じことを行う必要が行われているが表示されますCow "クラス私は間にいくつかのファンタジーコードを入れなければなりません。

case 2: 
    Cow cowWithBestMilk; //Used to save the cow with the most milk production 

    ///Lets Search 
    for(int i = 0; i < animals.Count; ++i) 
    { 
     //check lifestock, but only cows 
     if(LivestockType == "Cow") 
     { 
     //Amount to check of my livestock 
     double livestockMilk = animals[i].getMilkAmount(); //<-- fantasy code, you have to acces here the AmountMilk of your cow 
     if(cowWithBestMilk != null) 
     { 
      //check if the cow from the lifestock got more milk than the other cow 
      if(livestockMilk > cowWithBestMilk.getMilkAmount()) //<-- you can use >= instead of > if you want to have the last cow with the same amount 
      { 
      cowWithBestMilk = animals[i]; 
      } 
     } 
     else 
     { 
     //there was no other cow until now 
     cowWithBestMilk = animals[i]; 
     } 

     } //if type cow 

    } //for 

    if(cowWithBestMilk != null) 
    { 
     Console.WriteLine("Cow that produced the most Milk:" + cowWithBestMilk.getHerIdAsString()); //<--fantasy code here 
    } 
    else 
    { 
     Console.WriteLine("Who let the cows out?"); 
    } 

    break; 

ここでは何をしていますか? 最初にcowWithBestMilkを宣言し、ループ内ですべての家畜を繰り返し、cowWithBestMilkを上書きします。家畜の反復で牛が見つかった場合、以前の反復からの牛(WithBestMilk)よりも多量の牛乳が生産されます。

最後に、コンソール出力時に、私たちの家畜が牛を持っていなかったケースが代替出力でカバーされています。

私はこれが少し助けてくれることを願っています。

関連する問題