[go: up one dir, main page]

0% found this document useful (0 votes)
13 views2 pages

FClass Java

Uploaded by

bindughy2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views2 pages

FClass Java

Uploaded by

bindughy2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

FClass.

java

1 /* Problem Statement:
2
3 A car rental agency, "SafeRide Rentals," manages a fleet of cars. Customers can request to
rent a certain number of cars. The agency has a limited number of cars available at any given
time. Write a Java program to simulate this car rental process.
4
5 Specific Requirements:
6
7 Interface Rentable:
8
9 Define an interface Rentable with an abstract method rentCar() that returns a String.
10 Class RentalAgency:
11
12 Create a class RentalAgency with a String instance variable name to store the agency's name.
13 Implement a constructor to initialize the name.
14 Create a private inner class CarRental that implements the Rentable interface.
15 The CarRental class should have two integer instance variables: reqCars (number of cars
requested) and availCars (number of cars available, initialized to 10).
16 The CarRental class should have a constructor that takes an integer representing the number
of cars requested and initializes reqCars and availCars.
17 The CarRental class should implement the rentCar() method as follows:
18 If availCars is less than reqCars, return the string "Cannot rent".
19 Otherwise, decrement availCars by reqCars and return the string "Rented successfully.
Available cars are " followed by the updated availCars value.
20 Implement a public method reserveCar(int c) in the RentalAgency class that takes an integer c
(representing the number of cars requested) as input.
21 This method should create and return a new CarRental object with the given number of cars
requested.
22 Class FClass:
23
24 Create a class FClass with a main method.
25 In the main method:
26 Create a Scanner object to read input from the console.
27 Create a RentalAgency object named a1 with the name "SafeRide Rentals".
28 Read an integer from the console representing the number of cars a customer wants to rent.
29 Call the reserveCar() method on a1 with the input integer and store the returned Rentable
object in a variable r1.
30 Call the rentCar() method on r1 and print the returned string to the console.
31 Close the Scanner.
32 Objective:
33
34 The program should simulate a car rental process where a customer requests a certain number
of cars, and the program determines whether the rental is successful based on the
availability of cars. The program should then output an appropriate message. */
35
36
37 import java.util.Scanner;
38
39 interface Rentable {
40 public abstract String rentCar();
41 }
42
43 class RentalAgency {
44 String name;
45
46 public RentalAgency(String n) {
47 this.name = n;
48 }
49
50 private class CarRental implements Rentable {
51 int reqCars;
52 int availCars;
53
54 public CarRental(int cars) {
55 reqCars = cars;
56 availCars = 10;
57 }
58
59 public String rentCar() {
60 if (availCars < reqCars) {
61 return "Cannot rent";
62 } else {
63 availCars = availCars - reqCars;
64 return "Rented successfully. Available cars are " + availCars;
65 }
66 }
67 }
68
69 //****** Define the method reserveCar() here
70 public Rentable reserveCar(int c) {
71 return new CarRental(c);
72 }
73 }
74
75 public class FClass {
76 public static void main(String[] args) {
77 Scanner sc = new Scanner(System.in);
78 RentalAgency a1 = new RentalAgency("SafeRide Rentals");
79
80 Rentable r1 = a1.reserveCar(sc.nextInt());
81 System.out.println(r1.rentCar());
82 sc.close();
83 }
84 }

You might also like