익명클래스 1회성 객체생성..추상 클래스나 인터페이스..
package day19_AnonymousClass;
abstract class Japen {
public abstract void out();
}
interface Korea {
public void insert();
}
public class AnonymousTest {
public static void main(String[] args) {
Japen jj = new Japen() {
@Override
public void out() {
System.out.println("추상클래스 익명객체");
}
};
Korea kk = new Korea() {
@Override
public void insert() {
System.out.println("인터페이스 익명객체");
}
};
jj.out();
kk.insert();
}
}
package day19_AnonymousClass;
class KoreaOut{
String name;
public KoreaOut(String name) {
super();
this.name = name;
}
class KoreaIn {
int age;
public KoreaIn(int age) {
super();
this.age = age;
}
public void out() {
System.out.println("이름 : "+name);
System.out.println("나이 : "+age);
}
}
}
public class NestedTest {
public static void main(String[] args) {
KoreaOut out = new KoreaOut("홍길동");
KoreaOut.KoreaIn in = out.new KoreaIn(22);
in.out();
}
}
package day19_AnonymousClass;
class KoreaOut{
String name;
public KoreaOut(String name) {
super();
this.name = name;
}
}
class KoreaIn extends KoreaOut{
int age;
public KoreaIn(String name, int age) {
super(name);
this.age = age;
}
public void out() {
System.out.println("이름 : "+name);
System.out.println("나이 : "+age);
}
}
public class NestedTest {
public static void main(String[] args) {
KoreaIn in = new KoreaIn("홍길동",22);
in.out();
}
}
package day19_AnonymousClass;
class KoreaOut{
String name;
String address;
public KoreaOut(String name, String address) {
super();
this.name = name;
this.address = address;
}
class KoreaIn {
int age;
String phone;
public KoreaIn(int age, String phone) {
super();
this.age = age;
this.phone = phone;
}
public void out() {
System.out.println("이름 : "+name);
System.out.println("주소 : "+address);
System.out.println("나이 : "+age);
System.out.println("전화번호 : "+phone);
}
}
}
public class NestedTest {
public static void main(String[] args) {
KoreaOut out = new KoreaOut("홍길동","수원시팔달구");
KoreaOut.KoreaIn in = out.new KoreaIn(22,"010-1234-1234");
in.out();
}
}
package day19_AnonymousClass;
class KoreaOut{
String name;
String address;
public KoreaOut(String name, String address) {
super();
this.name = name;
this.address = address;
}
}
class KoreaIn extends KoreaOut{
int age;
String phone;
public KoreaIn(String name, String address, int age, String phone) {
super(name, address);
this.age = age;
this.phone = phone;
}
public void out() {
System.out.println("이름 : "+name);
System.out.println("주소 : "+address);
System.out.println("나이 : "+age);
System.out.println("전화번호 : "+phone);
}
}
public class NestedTest {
public static void main(String[] args) {
KoreaIn in = new KoreaIn("홍길동","수원시팔달구",23,"010-1234-1234");
in.out();
}
}
package day19_AnonymousClass;
public class DTO {
String busea,name;
int age;
public DTO() {}
public DTO(String busea, String name, int age) {
super();
this.busea = busea;
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "DTO [부서명 : " + busea + ", 이름 : " + name + ", 나이 : " + age + "]";
}
}
package day19_AnonymousClass;
import java.util.Scanner;
public class Menu {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
DAO dao = new DAO();
while(true) {
System.out.println("[1]데이터입력");
System.out.println("[2]데이터출력");
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;
}
}
}
}
package day19_AnonymousClass;
import java.util.ArrayList;
public class DAO {
static String i ="인사팀";
static String j ="자재팀";
static String c ="총무팀";
enum buse{i,j,c}; // i:인사팀 j:자재팀 c:총무팀
ArrayList<DTO> list = new ArrayList<DTO>();
public void insert() {
list.add(new DTO(i,"홍길동",24));
list.add(new DTO(j,"고고고",45));
list.add(new DTO(c,"하하하",62));
list.add(new DTO(i,"박박박",27));
list.add(new DTO(c,"김김김",36));
}
public void out() {
System.out.println("부서명\t이름\t나이");
System.out.println("==================");
for(DTO a:list) {
System.out.println(a.toString());
}
}
}
package day19_AnonymousClass;
public class SampleDTO {
String spname;
int count,price,money;
public SampleDTO() {}
public SampleDTO(String spname, int count, int price) {
super();
this.spname = spname;
this.count = count;
this.price = price;
this.money = count * price;
}
@Override
public String toString() {
return "SampleDTO [spname=" + spname + ", count=" + count + ", price=" + price + ", money=" + money + "]";
}
}
package day19_AnonymousClass;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
public class SampleDAO {
ArrayList<SampleDTO> list = new ArrayList<SampleDTO>();
public void insert() throws FileNotFoundException {
FileReader fr = new FileReader("c:/data/12292.txt");
BufferedReader br = new BufferedReader(fr);
while(true) {
try {
String str = br.readLine();
if(str==null)break;
String spname = str.substring(0, 1);
int su = Integer.parseInt(str.substring(1, 3));
int dan = Integer.parseInt(str.substring(3, 7));
if(spname.equals("a"))spname="사과";
if(spname.equals("b"))spname="바나나";
if(spname.equals("m"))spname="참외";
list.add(new SampleDTO(spname,su,dan));
}
catch (IOException e) {
e.printStackTrace();
}
}
}
public void out() {
System.out.println("상품명\t수량\t단가\t금액");
System.out.println("============================");
for(SampleDTO a:list) {
System.out.println(a.toString());
}
}
}
package day19_AnonymousClass;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
public class SampleDAO {
enum name {
a("사과"),b("바나나"),m("참외");
private final String value;
name(String value) {
this.value = value;
}
public String getValue() {
return value;
}
}
ArrayList<SampleDTO> list = new ArrayList<SampleDTO>();
public void insert() throws FileNotFoundException {
FileReader fr = new FileReader("c:/data/12292.txt");
BufferedReader br = new BufferedReader(fr);
while(true) {
try {
String str = br.readLine();
if(str==null)break;
String spname = str.substring(0, 1);
int su = Integer.parseInt(str.substring(1, 3));
int dan = Integer.parseInt(str.substring(3, 7));
list.add(new SampleDTO(name.valueOf(spname).getValue(),su,dan));
}
catch (IOException e) {
e.printStackTrace();
}
}
}
public void out() {
System.out.println("상품명\t수량\t단가\t금액");
System.out.println("============================");
for(SampleDTO a:list) {
System.out.println(a.toString());
}
}
}
package day19_AnonymousClass;
import java.text.DecimalFormat;
import java.util.Random;
import java.util.Scanner;
public class RandomTest {
public static void main(String[] args) {
// 난수 - > 임의의 수 발생 .. Math, Random..
// 주사위를 600번 던졌을때 1-6 까지 나온 횟수?
Scanner sc =new Scanner (System.in);
DecimalFormat df = new DecimalFormat("#,##0.000");
int [] a = new int [6]; // 0~5
Random rd = new Random();
System.out.print("몇번 던질까요?");
int su = sc.nextInt();
for(int i=1;i<=su;i++) {
int n = rd.nextInt(6)+1; // 주사위를 던져..값 1-6
a[n-1]++; // n=1 이면 a[0]에 1추가
// n=2 이면 a[1]에 1추가
}
for(int i=0;i<=5;i++) {
System.out.println((i+1)+"이 나온 횟수 : "+a[i]+"\t 비율 : "+df.format((double)a[i]/su*100)+"%");
}
}
}
package day19_AnonymousClass;
import java.util.Random;
import java.util.Scanner;
public class QuizTest {
public static void main(String[] args) {
Random rd = new Random();
int n = rd.nextInt(100)+1;
Scanner sc = new Scanner(System.in);
int count=0;
while(true) {
count++;
System.out.println("숫자를 입력하여 정답을 맞추세요");
int dap = sc.nextInt();
if(n>dap) System.out.println("정답은 "+dap+"보다 큽니다");
else if(n<dap) System.out.println("정답은 "+dap+"보다 작습니다");
else {
if(count<=7) {
System.out.println("천재인가? 정답입니다! 당신은 "+count+"회만에 맞췄습니다");
}
else if(count<=12) {
System.out.println("우수합니다. 정답입니다! 당신은 "+count+"회만에 맞췄습니다");
}
break;
}
}
}
}
'JAVA' 카테고리의 다른 글
230102_Thread (0) | 2023.01.02 |
---|---|
221230_SingleTon (0) | 2022.12.30 |
221228_SingleTon (0) | 2022.12.28 |
221227_static (0) | 2022.12.27 |
221226_배열 (0) | 2022.12.26 |
댓글