Java Object-Oriented Comprehensive Application—Car Query System

1. Complete an automobile query system for object-oriented encapsulation, inheritance, polymorphism, abstraction, interface, and exception knowledge.

Requirement description: There are 3 types of cars: cars, buses, trucks, among which the number of seats in the car is 4, the number of seats in the bus is 53, and the number of seats in the truck is 2, which requires packaging, inheritance, and abstraction To complete the definition of the vehicle.

The vehicle information can be modified. The truck can transport goods but the load cannot exceed 12 tons. Use custom exceptions to handle errors. Cars and buses do not have this function and require the use of interfaces.

Car

package com.song.test;

public abstract class Car {
    
    
	private String name;
	private String color;
	public String getName() {
    
    
		return name;
	}
	public void setName(String name) {
    
    
		this.name = name;
	}
	public String getColor() {
    
    
		return color;
	}
	public void setColor(String color) {
    
    
		this.color = color;
	}
	public Car(String name, String color) {
    
    
		super();
		this.name = name;
		this.color = color;
	}
	public abstract String seatNum();
}

Then

package com.song.test;

public class Sedan extends Car {
    
    

	public Sedan(String name, String color) {
    
    
		super(name, color);
	}

	@Override
	public String seatNum() {
    
    
		// TODO Auto-generated method stub
		return "4座";
	}
	
}

Bus

package com.song.test;

public class Bus extends Car {
    
    

	public Bus(String name, String color) {
    
    
		super(name, color);
		// TODO Auto-generated constructor stub
	}

	@Override
	public String seatNum() {
    
    
		// TODO Auto-generated method stub
		return "53座";
	}

}

Truck

package com.song.test;

public class Truck extends Car implements Container {
    
    
	
	private int weight;

	public Truck(String name, String color,int weight) {
    
    
		super(name, color);
		this.weight = weight;
		// TODO Auto-generated constructor stub
	}

	@Override
	public String seatNum() {
    
    
		// TODO Auto-generated method stub
		return "2座";
	}

	@Override
	public int getweight() {
    
    
		// TODO Auto-generated method stub
		return this.weight;
	}

}

Container

package com.song.test;

public interface Container {
    
    
	public int getweight();
}

CarException

package com.song.test;

public class CarException extends Exception {
    
    
	public CarException(String error) {
    
    
		super(error);
	}
}

Test

package com.song.test;

import java.util.Scanner;

public class Test {
    
    
	private static Scanner scanner;
	private static Sedan sedan;
	private static Bus bus;
	private static Truck truck;
	private static Car[] cars;
	static {
    
    
		scanner = new Scanner(System.in);
		sedan = new Sedan("小轿车","黑色");
		bus = new Bus("大巴车","绿色");
		truck = new Truck("卡车","蓝色",2);
		cars = new Car[3];
		cars[0] = sedan;
		cars[1] = bus;
		cars[2] = truck;
	}
	
	public void showCars() {
    
    
		System.out.println("欢迎使用本汽车管理系统");
		System.out.println("车辆名称\t\t车辆颜色\t\t座位数\t\t载重量");
		for(Car car:cars) {
    
    
			if(car instanceof Truck) {
    
    
				Truck truck = (Truck)car;
				System.out.println(car.getName()+"\t\t"+car.getColor()+"\t\t"+car.seatNum()+"\t\t"+truck.getweight());
			}else {
    
    
				System.out.println(car.getName()+"\t\t"+car.getColor()+"\t\t"+car.seatNum()+"\t\t不能拉货");
			}
		}
		System.out.println("1.小轿车\t2.大巴车\t3.卡车");
		System.out.print("请选择要修改的车辆:");
		int num = scanner.nextInt();
		switch(num) {
    
    
			case 1:
				update("sedan");
				break;
			case 2:
				update("bus");
				break;
			case 3:
				update("truck");
				break;
			default:
				System.out.println("车辆不存在!");
				break;
		}
	}
	
	public void update(String type) {
    
    
		String name = null;
		String color = null;
		if(type.equals("sedan")) {
    
    
			System.out.print("输入车辆名称");
			name = scanner.next();
			System.out.print("输入车辆颜色");
			color = scanner.next();
			Sedan sedan = new Sedan(name,color);
			cars[0] = sedan;
		}
		if(type.equals("bus")) {
    
    
			System.out.print("输入车辆名称");
			name = scanner.next();
			System.out.print("输入车辆颜色");
			color = scanner.next();
			Bus bus = new Bus(name,color);
			cars[1] = bus;
		}
		if(type.equals("truck")) {
    
    
			System.out.print("输入车辆名称");
			name = scanner.next();
			System.out.print("输入车辆颜色");
			color = scanner.next();
			System.out.print("输入载重量");
			int weight = scanner.nextInt();
			if(weight > 12) {
    
    
				CarException carException = new CarException("卡车的载重量不能超过12吨");
				try {
    
    
					throw carException;
				} catch (CarException e) {
    
    
					// TODO Auto-generated catch block
					e.printStackTrace();
					return;
				}
			}
			Truck truck = new Truck(name,color,weight);
			cars[2] = truck;
		}
		showCars();
	}
	
	public static void main(String[] args) {
    
    
		Test test = new Test();
		test.showCars();
	}
}

The high-level part of object-oriented, including Object class, wrapper class, interface and exception. The Object class is the parent class of all Java classes. It defines the basic information of the Java system. It is passed to each class of Java through inheritance, and the entire Java system has strong flexibility through method rewriting and polymorphism.

Wrapping classes are a set of classes that Java provides encapsulation for basic data types, through which we can convert basic data types into objects, which is very important in object-oriented programming.

Interface is an extension of abstract classes and an important way to achieve polymorphism in Java. It can reduce the coupling of the program and make the program more flexible and changeable. Interfaces are equivalent to parts, and we can assemble and integrate these parts freely.

Exception is a mechanism for handling errors in Java. It is also based on object-oriented thinking. Errors are abstracted into objects and then processed. What needs attention here is the use of several keywords related to exceptions, try, catch, finally, throw, throws.

Guess you like

Origin blog.csdn.net/qq_40220309/article/details/105687299