none
invoice class in c# RRS feed

  • Question

  • I need to create an invoice class for the following problem any ideas, I am having trouble figuring this out

    Create an Invoice class that could be used by a bookstore. Items to include as data members are item number, description, unit price, and quantity purchased. Include appropriate constructors and properties plus an additional method that calculates the total cost using the quantity and unit price. Override the ToString ( ) method to return the item description and total cost. Create a second class to test your Invoice class.

    Thanks

    Thursday, September 19, 2013 1:49 PM

Answers

  • You could add a call to Console.ReadLine() in order to prevent the application from closing down before you press a key on the keyboard. Then you should be able to actually see the output:

        public static void Main(string[] args) {
          Invoice invoice = newInvoice("001", "My Way", 10.95m, 2);
    
          Console.WriteLine(invoice);
    
          AssertEquals("My Way $21.90", invoice.ToString());
    
          Console.ReadLine();
        }
    

    • Marked as answer by Igallegos Friday, September 20, 2013 1:02 PM
    Friday, September 20, 2013 8:21 AM

All replies

  • That sounds like a homework problem.

    David


    David http://blogs.msdn.com/b/dbrowne/

    Thursday, September 19, 2013 1:50 PM
  • You could implement the class something like below:

     public class Invoice
      {
        #region Constructors
        public Invoice() {
        }
    
        public Invoice(int itemNumber, string description, double unitPrice, int quantityPurchased) {
          this.ItemNumber = itemNumber;
          this.Description = description;
          this.UnitPrice = unitPrice;
          this.QuantityPurchased = quantityPurchased;
        }
        #endregion
    
        #region Properties
        public int ItemNumber {
          get;
          set;
        }
    
        public string Description {
          get;
          set;
        }
    
        public double UnitPrice {
          get;
          set;
        }
    
        public int QuantityPurchased {
          get;
          set;
        }
        #endregion
    
        #region Methods
        public double CalculateCost() {
          return QuantityPurchased * UnitPrice;
        }
    
        public override string ToString() {
          return string.Format("Item description: {0}{1}Total cost: {2}", this.Description, Environment.NewLine, this.CalculateCost());
        }
        #endregion
      }
    

    Note that it has two constructors and an auto-implemented property for each of the data members. In the test class you should test the ToString() method:

    Invoice inv = new Invoice(1, "desc...", 10, 10);
    //change a property...
    inv.UnitPrice += 1;
    Console.WriteLine(inv.ToString());
    
    Thursday, September 19, 2013 2:20 PM
  • it is a homework problem I was just having trouble with it this is what I have so far

           

    public

    classInvoice


        {

      

    publicdecimaltotal;  //instance variable to store invoice total


      

    //public string InitialPartNumber;


      

    //public string InitialDescription;


      

    //public int InitialQuantity;


      

    //public decimal InitialPrice;


      

    //public decimal InvoiceAmount;


      

    // auto-imlemented property for class Invoice


      

    publicstringPartNumber { get; set; }

      

    publicstringDescription { get; set; }

      

    privateintquantity; // quantity of items purchased


      

    privatedecimalprice; // price per item


      

    publicdecimalinvoiceAmount;

      

    publicInvoice(stringpartNumber, stringdescription, intquantity, decimalprice)

       {

          PartNumber = partNumber;

          Description = description;

          Quantity = quantity;

          Price = price;   

       }

    //end constructor


      

    // begin GetInvoiceAmount Method


      

    publicvoidGetInvoiceAmount()

       {

          invoiceAmount = Price * Quantity;   

       }

      

    //Begin Instance Variable Property Assignment


      

    publicintQuantity

       {

         

    get


          {

            

    returnquantity;

          }

    //end get


         

    set


          {

            

    if(value>=0 )

             quantity =

    value;

          }

    //end set


       }

    //end property Quantity


      

    publicdecimalPrice

       {

         

    get


          {

            

    returnprice;

          }

    //end get


         

    set


          {

            

    if( value>=0 )

             price =

    value;

          }


      

       }


    }

    but this is giving me 3 errors so it is not working

    Thursday, September 19, 2013 2:24 PM
  • getting an error on the last line of this is  there something that should be added?  It is coming up as a red line after the last }
    Thursday, September 19, 2013 2:28 PM
  • also an error on this line

    return

    string.Format("Item description: {0}{1}Total cost: {2}", this.Description, Environment.NewLine, this.CalculateCost());

    states the name "Environment does not exist in current context

    any ideas?

    Thursday, September 19, 2013 2:32 PM
  • No, just the closing bracket of the namespace:

    namespace YourNamespace
    {
    public class Invoide 
    {
      ..//as per the previous post
    }
    }
    

    Thursday, September 19, 2013 2:33 PM
  • closed it up now getting the following error

    Error 1 Program 'C:\Users\Ignacio\Documents\Visual Studio 2010\Projects\Gallegos,Ignacio,Chapter 4\Gallegos,Ignacio,Chapter 4\obj\x86\Debug\Gallegos,Ignacio,Chapter 4.exe' does not contain a static 'Main' method suitable for an entry point Gallegos,Ignacio,Chapter 4

    I think I got everything now just need to know what static 'Main' method to use any ideas on this?

    Thursday, September 19, 2013 2:46 PM
  • Add a using statement at the top of the file:

    using System;

    or reference the type with the namespace:

    string.Format("Item description: {0}{1}Total cost: {2}", this.Description, System.Environment.NewLine, this.CalculateCost());

    Regarding the Main method, what kind of project are you creating?


    Thursday, September 19, 2013 3:11 PM
  • Hi igallegous,

    did you have program.cs file in your project

    program.cs file contains static main method

    you need to add the below code in your place

            [STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
            }

    Form1 is the Form Name


    Happy Coding, RDRaja



    Thursday, September 19, 2013 3:28 PM
  • You need to tell us what the errors are or we cannot help you.
    Thursday, September 19, 2013 9:24 PM
  • Dharmalinga Raja

    where would I put this in the code?  I have added like this just not sure if this is correct

    using

    System;


    using

    System.Collections.Generic;


    using

    System.Linq;


    using

    System.Text;



    namespace

    invoice  

    {

       

    publicclassInvoice


        {

             [

    STAThread]

           

    staticvoidMain()

            {

                Application.EnableVisualStyles();

                Application.SetCompatibleTextRenderingDefault(

    false);

                Application.Run(

    newForm1());}

           

           


            #region

    Constructors

           

    publicInvoice()

            {

            }

     but now I am getting these errors

    Error 1 The name 'Application' does not exist in the current context C:\Users\Ignacio\Documents\Visual Studio 2010\Projects\Gallegos,Ignacio,Chapter 4\Gallegos,Ignacio,Chapter 4\program.cs 13 13 Gallegos,Ignacio,Chapter 4

    and

    Error 4 The type or namespace name 'Form1' could not be found (are you missing a using directive or an assembly reference?) C:\Users\Ignacio\Documents\Visual Studio 2010\Projects\Gallegos,Ignacio,Chapter 4\Gallegos,Ignacio,Chapter 4\program.cs 15 33 Gallegos,Ignacio,Chapter 4

    Friday, September 20, 2013 5:01 AM
  • this is what I have

    using

    System;



    namespace

    string.Format("Item description: {0}{1}Total cost: {2}", this.Description, System.Environment.NewLine, this.CalculateCost());  

    {

       

    publicclassInvoice


        {

             [STAThread]

           

    staticvoidMain()

            {

                Application.EnableVisualStyles();

                Application.SetCompatibleTextRenderingDefault(

    false);

                Application.Run(

    newForm1());}

           

           


            #region

    Constructors

           

    publicInvoice()

            {

            }

           

    publicInvoice(intitemNumber, stringdescription, doubleunitPrice, intquantityPurchased)

            {

               

    this.ItemNumber = itemNumber;

               

    this.Description = description;

               

    this.UnitPrice = unitPrice;

               

    this.QuantityPurchased = quantityPurchased;

            }


            #endregion



            #region

    Properties

           

    publicintItemNumber

            {

               

    get;

               

    set;

            }

           

    publicstringDescription

            {

               

    get;

               

    set;

            }

           

    publicdoubleUnitPrice

            {

               

    get;

               

    set;

            }

           

    publicintQuantityPurchased

            {

               

    get;

               

    set;

            }


            #endregion



            #region

    Methods

           

    publicdoubleCalculateCost()

            {

               

    returnQuantityPurchased * UnitPrice;

            }

           

    publicoverridestringToString()

            {

               

    returnstring.Format("Item description: {0}{1}Total cost: {2}", this.Description, Environment.NewLine, this.CalculateCost());

            }


            #endregion


        }

    }

    the only error with this is

    Error 1 Identifier expected; 'string' is a keyword C:\Users\Ignacio\Documents\Visual Studio 2010\Projects\Gallegos,Ignacio,Chapter 4\Gallegos,Ignacio,Chapter 4\program.cs 3 11 Gallegos,Ignacio,Chapter 4

    Friday, September 20, 2013 5:06 AM
  • Create an Invoice class that could be used by a bookstore. Items to include as data members are item number, description, unit price, and quantity purchased. Include appropriate constructors and properties plus an additional method that calculates the total cost using the quantity and unit price. Override the ToString ( ) method to return the item description and total cost. Create a second class to test your Invoice class.

    in c# application

    Friday, September 20, 2013 5:07 AM
  • which type project you are used ,windows or console application

    Happy Coding, RDRaja

    Friday, September 20, 2013 5:44 AM
  • this is what I have now and it seems to work

    using

    System;



    public

    classInvoiceTester


    {

       

    publicstaticvoidMain(string[] args)

        {

           

    Invoiceinvoice = newInvoice("001", "My Way", 10.95m, 2);

           

    Console.WriteLine(invoice);

           

    // If the ToString method in invoice does not equal expected string, throw exception...


            AssertEquals(

    "My Way $21.90", invoice.ToString());

        }

       

    publicstaticvoidAssertEquals(stringexpected, stringactual)

        {

           

    if(expected != actual)

            {

               

    thrownewException(string.Format("Expected <{0}> but <{1}>", expected, actual));

            }

        }

    }



    public

    classInvoice


    {

       

    publicstringItemNumber { get; set; }

       

    publicstringDescription { get; set; }

       

    publicdecimalUnitPrice { get; set; }

       

    publicintQuantity { get; set; }

       

    publicInvoice(stringitemNumber, stringdescription, decimalunitPrice, intquantity)

        {

            ItemNumber = itemNumber;

            Description = description;

            UnitPrice = unitPrice;

            Quantity = quantity;

        }

       

    publicdecimalTotalCost()

        {

           

    returnUnitPrice * Quantity;

        }

       

    publicoverridestringToString()

        {

           

    returnstring.Format("{0} ${1}", Description, TotalCost());

        }

    }

    it is console application, when I start debugging everything works with no errors now but how do I view it on the display screen to make sure my work is correct.

    thanks for all your help

    Friday, September 20, 2013 5:59 AM
  • @ but how do I view it on the display screen to make sure my work is correct.

    Console.WriteLine(invoice);——This will output the instance of invoice onto the Console for you to view.


    If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer

    Help by clicking:
    Click here to donate your rice to the poor
    Click to Donate
    Click to feed Dogs & Cats


    Found any spamming-senders? Please report at: Spam Report

    Friday, September 20, 2013 6:24 AM
  • You could add a call to Console.ReadLine() in order to prevent the application from closing down before you press a key on the keyboard. Then you should be able to actually see the output:

        public static void Main(string[] args) {
          Invoice invoice = newInvoice("001", "My Way", 10.95m, 2);
    
          Console.WriteLine(invoice);
    
          AssertEquals("My Way $21.90", invoice.ToString());
    
          Console.ReadLine();
        }
    

    • Marked as answer by Igallegos Friday, September 20, 2013 1:02 PM
    Friday, September 20, 2013 8:21 AM
  • Thank you everyone who gave input on this I finally got it to work
    Friday, September 20, 2013 1:02 PM