본문 바로가기
JAVA

230103_Collection

by 경 훈 2023. 1. 3.

Collection FrameWork

1.List : 순서가 있다. 중복 허락

ArrayList, Vector, Linkedlist

2.Set : 순서가 없다. 중복 불가

HashSet, TreeSet

3.Map

 

package day22_ThreadTotal;

import java.util.Random;

import javax.swing.JOptionPane;

//문제
class Number extends Thread {
	int ks; //문제를 맞힌 갯수
	public void run() { //난수 2개를 생성
		Random rd = new Random();
		while(true) {
			int a = rd.nextInt(10)+1;
			int b = rd.nextInt(10)+1;
			String dap=JOptionPane.showInputDialog(a+"*"+b+"="+"?");
			int c = Integer.parseInt(dap);
			if(c==(a*b)) {
				System.out.println("답이 맞아요");
				ks++;
			}
			else {
				System.out.println("답이 틀려요");
			}
		}//while-end
	}
}
//30초
class Timer extends Thread {
	public void run() {
		Number nn = new Number();
		nn.start();
		for(int i=15;i>=0;i--) {
			try {
				sleep(1000);
				System.out.println(i+"초");
			}
			catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		System.out.println("총 맞은갯수 : "+nn.ks+"개 맞혔네요.");
		System.exit(0);
	}	
}
public class Gop_Thread {
	public static void main(String[] args) {
		Timer tt = new Timer();
		tt.start();
	}
}

 

package day22_ThreadTotal;

import java.util.ArrayList;

class My {
	String name;
	int age;
	public My() {} //기본 생성자 .. default constructor
	public My(String name, int age) { //객체 초기화 생성자
		super();
		this.name = name;
		this.age = age;
	}
	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;
	}
	@Override
	public String toString() {
		return "My [name=" + name + ", age=" + age + "]";
	}
}
public class ObjectTest {

	public static void main(String[] args) {
		//객체 생성하는 방법 3가지
		ArrayList<My> list = new ArrayList<My>();
		//1
		My m1 = new My("홍길동",45);
		list.add(m1);
		//2
		list.add(new My("백두산",33));
		//3 세터와게터
		My m3 = new My();
		m3.setName("광교산");
		m3.setAge(22);
		list.add(m3);
		System.out.println("빠른 for 를 이용한 출력");
		for(My a:list) {
			//System.out.println(a.name+"\t"+a.age);
			System.out.println(a.toString());
		}
		System.out.println("람다식을 이용한 출력");
		list.forEach(a->System.out.println(a.toString()));
	}
}

 

package day22_ThreadTotal;

import java.util.ArrayList;

class My2 {
	String id,pw,add;
	public My2() {}
	public My2(String id, String pw, String add) {
		super();
		this.id = id;
		this.pw = pw;
		this.add = add;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getPw() {
		return pw;
	}
	public void setPw(String pw) {
		this.pw = pw;
	}
	public String getAdd() {
		return add;
	}
	public void setAdd(String add) {
		this.add = add;
	}
	@Override
	public String toString() {
		return "My2 [id=" + id + ", pw=" + pw + ", add=" + add + "]";
	}
}
public class Sample1 {

	public static void main(String[] args) {
		ArrayList<My2> list = new ArrayList<My2>();
		//아이디,패스워드,주소를 입력하여 객체를 생성후 출력
		My2 m1 = new My2("abcd","a1234","수원시팔달구");
		list.add(m1);
		list.add(new My2("pdpdpd","nn34nn","서울시강남구"));
		My2 m3 = new My2();
		m3.setId("fffff");
		m3.setPw("gg3456");
		m3.setAdd("강원도강릉시");
		list.add(m3);
		System.out.println("빠른 for문 출력");
		for(My2 a:list) {
			System.out.println(a.toString());
		}
		System.out.println("람다식 출력");
		list.forEach(a->System.out.println(a.toString()));
	}
}

 

package day22_Collection;

import java.util.ArrayList;
import java.util.HashSet;

public class ListTest {

	public static void main(String[] args) {
		ArrayList<String> list = new ArrayList<String>();
		ArrayList<String> list1 = new ArrayList<String>();
		list1.add("백두산");
		list1.add("천지");
//		HashSet<String> list = new HashSet<String>();
		list.add("한라산");
		list.add("한라산");
		list.add("한라산");
		list.addAll(list1);
		
		for(String a:list) {
			System.out.print(a+"\t");
		}
//		list.forEach(a->System.out.println(a.toString()));
	}
}

package day22_Collection;

public class DTO {
	String name,id,phone,add;
	int age;
	public DTO() {}
	public DTO(String name, String id, String phone, String add, int age) {
		super();
		this.name = name;
		this.id = id;
		this.phone = phone;
		this.add = add;
		this.age = age;
	}
	@Override
	public String toString() {
		return "DTO [name=" + name + ", id=" + id + ", phone=" + phone + ", add=" + add + ", age=" + age + "]";
	}
}

 

package day22_Collection;

import java.io.FileNotFoundException;
import java.util.InputMismatchException;
import java.util.Scanner;

public class Menu {

	public static void main(String[] args) throws FileNotFoundException {
		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.println("원하는 작업 입력");
			try {
				String n = sc.next();
				int nn = Integer.parseInt(n);
				switch (nn) {
				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 day22_Collection;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class DAO {
	//이름 아이디 전화 주소 나이 를 입력..객체 출력
	//입력형식..스캐너 입력 or 파일처리
	ArrayList<DTO> list = new ArrayList<DTO>();
	public void insert() throws FileNotFoundException {
		FileReader fr = new FileReader("c:/data/01031.txt");
		BufferedReader br = new BufferedReader(fr);
		while(true) {
			try {
				String str = br.readLine();
				if(str==null)break;
				String name = str.substring(0,3);
				String id = str.substring(3,7);
				String phone = str.substring(7,20);
				String add = str.substring(20,26);
				int age = Integer.parseInt(str.substring(26));
				list.add(new DTO(name,id,phone,add,age));
			} 
			catch (IOException e) {
				e.printStackTrace();
			}
			catch (NumberFormatException e) {
				System.out.println("나이에 문자열 입력 오류");
			}
		}
	}
	public void out() {
		list.forEach(a->System.out.println(a.toString()));
	}
}

package day22_Collection;

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

public class DAO {
	//이름 아이디 전화 주소 나이 를 입력..객체 출력
	//입력형식..스캐너 입력 or 파일처리
	ArrayList<DTO> list = new ArrayList<DTO>();
	Scanner sc = new Scanner(System.in);
	public void insert() {
		Input in = new Input();
		DTO dto = new DTO();
		String name = in.munja("이름");
		String id = in.munja("아이디");
		String phone = in.munja("전화번호");
		String add = in.munja("주소");
		int age = in.number("나이");
		list.add(new DTO(name,id,phone,add,age));
	}
	public void out() {
		list.forEach(a->System.out.println(a.toString()));
	}
}

 

package day22_Collection;

import java.util.Scanner;

public class Input {
	Scanner sc = new Scanner(System.in);
	public String munja(String str) {
		System.out.print(str+"을 입력해주세요");
		return sc.next();
	}
	public int number(String str) {
		int ss= 0;
		System.out.print(str+"을 입력해주세요");
		try {
			String su = sc.next();
			ss=Integer.parseInt(su);
		}
		catch (NumberFormatException e) {
			System.out.println("문자열 오류");
		}
		return ss;
	}
}

package day22_Collection;

import java.util.HashSet;
import java.util.TreeSet;

public class HashSetTest {

	public static void main(String[] args) {
		HashSet<Integer> hs = new HashSet<Integer>();
		for(int i=0;hs.size()<6;i++) {
			int n = (int)(Math.random()*45+1);
			hs.add(n);
		}
		hs.forEach(k->System.out.print(k+" "));
		System.out.println("\n================");
		TreeSet<Integer> ts = new TreeSet<Integer>();
		for(int i=0;ts.size()<6;i++) {
			int n = (int)(Math.random()*45+1);
			ts.add(n);
		}
		ts.forEach(k->System.out.print(k+" "));
	}
}

package day22_Collection;

import java.text.DecimalFormat;
import java.util.Scanner;
import java.util.TreeSet;

public class HashSetTest {

	public static void main(String[] args) {
		TreeSet<Integer> ts = new TreeSet<Integer>();
		DecimalFormat df = new DecimalFormat("#,##0");
		Scanner sc = new Scanner(System.in);
		System.out.println("어서오세요?\n얼마를 가지고 계시나요?");
		int money = sc.nextInt();
		if(money<1000) {
			System.out.println("죄송합니다."+money+"원으론 구매하지못합니다.\n최소금액은 1,000원입니다.");
			System.exit(0);
		}
		System.out.println("몇번하시겠어요?");
		int count = sc.nextInt();
		int na = money - (count*1000);
		if(na<0) {
			System.out.println("죄송합니다.\n가지고 계신 돈보다 구매금액이 "+(na*-1)+"원 초과했습니다.");
			System.exit(0);
		}
		for(int c=1;c<=count;c++) {
			ts.clear();
			for(int i=0;ts.size()<6;i++) {
				int n = (int)(Math.random()*45+1);
				ts.add(n);
			}
			ts.forEach(k->System.out.print(k+" "));
			System.out.println("");
		}
		System.out.println("===================");
		System.out.println(count+"번 로또번호를 드렸고,\n거스름돈은 "+df.format(na)+"원입니다.");
	}
}

package day22_Collection;

import java.text.DecimalFormat;
import java.util.Scanner;

public class Sample1 {

	public static void main(String[] args) {
		//급여금액을 각 화폐수의 수를 출력??
		//급여? 999999
		DecimalFormat df = new DecimalFormat("#,##0");
		Scanner sc = new Scanner(System.in);
		System.out.println("급여를 입력하시오");
		int pay = sc.nextInt();
		int [] a = {50000,10000,5000,1000,500,100,50,10,5,1};
		int [] b = new int[10];
		for(int i=0;i<=9;i++) {
			b [i] = pay/a[i];
			pay = pay-a[i]*b[i];
			System.out.println(df.format(a[i])+"원권 갯수 : "+b[i]+"개");
		}
	}
}

 

'JAVA' 카테고리의 다른 글

230105_Map  (0) 2023.01.05
230104_Map  (0) 2023.01.04
230102_Thread  (0) 2023.01.02
221230_SingleTon  (0) 2022.12.30
221229_SingleTon  (0) 2022.12.29

댓글