Write a rental information for a leasing company in polymorphic form

Source: Internet
Author: User

Parent class: Motovehicle

Public abstract class Motovehicle {

String No;

String brand;

String color;

String mileage;

int days;

public abstract float calcrent (int days); }

Sub-class 1:car

Public final class Car extends Motovehicle {

String type;

@Override

public float calcrent (int days) {

float rent = 0;

if (Type.equals ("Buick Business Class GL8")) {

Rent = 600*days;

}else if (type.equals ("BMW 550i")) {

Rent = 500*days;

}else if (type.equals ("Buick Boulevard")) {

Rent = 300*days;

}

return rent;

}

Public Car () {

Super ();

}

Public Car (String type) {

Super ();

This.type = type;

}

}

Sub-class 2:bus

Public final class Bus extends Motovehicle {

int seatcount;

@Override

public float calcrent (int days) {

float rent = 0;

if (seatcount<=16) {

Rent = 800*days;

}else{

Rent = 1500*days;

}

return rent;

}

Public Bus () {

Super ();

}

Public Bus (int seatcount) {

Super ();

This.seatcount = Seatcount;

}

}

Test class: Motovehicletext

Import Java.util.Scanner;

public class Motovehicletext {

public static void Main (string[] args) {

String kind;

float rent = 0;

Scanner input = new Scanner (system.in);

System.out.println ("Please enter the type of rental car");

Kind = Input.next ();

if (Kind.equals ("Car")) {

Car A = new car ();

System.out.println ("Please enter the rental time:");

A.days = Input.nextint ();

System.out.println ("Please enter the model number of the car rental:");

A.type = Input.next ();

Rent = a.calcrent (a.days);

}else if (kind.equals ("Bus")) {

Bus B = new bus ();

System.out.println ("Please enter the rental time:");

B.days = Input.nextint ();

System.out.println ("Please enter the number of seats for the rental car:");

B.seatcount = Input.nextint ();

Rent = b.calcrent (b.days);

}

System.out.println ("Your rental fee is +rent");

Input.close ();

}

}

Write a rental information for a leasing company in polymorphic form

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.