This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Mar 1st, 2011  |  syntax: Java  |  size: 2.82 KB  |  views: 176  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
This paste has a previous version, view the difference. Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. package creditcard;
  2. import java.io.FileWriter;
  3. import java.io.BufferedWriter;
  4. import java.io.IOException;
  5.  
  6. public class Card {
  7.  
  8.     //Declarations
  9.     private String cardNumber;
  10.     private boolean test = false;
  11.     private String cardType = "Unknown";
  12.  
  13.     public Card(){
  14.         cardNumber = "0";
  15.     }//end Default Constructor
  16.  
  17.     public Card(String input){
  18.         cardNumber = input;
  19.     }//end Constructor
  20.  
  21.     //
  22.     public void typeMatcher(){
  23.         if (cardNumber.startsWith("37"))
  24.             cardType = "American Express";
  25.         if (cardNumber.startsWith("4"))
  26.             cardType = "Visa";
  27.         if (cardNumber.startsWith("5"))
  28.             cardType = "Mastercard";
  29.         if (cardNumber.startsWith("6"))
  30.             cardType = "Discover";
  31.     }//end typeMatcher
  32.  
  33.     // Returns true if card number is Valid.
  34.     public boolean isValid(String cardNumber){
  35.         if (cardNumber.length() > 12 && cardNumber.length() < 17)
  36.             if((sumOfDoubleEvenPlace(cardNumber)+sumOfOddPlace(cardNumber))/10==0)
  37.                 test = true;
  38.         return test;
  39.     }//end isValid
  40.  
  41.     // Get result from Step 2.
  42.     public int sumOfDoubleEvenPlace(String cardNumber){
  43.         int sum = 0;
  44.         int num = 0;
  45.         for (int i = cardNumber.length()-2; i>-1; i-=2){
  46.             num = Integer.parseInt(cardNumber.substring(i,i+1));
  47.             if (num<10)
  48.                 sum+=(num*2);
  49.             else
  50.                 sum+=getDigit(num);
  51.         }//end for
  52.         System.out.println(sum);
  53.         return sum;
  54.     }//end SumOfDoubleEvenPlace
  55.  
  56.     // Return this number if it is a single digit
  57.     // Otherwise return the sum of the two digits.
  58.     public int getDigit (int num){
  59.         int no1 = num/10;
  60.         int no2 = num%10;
  61.         return no1+no2;
  62.     }//end getDigit
  63.  
  64.     // Returns sum of odd place digits in number.
  65.     public int sumOfOddPlace(String cardNumber){
  66.         int sum = 0;
  67.         for (int i = cardNumber.length()-1; i>-1; i-=2)
  68.             sum += Integer.parseInt(cardNumber.substring(i,i));
  69.         System.out.println(sum);
  70.         return sum;
  71.     }//end sumOfOddPlace
  72.  
  73.     public void writeToFile(){
  74.         try{
  75.             FileWriter fw = new FileWriter("G:\\Output.txt",true);
  76.             BufferedWriter bw = new BufferedWriter(fw);
  77.             if (isValid(cardNumber)){
  78.                 bw.write(cardNumber+" is a valid "+cardType+" card.");
  79.                 bw.newLine();
  80.             }//end if
  81.             else{
  82.                 bw.write(cardNumber+" is an invalid "+cardType+" card.");
  83.                 bw.newLine();
  84.             }//end else
  85.             bw.close();
  86.             System.out.println("Success");
  87.         }//end try
  88.  
  89.         catch(IOException ioe){
  90.             ioe.printStackTrace();
  91.         }//end catch
  92.  
  93.     }//end outputToFile
  94.  
  95. }//end Card
clone this paste RAW Paste Data