Java Inventory Program problems

I have 2 error messages that I am having trouble figuring out what it is asking me for. The errors are:

DesktopInventory ProgramInventoryPart1.java:93: cannot find symbol
symbol  : variable printer
location: class InventoryPart1
        System.out.println("Item Number: " + printer.getItemNumber());

DesktopInventory ProgramInventoryPart1.java:95: cannot find symbol
symbol  : variable printer`enter code here`
location: class InventoryPart1
        System.out.println("Product Name: " + printer.getProductName());

The code so far is

import java.text.NumberFormat;
import java.util.locale;
import java.util.Scanner;

class Printer {

    private String itemNumber;
    private String productName;
    private double units;
    private double unitPrice;
    private double unitsTotal;
    //constructor
    public Printer (String itemNumber, String productName, double units, double unitsTotal) {
        setItemNumber(itemNumber);
        setProductName(productName);
        setUnits(units);
        setUnitPrice(unitPrice);
        unitsTotal = units ++;
    }

    //accessor methods for class variables
    public String getItemNumber () {
        return itemNumber;
    }

    public void setItemNumber (String itemNumber) {
        this.itemNumber = itemNumber;
    }

    public String getProductName () {
        return productName;
    }

    public void setProductName (String productName) {
        this.productName = productName;
    }

    public double getUnits () {
        return units;
    }

    public void setUnits (double units) {
        this.units = units;
    }

    public double getUnitPrice () {
        return unitPrice;
    }

    public void setUnitPrice (double unitPrice) {
        this.unitPrice = units * unitPrice;
    }

    public double getUnitsTotal () {
        return unitsTotal;
    }

    public void setUnitsTotal (double unitsTotal) {
        this.unitsTotal = units ++;
    }


}

public class InventoryPart1 {

    public static void main (String args[]) {


        int units;

        double unitPrice;

        double unitsTotal;
        unitsTotal = units ++;

        double unitsPrice;
        unitsPrice = units * unitPrice;

        double unitsTotalPrice;
        unitsTotalPrice = unitsTotal * unitPrice;

        double totalInventory;
        totalInventory = unitsTotal * unitsTotalPrice;


        NumberFormat nf = NumberFormat. getCurrencyInstance(Locale.US);

        //create an instance of the Printer class
        Printer epson = new Printer ("Epson579", "All In One", 2, 50.99);

        //use the methods from class Printer to output the inventory details.
        System.out.println("Item Number: " + printer.getItemNumber());

        System.out.println("Product Name: " + printer.getProductName());

        System.out.print("Number of Units: ");
        System.out.println(nf.format(units));

        System.out.print("Unit Price: ");
        System.out.println(nf.format(unitPrice));

        System.out.print("Units Total: ");
        System.out.println(nf.format(unitsTotal));

        System.out.print("Units Total Price: ");
        System.out.println(nf.format(unitsTotalPrice));

        System.out.print("Total Inventory: ");
        System.out.println(nf.format(totalInventory));
    }

}

Sorry new to this site and still trying to figure things out with the whole code entering,


Ah, you did not declare a variable called printer . You called it epson .


There were a couple of issues with your InventoryPart1 class, it wouldn't compile for me until I initialized the variables at the start of main. It's a good habit to get into, also you needed to use 'epson' and not 'printer':

public class InventoryPart1 {

    public static void main (String args[]) {


        int units = 0;

        double unitPrice = 0;

        double unitsTotal = units++;

        double unitsPrice = 0;
        unitsPrice = units * unitPrice;

        double unitsTotalPrice;
        unitsTotalPrice = unitsTotal * unitPrice;

        double totalInventory;
        totalInventory = unitsTotal * unitsTotalPrice;


        NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US);

        //create an instance of the Printer class
        Printer epson = new Printer ("Epson579", "All In One", 2, 50.99);

        //use the methods from class Printer to output the inventory details.
        System.out.println("Item Number: " + epson.getItemNumber());

        System.out.println("Product Name: " + epson.getProductName());

        System.out.print("Number of Units: ");
        System.out.println(nf.format(units));

        System.out.print("Unit Price: ");
        System.out.println(nf.format(unitPrice));

        System.out.print("Units Total: ");
        System.out.println(nf.format(unitsTotal));

        System.out.print("Units Total Price: ");
        System.out.println(nf.format(unitsTotalPrice));

        System.out.print("Total Inventory: ");
        System.out.println(nf.format(totalInventory));
    }
}

Couple of issues ...

  • instance name should be printer and not epson
  • total no of units should be a static field instead of instance variable
  • either initialize all the local variables before using them or delete them as you can calculate statistics using member methods from Printer class
  • When displaying use all the member methods from Printer class instead of local variables
  • Use NumberFormat.getIntegerInstance for formatting integers as NumberFormat.getCurrencyInstance is for formatting currency fields.
  • You would need to rethink on the units, unitsTotal, units price and inventory fields.

    import java.text.NumberFormat;
    import java.util.Locale;
    
    class Printer {
    
        private String itemNumber;
        private String productName;
        private double units;
        private double unitPrice;
        private static double unitsTotal;
    
        // constructor
        public Printer(String itemNumber, String productName, double units, double unitPrice) {
            setItemNumber(itemNumber);
            setProductName(productName);
            setUnits(units);
            setUnitPrice(unitPrice);
            unitsTotal += units;
        }
    
        // accessor methods for class variables
        public String getItemNumber() {
            return itemNumber;
        }
    
        public void setItemNumber(String itemNumber) {
            this.itemNumber = itemNumber;
        }
    
        public String getProductName() {
            return productName;
        }
    
        public void setProductName(String productName) {
            this.productName = productName;
        }
    
        public double getUnits() {
            return units;
        }
    
        public void setUnits(double units) {
            this.units = units;
        }
    
        public double getUnitPrice() {
            return unitPrice;
        }
    
        public void setUnitPrice(double unitPrice) {
            this.unitPrice = unitPrice;
        }
    
        public static double getUnitsTotal() {
            return unitsTotal;
        }
    }
    
    public class InventoryPart1 {
    
        public static void main(String args[]) {
            NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US);
            NumberFormat inf = NumberFormat.getIntegerInstance(Locale.US);
    
            // create an instance of the Printer class
            Printer printer = new Printer("Epson579", "All In One", 2, 50.99);
    
            // use the methods from class Printer to output the inventory details.
            System.out.println("Item Number      : " + printer.getItemNumber());
            System.out.println("Product Name     : " + printer.getProductName());
            System.out.println("Number of Units  : " + inf.format(printer.getUnits()));
            System.out.println("Unit Price       : " + nf.format(printer.getUnitPrice()));
            System.out.println("Units Total      : " + inf.format(printer.getUnitsTotal()));
            System.out.println("Units Total Price: " + nf.format(printer.getUnitPrice() * Printer.getUnitsTotal()));
            System.out.println("Total Inventory  : " + inf.format(Printer.getUnitsTotal()));
        }
    }
    

    Sample Run:

    Item Number      : Epson579
    Product Name     : All In One
    Number of Units  : 2
    Unit Price       : $50.99
    Units Total      : 2
    Units Total Price: $101.98
    Total Inventory  : 2
    
    链接地址: http://www.djcxy.com/p/68220.html

    上一篇: Java仅验证1和3之间的值

    下一篇: Java库存程序问题