给出客户需求任务。
•创建包含车辆ID(整数)作为车辆的车辆类 号和地块(整数)作为停车场号。实现getId()和 getLot()返回车辆ID和批次。
创建包含parkedVehicle,vehicle_lot_data并实现以下内容的ParkingLot类
HashMap<Integer, Vehicle> parkedVehicle = new HashMap<>();
HashMap<Integer, Integer> vehicle_lot_data = new HashMap<>();
•parkVehicle(车辆,地段)将车辆停在该地段,检查是否有效 停车,并检查同一车辆不允许在 多批。如果已经预订了很多物品,则返回“拍品已提取” 如果我们要预订的话,有人和“车辆已经在场” 同一辆车有多个批次。 •isLotBooked(int lot)返回 布尔值-检查已预订的批次。返回假 空的则为True,而预订的则为True。 •isVehicleExists(int vehicleId)return boolean-检查已经存在的车辆。 如果车辆存在则返回True,否则返回False 车辆。
步骤3:(Database.java)
创建一个类别数据库来存储停车场数据。
创建本地SQLite数据库(parking_database.db)•创建 DATABASE_NAME变量并存储parking_database.db。 • 实行 用于数据库创建和数据库删除的onCreate,onUpgrade。 •ParkVehicle(Vehicle)-将停放的车辆保存到数据库。 •getParkedVehicle(),getParkedVehicle(int vehicleId)-获取所有停放的 车辆返回(Cursor)•getAllBookedLots()-获取所有预订 停车场返回(光标)
具有以下架构的设计表
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(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。 •如果您不能停车 然后制作“已停放的吐司面包”,请选择其他批次或 车辆”,否则将“已停放车辆”消息。 •另外,不要 忘记对“已采取拍品”和“车辆”执行验证 已经存在”,则不要停放车辆。
但是我没有得到如何在ParkingLot中实现哈希图的步骤2。
我很累的是,根据情况,在这里我无法在任何地方使用哈希图。
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();
}
}
这是我所做的,但是需求中缺少一些步骤。比如说:步骤2我不确定在哪里使用哈希图在ParkingLot类中使用parkVehicle方法,在MainActivity中也类似地使用步骤4。
答案 0 :(得分:0)
可能是在缓存数据库数据的上下文中给出了哈希图。当您写入与车辆/批次相关的数据时,您将哈希图用作缓存(利用回写或直写策略,无论哪种方式都符合您的要求或需求),并且在读取数据时,始终会从哈希图读取它们,即缓存< / p>
回想一下,回写意味着您将新数据写入缓存,但不写入DB(稍后将数据写入DB),而直写意味着每当您写入新数据时,都会将其写入两个缓存和数据库