如何在另一个类中使用类属性/对象

时间:2019-02-10 05:40:55

标签: c#

在课堂上学习面向对象的编程,并获得了制作比萨追踪器的任务。我不明白如何在其他类中使用对象。

该程序获取客户订单并估计订单交付之前需要多长时间。为了估算交货时间,我的程序将需要跟踪订单和员工。订单可能包含1个或多个披萨。由于准备比萨饼需要花费时间,因此您的程序需要跟踪每个员工的待办事项清单,以准确估计比萨饼的制作时间。 讲师为我们提供了入门代码,我只需要制作课程即可。

namespace Assignment1
{
    // TODO Implement pizza tracker
    /// <summary>
    /// The main Pizza class.
    /// Contains methods for adding toppings to the pizza, the number of minutes
    /// required to prepare the pizza and a constructor that initializes the
    /// pizza's size to a given value.
    /// </summary>
    public class Pizza
    {
        Size size;
        List<string> toppings = new List<string>();
        double minutes;

        // constructor that initializes the pizza's
        // size to a given value.
        public void Pizzasize(Size psize)
        {
            size = psize;
        }

        // function that adds toppings.
        public void AddTopping(string top)
        {
            toppings.Add(top);
        }

        // function that gets the number of minutes to prepare a pizza.
        public double Numminutes()
        {
            if (size == Size.Small)
            {
                return 2 + (toppings.Count);
            }

            if (size == Size.Medium)
            {
                return 3 + (toppings.Count);
            }

            if (size == Size.Large)
            {
                return 4 + (toppings.Count);
            }

            return 0;
        }
    }

    /// <summary>
    /// The main Order class.
    /// Contains a method that adds a pizza to the order.
    /// </summary>
    public class Order
    {
        List<string> pizzas = new List<string>();
        double ready;

        // function that adds pizza's to the order.
        public void Addpizza(string pizza)
        {
            pizzas.Add(pizza);
        }
    }

    /// <summary>
    /// The main Employee class.
    /// Contains methods for adding employees to the store, and for placing orders.
    /// </summary>
    public class Employee
    {

        List<string> makepizzas = new List<string>();
        public int Id;
        public string employeeName;

        public void employee(int id, string name)
        {
            this.Id = id;
            this.employeeName = name;
        }

        public void Addtodo(string pizzatd)
        {
            makepizzas.Add(pizzatd);
        }
    }

    /// <summary>
    /// The main Store class.
    /// Contains methods for adding employees to the store, and for placing orders.
    /// </summary>
    public class Store
    {
        List<string> storeemployee = new List<string>();
        List<double> orders = new List<double>();
        string employee = Employee.employeeName;

        // Function to add an employee to the store. This function should
        // take an Employee object as a parameter.
        public void AddEmployee(Employee.employeeName)
        {
            storeemployee.Add(Employee.employeeName)
        }

        // Function to place an order. This function should take an Order object as a parameter, set
        // the order's field indicating how many minutes until the order is ready, and return the number of
        // minutes.
        public void Addorder()
        {
            storeemployee.Add()
        }
    }

    public enum Size
    {
        Small,
        Medium,
        Large
    }

    public enum Topping
    {
        Cheese,
        Pepperoni,
        Mushroom,
        GreenPepper,
        Bacon,
        Tomato,
        Ham,
        Pineapple
    }

1 个答案:

答案 0 :(得分:-1)

我不太了解您的要求,但我想我可能知道。

您似乎想要这样,但仍需要实现更多逻辑:

namespace Assignment1
{
    // TODO Implement pizza tracker
    /// <summary>
    /// The main Pizza class.
    /// Contains methods for adding toppings to the pizza, the number of minutes
    /// required to prepare the pizza and a constructor that initializes the
    /// pizza's size to a given value.
    /// </summary>
    public class Pizza
    {
        Size size;
        List<Topping> toppings = new List<Topping>();
        double minutes;

        // constructor that initializes the pizza's
        // size to a given value.
        public void Pizzasize(Size psize)
        {
            size = psize;
        }

        // function that adds toppings.
        public void AddTopping(Topping top)
        {
            toppings.Add(top);
        }

        // function that gets the number of minutes to prepare a pizza.
        public double Numminutes()
        {
            if (size == Size.Small)
            {
                return 2 + (toppings.Count);
            }

            if (size == Size.Medium)
            {
                return 3 + (toppings.Count);
            }

            if (size == Size.Large)
            {
                return 4 + (toppings.Count);
            }

            return 0;
        }
    }

    /// <summary>
    /// The main Order class.
    /// Contains a method that adds a pizza to the order.
    /// </summary>
    public class Order
    {
        List<Pizza> pizzas = new List<Pizza>();
        double ready;

        // function that adds pizza's to the order.
        public void Addpizza(Pizza pizza)
        {
            pizzas.Add(pizza);
        }
    }

    /// <summary>
    /// The main Employee class.
    /// Contains methods for adding employees to the store, and for placing orders.
    /// </summary>
    public class Employee
    {

        List<Pizza> makepizzas = new List<Pizza>();
        public int Id;
        public string employeeName;

        public void employee(int id, string name)
        {
            this.Id = id;
            this.employeeName = name;
        }

        public void Addtodo(Pizza pizzatd)
        {
            makepizzas.Add(pizzatd);
        }
    }

    /// <summary>
    /// The main Store class.
    /// Contains methods for adding employees to the store, and for placing orders.
    /// </summary>
    public class Store
    {
        List<Employee> storeemployee = new List<Employee>();
        List<Order> orders = new List<Order>();
        Employee employee = Employee.employeeName;

        // Function to add an employee to the store. This function should
        // take an Employee object as a parameter.
        public void AddEmployee(Employee emp)
        {
            storeemployee.Add(emp)
        }

        // Function to place an order. This function should take an Order object as a parameter, set
        // the order's field indicating how many minutes until the order is ready, and return the number of
        // minutes.
        public void Addorder(Orer order)
        {
            storeemployee.Add()
        }
    }

    public enum Size
    {
        Small,
        Medium,
        Large
    }

    public enum Topping
    {
        Cheese,
        Pepperoni,
        Mushroom,
        GreenPepper,
        Bacon,
        Tomato,
        Ham,
        Pineapple
    }