본문 바로가기

WhiteShip Java Study : 자바 처음부터 멀리까지

4주차) 과제

과제 1. live-study 대시 보드를 만드는 코드를 작성하세요.

  • 깃헙 이슈 1번부터 18번까지 댓글을 순회하며 댓글을 남긴 사용자를 체크 할 것.
  • 참여율을 계산하세요. 총 18회에 중에 몇 %를 참여했는지 소숫점 두자리가지 보여줄 것.
  • Github 자바 라이브러리(github-api.kohsuke.org/)를 사용하면 편리합니다.
  • 깃헙 API를 익명으로 호출하는데 제한이 있기 때문에 본인의 깃헙 프로젝트에 이슈를 만들고 테스트를 하시면 더 자주 테스트할 수 있습니다.

 

깃헙 API 를 사용하려면 access Token 을 발급받고,

docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token

Maven Central Repository 에서 의존성을 받아와서 사용하면 된다.

 

github-api.kohsuke.org/apidocs/index.html

Javadoc 을 보면 많은 정보를 얻을 수 있따.

 

Overview (GitHub API for Java 1.119 API)

 

github-api.kohsuke.org

public class application {
    public static void main(String[] args) throws IOException {
        GitHubAPI gitHubAPI = new GitHubAPI();
        GitIssueTracker gitIssueTracker = new GitIssueTracker();
        GHRepository repository = gitHubAPI.getRepository();

        gitIssueTracker.getCommentByEachIssue(repository);
        gitIssueTracker.printTotalIssue();
    }
}
public class GitHubAPI {
    private static final String OAUTH_TOKEN = "0703e591a5e19da09fb269e4bdf9d5877ca374c9";
    private static final String REPOSITORY_NAME = "whiteship/live-study";

    private GHRepository repository;

    public GitHubAPI() {
        try {
            GitHub gitHubInstance = new GitHubBuilder().withOAuthToken(OAUTH_TOKEN).build();
            this.repository = gitHubInstance.getRepository(REPOSITORY_NAME);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public GHRepository getRepository() {
        return repository;
    }
}
public class GitIssueTracker {
    private static final int ISSUE_NUMBER_TO_SEARCH = 5;

    private final UserSubmitMap userSubmitNumber = new UserSubmitMap();

    public void getCommentByEachIssue(GHRepository repository) throws IOException {
        for (int issueNumber = 1; issueNumber <= ISSUE_NUMBER_TO_SEARCH; issueNumber++) {
            GHIssue ghIssue = repository.getIssue(issueNumber);
            List<GHIssueComment> comments = ghIssue.getComments();
            checkComment(comments);
        }
    }

    private void checkComment(List<GHIssueComment> comments) {
        for (GHIssueComment comment : comments) {
            String commenterName = comment.getUserName();
            userSubmitNumber.addSubmitNumber(commenterName);
        }
    }

    public void printTotalIssue() {
        System.out.println("==================과제 제출 현황==================");
        userSubmitNumber.printStatement();
    }
}
public class UserSubmitMap {
    private static final int TOTAL_ROUND = 5;

    private Map<String, Integer> userSubmitNumber;

    public UserSubmitMap() {
        this.userSubmitNumber = new HashMap<>();
    }

    public void addSubmitNumber(String name) {
        if (!userSubmitNumber.containsKey(name)) {
            userSubmitNumber.put(name, 1);
        }else {
            int currentNumber = userSubmitNumber.get(name);
            userSubmitNumber.put(name, ++currentNumber);
        }
    }

    public void printStatement() {
        for (Map.Entry<String, Integer> entry : userSubmitNumber.entrySet()) {
            String userName = entry.getKey();
            int submitNumber = entry.getValue();
            System.out.println(String.format("%20s%10d%10.1f", userName, submitNumber, getPercentage(submitNumber)));
        }
    }

    private double getPercentage(int submitNumber) {
        return (double) submitNumber / TOTAL_ROUND *100;
    }
}

나도있당 ㅎ

 

 

 

 

과제 2. LinkedList를 구현하세요.

  • LinkedList에 대해 공부하세요.
  • 정수를 저장하는 ListNode 클래스를 구현하세요.
  • ListNode add(ListNode head, ListNode nodeToAdd, int position)를 구현하세요.
  • ListNode remove(ListNode head, int positionToRemove)를 구현하세요.
  • boolean contains(ListNode head, ListNode nodeTocheck)를 구현하세요.
public class ListNode {
    private Object data;
    private ListNode nextNode = null;

    public ListNode(Object data) {
        this.data = data;
    }

    public Object getData() {
        return data;
    }

    public ListNode getNextNode() {
        return nextNode;
    }

    public void setNextNode(ListNode nextNode) {
        this.nextNode = nextNode;
    }
}
public interface LinkedList {
    ListNode add(ListNode nodeToAdd, int position);
    ListNode remove(int positionToRemove);
    boolean contains(ListNode nodeToCheck);
}
public class LinkedListImpl implements LinkedList {
    private ListNode head;
    private ListNode tail;
    private int size = 0;

    @Override
    public ListNode add(ListNode nodeToAdd, int position) {
        if (head == null) {
            head = nodeToAdd;
            size++;
            return head;
        }
        if (position == 0) {
            nodeToAdd.setNextNode(head);
            this.size++;
            return head;
        }
        ListNode frontNode = findNode(position);
        ListNode backNode = frontNode.getNextNode();
        frontNode.setNextNode(nodeToAdd);
        nodeToAdd.setNextNode(backNode);
        this.size++;
        if (nodeToAdd.getNextNode() == null) {
            this.tail = nodeToAdd;
        }
        return head;
    }

    @Override
    public ListNode remove(int positionToRemove) {
        if (positionToRemove == 0) {
            this.head = head.getNextNode();
            size--;
            return head;
        }
        ListNode frontNodeToDelete = findNode(positionToRemove);
        ListNode NodeToDelete = frontNodeToDelete.getNextNode();
        frontNodeToDelete.setNextNode(NodeToDelete.getNextNode());
        size--;
        if (NodeToDelete == null) {
            this.tail = frontNodeToDelete;
        }
        return head;
    }

    @Override
    public boolean contains(ListNode nodeToCheck) {
        ListNode node = head;
        for (int i = 0; i < size; i++) {
            if (node.getData() == nodeToCheck.getData()) {
                return true;
            }
            node = node.getNextNode();
        }
        return false;
    }

    private ListNode findNode(int position) {
        ListNode pointer = head;
        for (int i = 0; i < position - 1; i++) {
            pointer = pointer.getNextNode();
        }
        return pointer;
    }

    public void printList() {
        ListNode node = head;
        System.out.print("[ ");
        while (node != null) {
            System.out.print(node.getData() + " ");
            node = node.getNextNode();
        }
        System.out.print("]");
    }
}
public class Main {
    public static void main(String[] args) {
        LinkedListImpl list = new LinkedListImpl();

        for (int i = 0; i < 10; i++){
            list.add(new ListNode(i), i);
        }

        list.remove(1);
        list.remove(5);

        list.printList();

        ListNode existNode = new ListNode(2);
        ListNode nonExistNode = new ListNode(11);
        System.out.println();
        System.out.println(list.contains(existNode));
        System.out.println(list.contains(nonExistNode));
    }
}

 

 

 

 

 

 

과제 3. Stack을 구현하세요.

  • int 배열을 사용해서 정수를 저장하는 Stack을 구현하세요.
  • void push(int data)를 구현하세요.
  • int pop()을 구현하세요.
public class Stack {
    private Integer[] stack;
    private int size = 0;

    public Stack() {
    }

    public Stack(int size) {
        this.stack = new Integer[size];
    }

    public void push(int data) {
        stack[size++] = data;
    }

    public int pop() {
        int dataToPop = stack[size - 1];
        stack[--size] = 0;
        return dataToPop;
    }

    public void printStack() {
        System.out.print("[ ");
        for (int i = 0; i < size; i++) {
            System.out.print(stack[i] + " ");
        }
        System.out.println("]");
    }
}
public class Main {
    public static void main(String[] args) {
        Stack stack = new Stack(5);
        stack.push(4);
        stack.push(5);
        stack.push(2);
        stack.pop();
        stack.printStack();
    }
}

 

 

'WhiteShip Java Study : 자바 처음부터 멀리까지' 카테고리의 다른 글

상속  (0) 2021.02.01
클래스  (0) 2021.01.30
제어문  (0) 2021.01.28
연산자  (0) 2021.01.28
자바 데이터 타입, 변수 그리고 배열  (0) 2021.01.16