Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- /* ==================================================
- Chapter 3: Logical and conditional expressions
- Ex4: Library Subscription
- ===================================================
- */
- public class MyProgram {
- public static void main(String[] args) {
- //variables
- final int ADULT =5;
- final int CHILD =3;
- final int MAX_LOAN=1;
- int userInput;
- char answer;
- int borrowedBooks;
- Scanner s=new Scanner(System.in);
- //user input
- System.out.print("1. Adult\n2. Child\nwhich subscription do you have? ");
- userInput=s.nextInt();
- System.out.print("how many borrowed books you have currently? ");
- borrowedBooks=s.nextInt();
- //if there are books borrowed -check the subscription type (adult/child)
- if(borrowedBooks>0){
- if(userInput==1 && borrowedBooks>=ADULT) //Adult type can not borrow more than 5
- System.out.println("you cant borrow more than "+ADULT+" books!");
- else if(userInput==2 && borrowedBooks>=CHILD) //child type can not borroe more than 3
- System.out.println("you cant borrow more than "+CHILD+" books!");
- else{
- //if within limits check if loan time valid
- System.out.print("is one of the books you borrowed longer than "+MAX_LOAN+" month? (y/n)");
- answer=s.next().charAt(0);
- if(answer=='y')
- System.out.println("you can not borrow while one of the borrowed books loan period is more than 1 month");
- else
- System.out.println("you can borrow a book");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment