๊ด€๋ฆฌ ๋ฉ”๋‰ด

gi_dor

Java Iterator , Score CRUD ๋ณธ๋ฌธ

Language/Java

Java Iterator , Score CRUD

๊ธฐ๋Œ 2023. 11. 17. 16:40

๐Ÿ’ป ํ•œ์ฃผ์˜ ๋งˆ์ง€๋ง‰ ์ˆ˜์—…
์ง€๋ฐฉ ๊ฒฐํ˜ผ์‹ ์ด์Šˆ๋กœ ๊ณผ์—ฐ ์ปฌ๋ ‰์…˜ ์ •๋ฆฌ ๋‹ค ํ• ์ˆ˜ ์žˆ์„๋Ÿฐ์ง€..


๐Ÿ”ธ Iterator

https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/Iterator.html

 

Iterator (Java SE 17 & JDK 17)

Type Parameters: E - the type of elements returned by this iterator All Known Subinterfaces: EventIterator, ListIterator , PrimitiveIterator , PrimitiveIterator.OfDouble, PrimitiveIterator.OfInt, PrimitiveIterator.OfLong, XMLEventReader All Known Implement

docs.oracle.com

 

๐Ÿ”นIterator <E> ์ธํ„ฐํŽ˜์ด์Šค

Iterator<E>
  • ์ปฌ๋ ‰์…˜์— ์ €์žฅ๋œ ์š”์†Œ ( ๊ฐ์ฒด ) ๋ฅผ ๋ฐ˜๋ณต์ฒ˜๋ฆฌ ํ•˜๋Š” ๊ตฌํ˜„ ๊ฐ์ฒด๊ฐ€ ๋ฐ˜๋“œ์‹œ ๊ตฌํ˜„ํ•  ๊ธฐ๋Šฅ์„ ์ •์˜ ํ•˜๊ณ ์žˆ๋Š” ํ‘œ์ค€ ์ธํ„ฐํŽ˜์ด์Šค
  • ๋ชจ๋“  ์ปฌ๋ ‰์…˜ ํ”„๋ ˆ์ž„์›Œํฌ์˜ ๊ตฌํ˜„ ํด๋ž˜์Šค๋Š” ์ž์‹ ์˜ ์ž๋ฃŒ๊ตฌ์กฐ์— ์ €์žฅ๋œ ์š”์†Œ๋“ค์„ ๋ฐ˜๋ณต์ฒ˜๋ฆฌํ•˜๋Š”
    Iterator <E> ๊ตฌํ˜„๊ฐ์ฒด๋ฅผ ์ œ๊ณตํ•œ๋‹ค
  • ๋ชจ๋“  ์ปฌ๋ ‰์…˜ ํ”„๋ ˆ์ž„์›Œํฌ์˜ ๊ตฌํ˜„ ํด๋ž˜์Šค๋Š” iterator() ๋ฉ”์„œ๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ์ž์‹ ์ด ๊ตฌํ˜„ํ•˜๊ณ  
    Iterator<E> ๊ตฌํ˜„๊ฐ์ฒด๋ฅผ ์ œ๊ณตํ•œ๋‹ค

 

โ–ถ ์ปฌ๋ ‰์…˜์— ์ €์žฅ๋œ ๊ฐ ์š”์†Œ์— ์ ‘๊ทผํ•˜๋Š” Iterator ์ธํ„ฐํŽ˜์ด์Šค๊ฐ€ ์žˆ๊ณ 
Collection ์ธํ„ฐํŽ˜์ด์Šค ์•ˆ์— Iterator ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•œ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” iterator()๊ฐ€ ์žˆ๋‹ค
iterator()๋Š” Collection ์ธํ„ฐํŽ˜์ด์Šค์— ์žˆ๋Š” ๋ฉ”์„œ๋“œ ์ด๋ฏ€๋กœ List์™€ Set์—๋„ ํฌํ•จ๋˜์–ด์žˆ๋‹ค

