为何输入正确优惠码仍弹出错误Toast?求解决方案
优惠码匹配功能问题排查与修复方案
问题原因
- 大小写匹配不一致:用户输入的优惠码转成了小写(
enteredPromotionCode = promotion.trim().toLowerCase()),但与staff传入的staffPromotionCode比较时,未对后者做小写转换,导致大小写不同时匹配失败。 - staff优惠码匹配后无后续逻辑:当输入的优惠码匹配staff设置的代码时,仅将
promotionCodeMatched设为true,但未执行房间预订核心逻辑,用户看不到成功提示,误以为匹配失败。 - 页面初始化错误:填充房间信息时,错误地将房间名设置到优惠码输入框(
etPromotion.setText(room.getRoomName())),应填充房间对应的优惠码字段。 - 冗余变量定义:在
onClick方法内重复定义了promotionCodeMatched数组,虽不影响逻辑,但易造成混淆。
修复方案
1. 统一大小写匹配规则
对staffPromotionCode也做小写转换,确保比较时大小写一致:
String staffCode = staffPromotionCode != null ? staffPromotionCode.trim().toLowerCase() : ""; if (enteredPromotionCode.equals(staffCode)) { promotionCodeMatched[0] = true; // 执行预订逻辑 }
2. 补全staff优惠码匹配后的预订流程
在匹配staff优惠码的分支中,直接执行房间预订操作,与Firestore匹配成功的逻辑保持一致。
3. 修正页面初始化的错误赋值
将优惠码输入框的赋值改为房间的优惠码:
etPromotion.setText(room.getPromotion());
4. 移除冗余变量定义
删除onClick方法内重复定义的final boolean[] promotionCodeMatched = {false};,直接使用全局变量。
完整修复后的代码
public class BookRoomActivity extends AppCompatActivity { String staffPromotionCode; EditText etRoomName, etprice, etDate, etCapacity, etPromotion; Button btnBook; FirebaseFirestore db; final boolean[] promotionCodeMatched = {false}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_book_room); // Retrieve promotion code set by staff from intent Intent z = getIntent(); staffPromotionCode = z.getStringExtra("Promotion"); btnBook = findViewById(R.id.btnBook); etRoomName = findViewById(R.id.etRoomName); etprice = findViewById(R.id.etPrice); etDate = findViewById(R.id.etDate); etCapacity = findViewById(R.id.etCapacity); etPromotion = findViewById(R.id.etPromotion); db = FirebaseFirestore.getInstance(); Intent i = getIntent(); Room room = (Room) i.getSerializableExtra("updatedRoom"); if (room != null) { etRoomName.setText(room.getRoomName()); // 修复:设置正确的优惠码而非房间名 etPromotion.setText(room.getPromotion()); etCapacity.setText(String.valueOf(room.getCapacity())); etDate.setText(String.valueOf(room.getDate())); etprice.setText(String.valueOf(room.getPrice())); } else { Toast.makeText(BookRoomActivity.this, "Error: Room details not found", Toast.LENGTH_SHORT).show(); } btnBook.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // Retrieve details from EditText fields String roomName = etRoomName.getText().toString().trim(); double price = Double.parseDouble(etprice.getText().toString().trim()); int date = Integer.parseInt(etDate.getText().toString()); int capacity = Integer.parseInt(etCapacity.getText().toString().trim()); String promotion = etPromotion.getText().toString().trim(); // 重置匹配状态 promotionCodeMatched[0] = false; // Retrieve user-entered promotion code String enteredPromotionCode = promotion.trim().toLowerCase(); // 统一大小写比较staff优惠码 String staffCode = staffPromotionCode != null ? staffPromotionCode.trim().toLowerCase() : ""; if (enteredPromotionCode.equals(staffCode)) { promotionCodeMatched[0] = true; // 直接执行预订逻辑 proceedWithBooking(roomName, date, capacity, promotion, price); } else { // Fetch the room details from Firestore db.collection("rooms") .whereEqualTo("roomName", roomName) .get() .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() { @Override public void onComplete(@NonNull Task<QuerySnapshot> task) { if (task.isSuccessful()) { for (QueryDocumentSnapshot document : task.getResult()) { Room room = document.toObject(Room.class); String firestorePromotionCode = room.getPromotion().trim().toLowerCase(); if (enteredPromotionCode.equals(firestorePromotionCode)) { promotionCodeMatched[0] = true; break; } } if (!promotionCodeMatched[0]) { Toast.makeText(BookRoomActivity.this, "Incorrect promotion code applied!", Toast.LENGTH_LONG).show(); } else { // Promotion code matched, proceed with booking proceedWithBooking(roomName, date, capacity, promotion, price); } } else { // Handle query failure Toast.makeText(BookRoomActivity.this, "Failed to retrieve promotion code: " + task.getException(), Toast.LENGTH_SHORT).show(); } } }); } } }); } // 提取预订逻辑为单独方法,避免重复代码 private void proceedWithBooking(String roomName, int date, int capacity, String promotion, double price) { // Create Room object Room newRoom = new Room(0, date, capacity, roomName, promotion, price); // Store Room object in Firestore CollectionReference roomsCollection = db.collection("rooms"); roomsCollection.add(newRoom) .addOnSuccessListener(new OnSuccessListener<DocumentReference>() { @Override public void onSuccess(DocumentReference documentReference) { // Room booking successful Toast.makeText(BookRoomActivity.this, "Room booked successfully!", Toast.LENGTH_LONG).show(); finish(); // Finish the activity and go back to the previous activity } }) .addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { // Room booking failed Toast.makeText(BookRoomActivity.this, "Failed to book room: " + e.getMessage(), Toast.LENGTH_SHORT).show(); } }); } }
内容的提问来源于stack exchange,提问作者Abzx
相关产品推荐
相关产品推荐

