By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,836 Members | 2,028 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,836 IT Pros & Developers. It's quick & easy.

Need help with a near final java program

P: 2
Alright im working on a program that prints out user imput in a frame, along with a barcode.. it is like the front of an envelope. Here is the description for the program.
This part just explains a check digit.. i think this part in my program is alright

These barcodes actually have four parts: a tall line as the first bar, a series of
tall and short lines corresponding to the digits in the ZIP code, a check digit,
and a tall line on the far right as the last bar. The ZIP code bar dictionary is
shown on the right. The check digit is a number that, when added to the
individual digits of the ZIP code, makes an even multiple of 10. For
example, given the ZIP code 44325, the check digit would be 2.
4 + 4 + 3 + 2 + 5 = 18
18 + 2 = 20
20 is a multiple of 10.

Your job is to take an address and print a label with the barcode (not literally using a printer, but rather in a JFrame). Use three dialog boxes to prompt the user for an address of the form:
First-name Last-name
Street address
City, State ZIP code
Print a label in a small, rectangular JFrame. You should print out the address lines and barcode to look very similar to the label on the first page. The neatness of your output is very important.

Specific Requirements:
You must have a separate class to draw the bars. Pass in a parameter that indicates whether it is a tall or short bar. Use an if statement in your drawing method that determines how it is drawn.
You must have a separate class for the address. This class will have functionality to get the three lines of address input from the user, display them on the frame and parse out the ZIP code from the third line. You should have an accessor method for the ZIP code so that you can use it elsewhere (i.e., in another class like main).
If the any line of the address is missing, send it to the
Dead Letter Box
c/o Local USPS
and print the barcode.
If there is no ZIP code, (StringTokenizer should give you a count of 3 tokens
before you start taking things out), do not print the barcode. Print an error
message on the frame in lieu of the address.
Return to Sender
Post not deliverable.
You must make a class that draws the barcode (which means you will need to
pass the ZIP code as a parameter to its constructor). You are required to use a
switch statement to draw the barcode.

And here is all my code.. this post is going to be huge, I'm new here so if there is any way that I could compress it let me know.. right now this is all I know to do


import javax.swing.*;
import java.awt.*;



public class mailComponent extends JComponent
{
int x1;
int x2;
int x3;
int x4;
int x5;
int ZipCode;
private Address label1;
int sum1;
bars one;

public mailComponent()
{
label1 = new Address();
ZipCode = label1.getZipCode();
calcDigits();
CheckSum();
one = new bars();
}

public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
label1.write(g2);
one.drawLong(g2);
drawBars(x1,g2);
drawBars(x2,g2);
drawBars(x3,g2);
drawBars(x4,g2);
drawBars(x5,g2);
drawBars(sum1,g2);
one.drawLong(g2);
}



public void calcDigits()
{
x5 = ZipCode%10;
ZipCode = (ZipCode-x5)/10;
x4 = ZipCode%10;
ZipCode = (ZipCode-x4)/10;
x3 = ZipCode%10;
ZipCode = (ZipCode-x3)/10;
x2 = ZipCode%10;
ZipCode = (ZipCode-x2)/10;
x1 = ZipCode%10;
System.out.println(x1);
System.out.println(x2);
System.out.println(x3);
System.out.println(x4);
System.out.println(x5);
}

public void CheckSum()
{

sum1 = x1+x2+x3+x4+x5;
sum1= sum1%10;

if (sum1 == 0)
sum1 = 0;
else
sum1 = 10-sum1;
}