iterator()๋ฅผ ํ˜ธ์ถœํ•ด Iterator์„ ์–ป์€๋‹ค์Œ ๋ฐ˜๋ณต๋ฌธ ์„ ์‚ฌ์šฉํ•ด ์ปฌ๋ ‰์…˜ ํด๋ž˜์Šค์˜ ์š”์†Œ๋“ค์„ ์ฝ์–ด์˜ด

 

๐Ÿ“Œ ์ ์šฉํ•ด๋ณด๊ธฐ

public interface Collection{
    public Iterator iterator();	
    // Iterator๋ฅผ ๊ตฌํ˜„ํ•œ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” iterator ๋ฉ”์„œ๋“œ
}


    public static void main(String[] args) {

        List<String> list = Arrays.asList("์ง„์ถฉ ์žฅ์ฐฝ", "์œ ๋ฆผ ํ‘œ๊ธฐ", "์œ™๋“œ ํ›„์‚ฌ๋ฅด");

        System.out.println("###  ์ผ๋ฐ˜ for๋ฌธ์„ ์‚ฌ์šฉ");

        // ์•„์‰ฝ๊ฒŒ๋„ ์ผ๋ฐ˜ for๋ฌธ์€ List์—๋งŒ ์“ธ์ˆ˜ ์žˆ๋‹ค Set์€ ๋ถˆ๊ฐ€
        int size = list.size();
        for (int index = 0; index < size; index++) {
            String name = list.get(index);
            System.out.println(name);
        }

        System.out.println();
        System.out.println("###  ํ–ฅ์ƒ๋œ for๋ฌธ์„ ์‚ฌ์šฉ");
// ๋ฐ‘์— iterator ์“ฐ๋Š”๊ฒƒ๋ณด๋‹ค ํ–ฅ์ƒ๋œ for๋ฌธ์ด ์‰ฝ๊ณ  ๊ฐ„๋‹จํ•˜๋‹ค
        for (String name : list) {
            System.out.println(name);
        }

        System.out.println();
        System.out.println("### Iterator ์‚ฌ์šฉ");

// ์‚ญ์ œ๊ฐ€ ํ•„์š”ํ•  ๋•Œ๋‚˜ ์“ฐ์ž
        Iterator<String> it = list.iterator();
        while (it.hasNext()) {
            String name = it.next();
            System.out.println(name);
        }

        System.out.println();
        System.out.println("### Stream ์‚ฌ์šฉ");
        list.stream().forEach(name -> System.out.println(name));
    }
}

 

