Java 6일차 File I/O 테스트

/* 성적 관리, 입출력 부분 추가 */

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.NoSuchElementException;
import java.util.Scanner;
import java.util.Vector;

public class FileTest
{
public static void main(String args[])
{
Vector<Student> vs = new Vector<>();

try
{
Scanner sc = new Scanner(new File("C:\\java\\workspace\\score.txt"));

while(sc.hasNextLine())
{
int num = sc.nextInt();
String n = sc.next();
int s1 = sc.nextInt();
int s2 = sc.nextInt();
int s3 = sc.nextInt();
vs.add(new Student(num, n, s1, s2, s3));
}
}
catch(FileNotFoundException e)
{
System.out.println("FileNotFoundException");
}
catch(NoSuchElementException e)
{
}

while(true)
{
System.out.println("1.입력 2.출력 3.삭제 4.종료");
Scanner scc = new Scanner(System.in);
int choice = scc.nextInt();

if(choice==1)
{
System.out.print("Student Number : ");
int num = scc.nextInt();

boolean check = true;
for(int i=0; i<vs.size(); i++)
{
if(vs.get(i).number == num)
check = false;
}

System.out.print("Name : ");
String n = scc.next();
System.out.print("Score1 : ");
int s1 = scc.nextInt();
System.out.print("Score1 : ");
int s2 = scc.nextInt();
System.out.print("Score1 : ");
int s3 = scc.nextInt();

if(check)
vs.add(new Student(num, n, s1, s2, s3));
else
System.out.println("Number overlap");

try
{
FileWriter fw = new FileWriter("C:\\java\\workspace\\score.txt");
fw.flush();

for(int i=0; i<vs.size(); i++)
{
fw.write(vs.get(i).number+" "+vs.get(i).name+" "
+vs.get(i).score1+" "+vs.get(i).score2+" "
+vs.get(i).score3+"\n");
}

fw.close();
}
catch (IOException e)
{
System.out.println("IOException");
}
}

else if(choice==2)
{
int sumAvg = 0;
for(int i=0; i<vs.size(); i++)
{
sumAvg += vs.get(i).sum;
vs.get(i).Print();
}
System.out.println("Total Average : "+sumAvg/vs.size());
}

else if(choice==3)
{
System.out.print("Remove Number : ");
int i = scc.nextInt();
vs.remove(i-1);
}

else if(choice==4)
break;
}
}
}

댓글 없음: