Commit 2613b598 authored by drnull03's avatar drnull03

Finished the code might add some changes

parents
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.quiz</groupId>
<artifactId>QuizRMI</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>QuizRMI</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<source>17</source> <!-- or 11, or 8 -->
<target>17</target> <!-- same as source -->
</configuration>
</plugin>
</plugins>
</build>
</project>
package com.quiz.client;
import com.quiz.common.*;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.util.Scanner;
public class QuizClient extends UnicastRemoteObject implements QuizCallback {
private QuizService service;
private String studentName;
protected QuizClient(String name) throws Exception {
super();
this.studentName = name;
Registry registry = LocateRegistry.getRegistry("localhost", 1099);
service = (QuizService) registry.lookup("QuizService");
service.registerCallback(studentName, this);
}
@Override
public void updateLeaderboard(String leaderboard) {
System.out.println("\n--- Leaderboard Update ---\n" + leaderboard);
}
public void start() throws Exception {
Scanner sc = new Scanner(System.in);
while (true) {
Question q = service.getQuestion(studentName);
System.out.println("Question: " + q.getQuestion());
System.out.print("Your Answer: ");
String ans = sc.nextLine();
String feedback = service.submitAnswer(studentName, ans);
System.out.println(feedback);
}
}
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = sc.nextLine();
QuizClient client = new QuizClient(name);
client.start();
}
}
package com.quiz.common;
import java.io.Serializable;
public class Question implements Serializable {
private String question;
private String answer;
public Question(String question, String answer) {
this.question = question;
this.answer = answer;
}
public String getQuestion() { return question; }
public String getAnswer() { return answer; }
}
package com.quiz.common;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface QuizCallback extends Remote {
void updateLeaderboard(String leaderboard) throws RemoteException;
}
package com.quiz.common;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface QuizService extends Remote {
Question getQuestion(String studentName) throws RemoteException;
String submitAnswer(String studentName, String answer) throws RemoteException;
void registerCallback(String studentName, QuizCallback callback) throws RemoteException;
}
package com.quiz.common;
import java.io.Serializable;
public class StudentScore implements Serializable {
private String name;
private int score;
public StudentScore(String name) {
this.name = name;
this.score = 0;
}
public String getName() { return name; }
public int getScore() { return score; }
public void incrementScore() { score++; }
}
package com.quiz.server;
import com.quiz.common.*;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.*;
public class QuizServer extends UnicastRemoteObject implements QuizService {
private Map<String, StudentScore> studentScores = new HashMap<>();
private List<Question> questions = new ArrayList<>();
private Map<String, Question> currentQuestions = new HashMap<>();
private Map<String, QuizCallback> callbacks = new HashMap<>();
protected QuizServer() throws RemoteException {
super();
questions.add(new Question("What is 2 + 2?", "4"));
questions.add(new Question("Capital of France?", "Paris"));
questions.add(new Question("Java is ___?", "Programming language"));
}
@Override
public Question getQuestion(String studentName) throws RemoteException {
studentScores.putIfAbsent(studentName, new StudentScore(studentName));
Question q = questions.get(new Random().nextInt(questions.size()));
currentQuestions.put(studentName, q);
return q;
}
@Override
public String submitAnswer(String studentName, String answer) throws RemoteException {
Question q = currentQuestions.get(studentName);
if (q == null) return "No question assigned!";
String feedback;
if (q.getAnswer().equalsIgnoreCase(answer.trim())) {
studentScores.get(studentName).incrementScore();
feedback = "Correct! Score: " + studentScores.get(studentName).getScore();
} else {
feedback = "Wrong! Score: " + studentScores.get(studentName).getScore();
}
updateLeaderboard();
return feedback;
}
@Override
public void registerCallback(String studentName, QuizCallback callback) throws RemoteException {
callbacks.put(studentName, callback);
}
private void updateLeaderboard() {
List<StudentScore> top = new ArrayList<>(studentScores.values());
top.sort((a, b) -> b.getScore() - a.getScore());
StringBuilder leaderboard = new StringBuilder("Leaderboard:\n");
for (int i = 0; i < Math.min(3, top.size()); i++) {
leaderboard.append((i+1) + ". " + top.get(i).getName() + " - " + top.get(i).getScore() + "\n");
}
for (QuizCallback cb : callbacks.values()) {
try { cb.updateLeaderboard(leaderboard.toString()); }
catch (Exception ignored) {}
}
}
public static void main(String[] args) {
try {
QuizServer server = new QuizServer();
Registry registry = LocateRegistry.createRegistry(1099);
registry.rebind("QuizService", server);
System.out.println("Quiz Server is running...");
synchronized (server) {
server.wait();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
package com.quiz;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Unit test for simple App.
*/
public class AppTest
extends TestCase
{
/**
* Create the test case
*
* @param testName name of the test case
*/
public AppTest( String testName )
{
super( testName );
}
/**
* @return the suite of tests being tested
*/
public static Test suite()
{
return new TestSuite( AppTest.class );
}
/**
* Rigourous Test :-)
*/
public void testApp()
{
assertTrue( true );
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment