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 teleN;
private String nachricht;
public ArrayList <String> speicherContact;
private ArrayList <String> speicherContact;
private ArrayList <String> speicherNummern;
private ArrayList <String> blockContact;
public Whatsapp(String name, String teleN) {
this.name = name;
this.nachricht = null;
this.teleN = teleN;
this.speicherContact = new ArrayList<>();
this.blockContact = new ArrayList<>();
speicherContact.add(this.name);
this.speicherNummern = new ArrayList<>();
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){
for (int i = 0; i <speicherContact.size(); i++ )
if (speicherContact.get(i).equalsIgnoreCase(name))
@ -73,26 +89,36 @@ public class Whatsapp {
// nachrichten zwischen zwei Usern
class Nachrichten{
Whatsapp sender;
Whatsapp receiver;
ArrayList<String> saveMessages;
private Whatsapp sender;
private Whatsapp receiver;
private ArrayList<String> saveMessages;
public Nachrichten(Whatsapp sender, Whatsapp receiver) {
this.sender = sender; //obai
this.receiver = receiver; //omar
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())) {
String blockMessage = "Du wurdest von " + receiver.getName() + " blockiert!";
System.err.println(blockMessage);
return;
}
if (sender.getblockContact().contains(receiver.getName())) {
String blockMessage = "Du hast " + receiver.getName() + " blockiert!";
System.err.println(blockMessage);
return;
}
if (receiver != null) {
String infoMessage = sender.getName() + " sendet " + receiver.getName() +" : " + message;
saveMessages.add(infoMessage);
}else
System.out.println("User does not exist");
if (receiver != null) {
receiver.setNachricht(message);
String infoMessage = sender.getName() + " sendet " + receiver.getName() + " : " + receiver.getNachricht();
saveMessages.add(infoMessage);
}
}
public ArrayList<String> getinfoMessage() {
return saveMessages;
}
@ -109,6 +135,7 @@ class Groups{
Whatsapp hersteller;
ArrayList<Whatsapp> admins;
ArrayList<Whatsapp> addmembers;
ArrayList<String> allMessages;
String groupsname;
@ -117,20 +144,42 @@ class Groups{
this.groupsname = groupsname;
this.addmembers = new ArrayList<>();
this.admins = new ArrayList<>();
this.allMessages = new ArrayList<>();
addmembers.add(hersteller);
admins.add(hersteller);
System.out.println(this.hersteller.getName() + "(Admin): hat die Groupe " + this.groupsname + " erstellt");
}
public void removeMemebr(Whatsapp remover, Whatsapp toBeRemoved) {
if (!admins.contains(remover)) {
System.err.println("Du bist kein Admin");
return;
}else
addmembers.remove(toBeRemoved);
public void sendeMessages(Whatsapp sender, String message) {
if (addmembers.contains(sender)) {
String speicher = sender.getName() + " : " + message ;
allMessages.add(speicher);
}
}
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) {
if (!addmembers.contains(member))
addmembers.add(member);
@ -138,15 +187,20 @@ class Groups{
System.err.println("member ist bereits da!");
}
public void addAdmin(Whatsapp admin,Whatsapp toBeAdmin) {
if (admins.contains(admin)){
this.admins.add(toBeAdmin);
System.out.println(admin.getName() + " hat " + toBeAdmin.getName() + " als Admin gennengezichnet");
}
else
System.err.println("Du musst Admin sein");
public void addAdmin(Whatsapp admin, Whatsapp toBeAdmin) {
if (!admins.contains(admin)) {
System.err.println("Du musst Admin sein");
return;
}
if (admins.contains(toBeAdmin)) {
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(){
return addmembers;
@ -161,19 +215,14 @@ class Groups{
System.out.println("Alle Members:");
int i = 1;
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))
System.out.println("Mitglied(Admin) " + i + " : " + memberName);
else
System.out.println("Mitglied " + i + " : " + memberName);
i++;
}
}
}

View File

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