부산IT JAVA 5

서현우·2022년 3월 24일
0

부산IT 자바

목록 보기
8/11

입력받은 객체를 배열에 저장

BookVO, BookVOMain 한세트

BookVO.java

package com.day05;

public class BookVO {
private String title;
private String writer;

public BookVO(String title, String writer) {
	this.title=title;
	this.writer=writer;
}

public String getTitle() {
	return title;
}

public String getWriter() {
	return writer;
}
}

BookVOMain.java

package com.day05;

import java.util.Scanner;

public class BookVOMain {
	//static으로 
	public static Scanner sc = new Scanner(System.in);
	//배열 arr ==>크기 50
	BookVO[] arr= new BookVO[50];
	int cnt;
	
	public static void showMenu() {
		System.out.println("선택하세요...");
		System.out.println("1. 데이터입력");
		System.out.println("2. 전체보기");
		System.out.println("3. 종료");
		System.out.println("선택:");
	}
	public void bookInsert() {
		System.out.println("데이터입력....");
		System.out.println("책제목>>");
		// aaa bbb ccc 같이 띄워쓴 것 한줄 전체 입력
		String title=sc.nextLine();
		System.out.println("저자>>");
		String writer=sc.nextLine();
		arr[cnt++]=new BookVO(title,writer);
	}
	public void bookList() {
		System.out.println("책리스트...");
		
		//cnt미만으로 범위지정 했으므로 if(arr[i]==null) break; 안써도 됨.
		for(int i=0;i<cnt;i++) {
			System.out.println("책제목:"+arr[i].getTitle());
			System.out.println("저자:"+arr[i].getWriter());
			System.out.println();
		}
	}
	
	public static void main(String[] args) {

		BookVOMain bm = new BookVOMain();
		while(true){
			//showMenu()를 스태틱으로
			BookVOMain.showMenu();
			int num = BookVOMain.sc.nextInt();
			//bookInsert()의 nextLine() 하나를 버림
			sc.nextLine();
			switch(num) {
			case 1: bm.bookInsert(); break;//추가
			case 2: bm.bookList(); break;//리스트
			case 3: System.out.println("종료"); System.exit(0);
			default: System.out.println("입력오류");
			}
	}
	}
}

String 클래스의 개념과 메서드(알고리즘만 정리해 놓음)

package com.day05;

public class StringTest04 {

	public static void main(String[] args) {
		String msg = "This/is/a/Java";
		//split()을 이용해서 Java a is This로 출력
		String[] arr = msg.split("/");
		for(int i=arr.length-1;i>=0;i--) {
			System.out.print(arr[i]+"\t");
		}
		System.out.println();
		
        //avaJ a si sihT로 출력
		for(int i=arr.length-1;i>=0;i--) {
			for(int j=arr[i].length()-1;j>=0;j--) {
				System.out.print(arr[i].charAt(j));
			}
			System.out.print(" ");
		}
		System.out.println();
		
        //알파벳에 숫자 더해서 문자변경
        System.out.println((char)('A'+2));
		
		//문자 하나하나에 2를 더해서 문자열을 출력
		String str = "Hi.. Glad to meet you!!!!"; //jK...
		for(int i=0;i<str.length()-1;i++) {
			System.out.print((char)(str.charAt(i)+2));
		}
		System.out.println();
		
		//str에서 meet만 추출
		//substring()
		System.out.println(str.substring(0,3));
		System.out.println(str.substring(str.indexOf("m"),str.indexOf("m")+4));
		System.out.println(str.substring(str.indexOf("m")));
		
		//indexOf(), lastIndexOf()
		System.out.println(str.indexOf("t"));
		System.out.println(str.lastIndexOf("t"));	
	}	
}

상속

Circle, Square, Shape, ShapeManager, ShapeMain 한세트

Circle.java

package com.day05;

public class Circle extends Shape {
//중심(x,y) 반지름 r
	private int r;
	public Circle(int x, int y, int r) {
		super(x,y);
		this.r=r;
	}
	public void print() {
		super.print();
		System.out.println("반지름:"+r);
	}
}

Square.java

package com.day05;

public class Square extends Shape{
	//중심(x,y) 가로(width) 세로(height)
	private int width;
	private int height;
	public Square(int x, int y, int width, int height) {
		super(x,y);
		this.width=width;
		this.height=height;
	}
	public void print() {
		super.print();
		System.out.println("가로:"+width);
		System.out.println("세로:"+height);
	}
}

Shape.java

package com.day05;

public class Shape {

	private int x;
	private int y;
	
	public Shape(int x, int y) {
		this.x=x;
		this.y=y;
	}
	
	public void print() {
		System.out.println("좌표(x,y)=("+x+","+y+")");
	}
}

ShapeManager.java

package com.day05;

public class ShapeManager {
	private Shape[] arr = new Shape[10];
	private int cnt;
	//도형입력
	public void inputData(int num) {
		System.out.println("도형입력");
		System.out.println("x좌표>>");
		int x=ShapeMain.sc.nextInt();
		System.out.println("y좌표>>");
		int y=ShapeMain.sc.nextInt();
		if(num==1) { //원
			System.out.println("반지름>>");
			int r = ShapeMain.sc.nextInt();
			arr[cnt++]=new Circle(x,y,r ); //Shape=Circle
		}	else if(num==2) { //사각형
			System.out.println("가로 폭>>");
			int w = ShapeMain.sc.nextInt();
			System.out.println("세로 높이>>");
			int h = ShapeMain.sc.nextInt();
			arr[cnt++]=new Square(x,y,w,h); //Shape=Square
		}
	}
	
//도형출력
	public void viewData() {
		for(int i=0;i<cnt;i++) {
			arr[i].print();
		}
	}

}

ShapeMain.java

package com.day05;

import java.util.Scanner;

public class ShapeMain {
	static Scanner sc = new Scanner(System.in);
	public static void showMenu() {
		System.out.println("선택");
		System.out.println("1.원,2.사각형,3.보기,4.종료");
		System.out.println("선택>>");
	}

	public static void main(String[] args) {
		ShapeManager sm = new ShapeManager();
		
		while(true) {
			ShapeMain.showMenu();
			int num = ShapeMain.sc.nextInt();
			switch(num) {
			case 1: 
			case 2:	sm.inputData(num); break;
			case 3:	sm.viewData(); break;
			case 4:	System.out.println("종료");
					System.exit(0);
			default: System.out.println("입력오류");
			}
		}	
	}
}

다형성 개념

profile
안녕하세요!!

0개의 댓글

관련 채용 정보