몰입하며 나아가는 개발이란

Language/Java

[Array List]practice ([Array List]연습), mvc pattern(?)

류하을 2019. 12. 2. 10:11

package main

package main;

import java.io.IOException;
import java.util.Scanner;

import controller.FileController;
import controller.StudentController;

public class mainClass {
	public static void main(String[] args) throws IOException {

		Scanner sc = new Scanner(System.in);
		StudentController studentController = new StudentController();
		FileController fileController = new FileController();

		out: while (true) {
			System.out.println("===== 비트 학원 성적 조회 프로그램 =====");
			System.out.println(
					"1.학생정보추가\n2.학생정보 삭제\n3.학생정보 검색\n4.학생정보 수정\n5.학생 성적조회\n6.학생 성적총점\n7.학생성적 평균\n8.학생성적순 출력\n9.메모장 출력\n0.종료");
			System.out.print("메뉴를 고르세요: ");
			int choice = 0;
			choice = sc.nextInt();
			switch (choice) {
			case 1:
				// 학생추가
				studentController.studentAdd(sc);
				break;
			case 2:
				// 학생삭제
				studentController.studentDel(sc);
				break;
			case 3:
				// 학생검색
				studentController.studentSelect(sc);
				break;
			case 4:
				// 학생수정
				studentController.studentUpdate(sc);
				break;
			case 5:
				// 학생 성적 조회
				studentController.studentTotal(sc);
				break;
			case 6:
				// 학생 전체 총점 평균
				studentController.studentTotalSum();
				break;
			case 7:
				// 학생 전체 성적평균
				studentController.studentTotalAvg();
				break;
			case 8:
				// 학생 성적순 출력
				studentController.studentSorting();
				break;
			case 9:
				// 메모장 출력
				fileController.saveFile(studentController);
				break;
			case 0:
				// 프로그램종료
				studentController.showAll();
				break out;
			default:
				System.out.println("잘못입력하셨습니다. 다시 입력해주세요.");
			}
		}
	}
}

package dto 

 

package dto;

public class StudentDto {

	private String name;
	private int age;
	private int kor;
	private int eng;
	private int math;
	private int sum;

	public StudentDto() {
		super();
		this.sum = kor + eng + math;
	}

	public StudentDto(String name, int age, int kor, int eng, int math) {
		super();
		this.name = name;
		this.age = age;
		this.kor = kor;
		this.eng = eng;
		this.math = math;
		this.sum = kor + eng + math;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public int getKor() {
		return kor;
	}

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

	public int getEng() {
		return eng;
	}

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

	public int getMath() {
		return math;
	}

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

	public int getSum() {
		return sum;
	}

	@Override
	public String toString() {
		return "StudentDto [name=" + name + ", age=" + age + ", kor=" + kor + ", eng=" + eng + ", math=" + math + "]";
	}

}

package controller

package controller;

import java.util.ArrayList;
import java.util.Scanner;

import dto.StudentDto;

public class StudentController {
	ArrayList<StudentDto> list;

	public StudentController() {
		list = new ArrayList<StudentDto>();

		list.add(new StudentDto("lee", 24, 85, 95, 75));
		list.add(new StudentDto("kim", 20, 95, 95, 95));
		list.add(new StudentDto("kang", 24, 75, 85, 70));
		list.add(new StudentDto("park", 25, 95, 90, 95));
	}

	public int selectOne(String name, int age) {
		int index = -1;
		for (int i = 0; i < list.size(); i++) {
			if (list.get(i).getName().equals(name) && list.get(i).getAge() == age) {
				index = i;
				break;
			}
		}
		return index;
	}

	public void showAll() {
		for (int i = 0; i < list.size(); i++) {
			System.out.println(list.get(i).toString());
		}
	}

	public void studentAdd(Scanner sc) {
		System.out.println("이름: ");
		String name = sc.next();
		System.out.println("나이: ");
		int age = sc.nextInt();
		System.out.println("국어: ");
		int kor = sc.nextInt();
		System.out.println("영어: ");
		int eng = sc.nextInt();
		System.out.println("수학: ");
		int math = sc.nextInt();
		list.add(new StudentDto(name, age, kor, eng, math));
		System.out.println("추가되었습니다.");
	}

	public void studentSelect(Scanner sc) {
		System.out.print("검색할 학생의 이름: ");
		String name = sc.next();
		System.out.print("나이: ");
		int age = sc.nextInt();
		int findOne = selectOne(name, age);
		System.out.println(list.get(findOne).toString());
	}

	public void studentDel(Scanner sc) {
		System.out.print("삭제할 학생의 이름: ");
		String name = sc.next();
		System.out.print("나이: ");
		int age = sc.nextInt();
		int findOne = selectOne(name, age);
		list.remove(findOne);
		System.out.println("삭제되었습니다.");
	}

	public void studentUpdate(Scanner sc) {
		System.out.print("수정할 학생의 이름: ");
		String name = sc.next();
		System.out.print("나이: ");
		int age = sc.nextInt();
		int findOne = selectOne(name, age);
		System.out.print("국어: ");
		list.get(findOne).setKor(sc.nextInt());
		System.out.print("영어: ");
		list.get(findOne).setEng(sc.nextInt());
		System.out.print("수학: ");
		list.get(findOne).setMath(sc.nextInt());
		System.out.println("수정이 완료되었습니다.");
	}

	public void studentTotal(Scanner sc) {
		System.out.print("성적 조회할 학생의 이름: ");
		String name = sc.next();
		System.out.print("나이: ");
		int age = sc.nextInt();
		int findOne = selectOne(name, age);
		double avg = (double) list.get(findOne).getSum() / 3.0;
		System.out.println(list.get(findOne).toString() + " 총점 : " + list.get(findOne).getSum() + " 평균 : " + (int) avg);
	}

	public void studentTotalSum() {
		if (list.size() > 0) {
			int totalSum = 0;
			for (int i = 0; i < list.size(); i++) {
				totalSum += list.get(i).getSum();
			}
			System.out.println("학생 총점 평균 : " + (int) ((double) totalSum / list.size()));
		} else {
			System.out.println("학생정보가 없습니다.");
			return;
		}
	}

	public void studentTotalAvg() {
		if (list.size() > 0) {
			double totalAvg = 0.0;
			for (int i = 0; i < list.size(); i++) {
				totalAvg += list.get(i).getSum() / 3.0;
			}
			System.out.println("학생평균점수 : " + (int) (totalAvg / list.size()));
		} else {
			System.out.println("학생정보가 없습니다.");
			return;
		}
	}

	public void studentSorting() {
		if (list.size() > 0) {
			ArrayList<StudentDto> sort = new ArrayList<StudentDto>();

			for (int i = 0; i < list.size(); i++) {
				sort.add(list.get(i));
			}

			for (int i = 0; i < sort.size(); i++) {
				System.out.println(sort.get(i).toString() + " 확인");
			}
			System.out.println(sort.get(0).getSum());
			System.out.println(sort.get(1).getSum());

			StudentDto temp;
			for (int i = 0; i < sort.size() - 1; i++) {
				for (int j = i + 1; j < sort.size(); j++) {
					if (sort.get(i).getSum() < sort.get(j).getSum()) {
						temp = sort.get(i);
						sort.set(i, sort.get(j));
						sort.set(j, temp);
					}
				}
			}

			for (int i = 0; i < sort.size(); i++) {
				System.out.println((i + 1) + " 등 " + sort.get(i).toString() + " sort");
			}

		} else {
			System.out.println("학생정보가 없습니다.");
			return;
		}
	}
}
package controller;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

public class FileController {
	File file;
	FileWriter fWriter;
	BufferedWriter bw;
	PrintWriter pw;

	public FileController() throws IOException {
		file = new File("d:/tmp/NewStudentFile.txt");
		fWriter = new FileWriter(file);
		bw = new BufferedWriter(fWriter);
		pw = new PrintWriter(bw);

	}

	public void saveFile(StudentController studentController) {
		for (int i = 0; i < studentController.list.size(); i++) {
			pw.print(studentController.list.get(i) + " ");
			pw.print("\n");
		}
		pw.close();
	}
}