Omar_Natour

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

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