자바에서 자료를 입력하는 방법
1.직접대입
2.세터를 이용 --> 스캐너를 이용해서 키보드 입력
3.파일처리 ->세터를 이용
* 파일 생성
단점 : 길이가 동일(레코드)해야됨
자료표현
bit -> 8bit(=byte) -> word -> record(처리단위)
-> file -> data base -> data bank
자바에서 상속(inheritence) 은 중요
:어떤 클래스를 상속 받으면 그 클래스의 필드명과 메소드를 그냥 사용가능
인터페이스는 두개 이상 상속 받아도 상관없다.
클래스는 오직 하나만 상속가능
**정렬
DTO 클래스에 implements Comparable 인터페이스 상속후
예제 )
수정렬
if(avg>o.avg)return -1;
else
return 1;
이름정렬
return name.compareTo(o.name);
출력 out 쪽에
Collections.sort(list); 입력 후 결과확인
package day13_file;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class File_Test {
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader("c:/data/1221.txt");
BufferedReader br = new BufferedReader(fr);
System.out.println("반\t이름\t국어\t영어\t수학");
System.out.println("=================================");
while(true) {
String str = br.readLine();
if(str == null) break;
int ban = Integer.parseInt(str.substring(0, 1));
String name = str.substring(1, 4);
int kor = Integer.parseInt(str.substring(4, 7));
int eng = Integer.parseInt(str.substring(7, 10));
int math = Integer.parseInt(str.substring(10, 13));
System.out.println(ban+"\t"+name+"\t"+kor+"\t"+eng+"\t"+math);
}
}
}
package day13_file;
//반 오름차순 정렬후 출력
public class ScoreDTO implements Comparable<ScoreDTO>{
int ban;
String name;
int kor,eng,math,tot;
double avg;
char hak;
//
//
public ScoreDTO() {}
public ScoreDTO(int ban, String name, int kor, int eng, int math) {
super();
this.ban = ban;
this.name = name;
this.kor = kor;
this.eng = eng;
this.math = math;
}
public int getBan() {
return ban;
}
public void setBan(int ban) {
this.ban = ban;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
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 getTot() {
return tot;
}
public void setTot(int tot) {
this.tot = tot;
}
public double getAvg() {
return avg;
}
public void setAvg(double avg) {
this.avg = avg;
}
public char getHak() {
return hak;
}
public void setHak(char hak) {
this.hak = hak;
}
@Override
public int compareTo(ScoreDTO o) {
// if(o.ban>ban) return -1;
// else
// return 1;
return name.compareTo(o.name);
}
}
package day13_file;
import java.io.IOException;
import java.util.Scanner;
public class ScoreMenu {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
ScoreDAO dao = new ScoreDAO();
while(true) {
System.out.println("1.자료입력");
System.out.println("2.자료출력");
System.out.println("3.자료삭제");
System.out.println("0.작업종료");
System.out.print("원하는 작업번호를 입력하시오");
int n = sc.nextInt();
switch(n) {
case 0:System.exit(0);break;
case 1:dao.insert();break;
case 2:dao.out();break;
case 3:dao.delete();break;
}
}
}
}
package day13_file;
public class Total {
public int tot(int kor, int eng, int math) {//총점을 구할려면
return kor+eng+math;
}
}
package day13_file;
public class Average {
public double avg(int tot) {
return (double)tot/3;
}
}
package day13_file;
public class Grade {
public char grade(double avg) {
char grade;
switch ((int)avg/10) {
case 10:
case 9:grade='A';break;
case 8:grade='B';break;
case 7:grade='C';break;
case 6:grade='D';break;
default:grade='F';break;
}
return grade;
}
}
package day13_file;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
public class ScoreDAO {//db에 접근하는 기능들을 모아놓은 클래스
ArrayList<ScoreDTO> list = new ArrayList<ScoreDTO>();
public void insert() throws IOException {//DTO클래스 set함수에 값을 넣는
FileReader fr = new FileReader("c:/data/1221.txt");
BufferedReader br = new BufferedReader(fr);
while(true) {
String str = br.readLine();
if(str == null)break;
int ban = Integer.parseInt(str.substring(0, 1));
String name = str.substring(1, 4);
int kor = Integer.parseInt(str.substring(4, 7));
int eng = Integer.parseInt(str.substring(7, 10));
int math = Integer.parseInt(str.substring(10, 13));
// int tot = kor+eng+math;
// double avg = (double)tot/3;
ScoreDTO dto = new ScoreDTO();
dto.setBan(ban);
dto.setName(name);
dto.setKor(kor);
dto.setEng(eng);
dto.setMath(math);
//각각의 클래스와 메소드 생성후 총점,평균,학점을 구하는
Total tt = new Total();
dto.setTot(tt.tot(dto.getKor(),dto.getEng(),dto.getMath()));
// dto.setTot(tot);
Average av = new Average();
dto.setAvg(av.avg(dto.getTot()));
// dto.setAvg(avg);
Grade gr = new Grade();
dto.setHak(gr.grade(dto.getAvg()));
// char hak;
// if(avg>=90)hak='A';
// else if(avg>=80) hak='B';
// else if(avg>=70) hak='C';
// else if(avg>=60) hak='D';
// else hak='F';
// dto.setHak(hak);
list.add(dto);
}
}
public void out() {
System.out.println("반\t이름\t국어\t영어\t수학\t총점\t평균\t학점");
System.out.println("===========================================================");
DecimalFormat df = new DecimalFormat("#,#00.0");
Collections.sort(list);
for(ScoreDTO a:list) {
System.out.print(a.getBan()+"\t");
System.out.print(a.getName()+"\t");
System.out.print(a.getKor()+"\t");
System.out.print(a.getEng()+"\t");
System.out.print(a.getMath()+"\t");
System.out.print(a.getTot()+"\t");
System.out.print(df.format(a.getAvg())+"\t");
System.out.println(a.getHak());
}
}
public void delete() {
}
}
package day13_inher;
class Father{
String name;
int age;
public void out() {
System.out.println("이름 : "+name);
System.out.println("나이 : "+age);
}
}
class Son extends Father {
}
public class Inheri_test {
public static void main(String[] args) {
Father ff = new Father();
ff.name="홍길동";
ff.age=33;
ff.out();
Son ss = new Son();
ss.name="한라산";
ss.age=23;
ss.out();
}
}
package day13_inher;
class One {
int a,b;
}
interface Twosu {
public void out();
}
class Hap extends One implements Twosu{
@Override
public void out() {
System.out.println(a+"+"+b+"="+(a+b));
}
}
class Gop extends One implements Twosu{
@Override
public void out() {
System.out.println(a+"*"+b+"="+(a*b));
}
}
class Cha extends One implements Twosu{
@Override
public void out() {
System.out.println(a+"-"+b+"="+(a-b));
}
}
public class Inheri_test {
public static void main(String[] args) {
Hap hh = new Hap();
hh.a=62;
hh.b=34;
hh.out();
Gop gg= new Gop();
gg.a=62;
gg.b=34;
gg.out();
Cha cc = new Cha();
cc.a=62;
cc.b=34;
cc.out();
}
}
package day_inher2;
class Animal {
String name,food;
int age;
boolean fly;
String bigo;
String per;
}
class Rabbit extends Animal{
public void out() {
System.out.println("이름 : "+name);
System.out.println("나이 : "+age);
System.out.println("음식 : "+food);
if(fly==true) {
bigo="O 날수있다";
}
else {
bigo="X 날수없다";
}
System.out.println("날개 : "+bigo);
if(food.equals("육류")) {
per="난폭하다";
}
else {
per="온순하다";
}
System.out.println("성격 : "+per);
System.out.println("============");
}
}
class Tiger extends Animal{
public void out() {
System.out.println("이름 : "+name);
System.out.println("나이 : "+age);
System.out.println("음식 : "+food);
if(fly==true) {
bigo="O 날수있다";
}
else {
bigo="X 날수없다";
}
System.out.println("날개 : "+bigo);
if(food.equals("육류")) {
per="난폭하다";
}
else {
per="온순하다";
}
System.out.println("성격 : "+per);
System.out.println("============");
}
}
class Eagle extends Animal{
public void out() {
System.out.println("이름 : "+name);
System.out.println("나이 : "+age);
System.out.println("음식 : "+food);
if(fly==true) {
bigo="O 날수있다";
}
else {
bigo="X 날수없다";
}
System.out.println("날개 : "+bigo);
if(food.equals("육류")) {
per="난폭하다";
}
else {
per="온순하다";
}
System.out.println("성격 : "+per);
System.out.println("============");
}
}
public class Her_Test {
public static void main(String[] args) {
Rabbit rr = new Rabbit();
rr.age=2;
rr.name="토끼";
rr.food="채소";
rr.fly=false;
rr.out();
Tiger tt = new Tiger();
tt.age=4;
tt.name="호랑이";
tt.food="육류";
tt.fly=false;
tt.out();
Eagle ee = new Eagle();
ee.age=12;
ee.name="독수리";
ee.food="육류";
ee.fly=true;
ee.out();
}
}
package day13_Classinher;
public class ScoreDTO extends Hakwon implements Comparable<ScoreDTO>{
int kor,eng,mat;
int tot;
double avg;
char hak;
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 getMat() {
return mat;
}
public void setMat(int mat) {
this.mat = mat;
}
public int getTot() {
return tot;
}
public void setTot(int tot) {
this.tot = tot;
}
public double getAvg() {
return avg;
}
public void setAvg(double avg) {
this.avg = avg;
}
public char getHak() {
return hak;
}
public void setHak(char hak) {
this.hak = hak;
}
@Override
public int compareTo(ScoreDTO o) {
if(avg>o.avg)return -1;
else
return 1;
}
}
package day13_Classinher;
public class Hakwon {
String name,junum;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getJunum() {
return junum;
}
public void setJunum(String junum) {
this.junum = junum;
}
}
package day13_Classinher;
public class Process {
public int tot(int kor,int eng,int mat) {
return kor+eng+mat;
}
public double avg(int tot) {
return (double)tot/3;
}
public char hak(double avg) {
char hak;
switch ((int)avg/10) {
case 10:
case 9:hak = 'A';break;
case 8:hak = 'B';break;
case 7:hak = 'C';break;
case 6:hak = 'D';break;
default:hak = 'F';break;
}
return hak;
}
}
package day13_Classinher;
import java.io.IOException;
import java.util.Scanner;
public class ScoreMain {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
ScoreDAO dao = new ScoreDAO();
while(true) {
System.out.print("0:기능중지"+"\t");
System.out.print("1:입력"+"\t");
System.out.println("2:출력"+"\t");
System.out.print("3:삭제"+"\t");
System.out.print("4:검색"+"\t");
System.out.println("5:수정");
System.out.print("원하는 번호 입력");
int n = sc.nextInt();
switch (n) {
case 0:System.exit(0);break;
case 1:dao.insert();break;
case 2:dao.out();break;
case 3:dao.delete();break;
case 4:dao.search();break;
case 5:dao.update();break;
}
}
}
}
package day13_Classinher;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class ScoreDAO {
ArrayList<ScoreDTO> list = new ArrayList<ScoreDTO>();
DecimalFormat df = new DecimalFormat("#,#00.00");
public void insert() throws IOException {//입력
//홍길동901230-1234567099100100
FileReader fr = new FileReader("c:/data/12212.txt");
BufferedReader br = new BufferedReader(fr);
while(true) {
ScoreDTO dto = new ScoreDTO();
String str = br.readLine();
if(str==null)break;
String name = str.substring(0, 3);
String junum = str.substring(3, 17);
int kor = Integer.parseInt(str.substring(17, 20));
int eng = Integer.parseInt(str.substring(20, 23));
int mat = Integer.parseInt(str.substring(23, 26));
dto.setName(name);
dto.setJunum(junum);
dto.setKor(kor);
dto.setEng(eng);
dto.setMat(mat);
Process pc = new Process();
dto.setTot(pc.tot(dto.getKor(), dto.getEng(), dto.getMat()));
dto.setAvg(pc.avg(dto.getTot()));
dto.setHak(pc.hak(dto.getAvg()));
list.add(dto);
}
}
public void out() {//출력
System.out.println("이름\t주민번호\t\t국어\t영어\t수학\t합계\t평균\t학점");
System.out.println("====================================================");
Collections.sort(list);
for(ScoreDTO a:list) {
System.out.print(a.getName()+"\t");
System.out.print(a.getJunum()+"\t");
System.out.print(a.getKor()+"\t");
System.out.print(a.getEng()+"\t");
System.out.print(a.getMat()+"\t");
System.out.print(a.getTot()+"\t");
System.out.print(df.format(a.getAvg())+"\t");
System.out.println(a.getHak());
}
}
public void delete() {//삭제
Scanner sc = new Scanner(System.in);
int po = -1;
System.out.println("검색할 이름을 입력하시오");
String dname = sc.next();
for(int i=0;i<list.size();i++) {
if(list.get(i).getName().equals(dname)) {
po=i;
break;
}
}
if(po==-1) {
System.out.println(dname+"은 없는 이름입니다.");
}
else {
System.out.println("이름 : "+list.get(po).getName());
System.out.println("주민 : "+list.get(po).getJunum());
System.out.println("국어 : "+list.get(po).getKor());
System.out.println("영어 : "+list.get(po).getEng());
System.out.println("수학 : "+list.get(po).getMat());
System.out.println("위 데이터를 삭제 할까요?");
String qq = sc.next();
if(qq.equals("Y")||qq.equals("y")||qq.equals("ㅛ"))
{
list.remove(po);
System.out.println("삭제했습니다");
}
else {
System.out.println("삭제보류");
}
}
}
public void search() {//검색
Scanner sc =new Scanner(System.in);
int po = -1;
System.out.println("검색할 이름을 입력하시오");
String sname = sc.next();
for(int i=0;i<list.size();i++) {
if(list.get(i).getName().equals(sname)) {
po=i;
break;
}
}
if(po==-1) {
System.out.println(sname+"은 없는 이름입니다.");
}
else {
System.out.println("이름 : "+list.get(po).getName());
System.out.println("주민 : "+list.get(po).getJunum());
System.out.println("국어 : "+list.get(po).getKor());
System.out.println("영어 : "+list.get(po).getEng());
System.out.println("수학 : "+list.get(po).getMat());
System.out.println("총점 : "+list.get(po).getTot());
System.out.println("평균 : "+df.format(list.get(po).getAvg()));
System.out.println("학점 : "+list.get(po).getHak());
System.out.println("위 데이터는 검색한 데이터입니다.");
System.out.println("");
}
}
public void update() {//수정
}
}
package day4_jsp3;
public class ScoreDTO implements Comparable<ScoreDTO>{
String name;
int kor,eng,mat;
int tot;
public ScoreDTO() {}
public ScoreDTO(String name, int kor, int eng, int mat, int tot) {
super();
this.name = name;
this.kor = kor;
this.eng = eng;
this.mat = mat;
this.tot = tot;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
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 getMat() {
return mat;
}
public void setMat(int mat) {
this.mat = mat;
}
public int getTot() {
return tot;
}
public void setTot(int tot) {
this.tot = tot;
}
@Override
public int compareTo(ScoreDTO o) {
if(tot<o.tot) return 1;
else return -1;
// return name.compareTo(o.name);
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
table {
border-collapse:collapse;
border-color: orange;
text-align: center;
}
th,td {
background-color: #dddddd;
text-align: center;
}
input {
background-color: #eeeeee;
}
</style>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="scoresave.jsp" method="post">
<table border="2" align="center">
<caption>회원정보입력</caption>
<tr>
<th>이름</th>
<td><input type="text" name="name"></td>
</tr>
<tr>
<th>국어</th>
<td><input type="text" name="kor"></td>
</tr>
<tr>
<th>영어</th>
<td><input type="text" name="eng"></td>
</tr>
<tr>
<th>수학</th>
<td><input type="text" name="mat"></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="전송">
<input type="reset" value="초기화">
</td>
</tr>
</table>
</form>
</body>
</html>
<%@page import="java.util.Collections"%>
<%@page import="day4_jsp3.ScoreDTO"%>
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
table {
border-collapse:collapse;
border-color: orange;
text-align: center;
}
th,td {
background-color: #eeeeee;
text-align: center;
}
</style>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%!
String name;
int kor,eng,mat,tot;
ArrayList<ScoreDTO> list = new ArrayList<ScoreDTO>();
%>
<%
request.setCharacterEncoding("utf-8");
name=request.getParameter("name");
kor=Integer.parseInt(request.getParameter("kor"));
eng=Integer.parseInt(request.getParameter("eng"));
mat=Integer.parseInt(request.getParameter("mat"));
tot=kor+eng+mat;
int grade = 0;
ScoreDTO dto = new ScoreDTO();
list.add(new ScoreDTO(name,kor,eng,mat,tot));
%>
<table border="1" align="center">
<tr>
<th>이름</th><th>국어</th><th>영어</th><th>수학</th><th>총점</th><th>순위</th>
</tr>
<%
Collections.sort(list);
for(ScoreDTO a:list) {
name=a.getName();
kor=a.getKor();
eng=a.getEng();
mat=a.getMat();
tot=a.getTot();
grade++;
%>
<tr>
<td><%=name%></td>
<td><%=kor%></td>
<td><%=eng%></td>
<td><%=mat%></td>
<td><%=tot%></td>
<td><%=grade%></td>
<tr>
<%
}
%>
</table>
<br>
<div align="center">
<a href="scoreForm.jsp"><input type="button" value="입력으로"></a>
</div>
</body>
</html>
'JAVA' 카테고리의 다른 글
221223_상속 (0) | 2022.12.23 |
---|---|
221222_상속 (0) | 2022.12.22 |
221220_Interface (0) | 2022.12.20 |
221219_Class (0) | 2022.12.19 |
221216_Class (0) | 2022.12.16 |
댓글