Computer Applications

The City Library charges late fine from members if the books were not returned on time as per the following table:

Number of days lateMagazines fine per dayText books fine per day
Up to 5 daysRs. 1Rs. 2
6 to 10 daysRs. 2Rs. 3
11 to 15 daysRs. 3Rs. 4
16 to 20 daysRs. 5Rs. 6
More than 20 daysRs. 6Rs. 7

Using the switch statement, write a program in Java to input name of person, number of days late and type of book — 'M' for Magazine and 'T' for Text book. Compute the total fine and display it along with the name of the person.

Java

Java Conditional Stmts

28 Likes

Answer

import java.util.Scanner;

public class KboatLibrary
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter name: ");
        String name = in.nextLine();
        System.out.print("Days late: ");
        int days = in.nextInt();
        System.out.println("Type M for Magazine");
        System.out.println("Type T for Text book");
        System.out.print("Enter book type: ");
        char type = in.next().charAt(0);
        int fine = 0;
        switch (type) {
            case 'M':
            if (days <= 5)
                fine = 1;
            else if (days <= 10)
                fine = 2;
            else if (days <= 15)
                fine = 3;
            else if (days <= 20)
                fine = 5;
            else
                fine = 6;
            break;
            
            case 'T':
            if (days <= 5)
                fine = 2;
            else if (days <= 10)
                fine = 3;
            else if (days <= 15)
                fine = 4;
            else if (days <= 20)
                fine = 6;
            else
                fine = 7;
            break;
            
            default:
            System.out.println("Invalid book type");
        }
        
        int totalFine = fine * days;
        System.out.println("Name: " + name);
        System.out.println("Total Fine: " + totalFine);
    }
}

Output

Answered By

13 Likes


Related Questions