Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // Application Program 15 STUDENT NAME: Siddharth Upadhyay
- // PERIOD: Second
- // ** Class Design ~ Postal Bar Codes **
- //
- // For faster sorting of letters, the United States Postal Service
- // encourages companies that send large volumns of mail to use a bar
- // code denoting the ZIP code
- //
- //
- // eg. *************** ECRLOT ** CO57
- //
- // CODE C671RTS2
- // JOHN DOE CO57
- // 1009 FRANKLIN BLVD
- // SUNNYVALE CA 95014-5143
- //
- //
- // || | | | || || | | | | || | | || | | |
- // ||||||||||||||||||||||||||||||||||||||||||||||||||||
- //
- //
- //
- // The encoding sceme for a five-digit ZIP code is
- //
- //
- // ____________ Frame Bars ____________
- // / \
- // / \
- // V V
- // || | | | || || | | |||
- // |||||| ||||| ||||| ||||| ||||| ||||||
- // \___/ \___/ \___/ \___/ \___/ \___/
- // Digit Digit Digit Digit Digit Check
- // 1 2 3 4 5 Digit
- //
- //
- // The five encoded digits are followed by a correction digit, which
- // is computed as follows: Add up all digits and choose the correction
- // digit to make the sum a multiple of 10. For example, the ZIP code
- // 95014 has a sum of digits 19, so the correction digit is 1 to make
- // the sum equal to 20.
- //
- // Each digit of the ZIP code, and correction digit, is encoded
- // according to the following table where 0 denotes a half bar and 1
- // a full bar.
- //
- //
- // | 7 | 4 | 2 | 1 | 0
- // -------+-------+-------+-------+-------+-------
- // 1 | 0 | 0 | 0 | 1 | 1
- // -------+-------+-------+-------+-------+-------
- // 2 | 0 | 0 | 1 | 0 | 1
- // -------+-------+-------+-------+-------+-------
- // 3 | 0 | 0 | 1 | 1 | 0
- // -------+-------+-------+-------+-------+-------
- // 4 | 0 | 1 | 0 | 0 | 1
- // -------+-------+-------+-------+-------+-------
- // 5 | 0 | 1 | 0 | 1 | 0
- // -------+-------+-------+-------+-------+-------
- // 6 | 0 | 1 | 1 | 0 | 0
- // -------+-------+-------+-------+-------+-------
- // 7 | 1 | 0 | 0 | 0 | 1
- // -------+-------+-------+-------+-------+-------
- // 8 | 1 | 0 | 0 | 1 | 0
- // -------+-------+-------+-------+-------+-------
- // 9 | 1 | 0 | 1 | 0 | 0
- // -------+-------+-------+-------+-------+-------
- // 0 | 1 | 1 | 0 | 0 | 0
- //
- //
- // Note that they represent all combinations of two full and three
- // half bars. The digit can be easily computed from the bar code
- // using the column weights 7, 4, 2, 1, 0.
- //
- // For example,
- // 01100 is 0*7 + 1*4 + 1*2 + 0*1 + 0*0 = 6
- //
- // The only exception is 0, which yield 11 according to the weight
- // formula.
- //
- //
- //
- // Write a program that asks the user for an address, including
- // a five digit ZIP code. Calculate and store the ZIP bar code.
- // Use : for half bars, and | for full bars
- //
- // For example,
- // 95014 becomes
- // ||:|:::|:|:||::::::||:|::|:::|||
- //
- //
- // Add a Tracker class to your program so that you program can read
- // a bar code (with : denoting half bars and | denoting full bars)
- // and convert it to a ZIP code. Indicate an error if the bar code
- // is not correct.
- //
- //
- // ( 50) Required instance variables for Envelope
- // Code
- // Name
- // Address
- // City
- // State
- // ZIP
- // ZIP bar code
- //
- // ( 60) Constructor (code, name, addr, city, state, zip)
- //
- // ( 70) toString() displays the address in envelope form
- // ** see example above.
- //
- // ( 75) Application class that asks the user for an address,
- // instantiates an Envelope, and displays the address
- // on the evelope.
- //
- // ( 85) Program correctly prints the Zip bar code on the envelope.
- //
- // ( 90) Required instance variables for Tracker and toString
- // ZIP
- // ZIP bar code
- //
- // (100) Application class that asks the user for a ZIP bar code,
- // instantiates a Tracker, and displays the ZIP address.
- //
- // (105) Tracker displays an error message of the Zip bar code
- // is invalid
- //
- //
- //
- package envelope;
- import static java.lang.System.*;
- import java.util.*;
- public class App15
- {
- public static void main(String[] args)
- {
- Envelope toMe = new Envelope("C671RTS2", "JOHN DOE", "1009 FRANKLIN BLVD",
- "SUNNYVALE", "CA", 95014);
- out.println(toMe);
- Tracker toTestBarCodes = new Tracker("||:|:::|:|:||::::::||:|::|:::|||");
- out.println(toTestBarCodes);
- }
- }
- class Envelope
- {
- private String code;
- private String name;
- private String address;
- private String city;
- private String state;
- private int zip;
- private String zipBarCode;
- Envelope(String code, String name, String address, String city, String state, int zip)
- {
- this.code = code.toUpperCase();
- this.name = name.toUpperCase();
- this.address = address.toUpperCase();
- this.city = city.toUpperCase();
- this.state = state.toUpperCase();
- this.zip = zip;
- zipBarCode = "";
- barCode();
- }
- private void barCode()
- {
- String rawZip = "" + zip;
- zipBarCode += "|";
- for(int x = 0; x < 4; x++)
- {
- int pieceOfZip = Integer.parseInt("" + rawZip.charAt(x));
- switch(pieceOfZip)
- {
- case 1: zipBarCode += " ||"; break;
- case 2: zipBarCode += " | |"; break;
- case 3: zipBarCode += " || "; break;
- case 4: zipBarCode += " | |"; break;
- case 5: zipBarCode += " | | "; break;
- case 6: zipBarCode += " || "; break;
- case 7: zipBarCode += "| |"; break;
- case 8: zipBarCode += "| | "; break;
- case 9: zipBarCode += "| | "; break;
- case 0: zipBarCode += "|| "; break;
- default: exit(0);
- }
- }
- zipBarCode += "|";
- }
- public String toString()
- {
- String label;
- label = "CODE " + code + "\n";
- label += name + "\n";
- label += address + "\n";
- label += city + " " + state + " " + zip + "\n";
- label += zipBarCode + "\n";
- for(int x = 0; x < zipBarCode.length(); x++)
- label += "|";
- label += "\n";
- return label;
- }
- }
- class Tracker
- {
- private int trackerZip;
- private String trackerZipBarCode;
- private String errorFinale;
- Tracker(String barCodeRaw)// A quaint little Constuctor
- {
- trackerZipBarCode = barCodeRaw;
- trackerZip = barCodeToZipCode(trackerZipBarCode);
- }
- private int barCodeToZipCode(String tempRawBarCode)
- {
- String rawBarCode = tempRawBarCode;
- String tempZip = "";
- int total = 0;
- // v-- Due to Check Piece
- for(int x = 1; x <= 25; x += 5)
- {
- //Seperates Bar Code set
- char sev = rawBarCode.charAt(0 + x);
- char fou = rawBarCode.charAt(1 + x);
- char two = rawBarCode.charAt(2 + x);
- char one = rawBarCode.charAt(3 + x);
- char zer = rawBarCode.charAt(4 + x);
- int totalPerDigit = 0; // (Re)Instantiate the current digit Total
- if(sev == '|')
- totalPerDigit += 7;
- if(fou == '|')
- totalPerDigit += 4;
- if(two == '|')
- totalPerDigit += 2;
- if(one == '|')
- totalPerDigit += 1;
- if(zer == '|')
- totalPerDigit += 0;
- if((sev == '|')&&(fou == '|'))
- totalPerDigit -= 11;
- tempZip += "" + totalPerDigit;
- total += totalPerDigit;
- }
- if(errorCheck(rawBarCode, total))
- return Integer.parseInt(tempZip);
- else
- return 0;
- }
- private boolean errorCheck(String fullBarCode, int totalValue)
- {
- String barCodeToCheck = fullBarCode;
- int totalValueOfZip = totalValue;
- String checkpiece = barCodeToCheck.substring(26,31);
- int extra = 0;
- //Seperates Bar Code set
- char sev = checkpiece.charAt(0);
- char fou = checkpiece.charAt(1);
- char two = checkpiece.charAt(2);
- char one = checkpiece.charAt(3);
- char zer = checkpiece.charAt(4);
- if(sev == '|')
- extra += 7;
- if(fou == '|')
- extra += 4; // Logical Goodness
- if(two == '|')
- extra += 2;
- if(one == '|')
- extra += 1;
- if(zer == '|')
- extra += 0;
- if((sev == '|')&&(fou == '|')&&(two == '|')&&(one == '|')) // Halbert Cases
- extra -= 14;
- else
- if((sev == '|')&&(fou == '|')&&(two == '|'))
- extra -= 13;
- else
- if((one == '|')&&(fou == '|')&&(two == '|'))
- extra -= 7;
- else
- if(((sev == '|')&&(fou == '|')))
- extra -= 11;
- if(barCodeToCheck.length() != 32)
- {
- out.println("The Bar Code Too Long");
- exit(0);
- }
- else
- if( (totalValueOfZip + extra) % 10 != 0 )
- {
- out.println("The Bar Code is a tad wrong.");
- exit(0);
- }
- return true;
- }
- public String toString()
- {
- String tracker = "";
- tracker += trackerZipBarCode + " becomes:\n";
- tracker += "\t" + trackerZip;
- return tracker;
- }
- }
Add Comment
Please, Sign In to add comment