Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Need help on Java program

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help on Java program

    I have an assignment for my java class. Here is the assignment followed by the code I have done so far. Any help would be greatly appreciated.

    1. Expand the class Inventory, developed in the previous part of the project, by doing the following:


    • Providing “pop-up” windows to ask for the names of the input and output files. Do this in the constructor; in other words, save the output file name for the finalizer.
    • Handle any exceptions that might be generated in the class.


    2. Create another program to make use of the class Inventory. The options – (1) modify the information for a part, (2) list the parts, (3) list the parts needing to be reordered, or (4) add a new part – should be menu driven. The “reports” (displays) should be neatly formatted. Your grade will be based on how well the program works and if it is “user friendly” – not how fancy the windows (if any) are.


    Turn in the two files – the Inventory class file and the main program file.


    Here is my code for the Inventory class file.

      import java.io.*;
       import java.util.*;
     
       class InventoryExample{
          static int SIZE = 100;	// Max. size for arrays
          int partNum[];
          int quantity[];
          double cost[];
          double wholesaleCost[];
          int reorderQuantity[];
          String description[];
          int invCnt;
          private PrintWriter op;
     
       //constructor
          public InventoryExample() throws FileNotFoundException {
             partNum = new int[SIZE];
             quantity = new int [SIZE];
             cost = new double[SIZE];
             wholesaleCost = new double[SIZE];
             reorderQuantity = new int[SIZE];
             description = new String[SIZE];
             invCnt = 0;
     
    	  // Get filename and create the file
             FileWriter writer = null;
             BufferedReader user = new BufferedReader(
                new InputStreamReader( System.in ) );
             String fileName = "";
     
             System.out.print("Enter Filename-->"); System.out.flush();
             try
             {
                fileName = user.readLine().trim();
                writer = new FileWriter( fileName );
             }
                catch ( IOException iox )
                {
                   System.out.println("Error in creating file");
                   return;
                }	
     
          }
     
       //method to add a new part, cost, and quantity
          public void addPart(int pn, int q, double c, double wc, int rq, String d){
             for(int i=0; i<invCnt; i++)
                if(partNum[i] == pn) 
                   return; // part already exits
          // otherwise, add part to data base	
             partNum[invCnt]=pn;
             quantity[invCnt]=q;
             cost[invCnt]=c;
             wholesaleCost[invCnt] = wc;
             reorderQuantity[invCnt] = rq;
             description[invCnt] = d;
             invCnt++;
          }
     
       //method to return the quantity of part 'pn' in stock
          public int getQuantity(int pn){
             for(int i=0;i<invCnt;i++){
                if(partNum[i]==pn) 
                   return quantity[i];
             }
             return -1;	// failure to find part
          }
     
       //method to return the cost of part 'pn'
          public double getCost(int pn){
             for(int i=0;i<invCnt;i++){
                if(partNum[i]==pn) 
                   return cost[i];
             }
             return -1;	// failure to find part
          }
     
       //method to return the wholesale cost of part 'pn'
          public double getWholesale(int pn){
             for(int i=0;i<invCnt;i++){
                if(partNum[i]==pn) 
                   return wholesaleCost[i];
             }
             return -1;	// failure to find part
          }
     
       //method to return the reorder point of part 'pn'
          public int getReorder(int pn){
             for(int i=0;i<invCnt;i++){
                if(partNum[i]==pn) 
                   return reorderQuantity[i];
             }
             return -1;	// failure to find part
          }
     
       //method to return the description of part 'pn'
          public String getDescription(int pn){
             for(int i=0;i<invCnt;i++){
                if(partNum[i]==pn) 
                   return description[i];
             }
             return "Not Found";	// failure to find part
          }
     
       //Accessors
       //method to set the quantity of part 'pn' in stock
          public void setQuantity(int pn, int quant){
             for(int i=0;i<invCnt;i++){
                if(partNum[i]==pn)  quantity[i] = quant;
             }
          	// failure to find part, nothing happens
          }
     
       //method to set the cost of part 'pn' in stock
          public void setCost(int pn, double amnt){
             for(int i=0;i<invCnt;i++){
                if(partNum[i]==pn)  cost[i] = amnt;
             }
          	// failure to find part, nothing happens
          }
     
       //method to set the wholesale cost of part 'pn' in stock
          public void setWholesale(int pn, double amnt){
             for(int i=0;i<invCnt;i++){
                if(partNum[i]==pn)  wholesaleCost[i] = amnt;
             }
          	// failure to find part, nothing happens
          }
     
       //method to set the reorder point of part 'pn' in stock
          public void setReorder(int pn, int num){
             for(int i=0;i<invCnt;i++){
                if(partNum[i]==pn)  reorderQuantity[i] = num;
             }
          	// failure to find part, nothing happens
          }
     
       //method to change the description of part 'pn' in stock
          public void setDescription(int pn, String str){
             for(int i=0;i<invCnt;i++){
                if(partNum[i]==pn)  description[i] = str;
             }
          	// failure to find part, nothing happens
          }
     
     
       //finalizer, 
          public void finalize(){
             for (int i=0; i<invCnt; i++) {
                op.print(partNum[i]+" ");
                op.print(quantity[i]+" ");
                op.print(cost[i]+" ");
                op.print(wholesaleCost[i]+" ");
                op.print(reorderQuantity[i]+" ");
                op.println(description[i]);
             }
             op.println("-1 0 0 0 0 Last Line");
             op.close();
          }
       }

    I believe that I have the input file part correct and the output part correct too. The inventory class does compile without any errors but I haven't been able to test it yet because I cannot get a test program to compile and work. This is where I start having a lot of the trouble. I will post what I have so far and the errors that I get when I compile. Please keep the laughter to a minimum as I am very new to this.

     import java.text.NumberFormat;
     
       class InventoryTest {
          public static void main(String args[])
          {
     
          // creates new inventory
             InventoryExample = new InventoryExample( );
     
          // add the software to the inventory
     
             getDescription = new InventoryExample("Part");
             InventoryExample.addpart(description);
     
             getWholesale = new InventoryExample("10");
             InventoryExample.addpart(description);
     
             getCost = new InventoryExample("10");
             InventoryExample.addpart(description);
     
             getQuantity = new InventoryExample("10");
             InventoryExample.addpart(description);
     
             getReorder = new InventoryExample("10");
             InventoryExample.addpart(description);
     
          // sort by sftitle
             InventoryExample.sortproduct( );
     
          // prints inventory
             for (int i = 0; i < InventoryExample.getproductCount(); i++) {
                System.out.println(InventoryExample.getproduct(i));
                System.out.println();
             }
          }
       }

    Here are the compile errors that I am getting.


    ----jGRASP exec: javac -g InventoryTest.java
    InventoryTest.java:12: cannot find symbol
    symbol : variable InventoryExample
    location: class InventoryTest
    InventoryExample = new InventoryExample( );
    ^
    InventoryTest.java:16: cannot find symbol
    symbol : variable getDescription
    location: class InventoryTest
    getDescription = new InventoryExample("Part");
    ^
    InventoryTest.java:16: cannot find symbol
    symbol : constructor InventoryExample(java.lang.String)
    location: class InventoryExample
    getDescription = new InventoryExample("Part");
    ^
    InventoryTest.java:17: cannot find symbol
    symbol : variable description
    location: class InventoryTest
    InventoryExample.addpart(description);
    ^
    InventoryTest.java:19: cannot find symbol
    symbol : variable getWholesale
    location: class InventoryTest
    getWholesale = new InventoryExample("10");
    ^
    InventoryTest.java:19: cannot find symbol
    symbol : constructor InventoryExample(java.lang.String)
    location: class InventoryExample
    getWholesale = new InventoryExample("10");
    ^
    InventoryTest.java:20: cannot find symbol
    symbol : variable description
    location: class InventoryTest
    InventoryExample.addpart(description);
    ^
    InventoryTest.java:22: cannot find symbol
    symbol : variable getCost
    location: class InventoryTest
    getCost = new InventoryExample("10");
    ^
    InventoryTest.java:22: cannot find symbol
    symbol : constructor InventoryExample(java.lang.String)
    location: class InventoryExample
    getCost = new InventoryExample("10");
    ^
    InventoryTest.java:23: cannot find symbol
    symbol : variable description
    location: class InventoryTest
    InventoryExample.addpart(description);
    ^
    InventoryTest.java:25: cannot find symbol
    symbol : variable getQuantity
    location: class InventoryTest
    getQuantity = new InventoryExample("10");
    ^
    InventoryTest.java:25: cannot find symbol
    symbol : constructor InventoryExample(java.lang.String)
    location: class InventoryExample
    getQuantity = new InventoryExample("10");
    ^
    InventoryTest.java:26: cannot find symbol
    symbol : variable description
    location: class InventoryTest
    InventoryExample.addpart(description);
    ^
    InventoryTest.java:28: cannot find symbol
    symbol : variable getReorder
    location: class InventoryTest
    getReorder = new InventoryExample("10");
    ^
    InventoryTest.java:28: cannot find symbol
    symbol : constructor InventoryExample(java.lang.String)
    location: class InventoryExample
    getReorder = new InventoryExample("10");
    ^
    InventoryTest.java:29: cannot find symbol
    symbol : variable description
    location: class InventoryTest
    InventoryExample.addpart(description);
    ^
    InventoryTest.java:32: cannot find symbol
    symbol : method sortproduct()
    location: class InventoryExample
    InventoryExample.sortproduct( );
    ^
    InventoryTest.java:35: cannot find symbol
    symbol : method getproductCount()
    location: class InventoryExample
    for (int i = 0; i < InventoryExample.getproductCount(); i++) {
    ^
    InventoryTest.java:36: cannot find symbol
    symbol : method getproduct(int)
    location: class InventoryExample
    System.out.println(InventoryExample.getproduct(i)) ;
    ^
    19 errors



    Basically I think it would be easier to scrap the test file and start new. Thanks for any help in advance guys!!!


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Need help on Java program

    Is the compiled InventoryExample.class file in the same directory as the InventoryTest?

  3. #3
    Junior Member
    Join Date
    May 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help on Java program

    Yes the InventoryExample.java file is in the same directory

Similar Threads

  1. My first Java Program
    By Creeeds in forum Java Theory & Questions
    Replies: 1
    Last Post: March 15th, 2011, 08:36 AM
  2. Help with this java program
    By Nemphiz in forum Object Oriented Programming
    Replies: 3
    Last Post: April 13th, 2010, 03:09 AM
  3. How do you add GUI to this java program?
    By leyla in forum AWT / Java Swing
    Replies: 1
    Last Post: October 18th, 2009, 01:32 PM
  4. Convert Java Program to Java ME code
    By rinchan11 in forum Java ME (Mobile Edition)
    Replies: 1
    Last Post: October 5th, 2009, 10:18 PM
  5. Java Program Help
    By javakid93 in forum Java Theory & Questions
    Replies: 6
    Last Post: July 27th, 2009, 11:03 AM