-2

So i need to create a class called "store" in this class i need to have a method called "start", now in said method i need to create 9 objects from different classes. I then need to call on said method in the main program and be able to refer to said created objects after having called the message.

class Store
{
    public void start()
    {
        Pizza vesuvio = new Pizza("Vesuvio", 75, "Tomato", "Cheese", "Ham");
        Pizza vegetarian = new Pizza("Vegetarian", 80, "Tomato", "cheese", "vegetables");
        Pizza contadina = new Pizza("Containda", 75, "Tomato", "cheese", "mushrooms", "olives");

        Customer victor = new Customer("Victor", "Hansen");
        Customer jacob = new Customer("Jacob", "Pedersen");
        Customer maghrete = new Customer("Maghrete", "Ingrid");

        Order victorOrdre = new Order(vesuvio.PizzaPrice, 1.25, vesuvio.PizzaPrice * 1.25);
        Order jacobOrdre = new Order(vegetarian.PizzaPrice, 1.25, vegetarian.PizzaPrice * 1.25);
        Order maghreteOrdre = new Order(contadina.PizzaPrice, 1.25, contadina.PizzaPrice * 1.25);
        return;
    }
}

This is the method i have created but i can't seem to be able to call the method in the main program no matter what i return or what i change the type to. This is what i've been instructed to do

  1. To test your application you should create a class Store with a method Start.
  2. Call the Start method from the main method in the class Program.
  3. In the Start method you should:
  4. Create 3 Pizza objects, 3 Customer objects and 3 Order objects each with a different pizza.
  5. Print out order information
  6. Using the object reference to each Order object, you should print out the pizza name, the customer name and the total price for each order.
public class Order
{
    // instance fields

    private double _tax;
    private int _priceBeforeTaxes;
    private double _totalPrice;

    //properties

    public double Tax
    {
        get { return _tax; }
        set { _tax = 0.25; }

    }

    public int PriceBeforeTaxes
    {
        get { return _priceBeforeTaxes; }
    }

    public double TotalPrice
    {
        get { return _totalPrice; }
    }

    //constructor
    public Order(int priceBeforeTax, double tax, double totalPrice)
    {
        _priceBeforeTaxes = priceBeforeTax;
        _tax = tax;
        _totalPrice = totalPrice;
    }

    //methods

    public override string ToString()
    {
        string obj = $"order total before taxes is {PriceBeforeTaxes} with taxes of 25% is equal to {TotalPrice} ";
        return obj;
    }

    public double CalculateTotalPrice(int PriceBeforeTaxes, double Tax)
    {
        double CalculatedTotalPrice = PriceBeforeTaxes * Tax;
        return CalculatedTotalPrice;

    }
}

public class Pizza
{

    private string _pizzaName;
    public int _pizzaPrice;
    private string _pizzaToppings1;
    private string _pizzaToppings2;
    private string _pizzaToppings3;
    private string _pizzaToppings4;

    //Properties
    public string PizzaName
    {
        get { return _pizzaName; }
    }

    public int PizzaPrice
    {
        get { return _pizzaPrice; }
    }

    public string PizzaToppings1
    {
        get { return _pizzaToppings1; }
    }

    public string PizzaToppings2
    {
        get { return _pizzaToppings2; }
    }
    public string PizzaToppings3
    {
        get { return _pizzaToppings3; }
    }
    public string PizzaToppings4
    {
        get { return _pizzaToppings4; }
    }

    //constructor
    public Pizza(string pizzaName, int pizzaPrice, string pizzaToppings1, string pizzaToppings2, string pizzaToppings3)
    {
        _pizzaName = pizzaName;
        _pizzaPrice = pizzaPrice;
        _pizzaToppings1 = pizzaToppings1;
        _pizzaToppings2 = pizzaToppings2;
        _pizzaToppings3 = pizzaToppings3;
    }

    public Pizza(string pizzaName, int pizzaPrice, string pizzaToppings1, string pizzaToppings2, string pizzaToppings3, string pizzaToppings4)
    {
        _pizzaName = pizzaName;
        _pizzaPrice = pizzaPrice;
        _pizzaToppings1 = pizzaToppings1;
        _pizzaToppings2 = pizzaToppings2;
        _pizzaToppings3 = pizzaToppings3;
        _pizzaToppings4 = pizzaToppings4;
    }

    //method
    public override string ToString()
    {
        string obj = $"{PizzaName} with {PizzaToppings1}, {PizzaToppings2}, {PizzaToppings3}, {PizzaToppings4} costs {PizzaPrice}kr";
        return obj;
    }


}

public class Customer
{

    private string _customerFirstName;
    private int _customerNumber;
    private string _customerAddress;
    private string _customerLastName;

    //properties
    public string CustomerFirstName
    {
        get { return _customerFirstName; }
        set { _customerFirstName = value; }
    }

    public string CustomerLastName
    {
        get { return _customerLastName; }
        set { _customerLastName = value; }
    }

    public int CustomerNumber
    {
        get { return _customerNumber; }
        set { _customerNumber = value; }
    }

    public string CustomerAddress
    {
        get { return _customerAddress; }
        set { _customerAddress = value; }
    }

    //constructor
    public Customer(string CustomerFirstName, string CustomerLastName, int CustomerNumber, string CustomerAddress)
    {
        _customerFirstName = CustomerFirstName;
        _customerNumber = CustomerNumber;
        _customerAddress = CustomerAddress;
        _customerLastName = CustomerLastName;

        if (CustomerNumber < 100000000 && CustomerNumber > 9999999)
        {
        }
        else
        {
            Console.WriteLine("this is not a valid phone number, valid phone number must be 8 digits. Please try again.");
        }
    }

    //methods
    public override string ToString()
    {
        string obj = $"{CustomerFirstName}, {CustomerLastName}, \n{CustomerNumber}, \n{CustomerAddress}";
        return obj;
    }
}

class Program
{
    static void Main(string[] args)
    {
        Store store = new Store();
        store.start();
    }
}

That is where I'm supposed to call the method and refer to the objects I created in the method.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Where is the `main` code calling the `start` method? Where are the `Pizza`, `Customer` and `Order` classes? I suggest you peruse the SO [tour] section as it shows how SO works. The [ask] section may help. In addition, you may find the SO [Asking](https://stackoverflow.com/help/asking) section useful. – JohnG Oct 31 '21 at 23:10
  • Can you post your classes pls – Serge Oct 31 '21 at 23:18
  • Where did you obtain these classes? Have you created them yourself? – Serge Oct 31 '21 at 23:35
  • Yes i have created them myself. I'm a student, it's a part of our assignment. – Nikolaja12 Oct 31 '21 at 23:37
  • Code formats as mono space if there are at least four spaces at the start of a line. This is why your namespace parts look like normal words. You can highlight code using the mouse and click the `{}` button above the text box. This will indent all by 4 spaces, enabling code blocks for the highlighted text – Caius Jard Oct 31 '21 at 23:54

2 Answers2

0

Creating a class is straight forward if you're using visual studio. This MSDN article (which seems to be exactly what you're looking for) goes more indepth on how to create and use classes. Have a look at the example section: https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/types/classes

Below I've gone more in depth on each of your requirements.


  1. To test your application you should create a class Store with a method Start.
namespace PizzaStore
{
    public class Store
    {
        public void Start()
        {
            // Create 3 Pizza objects
            // Create 3 Customer objects
            // Create 3 Order objects each with a different pizza.
        }
    }
}
  1. Call the Start method from the main method in the class Program.

Find the file called Program.cs, if you are using the new .NET 6 framework then this file will look different. Otherwise if you're using the .Net Framework 4.x then it should look like a traditional class. See this MSDN documentation for more information: https://learn.microsoft.com/en-gb/dotnet/core/tutorials/top-level-templates

To call the Start() function, you will need to create a new instance of the Store object and then call the function via referencing that instance.

Store store = new Store();
store.Start();

If you decide to use this code, you will need to make sure you include a using statement at the top of your file. You can find more information on namespaces here: https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/types/namespaces

Namespace:

using PizzaStore;

Otherwise you will have to refer to the class including the namespace.

PizzaStore.Store store = new PizzaStore.Store();
store.Start();
  1. and 4. In the Start method you should: Create 3 Pizza objects, 3 Customer objects and 3 Order objects each with a different pizza.

It seems like you've already created the objects. As far as I can see, what you've done is correct in regards to creating the objects. You can set the variables up in different ways (for example using properties: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/using-properties) but what you've done by initialising the values in the constructor is perfectly fine.

The only improvement I would suggest is to remove the return; at the end of the function. This serves no purpose and may be confusing.

  1. and 6. Print out order information: Using the object reference to each Order object, you should print out the pizza name, the customer name and the total price for each order.

I assume you are using a console project. In which case writing to the console is simple.

To write a single line you can use:

Console.WriteLine("Hello, World!");

To write something without creating a new line you can use:

Console.Write("Hello, World!");

In order to reference each object you've made, it's as simple as using the declared variable name you created for them, for example:

Console.WriteLine(vesuvio.PizzaName);

This final requirement of your task will need you to modify the code you currently have. When you're creating an order, you will need to enter data which will help identify the order.

Right now, you have the price of the pizza and what I am assuming is the tax rate and the final total price.

Unfortunately, this does not help us identify the order because we can't figure out what type of pizza was ordered and who ordered the pizza. Therefore, you will need to add these 2 key parts of the data within the order.

I recommend adding a property in the Order class for the objects you created called:

  • Pizza
  • Customer

You can do this in the same way you created the string and numbers to pass in the constructor. But make sure this is stored in the class and not just passed into the constructor.

For example your Customer class would look something like this:

public class Customer
{
    public Customer(string name, string surname)
    {
        CustomerName = name;
        CustomerSurname = surname;
    }

    public string CustomerName { get; set; }
    public string CustomerSurname { get; set; }
}

And your Order class would look something like this:

public class Order
{
    public Order(double price, Customer customer)
    {
        OrderPrice = price;
        OrderCustomer = customer;
    }

    public double OrderPrice { get; set; }
    public Customer OrderCustomer { get; set; }
}

You can reference the order customer by doing something like this:

victorOrdre.OrderCustomer.CustomerName;

It seems like you've updated your answer with your code. That's very helpful in identifying how to help you. In regards to your update, it's clear you have a good understanding of classes and properties.

For the Pizza class, I would recommend you use a list with an array of pizza ingredients instead. This way you don't need to create a new variable for each topping.

public class Pizza
{
    public Pizza(string name, double price, params string[] args)
    {
        PizzaName = name;
        PizzaPrice = price;
        PizzaIngredients = new List<string>();

        foreach (var item in args)
        {
            PizzaIngredients.Add(item);
        }
    }

    public string PizzaName { get; set; }
    public double PizzaPrice { get; set; }
    public List<string> PizzaIngredients { get; set; }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
avgcoder
  • 372
  • 1
  • 9
  • 27
  • 1
    I'd recommend dropping the first paragraph. Also, typo in `class Costumer` – Caius Jard Oct 31 '21 at 23:52
  • I thank you for your indepth answer but sadly this did not seem to fix my issue. I am still unable to refer to the objects in my program.cs. I have added the *using PizzaStore* right under the *using System* but still didn't seem to help. – Nikolaja12 Nov 01 '21 at 00:14
  • What's the error you're getting? Unfortunately, I'm unable to help much more than this if all I have to go off of is "it doesn't work" or "didn't seem to help". In most cases, it would be enough to search for the compiler error online to find many articles with solutions. – avgcoder Nov 01 '21 at 00:36
  • `vesuvio` is a method scope variable. You won't be able to refer to it as it will be disposed of immediately after you call the start function. See this article for variable scope: https://www.geeksforgeeks.org/scope-of-variables-in-c-sharp/ – avgcoder Nov 01 '21 at 00:48
  • You can add a break point to prevent the console from closing, or you can add a `Console.ReadLine()` call. See this answer for more information regarding how to prevent the console from closing: https://stackoverflow.com/a/11512831/2255454 Creating a loop won't allow you to access the method scope variables. If you want to receive input from the user, you should look into a while loop and the console readkey and readline functions. Also deleting your comments is very unhelpful for future readers and anyone wanting to help you. – avgcoder Nov 01 '21 at 01:19
  • 1
    I am very sorry for my errors, this is my first time on here. I have now figuered out how to fix my issue thanks to you and i therefor wish to thank you for your patience and i will certainly use the experience i have gained talking with you, to better my etiquette whilst on this site and for future questions and follow ups i will have. – Nikolaja12 Nov 01 '21 at 01:55
0

It sounds to me, based on the instructions that you've posted, that all you need for full-marks is this:

class Program
{
    static void Main(string[] args)
    {
        Store store = new Store();
        store.Start();
    }
}

public class Store
{
    public void Start()
    {
        Order[] orders = new []
        {
            new Order(new Customer("Victor Hansen"), new Pizza("Vesuvio", 75m)),
            new Order(new Customer("Jacob Pedersen"), new Pizza("Vegetarian", 80m)),
            new Order(new Customer("Maghrete Ingrid"), new Pizza("Containda", 75m)),
        };

        foreach (Order order in orders)
        {
            Console.WriteLine($"{order.Customer.Name} has ordered {order.Pizza.Name} costing {order.Pizza.Price}.");
        }
    }
}

public class Order
{
    public Customer Customer { get; private set; }
    public Pizza Pizza { get; private set; }

    public Order(Customer customer, Pizza pizza)
    {
        this.Customer = customer;
        this.Pizza = pizza;
    }
}

public class Pizza
{
    public string Name { get; private set; }
    public decimal Price { get; private set; }

    public Pizza(string name, decimal price)
    {
        this.Name = name;
        this.Price = price;
    }
}

public class Customer
{
    public string Name { get; private set; }

    public Customer(string name)
    {
        this.Name = name;
    }
}
Enigmativity
  • 113,464
  • 11
  • 89
  • 172