Block funktion

master
obaya 2024-08-17 00:21:55 +02:00
parent 29b1d0cf27
commit d3ff1f170c
2 changed files with 108 additions and 53 deletions

View File

@ -5,14 +5,16 @@ public class Whatsapp {
private String name; private String name;
private String teleN; private String teleN;
private String nachricht; private String nachricht;
public ArrayList <String> speicherContact; private ArrayList <String> speicherContact;
private ArrayList <String> speicherNummern; private ArrayList <String> speicherNummern;
private ArrayList <String> blockContact;
public Whatsapp(String name, String teleN) { public Whatsapp(String name, String teleN) {
this.name = name; this.name = name;
this.nachricht = null; this.nachricht = null;
this.teleN = teleN; this.teleN = teleN;
this.speicherContact = new ArrayList<>(); this.speicherContact = new ArrayList<>();
this.blockContact = new ArrayList<>();
speicherContact.add(this.name); speicherContact.add(this.name);
this.speicherNummern = new ArrayList<>(); this.speicherNummern = new ArrayList<>();
speicherNummern.add(teleN); speicherNummern.add(teleN);
@ -38,6 +40,20 @@ public class Whatsapp {
} }
public void blocKontakt(Whatsapp block) {
if (!blockContact.contains(block.getName())) {
this.blockContact.add(block.getName());
System.out.println(this.name + " hat " + block.getName() + " blockiert");
}
else
System.err.println("Ist bereit Blockiert");
}
public ArrayList <String> getblockContact(){
return blockContact;
}
public String searchContact(String name){ public String searchContact(String name){
for (int i = 0; i <speicherContact.size(); i++ ) for (int i = 0; i <speicherContact.size(); i++ )
if (speicherContact.get(i).equalsIgnoreCase(name)) if (speicherContact.get(i).equalsIgnoreCase(name))
@ -73,25 +89,35 @@ public class Whatsapp {
// nachrichten zwischen zwei Usern // nachrichten zwischen zwei Usern
class Nachrichten{ class Nachrichten{
Whatsapp sender; private Whatsapp sender;
Whatsapp receiver; private Whatsapp receiver;
ArrayList<String> saveMessages; private ArrayList<String> saveMessages;
public Nachrichten(Whatsapp sender, Whatsapp receiver) { public Nachrichten(Whatsapp sender, Whatsapp receiver) {
this.sender = sender; //obai this.sender = sender; //obai
this.receiver = receiver; //omar this.receiver = receiver; //omar
this.saveMessages = new ArrayList<>(); this.saveMessages = new ArrayList<>();
} }
public void sendMessage(Whatsapp sender,Whatsapp receiver ,String message) { public void sendMessage(Whatsapp sender, Whatsapp receiver, String message) {
if (receiver.getblockContact().contains(sender.getName())) {
if (receiver != null) { String blockMessage = "Du wurdest von " + receiver.getName() + " blockiert!";
String infoMessage = sender.getName() + " sendet " + receiver.getName() +" : " + message; System.err.println(blockMessage);
saveMessages.add(infoMessage); return;
}else }
System.out.println("User does not exist"); if (sender.getblockContact().contains(receiver.getName())) {
String blockMessage = "Du hast " + receiver.getName() + " blockiert!";
System.err.println(blockMessage);
return;
}
if (receiver != null) {
receiver.setNachricht(message);
String infoMessage = sender.getName() + " sendet " + receiver.getName() + " : " + receiver.getNachricht();
saveMessages.add(infoMessage);
}
} }
public ArrayList<String> getinfoMessage() { public ArrayList<String> getinfoMessage() {
return saveMessages; return saveMessages;
@ -99,7 +125,7 @@ class Nachrichten{
public void printsaveMessages() { public void printsaveMessages() {
System.out.println("Chat Room: "); System.out.println("Chat Room: ");
for(String s:saveMessages) for(String s:saveMessages)
System.out.println(s); System.out.println(s);
} }
@ -109,6 +135,7 @@ class Groups{
Whatsapp hersteller; Whatsapp hersteller;
ArrayList<Whatsapp> admins; ArrayList<Whatsapp> admins;
ArrayList<Whatsapp> addmembers; ArrayList<Whatsapp> addmembers;
ArrayList<String> allMessages;
String groupsname; String groupsname;
@ -117,20 +144,42 @@ class Groups{
this.groupsname = groupsname; this.groupsname = groupsname;
this.addmembers = new ArrayList<>(); this.addmembers = new ArrayList<>();
this.admins = new ArrayList<>(); this.admins = new ArrayList<>();
this.allMessages = new ArrayList<>();
addmembers.add(hersteller); addmembers.add(hersteller);
admins.add(hersteller); admins.add(hersteller);
System.out.println(this.hersteller.getName() + "(Admin): hat die Groupe " + this.groupsname + " erstellt"); System.out.println(this.hersteller.getName() + "(Admin): hat die Groupe " + this.groupsname + " erstellt");
} }
public void removeMemebr(Whatsapp remover, Whatsapp toBeRemoved) { public void sendeMessages(Whatsapp sender, String message) {
if (!admins.contains(remover)) { if (addmembers.contains(sender)) {
System.err.println("Du bist kein Admin"); String speicher = sender.getName() + " : " + message ;
return; allMessages.add(speicher);
}else }
addmembers.remove(toBeRemoved);
} }
public ArrayList<String> getallMessages(){
return allMessages;
}
public void printallMessages() {
for (String s: allMessages)
System.out.println(s);
}
public void removeMember(Whatsapp remover, Whatsapp toBeRemoved) {
if (!admins.contains(remover)) {
System.err.println("Du bist kein Admin");
return;
}
if (!addmembers.contains(toBeRemoved)) {
System.err.println("Mitglied nicht in der Gruppe");
return;
}
addmembers.remove(toBeRemoved);
System.out.println(toBeRemoved.getName() + " wurde aus der Gruppe entfernt.");
}
public void addMembers(Whatsapp member) { public void addMembers(Whatsapp member) {
if (!addmembers.contains(member)) if (!addmembers.contains(member))
addmembers.add(member); addmembers.add(member);
@ -138,14 +187,19 @@ class Groups{
System.err.println("member ist bereits da!"); System.err.println("member ist bereits da!");
} }
public void addAdmin(Whatsapp admin,Whatsapp toBeAdmin) { public void addAdmin(Whatsapp admin, Whatsapp toBeAdmin) {
if (admins.contains(admin)){ if (!admins.contains(admin)) {
this.admins.add(toBeAdmin); System.err.println("Du musst Admin sein");
System.out.println(admin.getName() + " hat " + toBeAdmin.getName() + " als Admin gennengezichnet"); return;
} }
else if (admins.contains(toBeAdmin)) {
System.err.println("Du musst Admin sein"); System.err.println(toBeAdmin.getName() + " ist bereits Admin.");
return;
}
admins.add(toBeAdmin);
System.out.println(admin.getName() + " hat " + toBeAdmin.getName() + " als Admin benannt.");
} }
public ArrayList<Whatsapp> getaddmembers(){ public ArrayList<Whatsapp> getaddmembers(){
@ -161,19 +215,14 @@ class Groups{
System.out.println("Alle Members:"); System.out.println("Alle Members:");
int i = 1; int i = 1;
for (Whatsapp s : addmembers) { for (Whatsapp s : addmembers) {
String memberName = printer.speicherContact.contains(s.getName()) ? s.getName() : "Unbekannt"; String memberName = printer.getContact().contains(s.getName()) ? s.getName() : "Unbekannt";
if (admins.contains(s)) if (admins.contains(s))
System.out.println("Mitglied(Admin) " + i + " : " + memberName); System.out.println("Mitglied(Admin) " + i + " : " + memberName);
else else
System.out.println("Mitglied " + i + " : " + memberName); System.out.println("Mitglied " + i + " : " + memberName);
i++; i++;
} }
} }
} }

View File

@ -10,26 +10,32 @@ public class WhatsappTest {
Whatsapp oula = new Whatsapp("oula", "049321321"); Whatsapp oula = new Whatsapp("oula", "049321321");
// Nachrichten messages = new Nachrichten(obai,omar); Nachrichten messages = new Nachrichten(obai,omar);
// messages.sendMessage(obai, omar, "Hallo Omar"); messages.sendMessage(obai, omar, "Hallo omar");
// messages.sendMessage(omar, obai, "Hallo Obai"); messages.sendMessage(omar, obai, "Hallo obai");
// messages.sendMessage(omar, obai, "was machst du?"); messages.sendMessage(omar, obai, "was machst du?");
// messages.printsaveMessages(); messages.printsaveMessages();
omar.blocKontakt(obai);
Groups gr1 = new Groups(obai,"Almidani"); messages.sendMessage(omar, obai, "was machst du?");
gr1.addMembers(omar); messages.printsaveMessages();
gr1.addMembers(abd);
// Groups gr1 = new Groups(obai,"Almidani");
obai.addContact("abd","004848214"); // gr1.addMembers(omar);
obai.addContact("omar","004848214"); // gr1.addMembers(abd);
gr1.addAdmin(obai, omar); //
gr1.printaddmembers(obai); // obai.addContact("abd","004848214");
System.out.println(); // obai.addContact("omar","004848214");
System.out.println(); // gr1.addAdmin(obai, omar);
System.out.println(); // gr1.printaddmembers(obai);
gr1.printaddmembers(omar); // System.out.println();
// System.out.println();
// System.out.println();
// gr1.printaddmembers(omar);
// gr1.sendeMessages(abd, "Hallo Omar");
// gr1.sendeMessages(omar, "Hallo Abd");
//
//
// gr1.printallMessages();
} }
} }