根据客户的需求任务。
• 创建车辆类,其中包括车辆ID(整数)作为车辆编号和停车位(整数)作为停车场编号。实现getId()和getLot()方法来返回车辆ID和停车位。
创建停车场类,包括已停放的车辆、车辆停放数据,并实现以下功能:
HashMap<Integer, Vehicle> parkedVehicle = new HashMap<>();
HashMap<Integer, Integer> vehicle_lot_data = new HashMap<>();
• parkVehicle(车辆,停车位)将车辆停放在停车位上,检查有效性并检查同一辆车是否被允许停放在多个停车场。如果该停车位已被其他人预定,则返回“停车位已被占用”,如果我们尝试为同一辆车预订多个停车位,则返回“车辆已存在”。 • isLotBooked(int lot)返回布尔值 - 它检查已经预订的停车位。对于空停车位返回False,对于已预订的停车位返回True。 • isVehicleExists(int vehicleId)返回布尔值 - 它检查车辆是否已经存在。如果车辆存在则返回True,否则返回False。
步骤3:(Database.java)
创建一个名为Database的类来存储停车场的数据。
创建本地SQLite数据库(parking_database.db)• 创建DATABASE_NAME变量并存储parking_database.db。• 实现onCreate、onUpgrade以创建数据库和删除数据库。• ParkVehicle(Vehicle)- 将停放的车辆保存到数据库中。• getParkedVehicle(),getParkedVehicle(int vehicleId)- 获取所有停放的车辆返回(Cursor)。• getAllBookedLots()- 获取所有已预订的停车场返回(Cursor)
使用以下架构设计表格:
CREATE TABLE parking_lots (id integer PRIMARY KEY,
lot_number INTEGER,
vehicle_number INTEGER UNIQUE);
• 实现ParkVehicleNow()函数,从视图中获取用户输入数据,并将数据存储在vehicle_id_field_txt和vehicle_lot_number_field_txt变量中,然后调用内部park(int vehicle_id_field_txt,int vehicle_lot_number_field_txt)函数。• park(int vehicle_id_field_txt,int vehicle_lot_number_field_txt)-将使用ParkingLot类并使用ParkingLot实例pl停放车辆,并使用Databaseclass实例db将该车辆保存到数据库中。•如果无法停车,则会发出Toast“车辆已停放,请选择其他停车场或车辆”,否则将弹出“车辆已停放”消息。•还要记得对“停车场已满”和“车辆已经存在”进行验证,然后不要停车。
但我不明白第二步如何在ParkingLot中实现哈希映射。
根据场景,我尝试过,但无法在任何地方使用哈希映射。
public class ParkingLot {
HashMap<Integer, Vehicle> parkedVehicle = new HashMap<>();
HashMap<Integer, Integer> vehicle_lot_data = new HashMap<>();
public boolean isLotBooked(int lot, Context context) {
Database db = new Database(context);
return db.isAlreadyLotsExists(String.valueOf(lot));
}
public boolean isVehicleExists(int vehicleId, Context context) {
Database db = new Database(context);
return db.isAlreadyVehicleExists(String.valueOf(vehicleId));
}
}
public Vehicle(){
}
public Vehicle(int primaryId,int id, int lot){
this.id = id;
this.lot = lot;
this.primaryId = primaryId;
}
public Vehicle(int id, int lot){
this.id = id;
this.lot = lot;
}
public int id;
public int getPrimaryId() {
return primaryId;
}
public void setPrimaryId(int primaryId) {
this.primaryId = primaryId;
}
public int primaryId;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getLot() {
return lot;
}
public void setLot(int lot) {
this.lot = lot;
}
public int lot;
}
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Database db = new Database(this);
db.ParkVehicle(new Vehicle(1000,1));
db.ParkVehicle(new Vehicle(2000,2));
// Reading all contacts
Log.e("Reading: ", "Reading all contacts..");
List<Vehicle> contacts = db.getParkedVehicle(); //all parked vehicle
List<String> bookedSlots = db.getAllBookedLots(); //all booked slots
for (Vehicle cn : contacts) {
String log = "Id: " + cn.getPrimaryId() + " ,Vehicle: " + cn.getId() + " ,Lot: " +
cn.getLot();
// Writing Contacts to log
Log.e("ContactsAllVehicles: ", log);
}
for (String cn : bookedSlots) {
// Writing Contacts to log
Log.e("ContactsSlots: ", cn);
}
Vehicle vehicle= db.getParkedVehicle(1);
Log.e("ContactsLots",""+vehicle.getId());
//checking if lots already exisitng..
boolean alreadyExist=db.isAlreadyLotsExists("0");
Log.e("contactsslotsTest",""+alreadyExist);
}
public class Database extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "vehicles";
private static final String KEY_ID = "id";
private static final String LOT_NUMBER = "lot_number";
private static final String VEHICLE_NUMBER = "vehicle_number";
private static final String PARKING_LOTS = "parking_lots"; //table name
public Database(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
//3rd argument to be passed is CursorFactory instance
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_CONTACTS_TABLE = "CREATE TABLE " + PARKING_LOTS + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + LOT_NUMBER + " INTEGER,"
+ VEHICLE_NUMBER + " INTEGER UNIQUE" + ")";
db.execSQL(CREATE_CONTACTS_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + PARKING_LOTS);
// Create tables again
onCreate(db);
}
void ParkVehicle(Vehicle contact) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(LOT_NUMBER, contact.getLot()); // Lot number
values.put(VEHICLE_NUMBER, contact.getId()); // Vehicle number
// Inserting Row
db.insert(PARKING_LOTS, null, values);
//2nd argument is String containing nullColumnHack
db.close(); // Closing database connection
}
Vehicle getParkedVehicle(int id) { //single vehicle
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(PARKING_LOTS, new String[]{KEY_ID,
LOT_NUMBER, VEHICLE_NUMBER}, KEY_ID + "=?",
new String[]{String.valueOf(id)}, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
Vehicle contact = new Vehicle(Integer.parseInt(cursor.getString(0)),
Integer.parseInt(cursor.getString(1)), Integer.parseInt(cursor.getString(2)));
// return contact
return contact;
}
public List<Vehicle> getParkedVehicle() {
List<Vehicle> contactList = new ArrayList<Vehicle>();
// Select All Query
String selectQuery = "SELECT * FROM " + PARKING_LOTS;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Vehicle contact = new Vehicle();
contact.setPrimaryId(Integer.parseInt(cursor.getString(0)));
contact.setLot(Integer.parseInt(cursor.getString(1)));
contact.setId(Integer.parseInt(cursor.getString(2)));
// Adding contact to list
contactList.add(contact);
} while (cursor.moveToNext());
}
// return contact list
return contactList;
}
public List<String> getAllBookedLots(){
List<String> contactList = new ArrayList<>();
// Select All Query
String selectQuery = "SELECT * FROM " + PARKING_LOTS;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
cursor.moveToFirst();
// looping through all rows and adding to list
if (cursor!=null && cursor.moveToFirst()) {
while(cursor.isAfterLast() == false){
contactList.add(cursor.getString(cursor.getColumnIndex(LOT_NUMBER)));
Log.d("contactslotnumber",contactList.toString());
cursor.moveToNext();
}}
return contactList;
}
public boolean isAlreadyLotsExists(String name){
SQLiteDatabase sqLiteDatabase = this.getReadableDatabase();
String query="SELECT * FROM "+PARKING_LOTS+" WHERE "+LOT_NUMBER+" = "+name;
Cursor cursor = sqLiteDatabase.rawQuery(query,null);
if(cursor.getCount()>0)
return true;
else
return false;
}
public boolean isAlreadyVehicleExists(String name){
SQLiteDatabase sqLiteDatabase = this.getReadableDatabase();
String query="SELECT * FROM "+PARKING_LOTS+" WHERE "+VEHICLE_NUMBER+" = "+name;
Cursor cursor = sqLiteDatabase.rawQuery(query,null);
if(cursor.getCount()>0)
return true;
else
return false;
}
// return contact list
public int updateContact(Vehicle contact) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(LOT_NUMBER, contact.getLot());
values.put(VEHICLE_NUMBER, contact.getId());
// updating row
return db.update(PARKING_LOTS, values, KEY_ID + " = ?",
new String[]{String.valueOf(contact.getPrimaryId())});
}
// Deleting single contact
public void deleteContact(Vehicle contact) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(PARKING_LOTS, KEY_ID + " = ?",
new String[]{String.valueOf(contact.getPrimaryId())});
db.close();
}
// Getting contacts Count
public int getContactsCount() {
String countQuery = "SELECT * FROM " + PARKING_LOTS;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
cursor.close();
// return count
return cursor.getCount();
}
}
这是我完成的内容,但有些步骤缺失。例如:在ParkingLot类中使用hashmap来使用parkVehicle方法的Step 2,以及在MainActivity中的Step 4也是如此。