java求助 英文好的来,

问题描述:

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.
1个回答 分类:综合 2014-10-14

问题解答:

我来补答
大致写了下:
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();
}
}

}
 
 
展开全文阅读
剩余:2000
上一页:第一个解释一下
下一页:例二,求解