public void drawBars(int wall, Graphics2D g2)
{


switch (wall)
{
case 1: one.drawShort(g2);
one.drawShort(g2);
one.drawShort(g2);
one.drawLong(g2);
one.drawLong(g2);
break;
case 2: one.drawShort(g2);
one.drawShort(g2);
one.drawLong(g2);
one.drawShort(g2);
one.drawLong(g2);
break;
case 3: one.drawShort(g2);
one.drawShort(g2);
one.drawLong(g2);
one.drawLong(g2);
one.drawShort(g2);
break;
case 4: one.drawShort(g2);
one.drawLong(g2);
one.drawShort(g2);
one.drawShort(g2);
one.drawLong(g2);
break;
case 5: one.drawShort(g2);
one.drawLong(g2);
one.drawShort(g2);
one.drawLong(g2);
one.drawShort(g2);
break;
case 6: one.drawShort(g2);
one.drawLong(g2);
one.drawLong(g2);
one.drawShort(g2);
one.drawShort(g2);
break;
case 7: one.drawLong(g2);
one.drawShort(g2);
one.drawShort(g2);
one.drawShort(g2);
one.drawLong(g2);
break;
case 8: one.drawLong(g2);
one.drawShort(g2);
one.drawShort(g2);
one.drawLong(g2);
one.drawShort(g2);
break;
case 9: one.drawLong(g2);
one.drawShort(g2);
one.drawLong(g2);
one.drawShort(g2);
one.drawShort(g2);
break;
case 0: one.drawLong(g2);
one.drawLong(g2);
one.drawShort(g2);
one.drawShort(g2);
one.drawShort(g2);

}
}
}






import javax.swing.*;
import java.awt.*;
import java.util.StringTokenizer;

public class Address
{
//Class Variables
String s1;
String s2;
String s3;

//Constructor
public Address()
{
String prompt = "first and last name";
s1 = JOptionPane.showInputDialog(prompt);

String prompt2 = "Enter Street Address";
s2 = JOptionPane.showInputDialog(prompt2);

String prompt3 = "City, State Zip";
s3 = JOptionPane.showInputDialog(prompt3);

checkValid();
// noZip();
}

public void write(Graphics2D g2)
{
Font address1 = new Font ("Sans Serif", 0, 20);
g2.setFont(address1);
g2.drawString(s1, 100, 150);
g2.drawString(s2, 38, 110);
g2.drawString(s3, 38, 120);
}
public int getZipCode()
{

StringTokenizer st = new StringTokenizer(s3);
String tristan;
tristan = st.nextToken();
tristan = st.nextToken();
tristan = st.nextToken();
return Integer.parseInt(tristan, 10);

}
public void checkValid()
{
if (s1.equals(""))
{
s1 = "Dead Letter Box";
s2 = "c/o Local USPS";
s3 = "";
}
if (s2.equals(""))
{
s1 = "Dead Letter Box";
s2 = "c/o Local USPS";
s3 = "";
}
}
//this part is commented out because
//I don't know why its not working
/*public boolean noZip()
{
boolean zipCheck = true;
if (s3.equals(""))
{
s1 = "Return to Sender";
s2 = "Post not deliveable";
s3 = null;
zipCheck = false;
}
return zipCheck;
}
*/
}



import javax.swing.*;
import java.awt.*;
import java.awt.Rectangle;

public class bars
{
int x = 50;

public bars()
{
}

public void drawLong(Graphics2D g2)
{
Rectangle big = new Rectangle(x, 200,10, 20);
g2.fill(big);
g2.draw(big);
x = x + 20;
}

public void drawShort(Graphics2D g2)
{
Rectangle small = new Rectangle(x, 200, 10, 10);
g2.fill(small);
g2.draw(small);

x = x + 20;
}


}



import javax.swing.*;
import java.awt.*;

public class MailViewer
{
public static void main (String[] args)
{
JFrame one = new JFrame("Woooorrrrk?!!");

final int FRAME_WIDTH = 800;
final int FRAME_HEIGHT = 600;
one.setSize(FRAME_WIDTH, FRAME_HEIGHT);
one.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ;

mailComponent two = new mailComponent();

one.add(two);

one.setVisible(true);



}
}





There are my four files, the program works sorta.. I haven't fine tuned where the text appears in the frame yet, I am stressing over getting it to function properly first. I can't get the checking for bad addresses to work right, my brain is fried and its very late if anyone can help me out that would be really awesome.

I'm new to java so if my code looks like an idiot wrote it.. one did haha but please help him out anyway
Nov 16 '06 #1
Share this Question
Share on Google+
1 Reply


P: 2
Alright guys i have made some nice progress with this, but I am having one issure and i cannot figure out what needs to be done.. when i pass nothing into the Joptionpanes i have to return a return to sender thing. if either of the first 2 lines are blank, but the third is alright I have to print out the bar code.

if the third line is blank I print a different message (the exact wording is in my first post) and no barcode.. someone please help lol this thing is due tomorrow.. here are the 2 files that need fixing
Expand|Select|Wrap|Line Numbers
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.util.StringTokenizer;
  4.  
  5. public class Address
  6. {
  7.     //Class Variables    
  8.     String s1;
  9.     String s2;
  10.     String s3;
  11.     boolean zipCheck = true;
  12.  
  13.             //Constructor
  14.             public Address()
  15.             {
  16.             String prompt = "first and last name";
  17.             s1 = JOptionPane.showInputDialog(prompt);
  18.  
  19.             String prompt2 = "Enter Street Address";
  20.             s2 = JOptionPane.showInputDialog(prompt2);
  21.  
  22.             String prompt3 = "City, State Zip";
  23.             s3 = JOptionPane.showInputDialog(prompt3);
  24.  
  25.             checkValid();
  26.         //    noZip();
  27.             }
  28.  
  29.         public void write(Graphics2D g2)
  30.               {
  31.                 Font address1 = new Font ("Arial", 0, 20);
  32.                 g2.setFont(address1);
  33.                 g2.drawString(s1, 50, 130);
  34.                 g2.drawString(s2, 50, 150);
  35.                 g2.drawString(s3, 50, 170);
  36.             }
  37.         public int getZipCode()
  38.             {
  39.             if (zipCheck != false) {
  40.                 StringTokenizer st = new StringTokenizer(s3); 
  41.                 String tristan;
  42.                 tristan = st.nextToken();
  43.                 tristan = st.nextToken();
  44.                 tristan = st.nextToken();
  45.                 return Integer.parseInt(tristan, 10);
  46.             }
  47.             else
  48.                 return 0;
  49.             }
  50.     public void checkValid()
  51.     {
  52.     if (s1.equals(""))
  53.         {
  54.         s1 = "Dead Letter Box";
  55.         s2 = "c/o Local USPS";
  56.         s3 = " "; 
  57.         zipCheck = false;
  58.         }
  59.     if (s2.equals(""))
  60.         {
  61.         s1 = "Dead Letter Box";
  62.         s2 = "c/o Local USPS";
  63.         s3 = " "; 
  64.         zipCheck = false;
  65.         }
  66.  
  67.         if (s3.equals(""))
  68.         {
  69.         s1 = "Return to Sender";
  70.         s2 = "Post not deliveable";
  71.         s3 = " ";
  72.         zipCheck = false;
  73.         }
  74.     }
  75.  
  76.     }
  77.  
  78.  

