*USE JAVA (THIS IS A GPA CALCULATOR AND SHOULD BE ABLE TO HANDLE MULTIPLE CLIENT
ID: 3728315 • Letter: #
Question
*USE JAVA (THIS IS A GPA CALCULATOR AND SHOULD BE ABLE TO HANDLE MULTIPLE CLIENTS AT THE SAME TIME)
For this project you will design a system to allow various users (unknown number connecting at any one time) to access a grade point calculation service on a "server". This service will allow users to either create an account or login to their previously created account. Then the user will need to complete the following items:
Add a Course Grade
Remove a Course Grade
Update a Course Grade
Get their GPA
This project must be completed in the Client Server architecture (you do not have to worry about persistance, but you do need a stateful server) Your program should handle many client requests at once.
As usual submit all source code files along with a ReadMe.txt to enable me to run your Server and how to run 1+ clients.
*USE JAVA (THIS IS A GPA CALCULATOR AND SHOULD BE ABLE TO HANDLE MULTIPLE CLIENTS AT THE SAME TIME)
Explanation / Answer
AS per your requirement the below one is solution please follow it
AppendableObjectInputStream.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates * and open the template in the editor. */
package clientServerGPA;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.OutputStream;
class AppendableObjectInputStream extends ObjectInputStream {
public AppendableObjectInputStream(InputStream out) throws IOException { super(out); }
@Override protected void readStreamHeader() throws IOException { }
}
AppendableObjectOutputStream.java
/*
* To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package clientServerGPA;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
public class AppendableObjectOutputStream extends ObjectOutputStream {
public AppendableObjectOutputStream(OutputStream out) throws IOException { super(out); }
@Override protected void writeStreamHeader() throws IOException { reset(); }
}
GradeClass.java
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package clientServerGPA;
import java.io.Serializable;
public class GradeClass implements Serializable {
private static final long serialVersionUID = 5950169519310163575L; String CourseName; //CourseName of the student char Grade; int operation; String UserName; String password;
public GradeClass() {
} public GradeClass(String UserName, String pwd) { this.UserName = UserName; this.password = pwd;
} public GradeClass(String CourseName, char Grade) { this.CourseName = CourseName; this.Grade = Grade; }
public int getOperation() { return operation; }
public void setOperation(int operation) { this.operation = operation; }
public String getUserName() { return UserName; }
public void setUserName(String UserName) { this.UserName = UserName; }
public String getPassword() { return password; }
public void setPassword(String password) { this.password = password; }
public String getCourseName() { return CourseName; }
public void setCourseName(String CourseName) { this.CourseName = CourseName; }
public char getGrade() { return Grade; }
public void setGrade(char Grade) { this.Grade = Grade; }
public int semesterGPA() { int A = 0, B = 0, C = 0, D = 0, F = 0; //assigning values based on 4 grades A=4, B=3, C=2, D=1 F=0 switch (this.Grade) { case 'A': return 4; case 'B': return 3; case 'C': return 2; case 'D': return 1; case 'F': return 0; } return 0; } }
MultiThreadServerClass.java
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package clientServerGPA;
import java.io.DataInputStream; import java.io.DataOutputStream; import java.net.Socket; import java.util.ArrayList;
public class MultiThreadServerClass extends Thread {
Socket mSocket; int clientNo;
private ArrayList<GradeClass> arrayList; private ArrayList<UserNamePassword> userNamePasswordList; private UserNamePassword ob;
MultiThreadServerClass(Socket inSocket, int count) { mSocket = inSocket; clientNo = count; }
public void run() { try { arrayList = new ArrayList<>(); userNamePasswordList = new ArrayList<>();
AppendableObjectInputStream inStream = new AppendableObjectInputStream(mSocket.getInputStream()); AppendableObjectOutputStream outStream = new AppendableObjectOutputStream(mSocket.getOutputStream()); DataInputStream mDatainput = new DataInputStream(mSocket.getInputStream()); DataOutputStream mDataOutput = new DataOutputStream(mSocket.getOutputStream()); String inputMessage="", outPutMessage=""; ob = new UserNamePassword(); ob = (UserNamePassword) inStream.readObject(); boolean isExist = false; arrayList = (ArrayList<GradeClass>) inStream.readObject(); outStream = new AppendableObjectOutputStream(mSocket.getOutputStream()); outStream.writeObject(arrayList); outStream.flush(); //inStream.close(); //outStream.close(); //mSocket.close(); } catch (Exception ex) { System.out.println(ex); } finally { System.out.println("Client -" + clientNo + " exit "); } } }
User1Class.java
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package clientServerGPA;
import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.net.Socket; import java.net.SocketException; import java.util.ArrayList; import java.util.Scanner;
/** * * @author Patel Heggere */ public class User1Class {
private Socket socket = null; private AppendableObjectInputStream inputStream = null; private AppendableObjectOutputStream outputStream = null; private boolean isConnected = false;
private ArrayList<GradeClass> arrayList;
public User1Class() {
}
public void mConnect() throws ClassNotFoundException {
arrayList = new ArrayList<>(); while (!isConnected) { try { socket = new Socket("127.0.0.1", 8818); System.out.println("Connected"); isConnected = true; Scanner scanner = new Scanner(System.in); outputStream = new AppendableObjectOutputStream(socket.getOutputStream()); inputStream = new AppendableObjectInputStream(socket.getInputStream()); DataInputStream mDatainput = new DataInputStream(socket.getInputStream());
DataOutputStream mDataOutput = new DataOutputStream(socket.getOutputStream()); System.out.println("Enter UserName:"); String username = scanner.next(); System.out.println("Enter password:"); String pwd = scanner.next(); UserNamePassword ob1 = new UserNamePassword(username, pwd); //arrayList.add(ob1); outputStream.writeObject(ob1); for (;;) { System.out.println("1. Add 2. Remove 3. Update 4. exit"); int ch = scanner.nextInt(); switch (ch) { case 1: System.out.println("Enter course Name"); String cName = scanner.next(); System.out.println("Enter course Grade"); char gr = scanner.next().charAt(0); GradeClass gc = new GradeClass(cName, gr); gc.setOperation(1); arrayList.add(gc); outputStream = new AppendableObjectOutputStream(socket.getOutputStream()); outputStream.writeObject(arrayList); outputStream.flush(); inputStream = new AppendableObjectInputStream(socket.getInputStream()); arrayList = (ArrayList<GradeClass>) inputStream.readObject(); break; case 2: System.out.println("Enter course Name"); cName = scanner.next(); for (int i = 0; i < arrayList.size(); i++) { if (arrayList.get(i).CourseName.equalsIgnoreCase(cName)) { arrayList.remove(i); } } outputStream = new AppendableObjectOutputStream(socket.getOutputStream()); outputStream.writeObject(arrayList); inputStream = new AppendableObjectInputStream(socket.getInputStream()); arrayList = (ArrayList<GradeClass>) inputStream.readObject(); System.out.println("Removed Succesfully"); break; case 3: System.out.println("Enter course Name"); cName = scanner.next(); System.out.println("Enter New Grade"); gr = scanner.next().charAt(0); for (int i = 0; i < arrayList.size(); i++) {
if (arrayList.get(i).CourseName.equalsIgnoreCase(cName)) { arrayList.get(i).setCourseName(cName); arrayList.get(i).setGrade(gr); } } outputStream = new AppendableObjectOutputStream(socket.getOutputStream()); outputStream.writeObject(arrayList); inputStream = new AppendableObjectInputStream(socket.getInputStream()); arrayList = (ArrayList<GradeClass>) inputStream.readObject(); System.out.println("Updated Succesfully"); break; case 4: System.out.println("Enter course Name"); cName = scanner.next(); for (int i = 0; i < arrayList.size(); i++) { if (arrayList.get(i).CourseName.equalsIgnoreCase(cName)) { System.out.println("GPA:" + arrayList.get(i).semesterGPA()); } } outputStream = new AppendableObjectOutputStream(socket.getOutputStream()); outputStream.writeObject(arrayList); inputStream = new AppendableObjectInputStream(socket.getInputStream()); arrayList = (ArrayList<GradeClass>) inputStream.readObject(); System.out.println("GPA"); break; default: break; }
} } catch (SocketException se) { se.printStackTrace(); // System.exit(0); } catch (IOException e) { e.printStackTrace(); } } }
public static void main(String[] args) throws ClassNotFoundException { User1Class client = new User1Class(); client.mConnect(); } }
serverClass.java
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package clientServerGPA;
import java.net.ServerSocket; import java.net.Socket;
public class ServerClass { public ServerClass() {
}
public static void main(String[] args) throws Exception { try{ ServerSocket server=new ServerSocket(8818); int count=0; System.out.println("Server Started ...."); while(true){ count++; Socket serverClient=server.accept(); //server accept client connection request System.out.println(" >> " + "Client No:" + count + " started!"); MultiThreadServerClass sct = new MultiThreadServerClass(serverClient,count); //send the request to a separate thread sct.start(); } }catch(Exception e){ System.out.println(e); } } }
UserNamePassword.java
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package clientServerGPA;
import java.io.Serializable;
public class UserNamePassword implements Serializable{ String UserName; String password;
public UserNamePassword() { } public UserNamePassword(String UserName, String password) { this.UserName = UserName; this.password = password; }
public String getUserName() { return UserName; }
public void setUserName(String UserName) { this.UserName = UserName; }
public String getPassword() { return password; }
public void setPassword(String password) { this.password = password; } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.