I have a class called BookRoomActivity
:
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.getRoomName());
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();
final boolean[] promotionCodeMatched = {false}; // Declare and initialize promotionCodeMatched
// Retrieve promotion code set by staff from intent
Intent z = getIntent();
staffPromotionCode = z.getStringExtra("Promotion");
// Retrieve user-entered promotion code
String enteredPromotionCode = promotion.trim().toLowerCase();
// Check if the entered promotion code matches the staff-set promotion code
if (enteredPromotionCode.equals(staffPromotionCode)) {
promotionCodeMatched[0] = true;
} 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
// 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();
}
});
}
} else {
// Handle query failure
Toast.makeText(BookRoomActivity.this, "Failed to retrieve promotion code: " + task.getException(), Toast.LENGTH_SHORT).show();
}
}
});
}
}
});;}}"
I am encountering an issue while implementing a promotion code feature. My objective is that when a user enters a promotion code, and it matches the code stored in my Firebase database, the app should return to the previous activity and display a ‘Room Booked successfully’ toast message. However, even when I enter the correct promotion code, the app consistently displays a ‘Incorrect promotion code applied!’ toast message instead. Could you help me understand why this is happening and suggest a solution?
So i have tried to check my firestore fields, and also googling and attempting to use similar solutions, but yet nothing seems to have worked and this was the best that i could find
2
Answers
Ok so I have managed to resolve the issue here.So what i would need to remove is
and then replace the entire bottom with
so what the error that i had here was that my field class should be writtten as promotion and that afterwards all i needed was to do is to set the success or failure toast messages
The problem you have in your code comes from this line:
Instead of setting the promotion code you are setting the room name.
You should be doing: