for some reason my code isn't breaking even though I write Quit multiple time the loop continues and It keeps asking me for messages even though it shouldn't so what the solution please? for some reason my code isn't breaking even though I write Quit multiple time the loop continues and It keeps asking me for messages even though it shouldn't so what the solution please?
package project;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.Updates;
import org.bson.Document;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
import java.util.Scanner;
public class ConnectToMongo {
public static void main( String args[] ) {
// Creating a Mongo client
MongoClient mongo = new MongoClient( "localhost" , 27017 );
// Creating Credentials
MongoCredential credential;
credential = MongoCredential.createCredential("sampleUser", "myDb","password".toCharArray());
System.out.println("Connected to the database successfully");
//Accessing the database
MongoDatabase database = mongo.getDatabase("myDb");
//we drop it la nma7e klshi b albo to prevent errors
database.drop();
database = mongo.getDatabase("myDb");
//Creating a collection
database.createCollection("Messages");
System.out.println("Collection messages created successfully");
database.createCollection("Users");
System.out.println("Collection users created successfully");
// printing the names of the collections that are created in the database
for (String name : database.listCollectionNames()) {
System.out.println(name);
}
//create an object to access the collection
MongoCollection<Document> Users = database.getCollection("Users");
MongoCollection<Document> messages = database.getCollection("Messages");
user user1 = new user("User1","123");
user user2 = new user("User2","456");
user1.insertToCollection(Users, 1);
user2.insertToCollection(Users, 2);
//Terminal system
Scanner scan1 = new Scanner(System.in);
String m1,m2;
int s=1;
while(s==1) {
System.out.println();
//date taba3 l msg b m1
m1 = createMessage(user1,user2,scan1,messages);
System.out.println();
//btjib l msg mn l database w btaamela print w bthot read true
sendMessage(m1,messages);
m2 =createMessage(user2,user1,scan1,messages);
System.out.println();
sendMessage(m2,messages);
if(m1=="Quit") {
break; //Here is the Problem!!!!!!!!!!!!!!!!!!!!!!!!
}
}
scan1.close();
Users.drop();
messages.drop();
}
public static String createMessage(user Sender, user Receiver, Scanner scan, MongoCollection<Document> messages) {
System.out.print(Sender.getUsername() +": ");
String messageContent = "";
wait(2000);
messageContent = messageContent +scan.nextLine();
Messages message = new Messages(Sender, Receiver, messageContent);
message.insertToCollection(messages);
return message.getSentTime().toString();
}
//hon aam njib l msg mn l database lahata nhoto bl terminal
public static void sendMessage(String dateOfMessage, MongoCollection<Document> messages) {
boolean read = true;
Document m = messages.findOneAndUpdate(Filters.eq("ID", dateOfMessage), Updates.set("Read Status", read));
String content = m.getString("Message");
String Sender = m.getString("Sender");
String Receiver = m.getString("Receiver");
System.out.println("Dear "+Receiver+", you have been sent a message from "+Sender+" stating the following: \n"+ content);
}
// to wait for the user to type
public static void wait(int ms){
try
{
Thread.sleep(ms);
}
catch(InterruptedException ex)
{
Thread.currentThread().interrupt();
}
}
}
for some reason my code isn't breaking even though I write Quit multiple time the loop continues and It keeps asking me for messages even though it shouldn't so what the solution please? for some reason my code isn't breaking even though I write Quit multiple time the loop continues and It keeps asking me for messages even though it shouldn't so what the solution please?