Professional Documents
Culture Documents
Answer Booklet: M251-M257: Object Oriented Programming Using Java
Answer Booklet: M251-M257: Object Oriented Programming Using Java
Answer Booklet: M251-M257: Object Oriented Programming Using Java
Answer Booklet
Personal Information
Student Name Mohammed Yahya Khalil Aboamara
Student ID 192911
Registration Information
Branch Kuwait
Section Number Assessment type Final-THE
Academic Year 2020/2021 Semester Spring
General Note
1 of 13
Mohammed Yahya Aboamara_192911
Tracing:
Think ( 1332466 ) 19
Think (1332) 7
Think (133) 7
1 1
Java Project:
Implementation:
2 of 13
Mohammed Yahya Aboamara_192911
//create constructor for [attendeesPassNumber, attendeesName, attendeesMajor,
attendeesGender]
public Attendees(int attendeesPassNumber, String attendeesName, String
attendeesMajor, String attendeesGender) {
this.attendeesPassNumber = attendeesPassNumber;
this.attendeesName = attendeesName;
this.attendeesMajor = attendeesMajor;
this.attendeesGender = attendeesGender;
}
//---------------------------getter----------------------------
// create the getter for attendeesPassNumber
public int getAttendeesPassNumber() {
return attendeesPassNumber;
}
// create the getter for attendeesName
public String getAttendeesName() {
return attendeesName;
}
// create the getter for attendeesMajor
public String getAttendeesMajor() {
return attendeesMajor;
}
// create the getter for attendeesGender
public String getAttendeesGender() {
return attendeesGender;
}
//---------------------------------setter---------------------------
//create the setter for attendeesPassNumber
public void setAttendeesPassNumber(int attendeesPassNumber) {
this.attendeesPassNumber = attendeesPassNumber;
}
//create the setter for attendeesName
public void setAttendeesName(String attendeesName) {
this.attendeesName = attendeesName;
}
//create the setter for attendeesMajor
public void setAttendeesMajor(String attendeesMajor) {
this.attendeesMajor = attendeesMajor;
}
//create the setter for attendeesGender
public void setAttendeesGender(String attendeesGender) {
this.attendeesGender = attendeesGender;
}
//---------------------------toString-------------------------------
3 of 13
Mohammed Yahya Aboamara_192911
}
//----In order to arrange the numbers according to the passport number i used
compareTo method,
// if the first number greater the the next number return 1 if else return -1
if equals return 0.
@Override
public int compareTo(Attendees A) {
if(attendeesPassNumber > A.attendeesPassNumber)
return 1;
else if(attendeesPassNumber == A.attendeesPassNumber)
return 0;
else
return -1;
}
}
Conference class::::
import java.io.*;
import java.util.*;
// a method that read from workshop name and put the name in main collection
public ArrayList<String> readFromWsName() {
ArrayList<String> WSN = new ArrayList<>();
String shopName;
//try and catch to show the message error if the method do not find the
file
try {
Scanner in = new Scanner(new File("wsName.txt"));
while (in.hasNext()) {
shopName = in.nextLine();//read from file
WSN.add(shopName); //add to ArrayList
4 of 13
Mohammed Yahya Aboamara_192911
WS.put(shopName, new ArrayList<Attendees>()); //add to theMap
}
in.close();
} catch (Exception e) { // the exception error message if the file not
exist
System.out.println("The file is not found");
}
return WSN; //return the Arraylist
}
//add method that adds the attendees in to temp arraylist then to the
workshop arrayList
public void addAttendees(String Ws, Attendees A) throws DuplicateException {
for (String workShopName : WS.keySet()) {// a for loop to trace all the
attendees the adding them to temp
if (workShopName.equals(Ws)) {
ArrayList<Attendees> temp = WS.get(workShopName);
for (Attendees attend : temp) {
if (attend.equals(A)) { // if there a match print repeated
System.out.println(" this attendees are repeated ");
throw new DuplicateException("The attendee is repeated");
}
}
temp.add(A);//add to list
WS.put(workShopName, temp); // add workshop name , list to map
System.out.println("this attendees is added ");
}
}
}
//remove method that removes the attendees in to temp arraylist then to the
workshop arrayList
public void removeAttendees(String W, int attendeesPassNumber) throws
DuplicateException {
ArrayList<Attendees> temp;//creating temp arraylist
boolean remove = false;
for (String WS : WS.keySet()) { // to trace all the attendees and find
the match
if (WS.equals(W)) {
temp = this.WS.get(WS);
for (Attendees a : temp) { // when finding a match then remove
attendees
if (a.getAttendeesPassNumber() == attendeesPassNumber) {
temp.remove(a);
this.WS.put(WS, temp);// put a temp list again after
removing the attendees
remove = true;
break;
}
}
}
}
if(remove ==true) // when the method works show this message
System.out.println("this attendees is removed");
else // if not show this message
System.out.println("this attendees is not removed");
}
//a methods thats show all data after sorting tha data in collection
5 of 13
Mohammed Yahya Aboamara_192911
public String showAllTheData() {
String s = "";
for(String workShop : WS.keySet()) // s will pass to all data one by one
{
s+= "Work shop: " + workShop + "\n"; //takes the key
Collections.sort(WS.get(workShop)); // sort the list
for(Attendees a : WS.get(workShop))// then pass to arraylist of
attendees for the workshop
{
s+= a +"\n";
}
}
return s;
}
//
public boolean equals(Object o) {
if (o instanceof Conference) {
Conference wr = (Conference) o;
return WS.equals(wr.WS);
} else
return false;
}
//-------------------------toString------------------------
@Override
public String toString() {
String st = "";
for(String workShop : WS.keySet())
{
st+= "Work shop: " + workShop + "\n";
Collections.sort(WS.get(workShop));
for(Attendees a : WS.get(workShop))
{
st+= a +"\n";
}
}
return st;
}
}
Testing:
6 of 13
Mohammed Yahya Aboamara_192911
import java.io.IOException;
System.out.println(conf);
System.out.println(conf.showAllTheData());
}
}
Adding attendees
7 of 13
Mohammed Yahya Aboamara_192911
Remove attendees
8 of 13
Mohammed Yahya Aboamara_192911
9 of 13
Mohammed Yahya Aboamara_192911
GUI:
import javafx.application.Application;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import java.util.ArrayList;
10 of 13
Mohammed Yahya Aboamara_192911
Attendees a;
Conference c;
@Override
public void start(Stage ps) {
c = new Conference();//create object from conference
ArrayList<String> workshops = c.readFromWsName();
11 of 13
Mohammed Yahya Aboamara_192911
Button clear = new Button("Clear");
// FlowPane to make a row of buttons
FlowPane btnGrade = new FlowPane();
btnGrade.getChildren().addAll(addAttend, removeAttend,clear,rn);
//adding the panales one by one
GridPane a = new GridPane();
a.add(control, 0, 1);
a.add(btnGrade, 0, 2);
a.add(dp, 0, 3);
// to show the windows with my name in the top
Scene scene = new Scene(a, 450, 350);
ps.setTitle("MohammedYahyaKhalilAboamara_192911");
ps.setScene(scene);
ps.show();
// the set on action to give a action for the form
addAttend.setOnAction(e -> {
String workName =
wSTxt.getSelectionModel().getSelectedItem().toString();
int pNumber = Integer.parseInt(aPTxt.getText());
String name = aNTxt.getText();
String major = String.valueOf(Integer.parseInt(aMTxt.getText()));
String gender = aGtxt.getText();
//create an object
Attendees a1 = new Attendees(pNumber,name,major,gender);
// try and catch to show the error
try {
c.addAttendees(workName ,a1);
new Alert(Alert.AlertType.CONFIRMATION, "The attendee is
added").show();
// to make th field empty after add the info
aPTxt.setText("");
aNTxt.setText("");
aMTxt.setText("");
aGtxt.setText("");
} catch (DuplicateException duplicateException) {
new Alert(Alert.AlertType.ERROR, "Error! Duplicate the
attendee").show();
}
});
// using the set on action to remove attendees
removeAttend.setOnAction(e -> {
String work_Name =
wSTxt.getSelectionModel().getSelectedItem().toString();
int pass_Number = Integer.parseInt(aPTxt.getText());
try {
c.removeAttendees(work_Name ,pass_Number);
new Alert(Alert.AlertType.CONFIRMATION, "The attendee is
removed").show();
12 of 13
Mohammed Yahya Aboamara_192911
aMTxt.setText("");
aGtxt.setText("");
details.setText("");
});
// to read the name of attendees from show all th data methods
rn.setOnAction(e -> {
details.setText(c.showAllTheData());
});
}
public static void main(String[] args) {
launch(args);
}
13 of 13