Expand|Select|Wrap|Line Numbers
  1.  import javax.swing.*;
  2. import java.awt.*;
  3.  
  4.  
  5.  
  6. public class mailComponent extends JComponent
  7. {
  8. int x1;
  9. int x2;
  10. int x3;
  11. int x4;
  12. int x5;
  13. int ZipCode;
  14. private Address label1;
  15. int sum1;
  16. bars one;
  17.  
  18.     public mailComponent()    
  19.     {
  20.     label1 = new Address();
  21.     ZipCode = label1.getZipCode();
  22.     calcDigits();
  23.     CheckSum();
  24.     one = new bars();
  25.     }
  26.  
  27.         public void paintComponent(Graphics g)
  28.         {
  29.         Graphics2D g2 = (Graphics2D) g;
  30.         label1.write(g2);
  31.         if (ZipCode != 0) {
  32.             one.drawLong(g2);
  33.             drawBars(x1,g2);
  34.             drawBars(x2,g2);
  35.             drawBars(x3,g2);
  36.             drawBars(x4,g2);
  37.             drawBars(x5,g2);
  38.             drawBars(sum1,g2);
  39.             one.drawLong(g2);
  40.         }
  41.     }    
  42.  
  43.  
  44.         public void calcDigits()
  45.         {
  46.             x5 = ZipCode%10;
  47.             ZipCode = (ZipCode-x5)/10;
  48.             x4 = ZipCode%10;
  49.             ZipCode = (ZipCode-x4)/10;
  50.             x3 = ZipCode%10;
  51.             ZipCode = (ZipCode-x3)/10;
  52.             x2 = ZipCode%10;
  53.             ZipCode = (ZipCode-x2)/10;
  54.             x1 = ZipCode%10;    
  55.             System.out.println(x1);
  56.             System.out.println(x2);
  57.             System.out.println(x3);
  58.             System.out.println(x4);
  59.             System.out.println(x5);
  60.             }
  61.  
  62.         public void CheckSum()
  63.         {
  64.  
  65.             sum1 = x1+x2+x3+x4+x5;
  66.             sum1= sum1%10;
  67.  
  68.             if (sum1 == 0)
  69.                 sum1 = 0;
  70.             else 
  71.                 sum1 = 10-sum1;
  72.         }
  73.  
  74.         public void drawBars(int wall, Graphics2D g2)
  75.         {
  76.  
  77.  
  78.             switch (wall)
  79.             {
  80.             case 1:  one.drawShort(g2);
  81.                         one.drawShort(g2); 
  82.                         one.drawShort(g2); 
  83.                         one.drawLong(g2); 
  84.                         one.drawLong(g2);
  85.                 break;
  86.             case 2:  one.drawShort(g2); 
  87.                         one.drawShort(g2); 
  88.                         one.drawLong(g2);
  89.                         one.drawShort(g2); 
  90.                         one.drawLong(g2);
  91.                 break;
  92.             case 3:  one.drawShort(g2); 
  93.                         one.drawShort(g2);
  94.                         one.drawLong(g2);
  95.                         one.drawLong(g2); 
  96.                         one.drawShort(g2);
  97.                 break;
  98.             case 4:  one.drawShort(g2); 
  99.                         one.drawLong(g2); 
  100.                         one.drawShort(g2); 
  101.                         one.drawShort(g2); 
  102.                         one.drawLong(g2);
  103.                 break;
  104.             case 5:  one.drawShort(g2); 
  105.                         one.drawLong(g2); 
  106.                         one.drawShort(g2); 
  107.                         one.drawLong(g2); 
  108.                         one.drawShort(g2);
  109.                 break;
  110.             case 6:  one.drawShort(g2); 
  111.                         one.drawLong(g2); 
  112.                         one.drawLong(g2); 
  113.                         one.drawShort(g2);
  114.                         one.drawShort(g2);
  115.                 break;
  116.             case 7:  one.drawLong(g2); 
  117.                         one.drawShort(g2); 
  118.                         one.drawShort(g2); 
  119.                         one.drawShort(g2); 
  120.                         one.drawLong(g2);
  121.                 break;
  122.             case 8:  one.drawLong(g2); 
  123.                         one.drawShort(g2); 
  124.                         one.drawShort(g2); 
  125.                         one.drawLong(g2); 
  126.                         one.drawShort(g2);
  127.                 break;
  128.             case 9:  one.drawLong(g2); 
  129.                         one.drawShort(g2); 
  130.                         one.drawLong(g2); 
  131.                         one.drawShort(g2); 
  132.                         one.drawShort(g2);
  133.                 break;
  134.             case 0:  one.drawLong(g2); 
  135.                         one.drawLong(g2); 
  136.                         one.drawShort(g2); 
  137.                         one.drawShort(g2); 
  138.                         one.drawShort(g2);
  139.  
  140.             }
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147.         }
  148.  
  149.  
  150. }
  151.  
Nov 17 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.