综合案例
程序员文章站
2022-07-04 16:26:07
...
综合案例(Comprehensive case)
封装、继承、多态、抽象、接口、异常完成一个汽车管理系统。
需求描述:共有 3 种类型的汽车:小轿车、大巴车、卡车,其中小轿车的座位数是 4 座,大巴车座位数是 53 座,卡车座位数是 2 座,要求使用封装、继承、抽象来完成车辆的定义。
可以对车辆信息进行修改,卡车可以运货但是载重量不能超过 12 吨,使用自定义异常来处理错误,小轿车和大巴车没有此功能,要求使用接口来实现。
Car类
package com.mie.car;
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 setNumber();
}
小轿车Sedan类
package com.mie.car;
public class Sedan extends Car{
public Sedan(String name, String color) {
super(name, color);
// TODO Auto-generated constructor stub
}
@Override
public String setNumber() {
// TODO Auto-generated method stub
return "4座";
}
}
大巴Bus类
package com.mie.car;
public class Bus extends Car{
public Bus(String name, String color) {
super(name, color);
// TODO Auto-generated constructor stub
}
@Override
public String setNumber() {
// TODO Auto-generated method stub
return "53座";
}
}
卡车Truck类
package com.mie.car;
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 setNumber() {
// TODO Auto-generated method stub
return "2座";
}
@Override
public int getWeight() {
// TODO Auto-generated method stub
return this.weight;
}
}
接口Container
package com.mie.car;
public interface Container {
public int getWeight();
}
自定义异常类CarException
package com.mie.car;
@SuppressWarnings("serial")
public class CarException extends Exception{
public CarException(String error) {
super(error);
}
}
测试Test类
package com.mie.car;
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.setNumber()+"\t\t"+truck.getWeight());
}
else {
System.out.println(car.getName()+"\t\t"+car.getColor()+"\t\t"+car.setNumber()+"\t\t不能载货");
}
}
System.out.println("1.小轿车\t2.大巴\t3.卡车");
System.out.println("请选择要修改的车辆:");
int num=scanner.nextInt();
switch (num) {
case 1:
update("sedan");
break;
case 2:
update("sedan");
break;
case 3:
update("sedan");
break;
default:
System.out.println("车辆不存在");
break;
}
}
public void update(String type) {
String name=null;
String color=null;
if (type.equals("sedan")) {
System.out.println("请输入车辆的名称");
name =scanner.next();
System.out.println("请输入车辆颜色");
color =scanner.next();
Sedan sedan=new Sedan(name, color);
cars[0]=sedan;
}
if (type.equals("bus")) {
System.out.println("请输入车辆的名称");
name =scanner.next();
System.out.println("请输入车辆颜色");
color =scanner.next();
Sedan sedan=new Sedan(name, color);
cars[1]=sedan;
}
if (type.equals("truck")) {
System.out.println("请输入车辆的名称");
name =scanner.next();
System.out.println("请输入车辆颜色");
color =scanner.next();
Sedan sedan=new Sedan(name, color);
System.out.println("请输入载重量");
int weight =scanner.nextInt();
if (weight>12) {
CarException carException=new CarException("卡车的载重量不能超过12吨");
try {
throw carException;
} catch (CarException e) {
e.printStackTrace();
return;
}}
Truck truck=new Truck(name, color, weight);
cars[2]=sedan;
}
showCars();
}
public static void main(String[] args) {
Test test=new Test();
test.showCars();
}
}