public class IteratorApp2 {
    public static void main(String[] args) {

        // List๋Š” ์ค‘๋ณต ์ƒ๊ด€ ์•ˆํ•จ, ์ˆœ์„œ ์žˆ์Œ
        List<String> list = new ArrayList<>();
        list.add("์นธ ์ผ€์‹");
        list.add("์ฒ ๋ถ€๋„");
        list.add("์ฒ ๋ถ€๋„");
        list.add("์ฒ™๊ฐ€๊ตฐ");
        list.add("๋น„ํ™”๊ตฐ");
        list.add("๋น„ํ™”๊ตฐ");
        list.add("์‹ ์ฑ…");

        // list๊ฐ์ฒด์—์„œ ์ฒ˜์Œ์œผ๋กœ ๋ฐœ๊ฒฌ๋œ ๊ฐ์ฒด๋งŒ ์‚ญ์ œ
        list.remove("์ฒ ๋ถ€๋„");
        System.out.println("### ์ผ๋ฐ˜ ์ถœ๋ ฅ");
        System.out.println(list);   // [์นธ ์ผ€์‹, ์ฒ ๋ถ€๋„, ์ฒ™๊ฐ€๊ตฐ, ๋น„ํ™”๊ตฐ, ๋น„ํ™”๊ตฐ, ์‹ ์ฑ…]

        System.out.println();
        System.out.println("### ์ผ๋ฐ˜ for๋ฌธ");

        for (String name : list) {
            System.out.println(name);
        }

        System.out.println();
        System.out.println("### ํ–ฅ์ƒ๋œ for๋ฌธ");
        // ํ–ฅ์ƒ for๋ฌธ์œผ๋กœ ArrayList์— ์ €์žฅ๋œ ์š”์†Œ ๋ฐ˜๋ณตํ•ด์„œ ์กฐํšŒ
        //  for๋ฌธ์œผ๋กœ ๋ฐ˜๋ณต ํ•˜๋Š”๋™์•ˆ ์ปฌ๋ ‰์…˜ ์ €์žฅ๋œ ๊ฐ์ฒด ์‚ญ์ œ ์•ˆ๋œ๋‹ค์ž‰
        for (String name : list) {
//            if (name.equals("๋น„ํ™”๊ตฐ")) {
//                list.remove(name);  // Exception in thread "main" java.util.ConcurrentModificationException
//            }
            System.out.println(name);
        }

        System.out.println();
        System.out.println("### Iterator ");
        Iterator<String> it = list.iterator();

        // Iterator ๊ตฌํ˜„๊ฐ์ฒด๋ฅผ ์ด์šฉํ•ด ArrayList ์š”์†Œ ๋ฐ˜๋ณตํ•ด์„œ ์กฐํšŒ
        while (it.hasNext()) {    // boolean hasNext() ์ฝ์„ ์š”์†Œ๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ
            String name = it.next();    // Object next() ๋‹ค์Œ ์š”์†Œ ์ฝ๊ธฐ
            System.out.println(name);
        }
    }
}

 

  HashSet<String> list = new HashSet<>();
  list.add("ํ—Œ์›");
  list.add("์น˜์šฐ");
  list.add("์น˜์šฐ๋น„");

  // ํ–ฅ์ƒ๋œ for๋ฌธ์œผ๋กœ HashSet์— ์ €์žฅ๋œ ์š”์†Œ๋ฅผ ๋ฐ˜๋ณตํ•ด์„œ ์กฐํšŒ
  for (String name : list) {
    System.out.println(name);
  }
  
  // Iterator์˜ ๊ตฌํ˜„๊ฐ์ฒด๋ฅผ ์ด์šฉํ•ด์„œ HashSet์— ์ €์ •๋œ ์š”์†Œ๋ฅผ ๋ฐ˜๋ณตํ•ด์„œ ์กฐํšŒ
  Iterator<String> it = list.iterator();
  
  while (it.hasNext()) {
    String name = it.next();
    System.out.println(name);
  }

 

  HashMap<String, String> map = new HashMap<>();
  map.put("hong", "ํŒŒ์ฃผ ์—ฐ๋…ธ");
  map.put("kim", "ํŒŒ์ฃผ ์žฅ์ฐฝ");
  map.put("lee", "์ฒญ์„œ");

  Set<String> keySet = map.keySet();
  // ๋งต์—์„œ ์กฐํšŒ๋œ key์˜ ์ง‘ํ•ฉ์— ์ €์žฅ๋œ key๊ฐ’์„ ๋ฐ˜๋ณตํ•ด์„œ ์กฐํšŒ
  for (String key : keySet) {
    System.out.print(key);
  }

  Iterator<String> it = keySet.iterator();
  while (it.hasNext()) {
    String key = it.next();
    System.out.println(key);
  }

 

 

ArrayList>String> c1 = new ArrayList<>();
Vector<Integer> c2 = new Vector<>();
HashSet<User> c3 = new HashSet<>();
TreeSet<Score> c4 = new TreeSet<>();

Iterator<String> iter1 = c1.iterator();
Iterator<String> iter2 = c2.iterator();
Iterator<String> iter3 = c3.iterator();
Iterator<String> iter4 = c4.iterator();

 

boolean hasNext()
  • ๋ฐ˜๋ณต์ฒ˜๋ฆฌํ•  ์š”์†Œ๊ฐ€ ๋‚จ์•„ ์žˆ๋‹ค๋ฉด true ๋ฐ˜ํ™˜
E next()
  • ๋‚จ์•„ ์žˆ๋Š” ์š”์†Œ ์ค‘์—์„œ ํ•˜๋‚˜๋ฅผ ๋ฐ˜ํ™˜
void remove()
  • ํ˜„์žฌ ๋ฐ˜๋ณต์ฒ˜๋ฆฌ ์ค‘์— ์š”์†Œ๋ฅผ ์‚ญ์ œํ•œ๋‹ค
void forEachReamaining(Consumer<E> action)
  • ๋‚จ์•„์žˆ๋Š” ์š”์†Œ๋ฅผ ์ฐจ๋ก€์ฐจ๋ก€ ์ฒ˜๋ฆฌํ•˜๋Š” Consumer ์ธํ„ฐํŽ˜์ด์Šค ๊ตฌํ˜„๊ฐ์ฒด๋ฅผ ์ „๋‹ฌํ•ด์„œ ์š”์†Œ๋“ค์„ ์ฒ˜๋ฆฌํ•œ๋‹ค

 

๐Ÿ“Œ ๊ฒฐ๋ก 

Iterator ๋Š” ๋ฐ˜๋ณต์ฒ˜๋ฆฌ ํ•  ๋•Œ ์“ฐ๋Š”๊ฒƒ์ธ๋ฐ , ์•ˆ์— ์žˆ๋Š” ์š”์†Œ๋“ค ์‚ญ์ œ ๋ฅผ ํ•ด์•ผํ•  ๊ฒฝ์šฐ์— ์“ฐ์ด๋ฉฐ
ํ–ฅ์ƒ๋œ for๋ฌธ์€ ์‚ญ์ œ๊ฐ€ ๋˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์ด์ œ๋Š” ์ต์ˆ™ํ•œ ํ–ฅ์ƒ๋œ for๋ฌธ์„ ์‚ฌ์šฉํ•˜์ž

 

 


 

๐Ÿ”ธ ํ•™์ƒ ์„ฑ์  ํ”„๋กœ๊ทธ๋žจ

app
ใ„ดReadme.md
ใ„ด Score.java
ใ„ด ScoreUI.java
ใ„ด ScoreService.java
ใ„ด ScoreRepository.java
  • Score.java - ํ•™์ƒ์˜ ์„ฑ์  ์ •๋ณด๋ฅผ ํ‘œํ˜„ํ•˜๋Š” ํด๋ž˜์Šค
  • ScoreUI.java - ์„ฑ์ •๊ด€๋ฆฌ UI ํด๋ž˜์Šค
  • ScoreService.java - ์‹ค์ œ ์„ฑ์ ๊ด€๋ฆฌ ์—…๋ฌด๋กœ์ง ํด๋ž˜์Šค
  • ScoreRepository.java - ์„ฑ์ ๊ด€๋ฆฌ ์˜์†ํ™” ํด๋ž˜์Šค

๐Ÿ”น Score.java

public class Score {

    // ๋ฉค๋ฒ„๋ณ€์ˆ˜ ์€๋‹‰ํ™”
    private int no;
    private String name;
    private int kor;
    private int eng;
    private int math;

    private int total;
    private int average;

//  ์ƒ์„ฑ์ž
    public Score(int no, String name, int kor, int eng, int math) {
        this.no = no;
        this.name = name;
        this.kor = kor;
        this.eng = eng;
        this.math = math;

        reduce();
    }

// method
    public void reduce() {
        total = kor + eng + math;
        average = total / 3;
    }


// getter

    public int getNo() {
        return no;
    }

    public int getKor() {
        return kor;
    }

    public int getEng() {
        return eng;
    }

    public int getMath() {
        return math;
    }

    public String getName() {
        return name;
    }

    public int getTotal() {
        return total;
    }

    public int getAverage() {
        return average;
    }

// Setter


    public void setKor(int kor) {
        this.kor = kor;
    }

    public void setEng(int eng) {
        this.eng = eng;
    }

    public void setMath(int math) {
        this.math = math;
    }

}

 

๐Ÿ”น ScoreService

public class ScoreService {


    private ScoreRepository repo = new ScoreRepository();

