Options

Java - How do I do this?

westwardwestward Member Posts: 77 ■■□□□□□□□□
Creating a script that stores and generates invoices, with an invoice number that should increment.

Each time I call the method, it just generates Invoice "1". I know I need to move the declaration "invoiceNumber = 0" to outside the class so it is only called once, but where can it go so it is only called once, and will increment from there?

Also, is there an easy way to check if a variable exists? Using methods I know from php/jscript don't work since it calls an error when the variable doesn't exist.

Thank you!

Comments

  • Options
    wastedtimewastedtime Member Posts: 586 ■■■■□□□□□□
    I don't see any source code?

    While I am a novice java guy....
    To check to see if an object exist you can see if it is a null.

    example:
    if (object == null) {
        //do something as the object doesn't exist.
    } else {
        //it does exist....now what?
    }
    
  • Options
    RobertKaucherRobertKaucher Member Posts: 4,299 ■■■■■■■■■■
    wastedtime wrote: »
    I don't see any source code?

    Always post some code! Without it your questions are more confusing than anything else. How are you storing the invoice number?

    Personally I would just wrap any code accessing this variable in a try/catch and specifically deal with the NullPointerException.
    try
    {
         myNullVariable++;
    }
    catch(NullPointerException)
    {
         //Code to deal with the situation where the variable is null.
    }
    
  • Options
    NightShade03NightShade03 Member Posts: 1,383 ■■■■■■■□□□
    westward wrote: »
    CEach time I call the method, it just generates Invoice "1". I know I need to move the declaration "invoiceNumber = 0" to outside the class so it is only called once, but where can it go so it is only called once, and will increment from there?

    If I understand this correctly you are trying to create a global variable outside of your method. The reason that your invoiceNumber is always returning 1 is because it is getting over-written inside the method call. You prob have something like:
    public void myMethod(){
    
        int invoiceNumber = 0;
    
        /* Do something here */
    
        invoiceNumber++;
    }
    

    You need to declare the variable outside the method in order for it to keep it's values. You should Google accessor/mutator methods which can manipulate global variables.

    Again this is all based on the fact that I'm interpreting your original question correctly.
  • Options
    westwardwestward Member Posts: 77 ■■□□□□□□□□
    Ok, so I appear to have solved my problem. Here is my complete code - it accomplishes the task but I'd like to know if I am doing it the "best" way possible.

    The goal of Tester class is to simply run the Invoice method with the values and print them to be sure they're processing through the getters and setters properly. Lastly, checking to be sure the invoicing is counting correctly

    CLASS:
    package task1;
    
    public class Invoice {
    
        // Initiate the number of invoices
        public static int numberOfInvoices = 0;
    
        // Initiate private variables
        private String companyName;
        private double amountDue;
        private String chargeDate;
        private int invoiceNumber = ++numberOfInvoices;
    
        // Declare a class object Invoice
        public Invoice(String companyName, double amountDue, String chargeDate){
    
            // Declare variables
            setCompanyName(companyName);
            setAmountDue(amountDue);
            setChargeDate(chargeDate);
        }
    
        public void setCompanyName(String coName)
        {
            companyName = coName;
        }
        public void setAmountDue(double Amount)
        {
            amountDue = Amount;
        }
        public void setChargeDate(String dateCharged)
        {
            chargeDate = dateCharged;
        }
    
        public String getCompanyName()
        {
            return companyName;
        }
        public double getAmountDue()
        {
            return amountDue;
        }
        public String getChargeDate()
        {
            return chargeDate;
        }
        public int getInvoiceNumber()
        {
            return invoiceNumber;
        }
        // Retuns the number of invoices generated so far
        public static int getNumberOfInvoices() {
            return numberOfInvoices;
        }
    }
    

    TESTER:
    package task1;
    
    public class InvoiceTester {
        public static void main(String[] args) {
    
            // CREATE FIRST INVOICE
            Invoice Tester01 = new Invoice("Amazing Software", 5000.00, "January 18, 2009");
    
            // Print this invoices using our methods and verify all values
            System.out.println("Return data from Tester01 Invoice:");
            System.out.println("Company Name is: " + Tester01.getCompanyName());
            System.out.println("Invoice amount is: " + Tester01.getAmountDue());
            System.out.println("Date of charge is: " + Tester01.getChargeDate());
            System.out.println("Invoice number is: " + Tester01.getInvoiceNumber());
            
            // CREATES SECOND INVOICE
            Invoice Tester02 = new Invoice("Best Programs", 4000.00, "February 18, 2009");
    
            // Print this invoices using our methods and verify all values
            System.out.println("Return data from Tester02 Invoice:");
            System.out.println("Company Name is: " + Tester02.getCompanyName());
            System.out.println("Invoice amount is: " + Tester02.getAmountDue());
            System.out.println("Date of charge is: " + Tester02.getChargeDate());
            System.out.println("Invoice number is: " + Tester02.getInvoiceNumber());
    
            // CREATES THIRD INVOICE
            Invoice Tester03 = new Invoice("Champion Code", 3000.00, "March 18, 2009");
    
            // Print this invoices using our methods and verify all values
            System.out.println("Return data from Tester03 Invoice:");
            System.out.println("Company Name is: " + Tester03.getCompanyName());
            System.out.println("Invoice amount is: " + Tester03.getAmountDue());
            System.out.println("Date of charge is: " + Tester03.getChargeDate());
            System.out.println("Invoice number is: " + Tester03.getInvoiceNumber());
    
            // Show the total number of invoices generated so far
            System.out.println("The total number of invoices is: " + Tester03.getNumberOfInvoices());
    
        }
    }
    
  • Options
    RobertKaucherRobertKaucher Member Posts: 4,299 ■■■■■■■■■■
    Personally I don't think numberOfInvoices should be an attribute of any invoice. The proper way to do that, IMO, would be to create an InvoiceManager class that is responsible for creating and managing the invoices. Each invoice would be held in some sort of collection of Invoices held by the Manager class.
Sign In or Register to comment.