How to solve the problem of #Java# polymorphic taxi?

noun in need
        Car rental company, car, sedan, bus, Buick, BMW, Jinbei, Jinlong, X6, 550i, GL8, boulevard, number of seats, daily rent, discount, license plate number (Beijing NY28588, Beijing CNY3284, Beijing NT37465, Beijing NT96968 , Beijing 6566754, Beijing 6566754, Beijing 9696996, Beijing 8696998)
Classes and class attributes
Find out the classes and class attributes that need to be used based on known nouns
Car category: license plate number, car brand, daily rent
Passenger cars: license plate number, car brand, daily rent, number of seats
Car category: license plate number, car brand, daily rent, car model
    Automobile business category:
    Car rental management class (test class)
analyze:
Both passenger car and sedan belong to car, car is the parent class of passenger car and car, and car and passenger car are subclasses of car.
Passenger cars and cars both have the same attributes (license plate number, car brand, daily rent); then the passenger car class and the car class can inherit the attributes of the car class.
In addition to the same attributes, it also has private attributes. The passenger car class has the number of seats, and the sedan class has the model of the car.
There should also be a car business class to complete the car rental function.
The car rental management class is used to test the car rental system.
verb in need
Calculating rent, leasing, and program entry are the major methods required in the class. Complete the optimized design code:

Create .Java files: MotoVehicle class (car class), Car class (sedan car class), Bus class (passenger car class), MotoOperation class (automobile business class), Test class (test class)
stage division
Phase 1: Create and complete the car class
Phase 2: Create and finalize the car class
Phase 3: Create and complete the bus class
Phase 4: Create and complete the car business class
Phase 5: Create and complete the test class
Phase 1: Create and complete the car class
Parent MotoVehicle

//car
public abstract class MotoVehicle {
    //License plate number Brand Daily rent
    private String vehicleId;
    private String brand;
    private int perRent;
    //No parameter constructor
    public MotoVehicle() {
    }
    //parameterized constructor
    public MotoVehicle(String vehicleId, String brand, int perRent) {
        this.vehicleId = vehicleId;
        this.brand = brand;
        this.perRent = perRent;
    }
//get and set methods
    public String getVehicleId() {
        return vehicleId;
    }
    public void setVehicleId(String vehicleId) {
        this.vehicleId = vehicleId;
    }
    public String getBrand() {
        return brand;
    }
    public void setBrand(String brand) {
        this.brand = brand;
    }
    public int getPerRent() {
        return perRent;
    }
    public void setPerRent(int perRent) {
        this.perRent = perRent;
    }
    //Calculate rent (abstract method)
    public abstract double calcRent(int days);
}

Phase 2: Create and complete the car class

The subclass Car inherits the parent class MotoVehicle

//Sedan
public class Car extends MotoVehicle {
 
	// model
	private String type;
 
	// No parameter constructor
	public Car() {
		super();
	}
 
	// parameterized constructor
	public Car(String vehicleId, String brand, int perRent, String type) {
		super(vehicleId, brand, perRent);
		this.type = type;
	}
 
	public String getType() {
		return type;
	}
 
	public void setType(String type) {
		this.type = type;
	}
 
	@Override
	public double calcRent(int days) {
		double price = this.getPerRent() * days;
		if (days > 7 && days <= 30) {
			price = price * 0.9;
		} else if (days > 30 && days <= 150) {
			price = price * 0.8;
		} else if (days > 150) {
			price = price * 0.7;
		}
		return price;
	}
 
}

The third stage: Create and complete the bus class

The subclass Bus inherits the parent class MotoVehicle

//Sedan
public class Car extends MotoVehicle {
 
	// model
	private String type;
 
	// No parameter constructor
	public Car() {
		super();
	}
 
	// parameterized constructor
	public Car(String vehicleId, String brand, int perRent, String type) {
		super(vehicleId, brand, perRent);
		this.type = type;
	}
 
	public String getType() {
		return type;
	}
 
	public void setType(String type) {
		this.type = type;
	}
 
	@Override
	public double calcRent(int days) {
		double price = this.getPerRent() * days;
		if (days > 7 && days <= 30) {
			price = price * 0.9;
		} else if (days > 30 && days <= 150) {
			price = price * 0.8;
		} else if (days > 150) {
			price = price * 0.7;
		}
		return price;
	}
 
}

Phase 4: Create and complete the car business class
MotoOperation class

import java.util.ArrayList;
import java.util.Scanner;
 
//Automobile business
public class MotoOperation {
	Scanner in = new Scanner(System.in);
	// Create an array of cars
	Car[] car = new Car[4];
	// Create a bus class
	Bus[] bus = new Bus[4];
 
	public void inti() {
		// Save the car information to the car class array
		car[0] = new Car("Beijing NY28588", "BMW", 800, "X6");
		car[1] = new Car("Beijing CNY28588", "BMW", 600, "550i");
		car[2] = new Car("Beijing NT37465", "buick", 300, "Boulevard");
		car[3] = new Car("Beijing NT96968", "buick", 600, "GL8");
		// Save bus information to bus class array
		bus[0] = new Bus("Beijing 6566754", "gold cup", 800, 16);
		bus[1] = new Bus("Beijing 9696996", "gold cup", 1500, 34);
		bus[2] = new Bus("Beijing 8696997", "golden dragon", 800, 16);
		bus[3] = new Bus("Beijing 8696998", "golden dragon", 1500, 34);
	}
 
	// car rental method
	public void start() {
		System.out.println("**********Welcome to the leasing company**********");
		System.out.println("1-car\t2-bus");
		System.out.println("Please select the type of car you would like to rent:");
		int type = in.nextInt();
		// If you enter a number other than 1 and 2, re-enter
		while (type < 1 || type > 2) {
			System.out.println("Input error, please re-select the type of car you want to rent:");
			type = in.nextInt();
		}
		if (type == 1) {// The user chooses to rent a car
			// How to rent a car
			car();
		} else if (type == 2) {// User selects rental car
			// How to rent a car
			bus();
		}
	}
 
	// car rental method
	public void car() {
		String brand = "";
		String type = "";
		System.out.println("Please select the car brand you want to rent: 1-buick\t2-BMW");
		int chooseBrand = in.nextInt();
		// If you enter a number other than 1 and 2, re-enter
		while (chooseBrand < 1 || chooseBrand > 2) {
			System.out.println("Input error, please re-select the car brand you want to rent:");
			chooseBrand = in.nextInt();
		}
 
		// User chooses Buick
		if (chooseBrand == 1) {
			// The sedan brand is assigned to Buick
			brand = "buick";
			System.out.println("Please select the car model you want to rent: 1-Boulevard\t2-GL8");
			int chooseType = in.nextInt();
			// If you enter a number other than 1 and 2, re-enter
			while (chooseType < 1 || chooseType > 2) {
				System.out.println("Input error, please re-select the car model you want to rent:");
				chooseType = in.nextInt();
			}
			// If the user chooses Buick, assign the car model as Boulevard; if the user chooses Buick, assign the car model as GL8;
			type = chooseType == 1 ? "Boulevard" : "GL8";
		}
 
		// User chooses BMW
		else if (chooseBrand == 2) {
			// Car brand assigned to BMW
			brand = "BMW";
			System.out.println("Please select the type of car you want to rent: 1-X6\t2-50i");
			int chooseType = in.nextInt();
			// If you enter a number other than 1 and 2, re-enter
			while (chooseType < 1 || chooseType > 2) {
				System.out.println("Input error, please re-select the car model you want to rent:");
				chooseType = in.nextInt();
			}
			// If the user selects BMW, assign the car model as X6; if the user selects BMW, assign the car model as 550i;
			type = chooseType == 1 ? "X6" : "550i";
		}
 
		// Output pickup information
		for (int i = 0; i < car.length; i++) {
			if (car[i].getBrand().equals(brand)
					&& car[i].getType().equals(type)) {
				// Calculate car rental
				System.out.println("Please enter the number of days you would like to rent:");
				int days = in.nextInt();
				double sumPrice = calcRent(days, type);
				System.out.println("If the car rental is successful, please pick up the car according to the license plate number as follows:" + car[i].getVehicleId());
				System.out.println("You need to pay:" + sumPrice + "Yuan");
			}
		}
 
	}
 
