I'm confused the instructions say create a class called pizza with two fields PizzaType and Pieces. The Pizza constructor should call two data-entry methods GetType() and GetPieces() Is this the proper code for the constructor:
public pizza (string Types, int Pieces)

Dani AI

Generated

Short clarification and practical guidance relating to the posts by , and .

The constructor name must match the class name (use Pizza, not pizza) and constructors are intended to bring an object into a valid state. Calling user-interactive code inside a constructor (the approach shown by ) is common in beginner exercises but is poor practice in real code because it mixes I/O with object creation and makes testing and reuse harder (see Constructors (C# Programming Guide)).

A recommended pattern separates validation/initialization from user interaction. Keep the constructor focused on validation and assignment:

public class Pizza
{
    public string PizzaType { get; }
    public int Pieces { get; }

    public Pizza(string pizzaType, int pieces)
    {
        if (string.IsNullOrWhiteSpace(pizzaType))
            throw new ArgumentException("Type required", nameof(pizzaType));
        if (pieces <= 0)
            throw new ArgumentOutOfRangeException(nameof(pieces), "Pieces must be > 0");

        PizzaType = pizzaType;
        Pieces = pieces;
    }
}

Gathering input can live in a separate factory/helper that handles parsing and retries, then constructs the Pizza. That keeps constructors deterministic and makes it easier for the class that “establishes several objects and handles any throws” to catch specific exceptions. Prefer int.TryParse for robust parsing, throw ArgumentException/ArgumentOutOfRangeException for invalid arguments, and create a custom exception only if the exercise explicitly requires it. For exception handling guidance see Exceptions in C#.

For coursework, following the book may require interactive constructors to exercise exception flow; for production code, separate concerns: I/O and user prompts belong outside constructors.

Recommended Answers

All 3 Replies

yes and no. that is a valid constructor but what I think your instructions want is

public pizza(){
getType();
getPieces();
}

private void GetType(){
//code to prompt user for type goes here
}


private void GetPieces(){
//code to prompt user for number of pieces
}

Hi,

I agree w/ previous post and want to say that instructions of your question are lame. I can't see a real world case where you call a user interactive method on a constructor.

Lore Soth

I'm a newbie at this programming stuff so there is alot of concepts I can't grasp right away. The instructions I supplied were a part of a project which included creating a exception class, creating a class with two methods that throw exceptions, and a class that establishes several objects and handles any throws. I going to assume that the book has me doing it this way for a reason. Perhaps because I didn't spell out the whole project, the instructions appear lame or it doesn't seem to apply to a real word case. You have to learn somehow. Thanks for the code sample that was supplied.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.