java求助 英文好的来,Define a class called PoliceDatabase with the following instance variables://这个Arraylist 不会写 CLASS 类vehicles,drivers,infractions 都写好了.vehicles – an ArrayList storing all vehicles in the databasedrivers
来源:学生作业帮助网 编辑:六六作业网 时间:2024/11/23 15:49:52
java求助 英文好的来,Define a class called PoliceDatabase with the following instance variables://这个Arraylist 不会写 CLASS 类vehicles,drivers,infractions 都写好了.vehicles – an ArrayList storing all vehicles in the databasedrivers
java求助 英文好的来,
Define a class called PoliceDatabase with the following instance variables:
//这个Arraylist 不会写 CLASS 类vehicles,drivers,infractions 都写好了.
vehicles – an ArrayList storing all vehicles in the database
drivers – an ArrayList storing all drivers (i.e., people who drive vehicles) in the database
infractions – an ArrayList storing all infractions that have ever been given to drivers
Write the following interesting methods:
//下面这些看不太懂,麻烦高手教下怎么写.
a zero-parameter constructor that initializes all the ArrayLists properly
registerDriver(Driver aDriver) which takes a Driver object as a parameter and then registers (i.e., remembers for later) the driver in the database.
registerVehicle(Vehicle aVehicle, String license) which takes a vehicle object as a parameter and the license ID of a driver who owns the vehicle and then registers the vehicle in the database by updating the vehicles list accordingly as well as storing the vehicle’s owner properly.
unregisterVehicle(String plate) which takes a vehicle’s plate as a parameter and then removes the vehicle from the database by updating the vehicles list accordingly.
reportStolen(String plate) records that the vehicle with the given plate number has been stolen.
changeOwner(String plate, String license) which updates the database by changing the owner information for the vehicle with the given plate to the driver with the given license ID.
java求助 英文好的来,Define a class called PoliceDatabase with the following instance variables://这个Arraylist 不会写 CLASS 类vehicles,drivers,infractions 都写好了.vehicles – an ArrayList storing all vehicles in the databasedrivers
大致写了下:
package com.lwx.temp;
public class Driver {
private String license;
public String getLicense() {
return license;
}
public void setLicense(String license) {
this.license = license;
}
}
/**
*
* 假设数据库中表如下:
*
* table_driver(license)
* table_vehicle(plate, license, status)
*
*
*/
package com.lwx.temp;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.ArrayList;
public class PoliceDatabase{
Connection conn;
ArrayList vehicles;
ArrayList drivers;
ArrayList infractions;
public PoliceDatabase(){
//zero-parameter constructor 无参数构造器
vehicles = new ArrayList();
drivers = new ArrayList();
infractions = new ArrayList();
// registerDriver 注册数据库驱动,假设是MySQL数据库
try{
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/数据库名称", "用户名", "密码");
//下面就可以进行初始化 vehicles/drivers/infractions 这3个ArrayList
// 代码就不详细写了...
}catch(Exception e){
e.printStackTrace();
}
}
public void registerDriver(Driver aDriver){
String sql = "insert into table_driver(license) values('" + aDriver.getLicense()+ "')";
executeSql(sql);
}
public void registerVehicle(Vehicle aVehicle, String license) {
String sql = "insert into table_vehicle(plate,license,status) values('" + aVehicle.getPlate()+ "','" + license + "','')";
executeSql(sql);
}
public void unregisterVehicle(String plate) {
String sql = "delete from table_vehicle where plate='" + plate + "'";
executeSql(sql);
}
public void reportStolen(String plate){
String sql = "update table_vehicle set status='STOLEN' where plate='" + plate + "'" ;
executeSql(sql);
}
public void changeOwner(String plate, String license){
String sql = "update table_vehicle set license='" + license + "' where plate='" + plate + "'" ;
executeSql(sql);
}
private void executeSql(String sql){
try{
Statement stmt = conn.createStatement();
stmt.execute(sql);
stmt.close();
}catch(Exception e){
e.printStackTrace();
}
}
}