Write a program for the customer who are willing to take the vehicles for rent.

Question

JAVA PROGRAMMING
Force the customer to enter a name, phone (allow 2 formats), email address, vehicle pickup date, # of rental days, and allow the user to rent up to 3 vehicles at once. After the customer selects the vehicles to rent, output the details for each vehicle, price per vehicle * a number of days, and total rental price.
Be sure to create validation methods: IsValidPhoneNumber method to validate the phone number
IsValidEmailAddress to validate the email address (must begin with a character, have the @ sign, and have an extension of 2 to 6 characters)
IsValidVehicleNumber to validate the number of vehicles to rent 1 to 3
Create a main program CVehicleFinal. Create a vehicle class parent class (CVehicle). Then, create child classes for each vehicle type (CCar, CMotorbike, CTrailer). The program must have all the attributes and methods shown in the diagram in this document.
Here are the requirements for this project:

INPUTS

The application must allow the user to type in the following “required” information for each vehicle rental calculator:
Customer Name
Phone Number (validate using regular expressions – create method IsValidPhoneNumber)
Email Address (validate using regular expressions – create method IsValidEmailAddress)
Number of Rental Days
Number of Vehicles to Rent
Select the type of Vehicle to Rent

OUTPUTS

For each transaction display the following:
Customer Name
Phone Number
Email Address
Type of Vehicle to Rent
Information about the Vehicle such as how the vehicle drives, number of MPGs, etc.
Total Rental for vehicle-based on Price * Number of Days (must display 2 decimal places)
Total Rental Amount for all vehicles (must display 2 decimal places)

Summary

Here in this question, we have to write a program for the customer who is willing to take the vehicles for rent. For this, we are going to take the details and will validate them, and on the basics of the number of vehicles the user asked, the vehicles are going to assign them. Total rent with all the details of customers and vehicles will get printed on the screen.

Explanation

Here in this program, we have started with class CVehicle, its data members, and constructor to implement the details of the customer along with the details of the vehicle user rented. Then we add some methods to check if the phone number, email id, and vehicle number are valid or not. After that three classes such as CCar, CMotorbike and Ctrailer are used to see the properties and the rent of each particular vehicle. Each vehicle in the program has its own display function to have the details. After that when the whole execution is done, the total calculation of rent is printed on the screen as output.

Code

import java.util.*;
/* Implement the CVehicle class */
class CVehicle 
{
    /* data members */
    String name,email,pickupDate,phone;
    int rentalDays;
    CCar c;
    CTrailer t;
    CMotorbike b;
    CVehicle(){}
    /* constructor of class CVehicle */
    CVehicle(String n,String e,String p,String pp,int r,CCar c1,CTrailer t1,CMotorbike b1)
    {
        name=n; email=e; pickupDate=p; phone=pp; rentalDays=r; 
        c=c1; t=t1; b=b1;
    }
    /* to check if the entered phone number is valid or not */
    boolean IsValidPhoneNumber()
    {
        int count=phone.length();
        if(count==10)
        return true;
        else 
        return false;
    }
    /* to check if the entered email is valid or not */
    boolean IsValidEmailAddress()
    {
        int temp=0;
        for(int i=0;i<email.length();i++){
            if(email.charAt(i)=='@'){
                temp=email.length()-i;
                break;
            }
        }
        if(temp>=2 && temp<=6)
        return true;
        else 
        return false;
    }
    /* to check if the # of vehicles is valid or not */
    boolean IsValidVehicleNumber()
    {
        int numOfVehicles=c.vehicles+b.vehicles+t.vehicles;
        if(numOfVehicles<=3)
        return true;
        else
        return false;
    }
    /* to display all the details which is entered */
    void display()
    {
        System.out.println("Customer Name: "+name);
        System.out.println("Phone Number: "+phone);
        System.out.println("Email Address: "+email);
        System.out.println("Types of Vehicles rented: ");
        if(c.vehicles>0)
        c.display();
        if(t.vehicles>0)
        t.display();
        if(b.vehicles>0)
        b.display();
        System.out.println();
    }
}
class CCar 
{
    double mpg, pricePerDay, total;
    int vehicles,days;
    /* constructor of class CCar*/
    CCar(double m, double p)
    {
        mpg=m;
        pricePerDay=p;
    }
    /* to display all the details of vehicles*/
    void display()
    {
        total=pricePerDay*days;
        System.out.println("Car:");
        System.out.println("MPG: "+mpg);
        System.out.println("Price per day: "+pricePerDay);
        System.out.println("Number of cars rented: "+vehicles);
        System.out.println("Total price for cars: "+total+"\n");
    }
}
class CMotorbike 
{
    double mpg, pricePerDay, total;
    int vehicles,days;
    /* constructor of class CMotorbike */
    CMotorbike(double m, double p)
    {
        mpg=m;
        pricePerDay=p;
    }
    /* to display all the details of Motorbike*/
    void display()
    {
        total=pricePerDay*days;
        System.out.println("Motorbike:");
        System.out.println("MPG: "+mpg);
        System.out.println("Price per day: "+pricePerDay);
        System.out.println("Number of bikes rented: "+vehicles);
        System.out.println("Total price for bikes: "+total+"\n");
    }
}
class CTrailer 
{
    double mpg, pricePerDay, total;
    int vehicles, days;
    /* constructor of class CTrailer*/
    CTrailer(double m, double p){
        mpg=m;
        pricePerDay=p;
    }
    /* to display all the details of Trailer*/
    void display(){
        total=pricePerDay*days;
        System.out.println("Trailer:");
        System.out.println("MPG: "+mpg);
        System.out.println("Price per day: "+pricePerDay);
        System.out.println("Number of trailers rented: "+vehicles);
        System.out.println("Total price for Trailers: "+total+"\n");
    }
}
/* driver code */
class Main 
{
    public static void main(String args[])
    {
        CCar c1=new CCar(50,3000);
        CMotorbike b1=new CMotorbike(30,2000);
        CTrailer t1=new CTrailer(20,1000);
        CVehicle v=new CVehicle();
        v.c=c1;
        v.b=b1;
        v.t=t1;
        Scanner s=new Scanner(System.in);
        System.out.print("Enter customer name: ");
        v.name=s.next();
        System.out.print("Enter customer email: ");
        v.email=s.next();
        System.out.print("Enter pickup date: ");
        v.pickupDate=s.next();
        System.out.print("Enter phone number: ");
        v.phone=s.next();
        System.out.print("Enter number of rental days: ");
        v.rentalDays=s.nextInt();
        System.out.print("Enter number of cars to rent: ");
        v.c.vehicles=s.nextInt();
        v.c.days=v.rentalDays;
        System.out.print("Enter number of bikes to rent: ");
        v.b.vehicles=s.nextInt();
        v.b.days=v.rentalDays;
        System.out.print("Enter number of trailers to rent: ");
        v.t.vehicles=s.nextInt();
        v.t.days=v.rentalDays;
        System.out.println();
        v.display();

    }
}

Output

 

Also read, what is the problem with the following call to getHashTable?

 

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *