싱글턴(SingleTon) : 객체를 1개 생성하여 공유 -> static
package day18_SingleTon2;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.text.DecimalFormat;
public class ReportDAO {
private SingleTon ston;
public ReportDAO() {
ston = SingleTon.korea();
}
public void insert() throws FileNotFoundException {
FileReader fr = new FileReader("c:/data/12281.txt");
BufferedReader br = new BufferedReader(fr);
while(true) {
try {
ReportDTO dto = new ReportDTO();
String str = br.readLine();
if(str==null) break; //자료가없으면
String name = str.substring(0,3);
int kor = Integer.parseInt(str.substring(3, 6));
int eng = Integer.parseInt(str.substring(6, 9));
int mat = Integer.parseInt(str.substring(9, 12));
int tot = kor+eng+mat;
double avg = (double)tot/3;
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.setName(name);
dto.setKor(kor);
dto.setEng(eng);
dto.setMat(mat);
dto.setTot(tot);
dto.setAvg(avg);
dto.setHak(hak);
ston.addin(dto);
}
catch (IOException e) {
e.printStackTrace();
}
}
}
public void out() {
DecimalFormat df = new DecimalFormat("#,#00.00");
System.out.println("이름\t국어\t영어\t수학\t총점\t평균\t학점");
System.out.print("==========================");
System.out.println("==========================");
for(ReportDTO aa:ston.out2()) {
System.out.print(aa.getName()+"\t");
System.out.print(aa.getKor()+"\t");
System.out.print(aa.getEng()+"\t");
System.out.print(aa.getMat()+"\t");
System.out.print(aa.getTot()+"\t");
System.out.print(df.format(aa.getAvg())+"\t");
System.out.println(aa.getHak()+"\t");
}
}
}
package day18_SingleTon2;
import java.util.ArrayList;
public class SingleTon {
private static SingleTon ston;
ArrayList<ReportDTO> list = new ArrayList<ReportDTO>();
//ston 객체를 공유..메소드를 호출하면 반환
public static SingleTon korea() {
if(ston==null)
ston = new SingleTon();
return ston;
}
//자료추가 메소드
public void addin(ReportDTO dto) {
list.add(dto);
}
//출력기능
public ArrayList<ReportDTO> out2() {
return list;
}
}
package day18_SingleTon2;
public class DTOtest {
String id,phone,address;
public DTOtest() {}
public DTOtest(String id, String phone, String address) {
super();
this.id = id;
this.phone = phone;
this.address = address;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
package day18_SingleTon2;
import java.io.FileNotFoundException;
import java.util.InputMismatchException;
import java.util.Scanner;
public class Maintest {
public static void main(String[] args) throws FileNotFoundException {
Scanner sc = new Scanner(System.in);
DAOtest dao = new DAOtest();
while(true) {
System.out.println("[1]데이터입력");
System.out.println("[2]데이터출력");
System.out.println("[0]종료");
System.out.print("원하는 작업번호 입력");
String qq = sc.next();
try {
int n = Integer.parseInt(qq);
switch (n) {
case 0:System.exit(0);break;
case 1:dao.insert();break;
case 2:dao.out();break;
}
}
catch(InputMismatchException e) {
System.out.println("★작업번호에 문자열 입력오류★");
}
catch(NumberFormatException e) {
System.out.println("★작업번호에 문자열 입력오류★");
}
}
}
}
package day18_SingleTon2;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
public class DAOtest {
private SingleTontest ston;
public DAOtest() {
ston = SingleTontest.gogo();
}
public void insert() throws FileNotFoundException {
FileReader fr = new FileReader("c:/data/12282.txt");
BufferedReader br = new BufferedReader(fr);
while(true) {
try {
DTOtest dto = new DTOtest();
String str = br.readLine();
if(str==null) break;
dto.setId(str.substring(0, 5));
dto.setPhone(str.substring(5, 18));
dto.setAddress(str.substring(18));
ston.addin(dto);
}
catch (IOException e) {
e.printStackTrace();
}
}
}
public void out() {
System.out.println("아이디\t전화번호\t\t주소");
System.out.print("=====================");
System.out.println("===================");
for(DTOtest aa:ston.out()) {
System.out.print(aa.getId()+"\t");
System.out.print(aa.getPhone()+"\t");
System.out.println(aa.getAddress()+"\t");
}
}
}
package day18_SingleTon2;
import java.util.ArrayList;
public class SingleTontest {
private static SingleTontest ston;
ArrayList<DTOtest> list = new ArrayList<DTOtest>();
public static SingleTontest gogo() {
if (ston==null)
ston = new SingleTontest();
return ston;
}
public void addin(DTOtest dto) {
list.add(dto);
}
public ArrayList<DTOtest> out() {
return list;
}
}
package day18_ClassTest;
class Father {
String name;
int money;
public Father(String name, int money) {
this.name = name;
this.money = money;
}
}
class Son extends Father{
String name;
int money;
public Son(String name, int money, String name2, int money2) {
super(name, money);//상속받은 클래스 생성자를 호출..값을 넘겨줌
this.name = name2;
this.money = money2;
}
public void out() {
System.out.println("아버지 이름 : "+super.name);
System.out.println("아버지 돈 : "+super.money);
System.out.println("아들 이름 : "+this.name);
System.out.println("아들 돈 : "+this.money);
}
}
public class Sample {
public static void main(String[] args) {
Son ss = new Son("홍길동",10000,"홍기남",5000);
ss.out();
}
}
package day18_ClassTest;
class Jubon {
String name,jumin,address;
public Jubon(String name, String jumin, String address) {
super();
this.name = name;
this.jumin = jumin;
this.address = address;
}
}
class School extends Jubon{
String hakbun;
int kor,eng,mat;
public School(String name, String jumin, String address, String hakbun, int kor, int eng, int mat) {
super(name, jumin, address);
this.hakbun = hakbun;
this.kor = kor;
this.eng = eng;
this.mat = mat;
}
public void out() {
System.out.println("이름 : "+super.name);
System.out.println("주민번호 : "+super.jumin);
System.out.println("주소 : "+super.address);
System.out.println("학번 : "+this.hakbun);
System.out.println("국어 : "+this.kor);
System.out.println("영어 : "+this.eng);
System.out.println("수학 : "+this.mat);
}
}
public class Sample2 {
public static void main(String[] args) {
School sh = new School("홍길동", "950101-1111222", "수원시 팔달구", "22-1234567", 80, 85, 100);
sh.out();
}
}
package day18_FileConstructor;
class Jumin {
String name,juminsumber,address;
public Jumin(String name, String juminsumber, String address) {
super();
this.name = name;
this.juminsumber = juminsumber;
this.address = address;
}
}
public class SchoolDTO extends Jumin{
String bun;
int kor,eng,mat;
public SchoolDTO(String name, String juminsumber, String address, String bun, int kor, int eng, int mat) {
super(name, juminsumber, address);
this.bun = bun;
this.kor = kor;
this.eng = eng;
this.mat = mat;
}
}
package day18_FileConstructor;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
public class SchoolDAO {
ArrayList<SchoolDTO> list = new ArrayList<SchoolDTO>();
public void insert() throws FileNotFoundException {
FileReader fr = new FileReader("c:/data/12283.txt");
BufferedReader br = new BufferedReader(fr);
while(true) {
try {
String str = br.readLine();
if(str==null)break;
String name = str.substring(0, 3);
String junum = str.substring(3, 17);
String address = str.substring(17, 23);
String bun = str.substring(23, 30);
int kor = Integer.parseInt(str.substring(30, 33));
int eng = Integer.parseInt(str.substring(33, 36));
int mat = Integer.parseInt(str.substring(36, 39));
list.add(new SchoolDTO(name, junum, address, bun, kor, eng, mat));
}
catch (IOException e) {
e.printStackTrace();
}
}
}
public void out() {
System.out.println("이름\t주민번호\t\t주소\t\t학번\t국어\t영어\t수학");
System.out.print("==================================");
System.out.println("==================================");
for(SchoolDTO a:list) {
System.out.print(a.name+"\t");
System.out.print(a.juminsumber+"\t");
System.out.print(a.address+"\t");
System.out.print(a.bun+"\t");
System.out.print(a.kor+"\t");
System.out.print(a.eng+"\t");
System.out.println(a.mat);
}
}
}
package day18_FileConstructor;
import java.io.FileNotFoundException;
import java.util.InputMismatchException;
import java.util.Scanner;
public class Schoolmenu {
public static void main(String[] args) throws FileNotFoundException {
Scanner sc = new Scanner(System.in);
SchoolDAO dao = new SchoolDAO();
while(true) {
System.out.println("[1]데이터입력");
System.out.println("[2]데이터출력");
System.out.println("[0]종료");
System.out.print("원하는 작업번호 입력");
String qq = sc.next();
try {
int n = Integer.parseInt(qq);
switch (n) {
case 0:System.exit(0);break;
case 1:dao.insert();break;
case 2:dao.out();break;
}
}
catch(InputMismatchException e) {
System.out.println("★작업번호에 문자열 입력오류★");
}
catch(NumberFormatException e) {
System.out.println("★작업번호에 문자열 입력오류★");
}
}
}
}
package day18_FileConstructor;
import java.text.DecimalFormat;
public class Nansu {
public static void main(String[] args) {
DecimalFormat df = new DecimalFormat("#,##0");
int a=0;
int b=0;
int c=0;
int d=0;
int e=0;
int f=0;
int p=1000000000;
for(int i=1;i<=p;i++) {
int n = (int)(Math.random()*6)+1;
if(n==1) a++;
else if(n==2) b++;
else if(n==3) c++;
else if(n==4) d++;
else if(n==5) e++;
else if(n==6) f++;
}
System.out.println(df.format(p)+"번 돌릴경우");
System.out.println("1의 경우의수 : "+df.format(a)+"번");
System.out.println("2의 경우의수 : "+df.format(b)+"번");
System.out.println("3의 경우의수 : "+df.format(c)+"번");
System.out.println("4의 경우의수 : "+df.format(d)+"번");
System.out.println("5의 경우의수 : "+df.format(e)+"번");
System.out.println("6의 경우의수 : "+df.format(f)+"번");
}
}
package day18_FileConstructor;
public class Nansu {
public static void main(String[] args) {
int [] a = new int [6];
int su = 6000000;
for(int i=1;i<=su;i++) {
int n = (int)(Math.random()*6+1);
a[n-1]++;
}
for(int i=0;i<=5;i++) {
System.out.println((i)+"이 나온횟수"+a[i]+"\t\t"+(double)a[i]/su*100+"%");
}
}
}
package day18_FileConstructor;
import java.util.Random;
public class Nansu {
public static void main(String[] args) {
Random rd = new Random();
int [] a = new int [6];
int su = 6000000;
for(int i=1;i<=su;i++) {
int n = (int)rd.nextInt(6)+1;
a[n-1]++;
}
for(int i=0;i<=5;i++) {
System.out.println((i)+"이 나온횟수"+a[i]+"\t\t"+(double)a[i]/su*100+"%");
}
}
}
'JAVA' 카테고리의 다른 글
221230_SingleTon (0) | 2022.12.30 |
---|---|
221229_SingleTon (0) | 2022.12.29 |
221227_static (0) | 2022.12.27 |
221226_배열 (0) | 2022.12.26 |
221223_상속 (0) | 2022.12.23 |
댓글