	// How to calculate car rental
	public double calcRent(int days, String type) {
		double price = 0;
		for (int i = 0; i < car.length; i++) {
			if (car[i].getType().equals(type)) {
				price = car[i].getPerRent() * days;
				if (days > 7 && days <= 30) {
					price = price * 0.9;
				} else if (days > 30 && days <= 150) {
					price = price * 0.8;
				} else if (days > 150) {
					price = price * 0.7;
				}
			}
		}
		return price;
	}
 
	// Bus rental method
	public void bus() {
		String brand = "";
		int count = 0;
		System.out.println("Please select the bus brand you want to rent: 1-gold cup\t2-golden dragon");
		int chooseBrand = in.nextInt();
		// If the user enters a number other than 1 and 2, re-enter
		while (chooseBrand < 1 && chooseBrand > 2) {
			System.out.println("Input error, please re-select the car brand you want to rent:");
			chooseBrand = in.nextInt();
		}
		// If the user chooses Jinbei, assign the bus brand as Jinbei; if the user chooses Jinlong, assign the bus brand as Jinlong;
		brand = chooseBrand == 1 ? "gold cup" : "golden dragon";
 
		System.out.println("Please select the number of bus seats you want to rent: 1-16 seat\t2-34 seat");
		int chooseCount = in.nextInt();
		// If the user enters a number other than 1 and 2, re-enter
		while (chooseCount < 1 && chooseCount > 2) {
			System.out.println("Input error, please re-select the number of bus seats you want to rent: 1-16 seat\t2-34 seat");
			chooseCount = in.nextInt();
		}
		// If the user selects 16 seats, assign the number of seats to the bus as 16; if the user selects 34 seats, assign the number of seats to the bus as 34;
		count = chooseCount == 1 ? 16 : 34;
 
		// Output pickup information
		for (int i = 0; i < bus.length; i++) {
			if (bus[i].getBrand().equals(brand) && bus[i].getSeatCount() == count) {
				// Calculate car rental
				System.out.println("Please enter the number of days you would like to rent:");
				int days = in.nextInt();
				double sumPrice = calcRent(days, count);
				System.out.println("If the car rental is successful, please pick up the car according to the license plate number as follows:" + bus[i].getVehicleId());
				System.out.println("You need to pay:" + sumPrice + "Yuan");
			}
		}
	}
 
	// The method of calculating the rental car
	public double calcRent(int days, int count) {
		double price = 0;
		for (int i = 0; i < bus.length; i++) {
			if (bus[i].getSeatCount() == count) {
				price = bus[i].getPerRent() * days;
				if (days > 7 && days <= 30) {
					price = price * 0.9;
				} else if (days > 30 && days <= 150) {
					price = price * 0.8;
				} else if (days > 150) {
					price = price * 0.7;
				}
			}
		}
		return price;
	}
}

Phase 5: Create and complete the test class

Test class

//Car rental management class: test class
public class Text {
 
	public static void main(String[] args) {
		//Instantiate the car business class
		MotoOperation moto = new MotoOperation();
		//Call the initialization car information method in the car business class
		moto.inti();
		//Call the car rental method in the car business class
		moto.start();
	}
}

Tags: Java jvm programming language

Posted by ashwin on Mon, 20 Mar 2023 00:42:13 +1030