Advertisement
Guest User

Untitled

a guest
Jan 19th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.86 KB | None | 0 0
  1.  
  2. import java.util.*;
  3.  
  4. public class LibraryBooks2{
  5.  
  6.  
  7.  
  8.     public static void main(String[] args){
  9.  
  10.         System.out.println("This program calculates the profits and orders of books");
  11.         System.out.println("Enter \"quit\" at any time to quit ");
  12.         userInteraction();
  13.         //math();
  14.         //printInfo();
  15.  
  16.  
  17.  
  18.     }
  19.  
  20.     public static void userInteraction(){
  21.  
  22.         q1(); //new book
  23.         q2(); //book id
  24.         q3(); //book cost
  25.         q4(); //# of copies
  26.         q5(); //# of students in class
  27.         q6(); //book required
  28.         q7(); //book new to school
  29.  
  30.  
  31.  
  32.     }
  33.  
  34.     public static void q1(){  //new book
  35.         Scanner scanner = new Scanner(System.in);
  36.         String qNum = "q1";
  37.         System.out.println("Do you want to add a book? ( y / n )");
  38.         ynValidation(scanner.nextLine(),qNum);
  39.  
  40.     }
  41.  
  42.     public static void q2(){  //book id
  43.         Scanner scanner = new Scanner(System.in);
  44.         String qNum = "q2";
  45.         System.out.println("What is the book ID? ( y / n )");
  46.         String bookID=scanner.nextLine();
  47.         System.out.println("You entered: " +bookID + "\nIs this correct? ( y / n )");
  48.         ynValidation(scanner.nextLine(), qNum);
  49.     }
  50.  
  51.     public static void q3(){   //book cost
  52.         Scanner scanner = new Scanner(System.in);
  53.         String qNum = "3";
  54.         System.out.println("What is the cost of the book?");
  55.         double validNum = numValidation(qNum);
  56.         System.out.println("You entered: $" +validNum + ".  Is this correct? ( y / n )");
  57.         ynValidation(scanner.nextLine(), qNum);
  58.  
  59.  
  60.     }
  61.  
  62.     public static void q4(){  //# of copies in storage
  63.         Scanner scanner = new Scanner(System.in);
  64.         String qNum = "4";
  65.         System.out.println("How many copies of this book are currently in storage?");
  66.         double validNum = numValidation(qNum);
  67.         int validInt = (int) validNum;
  68.         System.out.println("Confirm there are "+validInt+" copies in storage? ( y / n )");
  69.         ynValidation(scanner.nextLine(), qNum);
  70.  
  71.     }
  72.    
  73.     public static void q5(){  //# of students in class
  74.         Scanner scanner = new Scanner(System.in);
  75.         String qNum = "5";
  76.         System.out.println("How many students are enroled in class?");
  77.         double validNum = numValidation(qNum);
  78.         int validInt = (int) validNum;
  79.         System.out.println("Confirm there are "+validInt+" students in class? ( y / n )");
  80.         ynValidation(scanner.nextLine(), qNum);
  81.  
  82.     }
  83.    
  84.     public static void q6(){  //book required?
  85.         Scanner scanner = new Scanner(System.in);
  86.         String qNum = "6";
  87.         System.out.println("Is this book required for class? ( y / n )");
  88.         //double validNum = numValidation(qNum);
  89.         //int validInt = (int) validNum;
  90.         //System.out.println("Confirm there are "+validInt+"copies in storage? ( y / n )");
  91.         ynValidation(scanner.nextLine(), qNum);
  92.  
  93.     }
  94.    
  95.     public static void q7(){  //new book to school
  96.         Scanner scanner = new Scanner(System.in);
  97.         String qNum = "7";
  98.         System.out.println("Is this book new to school? ( y / n )");
  99.         //double validNum = numValidation(qNum);
  100.         //int validInt = (int) validNum;
  101.         //System.out.println("Confirm there are "+validInt+"copies in storage? ( y / n )");
  102.         ynValidation(scanner.nextLine(), qNum);
  103.  
  104.     }
  105.  
  106.  
  107.     public static double numValidation(String x){
  108.         Scanner scanner = new Scanner(System.in);
  109.         double y=0;
  110.         if(x.equals("3")){
  111.             try{
  112.                 y = scanner.nextDouble();
  113.             }catch(InputMismatchException exception)
  114.             {
  115.                 System.out.println("Invalid input, please input a number");
  116.                 q3();          
  117.             }
  118.         }
  119.         if(x.equals("4")){
  120.  
  121.             try{
  122.                 y = scanner.nextDouble();
  123.             }catch(InputMismatchException exception)
  124.             {
  125.                 System.out.println("Invalid input, please input a number");
  126.                 q4();
  127.             }
  128.             if(!(y % 1==0)){
  129.                 System.out.println("Invalid input, please input a WHOLE number");
  130.                 q4();
  131.             }
  132.         }
  133.         if(x.equals("5")){
  134.  
  135.             try{
  136.                 y = scanner.nextDouble();
  137.             }catch(InputMismatchException exception)
  138.             {
  139.                 System.out.println("Invalid input, please input a number");
  140.                 q5();
  141.             }
  142.             if(!(y % 1==0)){
  143.                 System.out.println("Invalid input, please input a WHOLE number");
  144.                 q5();
  145.             }
  146.         }
  147.  
  148.  
  149.         return y;
  150.  
  151.         //       
  152.         //        if(x <= 0){
  153.         //            System.out.println("please enter a valid number");
  154.         //        }
  155.         //      
  156.  
  157.  
  158.     }
  159.  
  160.  
  161.  
  162.     public static String ynValidation(String x, String y){
  163.         if(x.equals("quit")||x.equals("QUIT")){
  164.             System.out.println("Program exiting...");
  165.             System.exit(0);
  166.         }
  167.         if(x.equals("n")|| x.equals("N")){
  168.             if(y.equals("q1")){
  169.                 q1();
  170.             }
  171.             if(y.equals("q2")){
  172.                 q2();
  173.             }
  174.             if(y.equals("q3")){
  175.                 q3();
  176.             }
  177.             //System.out.println("Program exiting...");
  178.             //System.exit(0);
  179.         }
  180.         if(!(x.equals("y") || x.equals("Y"))){
  181.             System.out.println("invalid choice -- please try again or enter \"quit\" to exit");
  182.             if(y.equals("q1")){
  183.                 q1();
  184.             }
  185.             if(y.equals("q2")){
  186.                 q2();
  187.             }
  188.             if(y.equals("q3")){
  189.                 q3();
  190.             }
  191.             if(y.equals("q4")){
  192.                 q4();
  193.             }
  194.             if(y.equals("q5")){
  195.                 q5();
  196.             }
  197.             if(y.equals("q6")){
  198.                 q6();
  199.             }
  200.         }
  201.  
  202.  
  203.  
  204.  
  205.         return null;
  206.  
  207.  
  208.  
  209.  
  210.  
  211.     }
  212.  
  213.  
  214.  
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement