示例#1
0
 public void Remove(Sweet sweet)
 {
     if (!Sweets.Remove(sweet))
     {
         throw new ArgumentOutOfRangeException();
     }
     Weight -= sweet.Weight;
 }
示例#2
0
 public static int CompareSweetsBySugarPercentage(Sweet x, Sweet y)
 {
     if (x == null)
     {
         if (y == null)
         {
             return(0);
         }
         else
         {
             return(-1);
         }
     }
     else
     {
         if (y == null)
         {
             return(0);
         }
         return(x.SugarPercentage.CompareTo(y.SugarPercentage));
     }
 }
示例#3
0
 public static int CompareSweetsByWeight(Sweet x, Sweet y)
 {
     if (x == null)
     {
         if (y == null)
         {
             return(0);
         }
         else
         {
             return(-1);
         }
     }
     else
     {
         if (y == null)
         {
             return(0);
         }
         return(x.Weight.CompareTo(y.Weight));
     }
 }
示例#4
0
 public static int CompareSweetsByName(Sweet x, Sweet y)
 {
     if (x == null)
     {
         if (y == null)
         {
             return(0);
         }
         else
         {
             return(-1);
         }
     }
     else
     {
         if (y == null)
         {
             return(0);
         }
         return(x.Name.CompareTo(y.Name));
     }
 }
示例#5
0
文件: Program.cs 项目: mirxsp/edu
        static void Main(string[] args)
        {
            Present present = PresentParser.ParseFromFile("sweetlist.txt");
            int     n;

            while (true)
            {
                Console.Clear();
                Console.WriteLine("1.Show present containment");
                Console.WriteLine("2.Find chocolate candy");
                Console.WriteLine("3.Find sugar candy");
                Console.WriteLine("4.Find candy by sugar range");
                Console.WriteLine("5.Sort by sugar percentage");
                Console.WriteLine("6.Sort by weight");
                Console.WriteLine("7.Sort by name");

                Console.WriteLine("8.Exit");
                while (!int.TryParse(Console.ReadLine(), out n))
                {
                    Console.WriteLine("Wrong input");
                }
                if (n == 1)
                {
                    foreach (Sweet ns in present.Sweets)
                    {
                        Console.WriteLine(ns.Name);
                        Console.WriteLine(ns.Weight);
                        Console.WriteLine(ns.SugarPercentage);
                    }
                }
                if (n == 2)
                {
                    Chocolate res = present.FindFirstChocolateCandy();
                    if (res != null)
                    {
                        Console.WriteLine(res.Name);
                        Console.WriteLine(res.Type);
                        Console.WriteLine(res.Weight);
                    }
                    else
                    {
                        Console.WriteLine("There is no such candy");
                    }
                }
                if (n == 3)
                {
                    Sugar res = present.FindFirstSugarCandy();
                    if (res != null)
                    {
                        Console.WriteLine(res.Name);
                        Console.WriteLine(res.Type);
                        Console.WriteLine(res.Weight);
                    }
                    else
                    {
                        Console.WriteLine("There is no such candy");
                    }
                }
                if (n == 4)
                {
                    double x, y;
                    Console.WriteLine("Input sugar range:");
                    while (!double.TryParse(Console.ReadLine(), out x))
                    {
                        Console.WriteLine("Wrong input");
                    }
                    while (!double.TryParse(Console.ReadLine(), out y))
                    {
                        Console.WriteLine("Wrong input");
                    }
                    Sweet res = present.FindFirstBySugarPercentageRange(x, y);
                    if (res != null)
                    {
                        Console.WriteLine(res.Name);
                    }
                    else
                    {
                        Console.WriteLine("There is no such candy");
                    }
                }
                if (n == 5)
                {
                    present.Sort(Sweet.CompareSweetsBySugarPercentage);
                }
                if (n == 6)
                {
                    present.Sort(Sweet.CompareSweetsByWeight);
                }
                if (n == 7)
                {
                    present.Sort(Sweet.CompareSweetsByName);
                }
                if (n == 8)
                {
                    break;
                }
                Console.ReadKey();
            }
        }
示例#6
0
        public static Present ParseFromFile(string filename)
        {
            StreamReader reader = new StreamReader(File.OpenRead(filename));
            List <Sweet> sweets = new List <Sweet>();

            while (!reader.EndOfStream)
            {
                string name = reader.ReadLine(); //Reading sweet name

                double weight, sugarAmount;
                if (!double.TryParse(reader.ReadLine(), out weight))
                {
                    throw new FormatException();
                }
                if (!double.TryParse(reader.ReadLine(), out sugarAmount))
                {
                    throw new FormatException();                                                       //Reading sweet weight and sugarAmount,checking format
                }
                Sweet  obj;
                string type = reader.ReadLine();
                if (type == "Sweet")
                {
                    obj = new Sweet()
                    {
                        Name        = name,
                        Weight      = weight,
                        SugarAmount = sugarAmount
                    };
                }
                else if (type == "Chocolate")
                {
                    Chocolate.Types ctype;
                    if (!Chocolate.Types.TryParse(reader.ReadLine(), out ctype))
                    {
                        throw new FormatException();                                                          //Parsing chocolate type
                    }
                    double palmOilAmount;
                    if (!double.TryParse(reader.ReadLine(), out palmOilAmount))
                    {
                        throw new FormatException();                                                         //Parsing palm oil amount
                    }
                    obj = new Chocolate()
                    {
                        Name          = name,
                        Weight        = weight,
                        SugarAmount   = sugarAmount,
                        PalmOilAmount = palmOilAmount,
                        Type          = ctype
                    };
                }
                else if (type == "Sugar")
                {
                    Sugar.Types ctype;
                    if (!Sugar.Types.TryParse(reader.ReadLine(), out ctype))
                    {
                        throw new FormatException();                                                      //Sugar candy type parsing
                    }
                    double hardness;
                    if (!double.TryParse(reader.ReadLine(), out hardness))
                    {
                        throw new FormatException();                                                    //Hardness parsing
                    }
                    obj = new Sugar()
                    {
                        Name        = name,
                        Weight      = weight,
                        SugarAmount = sugarAmount,
                        Hardness    = hardness,
                        Type        = ctype
                    };
                }
                else
                {
                    throw new FormatException();
                }

                sweets.Add(obj);
            }
            reader.Close();
            return(new Present(sweets));
        }
示例#7
0
 public void Add(Sweet sweet)
 {
     Sweets.Add(sweet);
     Weight += sweet.Weight;
 }