Answer Booklet: M251-M257: Object Oriented Programming Using Java

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 13

Mohammed Yahya Aboamara_192911

Answer Booklet

M251-M257: Object Oriented Programming Using Java

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

Below, you can start your answer…

1 of 13
Mohammed Yahya Aboamara_192911
Tracing:
Think ( 1332466 ) 19

6 + Think ( 133246) 13+6

6 + Think (13324) 7+6

Think (1332) 7

Think (133) 7

3 + Think (13) 4+3

3 + Think (1) 3+1

1 1

Java Project:
Implementation:

public class Attendees implements Comparable<Attendees>{


//Define variables [attendeesPassNumber, attendeesName, attendeesMajor,
attendeesGender]
private int attendeesPassNumber;
private String attendeesName;
private String attendeesMajor;
private String attendeesGender;

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-------------------------------

//create the toString for [attendeesPassNumber, attendeesName,


attendeesMajor, attendeesGender]
@Override
public String toString() {
return "Attendees{" +
"attendeesPassNumber=" + attendeesPassNumber +
", attendeesName='" + attendeesName + '\'' +
", attendeesMajor='" + attendeesMajor + '\'' +
", attendeesGender='" + attendeesGender + '\'' +
'}';

3 of 13
Mohammed Yahya Aboamara_192911
}

//----In order to solve the problem of non-repetition of attendeesPassNumber,


we must use equals method---------
//the equals() method compares the two given variables, If any variables is
not matched, it returns false. If all matched, it returns true.
public boolean equals(Object T) {
if (this == T) return true;
if (T == null || getClass() != T.getClass()) return false;

Attendees attendees = (Attendees) T;


return (attendeesPassNumber == attendees.attendeesPassNumber) ;
}

//----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.*;

public class Conference {


// the whole data stored in one collection {{Map}}with all workshop name and
attendees.
private Map<String, ArrayList<Attendees>> WS;
public Conference() {
WS = new TreeMap<String, ArrayList<Attendees>>();
}

// 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;
}
}

DuplicateException class :::::


public class DuplicateException extends Exception {
public DuplicateException(String st)
{
super(st);
}
}

Testing:

6 of 13
Mohammed Yahya Aboamara_192911
import java.io.IOException;

public class Test {


public static void main(String[] args) throws IOException,
DuplicateException {

Conference conf = new Conference();


//test from file
conf.readFromWsName();
System.out.println(conf);

// adding the attendees info


Attendees a1= new Attendees(102513,"mohammed", "It","male");
Attendees a2= new Attendees(551681,"yahya", "Bs","male");
Attendees a3= new Attendees(541681,"farah", "El","female");
Attendees a4= new Attendees(654861,"kalil", "it","male");
Attendees a5= new Attendees(984984,"saad", "El","female");

// ------------------add Attendees ----------------------------


conf.addAttendees("Information Technology and computing introduction",
a1);
conf.addAttendees("Business Management introduction", a5);
conf.addAttendees("Business Management introduction", a2);
conf.addAttendees("Information Technology and computing introduction",
a3);
conf.addAttendees("English Literature introduction", a1);
conf.addAttendees("English Literature introduction", a2);
conf.addAttendees("English Literature introduction", a4);
//-------------------------remove Attendees-----------------------
conf.removeAttendees("Business Management introduction" , 551681);
conf.removeAttendees("Information Technology and computing
introduction" , 541681);
conf.removeAttendees("English Literature introduction" , 102513);

System.out.println(conf);
System.out.println(conf.showAllTheData());

}
}

Snapshot of output of the testing class:

Adding attendees

7 of 13
Mohammed Yahya Aboamara_192911

Remove attendees

8 of 13
Mohammed Yahya Aboamara_192911

read from the file

9 of 13
Mohammed Yahya Aboamara_192911

Print all the data

GUI:

import javafx.application.Application;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;

import java.util.ArrayList;

public class projectGUI extends Application {

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();

// create labels name for work shop


Label workShop___Lb = new Label("Work shop: ");
final ComboBox wSTxt = new ComboBox() ;
wSTxt.getItems().addAll(workshops);
//create Attendee Details label
Label aNLb = new Label("Attendee Details");
//create Passport number label
Label aPassLb = new Label("Passport number : ");
//create Passport number text fields
TextField aPTxt = new TextField();
//create name label with text fields
Label aNameLb = new Label("Name: ");
TextField aNTxt = new TextField();
//create Major label with text fields
Label aMajor = new Label("Major: ");
TextField aMTxt = new TextField();
//create Gender label with text fields
Label aGender = new Label("Gender:");
TextField aGtxt = new TextField();
// create add and remove button
Button addAttend = new Button("Add");
Button removeAttend = new Button("Remove");

//gridPane to add the element by controlling the form


GridPane control = new GridPane();
control.add(workShop___Lb, 0, 0);
control.add(wSTxt, 1, 0);
control.add(aNLb, 0, 1);
control.add(aPassLb, 0, 2);
control.add(aPTxt, 1, 2);
control.add(aNameLb, 0, 3);
control.add(aNTxt, 1, 3);
control.add(aMajor, 0, 4);
control.add(aMTxt, 1, 4);
control.add(aGender, 0, 5);
control.add(aGtxt, 1, 5);
control.add(addAttend, 2, 5);
control.add(removeAttend, 3, 5);

// create label for details and the textArea


Label dAl = new Label("Details: ");
TextArea details = new TextArea();
// create the border
BorderPane dp = new BorderPane();
//order the details
dp.setTop(dAl);
dp.setCenter(details);
//create the button to read names
Button rn = new Button("display names");
//create the clear button

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();

} catch (DuplicateException duplicateException) {


new Alert(Alert.AlertType.ERROR, "Error! can not remove
attendee").show();
}
});
// using the set on action to makes the fields empty
clear.setOnAction(e -> {
aPTxt.setText("");
aNTxt.setText("");

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

You might also like