hpilo

Chapter3_Logic_Ex4

Dec 15th, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.79 KB | None | 0 0
  1. import java.util.Scanner;
  2. /*  ==================================================
  3.       Chapter 3: Logical and conditional expressions
  4.  
  5.         Ex4: Library Subscription
  6.     ===================================================
  7. */
  8.  
  9. public class MyProgram {
  10.     public static void main(String[] args) {
  11.        
  12.         //variables
  13.         final int ADULT =5;
  14.         final int CHILD =3;
  15.         final int MAX_LOAN=1;
  16.  
  17.         int userInput;
  18.         char answer;
  19.         int borrowedBooks;
  20.         Scanner s=new Scanner(System.in);
  21.        
  22.         //user input
  23.         System.out.print("1. Adult\n2. Child\nwhich subscription do you have? ");
  24.         userInput=s.nextInt();
  25.         System.out.print("how many borrowed books you have currently? ");
  26.         borrowedBooks=s.nextInt();
  27.  
  28.         //if there are books borrowed -check the subscription type (adult/child)
  29.         if(borrowedBooks>0){
  30.            
  31.             if(userInput==1 && borrowedBooks>=ADULT) //Adult type can not borrow more than 5
  32.                 System.out.println("you cant borrow more than "+ADULT+" books!");
  33.             else if(userInput==2 && borrowedBooks>=CHILD)   //child type can not borroe more than 3
  34.                 System.out.println("you cant borrow more than "+CHILD+" books!");
  35.            
  36.             else{
  37.                 //if within limits check if loan time valid
  38.                 System.out.print("is one of the books you borrowed longer than "+MAX_LOAN+" month? (y/n)");
  39.                 answer=s.next().charAt(0);
  40.                 if(answer=='y')
  41.                     System.out.println("you can not borrow while one of the borrowed books loan period is more than 1 month");
  42.                 else
  43.                     System.out.println("you can borrow a book");
  44.             }
  45.         }
  46.  
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment