IT Developer

Library Classes in Java

Chapter 12

Library Classes in Java

Class 10 - Logix Kips ICSE Computer Applications with BlueJ


Share with a Friend

Java Program – Mobike Rental System


Define a class called mobike with the following description:

Instance variables/Data members:
int bno - to store the bike's number
int phno - to store the phone number of the customer
String name - to store the name of the customer
int days - to store the number of days the bike is taken on rent
int charge - to calculate and store the rental charge

Member Methods:
void input() - to input and store the details of the customer
void compute() - to compute the rental charge

The rent for a mobike is charged on the following basis:
First five days Rs 500 per day;
Next five days Rs 400 per day;
Rest of the days Rs 200 per day.

void display () - to display the details in the following format:

Bike No.    Phone No.   No. of days     Charge

Java Program – Mobike Rental System

import java.util.Scanner;

 

class mobike

{

    // Instance Variables

    int bno;

    int phno;

    String name;

    int days;

    int charge;

 

    // Method to input details

    void input()

    {

        Scanner sc = new Scanner(System.in);

 

        System.out.print("Enter Bike Number: ");

        bno = sc.nextInt();

 

        System.out.print("Enter Phone Number: ");

        phno = sc.nextInt();

 

        sc.nextLine(); // Clear buffer

 

        System.out.print("Enter Customer Name: ");

        name = sc.nextLine();

 

        System.out.print("Enter Number of Days: ");

        days = sc.nextInt();

    }

 

    // Method to compute rental charge

    void compute()

    {

        if(days <= 5)

        {

            charge = days * 500;

        }

        else if(days <= 10)

        {

            charge = (5 * 500) + (days - 5) * 400;

        }

        else

        {

            charge = (5 * 500) + (5 * 400) + (days - 10) * 200;

        }

    }

 

    // Method to display details

    void display()

    {

        System.out.println("\nBike No.\tPhone No.\tNo. of Days\tCharge");

        System.out.println(bno + "\t\t" + phno + "\t\t" + days + "\t\t" + charge);

    }

 

    // Main Method

    public static void main(String[] args)

    {

        mobike obj = new mobike();

 

        obj.input();

        obj.compute();

        obj.display();

    }

}

Output

Sample Output : 
Enter Bike Number: 101
Enter Phone Number: 987654321
Enter Customer Name: Rahul
Enter Number of Days: 12

Bike No.    Phone No.   No. of Days     Charge
101         987654321   12              6900

Charge Calculation Example (12 Days)

  • First 5 days → 5 × 500 = 2500
  • Next 5 days → 5 × 400 = 2000
  • Remaining 2 days → 2 × 200 = 400
  • Total = 2500 + 2000 + 400 = 6900