    // ์‹ ๊ทœ ์ •๋ณด๋ฅผ ๋“ฑ๋กํ•˜๋Š” ์„œ๋น„์Šค
    //  - ํ•ด๋‹น ํ•™๋ฒˆ์˜ ์„ฑ์  ์ •๋ณด๊ฐ€ ์žˆ๋Š”์ง€ ์ฒดํฌ
    //  - ์ •๋ณด๊ฐ€ ์กด์žฌํ•˜๋ฉด ์˜ค๋ฅ˜ ๋ฐœ์ƒ
    //  - ์ •๋ณด๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด ์„ฑ์ •์ •๋ณด๋ฅผ ์ €์žฅ์‹œํ‚จ๋‹ค
    // ๋ฐ˜ํ™˜ํƒ€์ž… void
    // ๋ฉ”์„œ๋“œ์ด๋ฆ„ : addNewScore
    // ๋งค๊ฐœ๋ณ€์ˆ˜ : Score score

    public void addNewScore(Score score) {
        Score saveScore = repo.findByNo(score.getNo());
        if (saveScore != null) {
            throw new RuntimeException("[" + score.getNo() + "] ์„ฑ์ ์ •๋ณด๊ฐ€ ์ด๋ฏธ ๋“ฑ๋ก ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค");
        }
        repo.save(score);
    }

    // ๋ชจ๋“  ์„ฑ์  ์ •๋ณด๋ฅผ ์ œ๊ณตํ•˜๋Š” ์„œ๋น„์Šค
    //  - ํ•™์ƒ์ •๋ณด๋ฅผ ํ•™๋ฒˆ์ˆœ์œผ๋กœ ์ •๋ ฌํ•ด์„œ ์ œ๊ณต
    // ๋ฐ˜ํ™˜ํƒ€์ž… : List<Score>
    // ๋ฉ”์„œ๋“œ๋ช… : getAllScore
    // ๋งค๊ฐœ๋ณ€์ˆ˜ : X

    /*
    interface Comparable<T> {
        int compareTo(T o1 , T o2);
        }
        o1 ๊ณผ o2์˜ ๋Œ€์†Œ ๊ด€๊ณ„๋ฅผ ๋น„๊ตํ•˜๋Š” ๊ตฌํ˜„ ๋ฉ”์„œ๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋ฉด o1๊ณผ o2๋Š” ์ •๋ ฌ์ด ๊ฐ€๋Šฅํ•˜๋‹ค
        Collections,sort(๋ฆฌ์ŠคํŠธ , Comparator๊ตฌํ˜„๊ฐ์ฒด)
        ๋ฆฌ์ŠคํŠธ.sort(Comparator ๊ตฌํ˜„๊ฐ์ฒด)
     */

    public List<Score> getAllScore() {
        List<Score> list = repo.findAll();

        // ๊ฐ๊ฐ์˜ ์„ฑ์  ์ •๋ณด๋ฅผ ํ•™๋ฒˆ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ ์ˆœ์œผ๋กœ ์ •๋ ฌ์‹œํ‚ค๊ธฐ ์œ„ํ•œ ๊ตฌํ˜„
//        list.sort(new Comparator<Score>() {
//            @Override
//            public int compare(Score s1, Score s2) {
//                return s1.getNo() - s2.getNo();
//            }
//        });
        list.sort((s1, s2) -> s1.getNo() - s2.getNo());

        return list;
    }

    // ๋ชจ๋“  ์„ฑ์  ์ •๋ณด๋ฅผ ์„ฑ์ ์ˆœ์œผ๋กœ ์ œ๊ณตํ•˜๋Š” ์„œ๋น„์Šค
    //  - ํ•™์ƒ ์„ฑ์  ์ •๋ณด๋ฅผ ์ด์ ์— ๋Œ€ํ•œ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•ด์„œ ์ œ๊ณตํ•œ๋‹ค
    // ๋ฐ˜ํ™˜ํƒ€์ž… : List<Score>
    // ๋ฉ”์„œ๋“œ๋ช… : getSortedScore
    // ๋งค๊ฐœ๋ณ€์ˆ˜ : X

