원시배열을 클래스배열로 바꾸기
배열에 숫자를 넣고 정렬
package re_html;
import java.util.*;
public class re_java8 {
public static void main(String[] args) {
re_java8_box rb = new re_java8_box();
// rb.boxs2();
rb.usernumsort();
}
}
class re_java8_box{
ArrayList<Object> al1 = null;
ArrayList<ArrayList<Object>> al2 = null;
Scanner sc = null;
Object data[][] = {
{"홍길동","강감찬","이순신"},
{55,80,100}
};
/*응용문제
사용자가 숫자 하나를 입력합니다
단, 배열에 같은 숫자가 있을 경우 재입력해야합니다
사용자가 입력한 숫자 값을 포함하여 내림차순으로 정렬하는 코드를 작성합니다.
*/
int numbers[] = {8, 10, 1, 22,16,14,36,31,20};
public void usernumsort(){
this.sc = new Scanner(System.in);
int user = 0;
while(true) { //입력 후 삽입
System.out.println("숫자 입력 : ");
user = this.sc.nextInt();
if(isDuplicate(user)==true) {
System.out.println("다시 입력해주세요.");
}else if(isDuplicate(user)==false) {
this.numbers[this.numbers.length-1]=user;
// printRsort(); //원시
printRsort2(); //클래스
break;
}
}
}
public boolean isDuplicate(int no) {
boolean ck = false;
int i = 0;
while (i < this.numbers.length) {
if (this.numbers[i] == no) {
ck = true;
}
i++;
}
return ck;
}
//원시배열로 정렬
public void printRsort() {
// Arrays.sort(this.numbers,Collections.reverseOrder());
Arrays.sort(this.numbers);
int i = numbers.length-1;
System.out.print("[");
while(i>=0) {
if(i==0) {
System.out.print(this.numbers[i]);
}else {
System.out.print(this.numbers[i]+", ");
}
i--;
}
System.out.println("]");
}
//클래스배열로 정렬
public void printRsort2() {
ArrayList<Integer> unlist = new ArrayList<Integer>();
int i = 0;
while(i<this.numbers.length) {
unlist.add(this.numbers[i]);
i++;
}
Collections.sort(unlist,Collections.reverseOrder());
System.out.println(unlist);
}
//1. ["홍길동","강감찬","이순신"],[55,80,100]
//2. ["홍길동",55],["강감찬", 80],["이순신",100]
//1번 예시형태의 메소드
public void boxs() {
int w = 0;
al2 = new ArrayList<>();
do {
/*
1차 클래스 배열에서 Arrays.asList를 이용할 경우 원시배열 그룹단위로 적용가능
단, 1차 클래스 배열에서 add로 원시배열그룹을 적용할 경우 오류가 발생 //데이터가 다 깨짐
*/
// this.al1 = new ArrayList<Object>(Arrays.asList(data[w]));
//위 한줄과 아래 네줄은 같은 역할
this.al1 = new ArrayList<>();
this.al1.add(data[w][0]);
this.al1.add(data[w][1]);
this.al1.add(data[w][2]);
al2.add(al1);
w++;
}while(w<data.length);
System.out.println(al2);
}
//2번 예시형태의 메소드
public void boxs2() {
//응용문제 원시 배열을 이용하여 2번형태로 출력되도록 코드를 작성
this.al2 = new ArrayList<>();
int i = 0;
int j = 0;
while(i<data[0].length) { //0~1
this.al1 = new ArrayList<>();
j = 0;
while(j<data.length) { //0~2
// System.out.println(j+", "+i);
this.al1.add(data[j][i]);
j++;
}
al2.add(al1);
i++;
}
System.out.println(al2);
}
}
숫자정렬 선생님 코드 + 추상클래스
package re_html;
import java.util.*;
public class re_java9 {
public static void main(String[] args) {
int numbers[] = {8, 10, 1, 22,16,14,36,31,20};
re_java9_box rb = new re_java9_box(numbers);
rb.main_method();
}
}
class re_java9_box extends ab1{
public re_java9_box(int numbers[]) { //즉시실행
this.sc = new Scanner(System.in);
this.number = numbers; //원시배열 복사
this.arr = new ArrayList<String>();
// System.out.println(Arrays.toString(this.number));
int w = 0;
while(w < numbers.length) {
this.arr.add(String.valueOf(numbers[w]));
w++;
}
System.out.println(this.arr);
}
@Override
public void main_method() {
System.out.println("숫자를 하나 입력해주세요 : ");
try {
int userno = this.sc.nextInt();
//contains() : 클래스 배열에서 해당 데이터가 있는지 확인하는 메소드 (true, false)
// boolean ck = this.arr.contains(String.valueOf(userno));
Integer num[] = new Integer[this.number.length+1]; //기존 데이터 배열 + 1을 해서 신규 원시 배열을 추가
boolean nock = true;
int w = 0;
while(w <this.number.length) {
if(userno == this.number[w]) {
nock = false;
break;
}
num[w] = this.number[w];
w++;
}
if(nock==false) {
System.out.println("해당 숫자는 이미 등록되어 있습니다.");
main_method();
}else {
num[num.length-1] = userno;
// Arrays.sort(num);
Arrays.sort(num,Collections.reverseOrder()); //내림차순 (Integer,String...) 클래스자료형만 먹음
System.out.println(Arrays.toString(num));
}
}catch(Exception e) {
System.out.println("숫자만 입력하세요");
new re_java9_box(this.number).main_method();
}finally {
this.sc.close();
}
}
}
자판기 + 추상클래스
추상클래스파일
package re_html;
import java.util.*;
//추상클래스
public abstract class ab1 {
Scanner sc = null;
ArrayList<String> arr = null;
ArrayList<ArrayList<String>> all = null;
String user = null;
int number[] = null;
String datas[] = null;
public abstract void main_method(); //오버라이드 필수
public Integer box1() {
return 0;
};
public String box2() {
return null;
}
public ArrayList<String> arr_method(){
return null;
}
public ArrayList<ArrayList<String>> all_method(){
return null;
}
}
자판기
package re_html;
import java.util.ArrayList;
import java.util.Scanner;
public class re_java10 {
public static void main(String[] args) {
re_java10_box rb = new re_java10_box();
rb.main_method();
}
}
class re_java10_box extends ab1{
String menu[][] = {
{"1","콜라","3200"},
{"2","커피","1700"},
{"3","아이스크림","800"},
{"4","오렌지 쥬스","1200"}
};
private int mn = 0;
private int ea = 0;
public re_java10_box() {
sc = new Scanner(System.in);
all = new ArrayList<ArrayList<String>>();
int i = 0;
int j = 0;
while(i<menu.length) {
j=0;
arr = new ArrayList<>();
while(j<menu[0].length) {
arr.add(menu[i][j]);
j++;
}
this.all.add(arr);
i++;
}
}
private void print_menu() {
System.out.println("[메뉴 배열 및 1EA 가격]");
int i = 0;
while(i<all.size()) {
System.out.print(all.get(i).get(0)+". ");
System.out.print(all.get(i).get(1)+" - ");
System.out.println(all.get(i).get(2));
i++;
}
System.out.println("[메뉴 선택]");
i = 0;
while(i<all.size()) {
System.out.print(all.get(i).get(0)+". ");
if(i==all.size()-1) {
System.out.print(all.get(i).get(1));
}else {
System.out.print(all.get(i).get(1)+", ");
}
i++;
}
System.out.println(" : ");
}
@Override
public void main_method() {
print_menu();
try {
while(true) { //메뉴 입력
this.mn = sc.nextInt();
if(this.mn>=1&&this.mn<=4) {
break;
}else {
System.out.println("다시 입력해주세요 : ");
}
}
System.out.println("해당 메뉴의 갯수를 입력하세요 : ");
while(true) { //개수 입력
this.ea = sc.nextInt();
if(this.ea>=1) {
break;
}else {
System.out.println("다시 입력해주세요 : ");
}
}
System.out.println("총 결제 금액 : "+ calc_amt()+"원");
}catch(Exception e){
System.out.println("숫자를 입력해주세요");
new re_java10_box().main_method();
}finally {
sc.close();
}
}
private int calc_amt() {
int m = Integer.parseInt(all.get(mn-1).get(2)) * ea;
return m;
}
}