题目

建立一个公交管理系统,帮助公交管理部门监督管理公交日常运营情况,根据公交线路、车辆情况、工作人员、乘客的信息。
从构建基本类开始入手。整个系统分为四个模块,分别为汽车管理模块(Bus)、线路模块(Route)、员工管理模块(Employee)、
乘客模块(Passenger)需要实现四个模块的增删改查功能。

  1. 创建汽车管理模块
package chapter02;

import java.util.ArrayList;
import java.util.Scanner;

public class Bus {
    private String type;       //定义车辆类型
    private String engineNumber;     //定义车辆编号
    private int seatingCapacity;       //定义载客量
    private int route;     //  定义线路

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getEngineNumber() {
        return engineNumber;
    }

    public void setEngineNumber(String engineNumber) {
        this.engineNumber = engineNumber;
    }

    public int getSeatingCapacity() {
        return seatingCapacity;
    }

    public void setSeatingCapacity(int seatingCapacity) {
        this.seatingCapacity = seatingCapacity;
    }

    public int getRoute() {
        return route;
    }

    public void setRoute(int route) {
        this.route = route;
    }

    //无参构造
    Bus() {
    }

    public void catInfo() {
        ArrayList<Bus> array = new ArrayList<>();
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("====================车辆管理系统==================");
            System.out.println("1.添加车辆信息\n2.查看车辆信息\n3.修改车辆信息\n4.删除车辆信息\n5。退出");
            System.out.println("================================================");
            System.out.println("请输入你的操作:");
            int number = sc.nextInt();
            if (number < 0 || number > 5) {
                System.out.println("输入错误,请重新输入!");
            } else if (number == 1) {
                addBusInfo(array);
            } else if (number == 2) {
                showInfo(array);
            } else if (number == 3) {
                replaceInfo(array);
            } else if (number == 4) {
                removeInfo(array);
            } else if (number == 5) {
                break;
            }
        }
    }

    //添加车辆信息
    public void addBusInfo(ArrayList<Bus> array) {
        Bus bus = new Bus();
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入车辆编号:");
        String engineNumber = sc.nextLine();
        boolean flag = dieInfo(array, engineNumber);
        if (flag) {
            System.out.println("车辆编号已存在!");
        } else {
            System.out.println("请输入车辆类型:");
            String type = sc.nextLine();
            System.out.println("请输入车辆载客量:");
            int seatingCapacity = sc.nextInt();
            System.out.println("请输入车辆运营路线:");
            int route = sc.nextInt();
            //创建对象
            bus.setType(type);
            bus.setEngineNumber(engineNumber);
            bus.setSeatingCapacity(seatingCapacity);
            bus.setRoute(route);
            array.add(bus);
            System.out.println("添加成功!");
        }

    }

    //查看车辆信息
    public void showInfo(ArrayList<Bus> array) {
        System.out.println("车辆编号\t\t车辆类型\t\t 车辆载客量\t\t车辆运营路线");
        for (Bus bus : array) {
            System.out.println(bus.getEngineNumber() + "\t\t\t " + bus.getType() + "\t\t\t" + bus.getSeatingCapacity() + "\t\t\t\t" + bus.getRoute());
        }
    }

    //修改车辆信息
    public void replaceInfo(ArrayList<Bus> array) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入需要修改的车辆编号:");
        String engineNumber = sc.nextLine();
        boolean flag = dieInfo(array, engineNumber);
        if (flag) {
            for (int i = 0; i < array.size(); i++) {
                Bus bus = array.get(i);
                if (bus.getEngineNumber().equals(engineNumber)) {
                    System.out.println("车辆编号\t\t车辆类型\t  车辆载客量\t\t车辆运营路线");
                    System.out.println(bus.getEngineNumber() + "\t\t\t " + bus.getType() + "\t\t\t" + bus.getSeatingCapacity() + "\t\t\t\t" + bus.getRoute());
                    System.out.println("请输入修改后的车辆类型:");
                    String type = sc.nextLine();
                    System.out.println("请输入修改后的车辆载客量:");
                    int seatingCapacity = sc.nextInt();
                    System.out.println("请输入修改后的车辆运营路线:");
                    int route = sc.nextInt();
                    bus.setType(type);
                    bus.setSeatingCapacity(seatingCapacity);
                    bus.setRoute(route);
                    System.out.println("修改成功!");
                }
            }
        } else {
            System.out.println("车辆编号不存在!");
        }
    }

    //删除车辆信息
    public void removeInfo(ArrayList<Bus> array) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入车辆编号:");
        String engineNumber = sc.nextLine();
        boolean flag = dieInfo(array, engineNumber);
        if (flag) {
            for (int i = 0; i < array.size(); i++) {
                Bus bus = array.get(i);
                if (bus.getEngineNumber().equals(engineNumber)) {
                    array.remove(bus);
                }
            }
            System.out.println("删除成功");
        } else {
            System.out.println("车辆编号不存在!");
        }
    }

    // 判断车辆信息不存在
    boolean dieInfo(ArrayList<Bus> array, String engineNumber) {
        boolean flag = false;
        for (Bus bus : array) {
            if (bus.getEngineNumber().equals(engineNumber)) {
                flag = true;
                break;
            }
        }
        return flag;
    }
}
  1. 创建线路模块
package chapter02;

import java.util.ArrayList;
import java.util.Scanner;

public class Route {
    private int numberStops;
    private String StartStation;
    private String destination;
    private String runningTime;
    private int index = 0;

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public Route() {
    }

    public int getNumberStops() {
        return numberStops;
    }

    public void setNumberStops(int numberStops) {
        this.numberStops = numberStops;
    }

    public String getStartStation() {
        return StartStation;
    }

    public void setStartStation(String startStation) {
        StartStation = startStation;
    }

    public String getDestination() {
        return destination;
    }

    public void setDestination(String destination) {
        this.destination = destination;
    }

    public String getRunningTime() {
        return runningTime;
    }

    public void setRunningTime(String runningTime) {
        this.runningTime = runningTime;
    }

    //路线操作
    public void catInfo() {
        ArrayList<Route> array = new ArrayList<>();
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("====================车辆管理系统==================");
            System.out.println("1.添加线路信息\n2.查看线路信息\n3.修改线路信息\n4.删除线路信息\n5。退出");
            System.out.println("================================================");
            System.out.println("请输入你的操作:");
            int number = sc.nextInt();
            if (number < 0 || number > 5) {
                System.out.println("输入错误,请重新输入!");
            } else if (number == 1) {
                addRouteInfo(array);
            } else if (number == 2) {
                showRouteInfo(array);
            } else if (number == 3) {
                replaceRouteInfo(array);
            } else if (number == 4) {
                removeRouteInfo(array);
            } else if (number == 5) {
                break;
            }
        }
    }

    //添加线路信息
    public void addRouteInfo(ArrayList<Route> array) {
        Route route = new Route();
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入线路运营总站数:");
        int NumberStops = sc.nextInt();
        System.out.println("请输入车辆起始站:");
        String StartStation = sc.next();
        System.out.println("请输入终点站:");
        String Destination = sc.next();
        System.out.println("请输入车辆运行时间:");
        String RunningTime = sc.next();
        route.setNumberStops(NumberStops);
        route.setStartStation(StartStation);
        route.setDestination(Destination);
        route.setRunningTime(RunningTime);
        index = index + 1;
        route.setIndex(index);
        array.add(route);
        System.out.println("添加成功!");
    }

    //查看线路信息
    public void showRouteInfo(ArrayList<Route> array) {
        if (array.size() == 0) {
            System.out.println("还没有线路信息,请先添加信息!");
        } else {
            System.out.println("编号\t\t线路运营总站数\t\t起始站\t\t终点站\t\t\t运营时间");
            for (Route route : array) {
                System.out.println(route.getIndex() + "\t\t\t" + route.getNumberStops() + "\t\t\t\t " + route.getStartStation() + "\t\t\t " + route.getDestination() + "\t\t\t\t  " + route.getRunningTime());
            }
        }
    }

    //修改车辆信息
    public void replaceRouteInfo(ArrayList<Route> array) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入车辆编号:");
        int index = sc.nextInt();
        boolean flag = Userinfo(array, index);
        if (flag) {
            for (int i = 0; i < array.size(); i++) {
                Route route = array.get(i);
                if (route.getIndex() == index) {
                    System.out.println("请输入修改后的线路站数:");
                    int NumberStops = sc.nextInt();
                    System.out.println("请输入修改后起始站:");
                    String StartStation = sc.next();
                    System.out.println("请输入终点站:");
                    String Destination = sc.next();
                    System.out.println("请输入车辆运行时间:");
                    String RunningTime = sc.next();
                    route.setNumberStops(NumberStops);
                    route.setStartStation(StartStation);
                    route.setDestination(Destination);
                    route.setRunningTime(RunningTime);
                    System.out.println("修改成功!");
                }
            }
        } else {
            System.out.println("你输入的线路编号不存在!");
        }

    }

    //删除线路信息
    public void removeRouteInfo(ArrayList<Route> array) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入需要删除的线路编号:");
        int index = sc.nextInt();
        boolean flag = Userinfo(array, index);
        if (flag) {
            for (int i = 0; i < array.size(); i++) {
                Route route = array.get(i);
                if (route.getIndex() == index) {
                    array.remove(route);
                }
            }
            System.out.println("删除成功!");
        } else {
            System.out.println("你输入的线路编号不存在!");
        }
    }
    
    //判断线路是否存在
    public boolean Userinfo(ArrayList<Route> array, int index) {
        boolean flag = false;
        for (Route route : array) {
            if (route.getIndex() == index) {
                flag = true;
                break;
            }
        }
        return flag;
    }
}
  1. 创建员工管理模块
package chapter02;

import java.beans.beancontext.BeanContext;
import java.util.ArrayList;
import java.util.Scanner;

public class Employee {
    private String name;
    private int ID;
    private int age;
    private double salary;
    private String department;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getID() {
        return ID;
    }

    public void setID(int ID) {
        this.ID = ID;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    public String getDepartment() {
        return department;
    }

    public void setDepartment(String department) {
        this.department = department;
    }

    Employee() {
    }

    void catinfo() {
        ArrayList<Employee> array = new ArrayList<>();
        System.out.println("=======================");
        System.out.println("1.添加员工信息\n2.查看员工信息\n3.修改员工信息\n4.删除员工信息\n5.退出");
        System.out.println("=======================");
        while (true) {
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入要进行的操作:");
            int number = sc.nextInt();
            if (number == 1) {
                addEmployeeInfo(array);
            } else if (number == 2) {
                showEmployeeInfo(array);
            } else if (number == 3) {
                alterEmployeeInfo(array);
            } else if (number == 4) {
                removeEmployeeInfo(array);
            } else if (number == 5) {
                break;
            } else {
                System.out.println("输入错误,请重新输入!");
            }
        }
    }

    //添加员工信息
    void addEmployeeInfo(ArrayList<Employee> array) {
        //输入信息
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入姓名:");
        String name = sc.nextLine();
        System.out.println("请输入员工编号:");
        int ID = sc.nextInt();
        boolean flag = dieInfo(array, ID);
        if (flag) {
            System.out.println("编号已存在!");
        } else {
            System.out.println("请输入年龄:");
            int age = sc.nextInt();
            System.out.println("请输入薪水:");
            double salary = sc.nextDouble();
            System.out.println("请输入部门:");
            String department = sc.next();
            // 创建对象
            Employee employee = new Employee();
            employee.setName(name);
            employee.setID(ID);
            employee.setAge(age);
            employee.setSalary(salary);
            employee.setDepartment(department);
            array.add(employee);
            System.out.println("添加成功!");
        }
    }


    //查看员工信息
    void showEmployeeInfo(ArrayList<Employee> array) {
        System.out.println("编号\t\t姓名\t\t年龄\t\t薪资\t\t部门");
        for (Employee employee : array) {
            System.out.println(employee.getID() + "\t\t" + employee.getName() + "\t\t" + employee.getAge() + "\t\t" + employee.getSalary() + "\t\t" + employee.getDepartment());
        }
    }

    //修改员工信息
    void alterEmployeeInfo(ArrayList<Employee> array) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入需要修改的编号:");
        int ID = sc.nextInt();
        boolean flag = dieInfo(array, ID);
        if (flag) {
            for (Employee employee : array) {
                if (employee.getID() == ID) {
                    System.out.println("请输入姓名:");
                    String name = sc.next();
                    System.out.println("请输入年龄:");
                    int age = sc.nextInt();
                    System.out.println("请输入薪水:");
                    double salary = sc.nextDouble();
                    System.out.println("请输入部门");
                    String department = sc.next();
                    employee.setName(name);
                    employee.setID(ID);
                    employee.setAge(age);
                    employee.setSalary(salary);
                    employee.setDepartment(department);
                    System.out.println("修改成功!");
                }
            }
        } else {
            System.out.println("没有该编号!");
        }
    }

    //删除员工信息
    void removeEmployeeInfo(ArrayList<Employee> array) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入需要删除的编号:");
        int ID = sc.nextInt();
        boolean flag = dieInfo(array, ID);
        if (flag) {
            for (int i = 0; i < array.size(); i++) {
                Employee employee = array.get(i);
                if (employee.getID() == ID) {
                    array.remove(employee);
                }
            }
            System.out.println("删除成功!");
        } else {
            System.out.println("没有该编号!");
        }
    }

    // 判断员工信息是否存在
    boolean dieInfo(ArrayList<Employee> array, int ID) {
        boolean flag = false;
        for (Employee employee : array) {
            if (employee.getID() == ID) {
                flag = true;
                break;
            }
        }
        return flag;
    }
}
  1. 乘客管理模块:
package chapter02;

import java.util.ArrayList;
import java.util.Scanner;

public class Passenger {
    private String name;
    private char sex;
    private int age;
    private String message;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public char getSex() {
        return sex;
    }

    public void setSex(char sex) {
        this.sex = sex;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    void catinfo() {
        System.out.println("=======================");
        System.out.println("1.添加乘客信息\n2.查看乘客信息\n3.修改乘客信息\n4.删除乘客信息\n5.退出");
        System.out.println("=======================");
        ArrayList<Passenger> array = new ArrayList<>();
        while (true) {
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入要进行的操作:");
            int number = sc.nextInt();
            if (number == 1) {
                addPassengerInfo(array);
            } else if (number == 2) {
                showPassengerInfo(array);
            } else if (number == 3) {
                alterPassengerInfo(array);
            } else if (number == 4) {
                removePassengerInfo(array);
            } else if (number == 5) {
                break;
            } else {
                System.out.println("输入错误,请重新输入!");
            }
        }
    }

    //添加乘客信息
    void addPassengerInfo(ArrayList<Passenger> array) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入乘客姓名:");
        String name = sc.next();
        boolean flag = dieInfo(array, name);
        if (flag) {
            System.out.println("乘客已存在!");
        } else {
            System.out.println("请输入乘客性别:");
            String s = sc.next();
            char sex = s.charAt(0);
            System.out.println("请输入乘客年龄:");
            int age = sc.nextInt();
            System.out.println("请输入乘客的信息:");
            String message = sc.next();
            //创建对象
            Passenger passenger = new Passenger();
            passenger.setName(name);
            passenger.setSex(sex);
            passenger.setAge(age);
            passenger.setMessage(message);
            array.add(passenger);
            System.out.println("添加成功 ");
        }
    }

    //查看乘客信息
    void showPassengerInfo(ArrayList<Passenger> array) {
        System.out.println("姓名\t\t年龄\t\t性别\t\t乘客信息");
        for (Passenger passenger : array) {
            System.out.println(passenger.getName() + "\t\t" + passenger.getAge() + "\t\t" + passenger.getSex() + "\t\t" + passenger.getMessage());
        }
    }

    //修改乘客信息
    void alterPassengerInfo(ArrayList<Passenger> array) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入乘客姓名:");
        String name = sc.nextLine();
        boolean flag = dieInfo(array, name);
        if (flag) {
            for (Passenger passenger : array) {
                if (passenger.getName().equals(name)) {
                    System.out.println("请输入乘客性别:");
                    String s = sc.next();
                    char sex = s.charAt(0);
                    System.out.println("请输入乘客年龄:");
                    int age = sc.nextInt();
                    System.out.println("请输入乘客的信息:");
                    String message = sc.next();
                    //创建对象
                    passenger.setName(name);
                    passenger.setSex(sex);
                    passenger.setAge(age);
                    passenger.setMessage(message);
                }
            }
        }
    }

    //删除乘客信息
    void removePassengerInfo(ArrayList<Passenger> array) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入乘客姓名:");
        String name = sc.nextLine();
        boolean flag = dieInfo(array, name);
        if (flag) {
            for (int i = 0; i < array.size(); i++) {
                Passenger passenger = array.get(i);
                if (passenger.getName().equals(name)) {
                    array.remove(passenger);
                }
            }
        }
    }

    // 判断员工信息是否存在
    boolean dieInfo(ArrayList<Passenger> array, String name) {
        boolean flag = false;
        for (Passenger passenger : array) {
            if (passenger.getName().equals(name)) {
                flag = true;
                break;
            }
        }
        return flag;
    }

}
  1. 主程序调用模块:
package chapter02;
import java.util.Scanner;

public class PublicCar {
    public static void main(String[] args) {
        Bus bus = new Bus();
        Route route = new Route();
        Employee employee = new Employee();
        Passenger passenger = new Passenger();
        while (true) {
            System.out.println("======================");
            System.out.println("1.车辆管理\n2.线路管理\n3.员工管理\n4.乘客管理\n5.退出");
            System.out.println("======================");
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入要进行的操作:");
            int number = sc.nextInt();
            if (number == 1) {
                bus.catInfo();
            } else if (number == 2) {
                route.catInfo();
            } else if (number == 3) {
                employee.catinfo();
            } else if (number == 4) {
                passenger.catinfo();
            } else if (number == 5) {
                System.out.println("操作结束!");
                break;
            }
        }
    }
}

参考文献

java语言程序设计面向对象的设计思想与实践