I m having problems with this program which sets out to read data thats being input and then uses the StaticProduct Class to check for invalid data for the Barcode entry, the program also requires me to create a new instance of the barcode by using p=Product(" "," ","code","price")etc can anyone help me get this to work or explain better examples of class instances to me.
/** * This is a program to Enter and check product codes and prices * and give a summary of values at the end * @author (Jeffrey Jones) * @version (version 2 5th April 2003) */ public class ProcessProduct { public static void main(String args[])
//declare variables
String manuf; String name; int sLength; String barcode; int price; int quantity; int totalPrice=0; int transactions=1; int totalQuantity=0; int totalValue=0; int averageCost=0;
//start of while loop checking for 0 to exit loop while (!manuf.equals("0")) { System.out.print("Enter Product Name : "); name = UserInput.readString();
System.out.print("Enter Bar Code : "); barcode = UserInput.readString();
String code=barcode; //check for invalid data
if (StaticProduct.isValidBarcode(code)) {p = new Product(""," ","code","price"); }//closing of If Statement else {//error handling System.exit(0); }//closing bracket of else
//check for quantity input and errors System.out.print("Enter Quantity : "); quantity = UserInput.readInt();
if (quantity<=0) { System.out.print(" Error, invalid value "); System.exit(0); }// check for invalid entries
//check for price input and errors if (price<=0) { System.out.print(" Error, invalid value "); }// check for invalid entries
//total price value totalPrice=price*quantity;
//Output of correctly inputted data System.out.println(manuf+":"+name+":"+barcode+":"+price); System.out.println(quantity+" @ "+price+" = "+totalPrice);
//update variables quantities //update total quantity totalQuantity = (totalQuantity + quantity); //keep count of total value totalValue = (totalValue + totalPrice); //keep count of totqal no of transactions transactions = (transactions++);
public static boolean isValidBarcode(String code) {
// validateBarcode length if ( code.length() != 13 ) { System.out.println("Invalid barcode " + code + " not 13 characters"); return false; }//closing if bracket
//check for valid barcode if length is correct
for ( int i = 0; i < code.length(); i++ ){// Check every char a digit if ( ! Character.isDigit( code.charAt(i) ) ){ return false; }//closing if bracket }//endfor
int sum1 = 0; // Sum first + third + etc. for ( int i = 0; i < code.length() - 1; i += 2 ){ sum1 += code.charAt(i) - '0'; }//end for loop
int sum2 = 0; // Sum second + fourth + etc. for ( int i = 1; i < code.length() - 1; i += 2 ){ sum2 += code.charAt(i) - '0'; }//end for loop
int check = sum1 + 3 * sum2; // 1st sum + three times 2nd sum. check = check % 10; // Remainder on division by 10. if ( check != 0 ){ check = 10 - check; }//closing if bracket
if (check != code.charAt(12) - '0'){ System.out.println("Invalid Barcode " + code + " check digit error"); }//closing if bracket
Your code seems basically correct except you try to instantiate p = new Product(...) without first declaring p to be of that type. You can fix the problem by declaring
Product p;
at the top of your main method, along with the others.
That gets the code to run at least. The big picture question is what happens to your p objects once you create them. In your code example, p becomes one Product after another as you loop through your prompts, then goes out of scope at the end of main().
There's no persistence here -- but I assume that's because this is just a mock-up of the real thing.