The Artima Developer Community
Sponsored Link

Java Answers Forum
Class Instances

1 reply on 1 page. Most recent reply: Apr 16, 2003 7:43 PM by Kirby Urner

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Jeffrey Jones

Posts: 2
Nickname: zebedeeee
Registered: Apr, 2003

Class Instances Posted: Apr 16, 2003 10:41 AM
Reply to this message Reply
Advertisement
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;




//Input Details
System.out.print("Enter Product Manufacturer : ");
manuf = UserInput.readString();



//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

System.out.print("Enter Price :");
price = UserInput.readInt();

//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++);

//Input Details
System.out.print("Enter Product Manufacturer : ");
manuf = UserInput.readString();

}//closure of loop

//display final totals
System.out.println("Transactions: "+transactions);
System.out.println("Total quantity: "+totalQuantity);
System.out.println("Total value: "+totalValue);
System.out.println("Average Cost: "+totalValue/totalQuantity);

System.exit(0);




}//closing bracket input and output of data

}//end class


/**
* Write a description of class StaticProduct here.
*
* @author Jeffrey Jones
* @version 1 1st April 2003
*/
public class StaticProduct
{

/**
* isValidBarcode method
*
*
* @return boolean
*/

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

return ( check == code.charAt(12) - '0' );



}//end isValidBarcode

}//end class


Kirby Urner

Posts: 2
Nickname: kirby
Registered: Apr, 2003

Re: Class Instances Posted: Apr 16, 2003 7:43 PM
Reply to this message Reply
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.

Kirby

PS: to actually get this code to run, I had to grab a UserInput class off the web, plus I made up a fake Product class which does nothing. The UserInput source I used is here: http://www.infj.ulst.ac.uk/e495pj/projects/past_wisdom/UserInput.java

Flat View: This topic has 1 reply on 1 page
Topic: Writing files in Java additional question Previous Topic   Next Topic Topic: Any software to update deprecated classes?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use