    List<Score> getSoredScore() {
        List<Score> list = repo.findAll();
        list.sort((s1, s2) -> s2.getTotal() - s1.getTotal());
        return list;
    }

    // ์„ฑ์  ์ •๋ณด๋ฅผ ๋ณ€๊ฒฝํ•˜๋Š” ์„œ๋น„์Šค
    //  - ์„ฑ์  ์ •๋ณด๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด ์˜ˆ์™ธ๋ฅผ ๋ฐœ์ƒ์‹œํ‚จ๋‹ค
    //  - ์„ฑ์  ์ •๋ณด๊ฐ€ ์กด์žฌํ•˜๋ฉด ๊ตญ์–ด ์˜์šฐ ์ˆ˜ํ•™์ ์ˆ˜๋ฅผ setter ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด ๋ณ€๊ฒฝ
    //  - ๋ณ€๊ฒฝ๋œ ์„ฑ์  ์ •๋ณด๋กœ ์ด์ ๊ณผ ํ‰๊ท ์„ ๋‹ค์‹œ ์ง‘๊ณ„ํ•˜๋„๋ก reduce() ๋ฉ”์„œ๋“œ ์‹คํ–‰
    // ๋ฐ˜ํ™˜ ํƒ€์ž… : void
    // ๋ฉ”์„œ๋“œ๋ช… : updateScore
    // ๋งค๊ฐœ๋ณ€์ˆ˜ :  int no , int kor , int eng , int math
    public void updateScore(int no, int kor, int eng, int math) {
        Score saveScore = repo.findByNo(no);
        if (saveScore == null) {
            throw new RuntimeException("[" + no + "] ๋ฒˆ ์„ฑ์  ์ •๋ณด๊ฐ€ ์กด์žฌ ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค");
        }
        saveScore.setKor(kor);
        saveScore.setEng(eng);
        saveScore.setMath(math);

        saveScore.reduce();

    }


}

 

๐Ÿ”น ScoreRepository

public class ScoreService {


    private ScoreRepository repo = new ScoreRepository();

    // ์‹ ๊ทœ ์ •๋ณด๋ฅผ ๋“ฑ๋กํ•˜๋Š” ์„œ๋น„์Šค
    //  - ํ•ด๋‹น ํ•™๋ฒˆ์˜ ์„ฑ์  ์ •๋ณด๊ฐ€ ์žˆ๋Š”์ง€ ์ฒดํฌ
    //  - ์ •๋ณด๊ฐ€ ์กด์žฌํ•˜๋ฉด ์˜ค๋ฅ˜ ๋ฐœ์ƒ
    //  - ์ •๋ณด๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด ์„ฑ์ •์ •๋ณด๋ฅผ ์ €์žฅ์‹œํ‚จ๋‹ค
    // ๋ฐ˜ํ™˜ํƒ€์ž… void
    // ๋ฉ”์„œ๋“œ์ด๋ฆ„ : addNewScore
    // ๋งค๊ฐœ๋ณ€์ˆ˜ : Score score

    public void addNewScore(Score score) {
        Score saveScore = repo.findByNo(score.getNo());
        if (saveScore != null) {
            throw new RuntimeException("[" + score.getNo() + "] ์„ฑ์ ์ •๋ณด๊ฐ€ ์ด๋ฏธ ๋“ฑ๋ก ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค");
        }
        repo.save(score);
    }

    // ๋ชจ๋“  ์„ฑ์  ์ •๋ณด๋ฅผ ์ œ๊ณตํ•˜๋Š” ์„œ๋น„์Šค
    //  - ํ•™์ƒ์ •๋ณด๋ฅผ ํ•™๋ฒˆ์ˆœ์œผ๋กœ ์ •๋ ฌํ•ด์„œ ์ œ๊ณต
    // ๋ฐ˜ํ™˜ํƒ€์ž… : List<Score>
    // ๋ฉ”์„œ๋“œ๋ช… : getAllScore
    // ๋งค๊ฐœ๋ณ€์ˆ˜ : X

