C#查找成本,计算项目成本并包含10%的消费税

时间:2016-10-14 07:11:36

标签: c#-2.0

我正在学习C#,并被要求创建一个杂货程序。我卡住了,似乎无法在网上找到任何帮助我的东西。我创建了一个杂货商品类,其中包含名称和价格的属性。我还创建了一个purchaseItem的子类,它有一个数量的整数。我现在需要创建一个方法来查找价格,计算价格并增加10%的消费税。任何关于如何做到这一点的想法。 这是我到目前为止 - 任何帮助将不胜感激:

namespace Groceries1
{
    class Program
    {
        static void Main(string[] args)
        {
            groceryItem mygroceryItem = new groceryItem();
            mygroceryItem.play();
            purchasedItem mypurchasedItem = new purchasedItem();
        }
        class groceryItem
        {
            private string myName = 0;
            private int myPrice = 0;

            public string Name
            {
                get
                {
                     return myName;
                }
                set
                {
                    myName = value;
                }
            }
            public int Price
            {
                get
                {
                    return myPrice;
                }
                set
                {
                    myPrice = value;
                }
            }
        }
        class purchasedItem
        {
            private int myquantity = 0;

                public int quantity
                {
                get
                {
                    return myquantity;
                }
                set
                {
                    myquantity = Price*quantity*1.1;
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

你可以试试这个,

using System;
using System.Collections.Generic;

namespace Groceries
{
    public class Program
    {
        public static void Main(string[] args)
        {
            FreshGrocery freshGrocery = new FreshGrocery();
            freshGrocery.Name = "Fresh grocery";
            freshGrocery.Price = 30;
            freshGrocery.Weight = 0.5;

            Grocery grocery = new Grocery();
            grocery.Name = "Grocery";
            grocery.Price = 50;
            grocery.Quantity = 2;

            ShoppingCart cart = new ShoppingCart();
            cart.Orders = new List<GroceryItem>();
            cart.Orders.Add(freshGrocery);
            cart.Orders.Add(grocery);

            double price = cart.Calculate();
            Console.WriteLine("Price: {0}", price);
        }
   }

   abstract class GroceryItem
   {
       private string name;
       private double price = 0;

       public string Name
       {
           get
           {
               return name;
           }
           set
           {
               name = value;
           }
       }

       public double Price
       {
           get
           {
               return price;
           }
           set
           {
               price = value;
           }
       }

       public abstract double Calculate();
   }    

   class FreshGrocery: GroceryItem
   {
       private double weight = 0;

       public double Weight
       {
           get
           {
               return weight;
           }
           set
           {
               weight = value;
           }
       }

       public override double Calculate() {
          return this.Price * this.Weight;
       }
   }    

   class Grocery: GroceryItem
   {
       private int quantity = 0;
       private double gst = 10; // In Percentage

       public int Quantity
       {
           get
           {
               return quantity;
           }
           set
           {
               quantity = value;
           }
       }

       public override double Calculate() {
          double calculatedPrice = this.Price * this.Quantity;

          // VAT
           if(calculatedPrice > 0)
           {
               calculatedPrice += calculatedPrice * (gst/100);
           }

          return calculatedPrice;
       }
   }

    class ShoppingCart
    {
        private List<GroceryItem> orders;

        public List<GroceryItem> Orders
        {
            get
            {
                return orders;
            }
            set
            {
                orders = value;
            }
        }

        public double Calculate()
        {
            double price = 0;
            if(this.Orders != null)
            {

                foreach(GroceryItem order in this.Orders)
                {
                    price += order.Calculate();
                }

            }
            return price;

        }
    }
}