Merthan 3 years ago
parent
commit
44cf1de8ec
  1. 6
      .classpath
  2. 54
      A1-Superstar/src/MainClass.java
  3. 16
      A1-Superstar/src/Repository.java
  4. 13
      A1-Superstar/src/User.java
  5. 7
      A1-Superstar/superstarExtra1.txt
  6. 0
      superstar1.txt
  7. 0
      superstar2.txt
  8. 0
      superstar3.txt
  9. 0
      superstar4.txt
  10. 7
      superstarExtra1.txt
  11. 0
      superstarExtraEmpty.txt

6
.classpath

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="src" path="A1-Superstar/src"/>
<classpathentry kind="output" path="A1-Superstar/bin"/>
</classpath>

54
A1-Superstar/src/MainClass.java

@ -8,8 +8,8 @@ public class MainClass {
//List<String> file=Repository.getFirstFile();
//List<String> file=Repository.getSecondFile();
//List<String> file=Repository.getThirdFile();
List<String> file=Repository.getFourthFile();
//List<String> file=Repository.getExtraOne();
//List<String> file=Repository.getFourthFile();
List<String> file=Repository.getExtraOne();
lines=file;
@ -20,23 +20,7 @@ public class MainClass {
System.out.println("Amount of users:"+fullUserList.size());
secondMain(userList,userList.get(0));if(true)return;
for(int i=0;i<userList.size();i++) {
//if(!userList.get(i).isSuperstar())
User currentUser=userList.get(i);
List<User> listWithoutCurrentUser= new ArrayList<User>(userList);
listWithoutCurrentUser.remove(currentUser);
}
checkUserList(userList,userList.get(0));if(true)return;
}
@ -49,44 +33,56 @@ public class MainClass {
return null;//No non-checked user found
}
public static void secondMain(List<User> userList,User comparedUser) {
public static void checkUserList(List<User> userList,User comparedUser) {
switch(userList.size()) {
case 0:System.out.println("No Superstar found"); System.exit(0);
case 1:System.out.println("The Superstar is: \""+userList.get(0)+"\""); System.exit(0);
case 2:{
if(userList.get(0).isFollowingOtherUser(userList.get(1))) {
userList.remove(userList.get(0));//Remove this user because he followed the other one
}else if(userList.get(1).isFollowingOtherUser(userList.get(0))) {
userList.remove(1);//Remove this user because he followed the other one
}
if(userList.size()==2)secondMain(new ArrayList<User>(),null);//Will call case 0 above.
if(userList.size()==2)checkUserList(new ArrayList<User>(),null);//Will call case 0 above.
secondMain(userList,null);//Second parameter wont be checked, as size will either be 1 or 0
checkUserList(userList,null);//Second parameter wont be checked, as size will either be 1 or 0
}
}
if(checkedUsers.contains(comparedUser)){//If this one was already checked
comparedUser=getNonCheckedUser();//Replace it with one that wasnt
if(comparedUser==null)System.out.println("Null user check");
}else {
checkedUsers.add(comparedUser);//Add it as it will be checked now.
}
if(comparedUser==null)System.out.println("Null user check2");
List<User> nonFollowingUsers=new ArrayList<User>();
for(User user:userList) {
if(!user.isFollowingOtherUser(comparedUser)) nonFollowingUsers.add(user);
if(!user.isSuperstar())continue;
if(!user.isFollowingOtherUser(comparedUser)) {
nonFollowingUsers.add(user);
//comparedUser.setSuperstarFalse();
}
}
if(nonFollowingUsers.size()==1) {//If size is one, we dont want to call that person superstar yet, as we have to check if the person is really not following anyone else
if(!nonFollowingUsers.get(0).startSuperstarCheck(fullUserList)) {
nonFollowingUsers.remove(0);//Makes a empty list, so no superstar detected.
}else {
checkUserList(Arrays.asList(nonFollowingUsers.get(0)),nonFollowingUsers.get(0));//Show superstar message
}
}
secondMain(nonFollowingUsers,getNonCheckedUser());
if(nonFollowingUsers.size()==0) {
if(comparedUser.startSuperstarCheck(fullUserList))
checkUserList(Arrays.asList(comparedUser),comparedUser);//Show superstar message
}
checkUserList(nonFollowingUsers,getNonCheckedUser());
}

16
A1-Superstar/src/Repository.java

@ -1,5 +1,7 @@
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
@ -7,10 +9,20 @@ import java.util.Scanner;
public class Repository {
static List<String> readFile(String name) {
List<String> lines = new ArrayList<String>();
//name="superstarExtraEmpty.txt";
try(Scanner scanner=new Scanner(new File(name))){
BufferedReader br = new BufferedReader(new FileReader((name)));
if (br.readLine() == null) {
System.out.println("File empty");
br.close();
System.exit(0);
}
br.close();
while (scanner.hasNextLine()) {
lines.add(scanner.nextLine());
@ -18,7 +30,7 @@ public class Repository {
}
return lines;
}catch (FileNotFoundException e) {
}catch (Exception e) {
throw new RuntimeException(e.getMessage());
}

13
A1-Superstar/src/User.java

@ -42,28 +42,21 @@ public class User {
if(MainClass.isXFollowingY(this, followed)){
addFollowedUser(followed);
setSuperstarFalse();//As user has been followed, this user can´t be a superstar
return true;
}
return false;
}
public boolean startSuperstarCheck(List<User> userList) {
if(!isSuperstar)return false;//If user has been marked as no superstar, no need for check
for(User user:userList) {//Go through every user
if(user.equals(this))continue;//Ignore
if(isFollowingOtherUser(user)) {
addFollowedUser(user);//Add to list if followed
return false;
}
}
/*
for(User user:userList) {
if(MainClass.isXFollowingY(user, this)) {//Check if all other persons are following this
user.addFollowedUser(this);//Add this as a followed person
}else {
return false;//If the user isnt followed by someone, return false
}
}*/
return true;
}

7
A1-Superstar/superstarExtra1.txt

@ -1,7 +0,0 @@
Merthan Marcel Keanu Timo Jan
Marcel Merthan
Marcel Keanu
Timo Jan
Timo Merthan
Jan Merthan
Keanu Merthan

0
A1-Superstar/superstar1.txt → superstar1.txt

0
A1-Superstar/superstar2.txt → superstar2.txt

0
A1-Superstar/superstar3.txt → superstar3.txt

0
A1-Superstar/superstar4.txt → superstar4.txt

7
superstarExtra1.txt

@ -0,0 +1,7 @@
Marcel SuperstarName Keanu Timo Jan
Marcel SuperstarName
Marcel Keanu
Timo Jan
Timo SuperstarName
Jan SuperstarName
Keanu SuperstarName

0
superstarExtraEmpty.txt

Loading…
Cancel
Save