    /*
    interface Comparable<T> {
        int compareTo(T o1 , T o2);
        }
        o1 ๊ณผ o2์˜ ๋Œ€์†Œ ๊ด€๊ณ„๋ฅผ ๋น„๊ตํ•˜๋Š” ๊ตฌํ˜„ ๋ฉ”์„œ๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋ฉด o1๊ณผ o2๋Š” ์ •๋ ฌ์ด ๊ฐ€๋Šฅํ•˜๋‹ค
        Collections,sort(๋ฆฌ์ŠคํŠธ , Comparator๊ตฌํ˜„๊ฐ์ฒด)
        ๋ฆฌ์ŠคํŠธ.sort(Comparator ๊ตฌํ˜„๊ฐ์ฒด)
     */

    public List<Score> getAllScore() {
        List<Score> list = repo.findAll();

        // ๊ฐ๊ฐ์˜ ์„ฑ์  ์ •๋ณด๋ฅผ ํ•™๋ฒˆ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ ์ˆœ์œผ๋กœ ์ •๋ ฌ์‹œํ‚ค๊ธฐ ์œ„ํ•œ ๊ตฌํ˜„
//        list.sort(new Comparator<Score>() {
//            @Override
//            public int compare(Score s1, Score s2) {
//                return s1.getNo() - s2.getNo();
//            }
//        });
        list.sort((s1, s2) -> s1.getNo() - s2.getNo());

        return list;
    }

    // ๋ชจ๋“  ์„ฑ์  ์ •๋ณด๋ฅผ ์„ฑ์ ์ˆœ์œผ๋กœ ์ œ๊ณตํ•˜๋Š” ์„œ๋น„์Šค
    //  - ํ•™์ƒ ์„ฑ์  ์ •๋ณด๋ฅผ ์ด์ ์— ๋Œ€ํ•œ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•ด์„œ ์ œ๊ณตํ•œ๋‹ค
    // ๋ฐ˜ํ™˜ํƒ€์ž… : List<Score>
    // ๋ฉ”์„œ๋“œ๋ช… : getSortedScore
    // ๋งค๊ฐœ๋ณ€์ˆ˜ : X

    List<Score> getSoredScore() {
        List<Score> list = repo.findAll();
        list.sort((s1, s2) -> s2.getTotal() - s1.getTotal());
        return list;
    }

    // ์„ฑ์  ์ •๋ณด๋ฅผ ๋ณ€๊ฒฝํ•˜๋Š” ์„œ๋น„์Šค
    //  - ์„ฑ์  ์ •๋ณด๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด ์˜ˆ์™ธ๋ฅผ ๋ฐœ์ƒ์‹œํ‚จ๋‹ค
    //  - ์„ฑ์  ์ •๋ณด๊ฐ€ ์กด์žฌํ•˜๋ฉด ๊ตญ์–ด ์˜์šฐ ์ˆ˜ํ•™์ ์ˆ˜๋ฅผ setter ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด ๋ณ€๊ฒฝ
    //  - ๋ณ€๊ฒฝ๋œ ์„ฑ์  ์ •๋ณด๋กœ ์ด์ ๊ณผ ํ‰๊ท ์„ ๋‹ค์‹œ ์ง‘๊ณ„ํ•˜๋„๋ก reduce() ๋ฉ”์„œ๋“œ ์‹คํ–‰
    // ๋ฐ˜ํ™˜ ํƒ€์ž… : void
    // ๋ฉ”์„œ๋“œ๋ช… : updateScore
    // ๋งค๊ฐœ๋ณ€์ˆ˜ :  int no , int kor , int eng , int math
    public void updateScore(int no, int kor, int eng, int math) {
        Score saveScore = repo.findByNo(no);
        if (saveScore == null) {
            throw new RuntimeException("[" + no + "] ๋ฒˆ ์„ฑ์  ์ •๋ณด๊ฐ€ ์กด์žฌ ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค");
        }
        saveScore.setKor(kor);
        saveScore.setEng(eng);
        saveScore.setMath(math);

        saveScore.reduce();

    }


}

๐Ÿ”น ScoreUI

public class ScoreUI {

    ScoreService service = new ScoreService();
    Scanner scanner = new Scanner(System.in);

    // ๋ชจ๋“  ์„ฑ์ ์ •๋ณด ์กฐํšŒํ•ด์„œ ์ถœ๋ ฅ
    public void ์ „์ฒด์กฐํšŒ() {
        System.out.println();
        System.out.println("[์ „์ฒด ์„ฑ์  ์กฐํšŒ]");

        List<Score> scores = service.getAllScore();
        System.out.println("---------------------------------------------");
        System.out.println("ํ•™๋ฒˆ\t์ด๋ฆ„\t\t๊ตญ์–ด\t์˜์–ด\t์ˆ˜ํ•™\t์ด์ \tํ‰๊ท ");
        System.out.println("---------------------------------------------");

        for (Score score : scores) {
            System.out.print(score.getNo() + "\t");
            System.out.print(score.getName() + "\t");
            System.out.print(score.getKor() + "\t");
            System.out.print(score.getEng() + "\t");
            System.out.print(score.getMath() + "\t");
            System.out.print(score.getTotal() + "\t");
            System.out.println(score.getAverage());
        }
    }

    public void ์‹ ๊ทœ๋“ฑ๋ก() {
        System.out.println("[ ์„ฑ์  ์ •๋ณด ๋“ฑ๋ก]");
        System.out.print("### ํ•™๋ฒˆ: ");
        int no = scanner.nextInt();
        System.out.print("### ์ด๋ฆ„: ");
        String name = scanner.next();
        System.out.print("### ๊ตญ์–ด: ");
        int kor = scanner.nextInt();
        System.out.print("### ์˜์–ด: ");
        int eng = scanner.nextInt();
        System.out.print("### ์ˆ˜ํ•™: ");
        int math = scanner.nextInt();

        Score score = new Score(no, name, kor, eng, math);

        service.addNewScore(score);
    }

    public void ์„ฑ์ ์ˆ˜์ •ํ•˜๊ธฐ() {
        System.out.println("[์„ฑ์ • ์ •๋ณด ์ˆ˜์ •ํ•˜๊ธฐ]");
        System.out.println("### ํ•™๋ฒˆ๊ณผ ์„ฑ์  ์ •๋ณด๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”");
        System.out.print("### ํ•™๋ฒˆ ์ž…๋ ฅ : ");
        int no = scanner.nextInt();
        System.out.print("### ๊ตญ์–ด ์ ์ˆ˜ ์ž…๋ ฅ : ");
        int kor = scanner.nextInt();
        System.out.print("### ์˜์–ด ์ ์ˆ˜ ์ž…๋ ฅ : ");
        int eng = scanner.nextInt();
        System.out.print("### ์ˆ˜ํ•™ ์ ์ˆ˜ ์ž…๋ ฅ : ");
        int math = scanner.nextInt();

        service.updateScore(no, kor, eng, math);

    }


    public static void main(String[] args) {
        ScoreUI ui = new ScoreUI();
//        ui.์‹ ๊ทœ๋“ฑ๋ก();

        ui.์„ฑ์ ์ˆ˜์ •ํ•˜๊ธฐ();
        ui.์ „์ฒด์กฐํšŒ();
//        ui.์„ฑ์ ์กฐํšŒ();
    }
}

 

 

728x90

'Language > Java' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

Java Exception  (4) 2023.11.22
Java ์ œ๋„ค๋ฆญ, ์ปฌ๋ ‰์…˜ , Set , List , Map , Stack  (1) 2023.11.20
Java Random , Math  (0) 2023.11.16
Java Wrapper , Date , Calendar , StringBuilder , Buffer  (6) 2023.11.15
Java System , String  (1) 2023.11.14