• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java project help - UPC calculator/toString?  RSS feed

 
Alzecha Maris
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey there everyone,

I'm new to the board, and was wondering if I could get some assistance with this project and a few concepts that I'm having trouble understanding so far.

Our goal is to write a pretty simple program, one that takes the 12 digit UPC code entered by a user and to not only spit it back out in a format with dashes using toString, and also returns the first digit, a 2 more groups of digits numbering 2-6 and 7-11, and finally display the 12th digit. It then performs an equation to check the last digit and make sure the UPC code is correct.

However, being so new to java (I only learned visual basic before), with this I was introduced to two new concepts that for some reason I simply cannot grasp for the life of me: Using and calling the toString method, and calling on methods that are created in a completely different class file. I've tested and researched online, talked to my professor, and despite all that I still just can't understand this.

So what I was wondering is if someone could help me walk through the process of understanding and making these concepts work in my code. I'm not asking for my assignment to be done for me (that would be counter productive) but maybe some step by step advice/help would finally make me understand methods, calling them and how toString works. I really need to learn these, as they're crucial to my upcoming assignments, and nothing I've tried to learn them has worked so far.

My code is mostly unfinished, I've had to restart to make it a bit cleaner. But I figure this is a good point to get assistance. The first section of code is my UPC class, which is meant to contain all my methods as well as the toString to be called on:



The second is my Driver class, which takes user input, calls methods and gives the output I stated above (This part is still very unfinished, but I'm focusing msotly on the UPC class at the moment):




I suppose my first question is if my code looks okay, or if there's anything I'm doing wrong. I'm sorry for the long post. But thank you so much for any and all help given, I really hope I can finally learn how to do this properly.
 
Alzecha Maris
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Edit: Made some changes to the code, was running into an error, but fixed it as well as added more code to the Driver class.
 
Ron McLeod
Bartender
Posts: 1603
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From a usability viewpoint, I wouldn't want to be the person doing the data entry with this - too much effort to enter a code, having to type a space between every digit. Also, if you use something like a barcode scanner for input, all the digits will come together, not separated by a space.

Why not read the entire code in as a String, and have a method to parse it in to the various pieces? The method could even return an UPC object back.
 
Alzecha Maris
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ron McLeod wrote:From a usability viewpoint, I wouldn't want to be the person doing the data entry with this - too much effort to enter a code, having to type a space between every digit. Also, if you use something like a barcode scanner for input, all the digits will come together, not separated by a space.

Why not read the entire code in as a String, and have a method to parse it in to the various pieces? The method could even return an UPC object back.


Thanks for the response.

Funny that you mention that, because just before coming back here, I was doing some research and learned to do that exact same thing. The s sections of 5 digits are now input as string, which are then converted to integers. The first and last numbers are still integers, currently testing if it could still remain this way or if I should make those part of the string as well.

Also, through some odd stroke of maybe luck or whatever, I finally just managed to understand methods, calling them and how to use toString. Almost all of the code is functional now.

The only part I'm figuring out now is how to get my new integers, which I converted from string and then split into individual integers, to be used by the method that will contain the equation over to the UPC class. I'm still tinkering with it at the moment.

Here's the new Driver class:




And also the new UPC class updates:

 
Alzecha Maris
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Quick update: Considering if it would be better to move the entire string to int conversion method and all variables involved over to the UPC class. I'm thinking maybe the section that performs the check digit equation could more easily access the variables that way.
 
Ron McLeod
Bartender
Posts: 1603
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some questions to consider:
  • Should the driver class need to understand structure of a UPC code?
  • Should the driver class have any responsibility in determining if the code is valid?
  • Does it make sense to create a UPC object if the code is not valid?

  • If the answers are all no, then you could move all the UPC-specific handling in to the UPC class and create a static method which takes-in the raw String entered from the keyboard (or other source), does the parsing and validation, and returns an UPC object, but only if the String was parsed correctly and the extracted code validated successfully.

    public static UPC create(String upcStr) {}
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!