Advertisement
Omar_Natour

Natour, O. 10/25/15 Csc-111-D01 Hw 5.47

Oct 26th, 2015
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.84 KB | None | 0 0
  1.  
  2. /*
  3.  * Name: Omar Natour
  4.  * Date: 10/26/15
  5.  * Course Number: CSC-111
  6.  * Course Name: Introduction to Java Programming
  7.  * Problem Number: 5.47
  8.  * Email: ojnatour0001@student.stcc.edu
  9.  * Problem Description: Use the first 12 digits of the isbn number to find the 13th.
  10.  */
  11.  
  12. import java.util.Scanner;
  13.  
  14. public class HomeWork6 {
  15.  
  16.     public static void main(String args[]) {
  17.         final String TITLE = "The ISBN-13 calculator";
  18.         final String CONTINUE_PROMPT = "Do this again? [y/N] ";
  19.  
  20.         System.out.println("Welcome to " + TITLE);
  21.         Scanner sc = new Scanner(System.in);
  22.         do {
  23.  
  24.             System.out.print("Enter the first 12 digits of an ISBN-13 as a string:");
  25.             System.out.println(calc(sc.nextLine()));
  26.  
  27.         } while (doThisAgain(sc, CONTINUE_PROMPT));
  28.  
  29.         sc.close();
  30.  
  31.         System.out.println();
  32.         System.out.println("Thank you for using " + TITLE);
  33.     }
  34.  
  35.     // ************************************************************************
  36.  
  37.     private static boolean doThisAgain(Scanner sc, String prompt) {
  38.         System.out.print(prompt);
  39.         String doOver = sc.nextLine();
  40.         return doOver.equalsIgnoreCase("Y");
  41.     }
  42.  
  43.     // ************************************************************************
  44.  
  45.     private static String calc(String twelve) {
  46.         int sum = 0;
  47.         String out = "";
  48.        
  49.         if (twelve.length() != 12)
  50.             return twelve + " is an invalid input.";
  51.         else {
  52.             for (int i = 0; i < twelve.length(); i++) {
  53.                 int asci = twelve.charAt(i);
  54.                
  55.                 if (!(asci >= 48 && asci <= 57))
  56.                     return twelve + " is an invalid input.";
  57.                 else {
  58.                    
  59.                     int dig = -48 + asci;
  60.                     out += dig;
  61.  
  62.                     if (i % 2 == 0)
  63.                         sum += dig;
  64.                     else
  65.                         sum += (3 * dig);
  66.                 }
  67.             }
  68.         }
  69.         int back = 10 - sum % 10;
  70.        
  71.         if (back == 10)
  72.             return "the ISBN-13 number is " + out + 0 + ".";
  73.         else
  74.             return "the ISBN-13 number is " + out + back + ".";
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement