//Google AdSense

1 - ② - ⑶ 하나의 패키지  서로다른 클래스에서 리턴 있고 입력 없는 메서드 선언 및 호출

서로다른 클래스에서 변수의 접근지정자가 private으로 지정되어 접근 불가할 때
어떻게 변수의 값을 setting / getting 할 수 있을까?
전역변수 하나에 setting method/ getting method를 선언

ex) private String uid;

<setting method>
return data type void
method name setUid
입력개수 1
매개변수 data type String
매개변수 name a
처리 과정 : 입력받은 값을 전역변수uid에 setting

<getting method>
return data type String
method name getUid
입력개수 0
매개변수 data type -
매개변수 name -
처리 과정 : data 영역의 전역변수uid에 담겨있는 값을 리턴

 

 

세팅이 된 모습

 

지역변수 (매개변수) 이름과 전역변수 이름이 같으면 어떻게 될까?

null이 출력된다.

메서드 내부의 지역변수들은 처리 후 데이터가 휘발된다.

 

this.를 붙이면 세팅이 된다.

 

 

1 - ② - ⑷ 하나의 패키지  서로다른 클래스에서 리턴 있고 입력 있는 메서드 선언 및 호출

 

 

package kr.or.ksmart.A;

public class User {
	
	private String uid;
	private String upw;
	private String uphone;
	private char ugender;
	private int uage;
	private String ulevel;
	
	
	public String getUid() {
		return uid;
	}
	public void setUid(String uid) {
		this.uid = uid;
		
	}
	public String getUpw() {
		return upw;
	}
	public void setUpw(String upw) {
		this.upw = upw;
	}
	public String getUphone() {
		return uphone;
	}
	public void setUphone(String uphone) {
		this.uphone = uphone;
	}
	public char getUgender() {
		return ugender;
	}
	public void setUgender(char ugender) {
		this.ugender = ugender;
	}
	public int getUage() {
		return uage;
	}
	public void setUage(int uage) {
		this.uage = uage;
	}
	public String getUlevel() {
		return ulevel;
	}
	public void setUlevel(String ulevel) {
		this.ulevel = ulevel;
	}

	
}

 

package kr.or.ksmart.A;

public class AA01 {

	public static void main(String[] args) {
				
		User u01 = new User();
		u01.setUid("id001");
		System.out.println(u01.getUid());
		
		u01.setUid("id001");
		u01.setUpw("pw001");
		u01.setUphone("010-001");
		u01.setUgender('남');
		u01.setUage(20);
		u01.setUlevel("구매자");
	
		uPrint(u01);
		
		
	
		User u02 = new User();
		
	
		u02.setUid("id002");
		u02.setUpw ("pw002");
		u02.setUphone ("010-002");
		u02.setUgender ('여');
		u02.setUage (30);
		u02.setUlevel ("판매자");
		
		uPrint(u02);
		
		
		
	
		User u03 = new User();

		
		u03.setUid ("id003");
		u03.setUpw ("pw003");
		u03.setUphone ("010-003");
		u03.setUgender ('여');
		u03.setUage (31);
		u03.setUlevel ("판매자");

		uPrint(u03);

		
	
		User u04 = new User();

		
		u04.setUid ("id004");
		u04.setUpw ("pw004");
		u04.setUphone ("010-004");
		u04.setUgender ('여');
		u04.setUage (32);
		u04.setUlevel ("관리자");

		uPrint(u04);
	

	}
	

	public static void uPrint (User getu){
		System.out.println("--- 회원가입 내역 ---");
		System.out.println(getu+"<-getu");
		System.out.println(getu.getUid() + "<- 아이디");
		System.out.println(getu.getUpw() + "<- 비밀번호");
		System.out.println(getu.getUphone() + "<- 전화번호");
		System.out.println(getu.getUgender() + "<- 성별");
		System.out.println(getu.getUage() + "<- 나이");
		System.out.println(getu.getUlevel() + "<- 권한");
		System.out.println("--- 회원가입 내역 End ---");
		
	}

	
}

흐름

 


 

AA01 Class ← Class 3가지 (User, Goods, Order) 호출

User class code

더보기
package kr.or.ksmart.A;

public class User {
	
	private String uid;
	private String upw;
	private String uphone;
	private char ugender;
	private int uage;
	private String ulevel;
	
	
	public String getUid() {
		return uid;
	}
	public void setUid(String uid) {
		this.uid = uid;
		
	}
	public String getUpw() {
		return upw;
	}
	public void setUpw(String upw) {
		this.upw = upw;
	}
	public String getUphone() {
		return uphone;
	}
	public void setUphone(String uphone) {
		this.uphone = uphone;
	}
	public char getUgender() {
		return ugender;
	}
	public void setUgender(char ugender) {
		this.ugender = ugender;
	}
	public int getUage() {
		return uage;
	}
	public void setUage(int uage) {
		this.uage = uage;
	}
	public String getUlevel() {
		return ulevel;
	}
	public void setUlevel(String ulevel) {
		this.ulevel = ulevel;
	}

	
}

 

Goods class code

더보기
package kr.or.ksmart.A;

public class Goods {

	private String pcode ;
	private String uid;
	private String pname;
	private int pjungsang ;
	private int phalin;
	private String psangse;
	
	public String getPcode() {
		return pcode;
	}
	public void setPcode(String pcode) {
		this.pcode = pcode;
	}
	public String getUid() {
		return uid;
	}
	public void setUid(String uid) {
		this.uid = uid;
	}
	public String getPname() {
		return pname;
	}
	public void setPname(String pname) {
		this.pname = pname;
	}
	public int getPjungsang() {
		return pjungsang;
	}
	public void setPjungsang(int pjungsang) {
		this.pjungsang = pjungsang;
	}
	public int getPhalin() {
		return phalin;
	}
	public void setPhalin(int phalin) {
		this.phalin = phalin;
	}
	public String getPsangse() {
		return psangse;
	}
	public void setPsangse(String psangse) {
		this.psangse = psangse;
	}
	
	
	
}

Order class code

더보기

 

package kr.or.ksmart.A;

public class Order {
	
	private String ocode;
	private String pcode;
	private String uid;
	private int ocount;
	private int oprice;
	private int ototal;
	private String oaddress;

	public String getOcode() {
		return ocode;
	}
	public void setOcode(String ocode) {
		this.ocode = ocode;
	}
	public String getPcode() {
		return pcode;
	}
	public void setPcode(String pcode) {
		this.pcode = pcode;
	}
	public String getUid() {
		return uid;
	}
	public void setUid(String uid) {
		this.uid = uid;
	}
	public int getOcount() {
		return ocount;
	}
	public void setOcount(int ocount) {
		this.ocount = ocount;
	}
	public int getOprice() {
		return oprice;
	}
	public void setOprice(int oprice) {
		this.oprice = oprice;
	}
	public int getOtotal() {
		return ototal;
	}
	public void setOtotal(int ototal) {
		this.ototal = ototal;
	}
	public String getOaddress() {
		return oaddress;
	}
	public void setOaddress(String oaddress) {
		this.oaddress = oaddress;
	}
	
	

}

AA01 class code

더보기
package kr.or.ksmart.A;

public class AA01 {

	public static void main(String[] args) {
				
		User u01 = new User();
		
		u01.setUid("id001");
		u01.setUpw("pw001");
		u01.setUphone("010-001");
		u01.setUgender('남');
		u01.setUage(20);
		u01.setUlevel("구매자");
	
		uPrint(u01);
		
		
	
		User u02 = new User();
		
	
		u02.setUid("id002");
		u02.setUpw ("pw002");
		u02.setUphone ("010-002");
		u02.setUgender ('여');
		u02.setUage (30);
		u02.setUlevel ("판매자");
		
		uPrint(u02);
		
		Goods g01 = new Goods();
		
		System.out.println(g01);
		
		g01.setPcode ("p001");
		g01.setUid (u02.getUid());
		g01.setPname ("노트북");
		g01.setPjungsang (2000000);
		g01.setPhalin (1000000);
		g01.setPsangse ("SW개발용");
		
		gPrint(g01);
		
		
		
		Order o01 =new Order();
		
		System.out.println(o01);
		
		o01.setOcode ("o001");
		o01.setPcode (g01.getPcode());
		o01.setUid (u02.getUid());
		o01.setOcount (3);
		o01.setOprice (g01.getPjungsang());
		o01.setOtotal (o01.getOcount() * o01.getOprice());
		o01.setOaddress ("금암동");
		
		oPrint(o01);
		
		
		
	
		User u03 = new User();

		
		u03.setUid ("id003");
		u03.setUpw ("pw003");
		u03.setUphone ("010-003");
		u03.setUgender ('여');
		u03.setUage (31);
		u03.setUlevel ("판매자");

		uPrint(u03);
		
		Goods g02 = new Goods();
		g02.setPcode ("p002");
		g02.setUid (u03.getUid());
		g02.setPname ("선풍기");
		g02.setPjungsang (50000);
		g02.setPhalin (40000);
		g02.setPsangse ("시원해");
		gPrint(g02);
		
		Order o02 =new Order();
		o02.setOcode("o002");
		o02.setPcode (g02.getPcode());
		o02.setUid (u03.getUid());
		o02.setOcount(5);
		o02.setOprice(g02.getPjungsang());
		o02.setOtotal(o02.getOcount() * o02.getOprice());
		o02.setOaddress ("서신동");
		oPrint(o02);

		
	
		User u04 = new User();

		
		u04.setUid ("id004");
		u04.setUpw ("pw004");
		u04.setUphone ("010-004");
		u04.setUgender ('여');
		u04.setUage (32);
		u04.setUlevel ("관리자");

		uPrint(u04);
		
		Goods g03 = new Goods();
	
		g03.setPcode ("p003");
		g03.setUid (u04.getUid());
		g03.setPname ("사과박스");
		g03.setPjungsang (100000);
		g03.setPhalin (80000);
		g03.setPsangse ("SW개발용");
	
		gPrint(g03);
		
		Order o03 =new Order();
		
		o03.setOcode ("o003");
		o03.setPcode(g03.getPcode());
		o03.setUid(u04.getUid());
		o03.setOcount(2);
		o03.setOprice (g03.getPjungsang());
		o03.setOtotal (o03.getOcount() * o03.getOprice());
		o03.setOaddress("덕진동");
	
		oPrint(o03);
	

	}
	

	public static void uPrint (User getu){
		System.out.println("--- 회원가입 내역 ---");

		System.out.println(getu.getUid() + "<- 아이디");
		System.out.println(getu.getUpw() + "<- 비밀번호");
		System.out.println(getu.getUphone() + "<- 전화번호");
		System.out.println(getu.getUgender() + "<- 성별");
		System.out.println(getu.getUage() + "<- 나이");
		System.out.println(getu.getUlevel() + "<- 권한");
		System.out.println("--- 회원가입 내역 End ---");
		
	}
	
	public static void gPrint (Goods getp) {
		System.out.println("--- 상품관리 내역 ---");

		System.out.println(getp.getPcode());
		System.out.println(getp.getUid());
		System.out.println(getp.getPname());
		System.out.println(getp.getPjungsang());
		System.out.println(getp.getPhalin());
		System.out.println(getp.getPsangse());
		System.out.println("--- 상품관리 내역 End ---");
	}

	public static void oPrint (Order geto) {

		
		System.out.println("--- 주문관리 내역 ---");
		System.out.println(geto.getOcode());
		System.out.println(geto.getPcode());
		System.out.println(geto.getUid());
		System.out.println(geto.getOcount());
		System.out.println(geto.getOprice());
		System.out.println(geto.getOtotal());
		System.out.println(geto.getOaddress());
		System.out.println("--- 주문관리 내역 End ---");
	}
	
}

천방지축 얼렁뚱땅 빙글빙글 돌아가는 흐름 ..

 

응용 - uPrint, pPrint, oPrint를 다른 TatlaPrint class로 분리하여보기

package kr.or.ksmart.A;

public class TotalPrint {
	
	
	public void uPrint (User getu){
		System.out.println("--- 회원가입 내역 ---");

		System.out.println(getu.getUid() + "<- 아이디");
		System.out.println(getu.getUpw() + "<- 비밀번호");
		System.out.println(getu.getUphone() + "<- 전화번호");
		System.out.println(getu.getUgender() + "<- 성별");
		System.out.println(getu.getUage() + "<- 나이");
		System.out.println(getu.getUlevel() + "<- 권한");
		System.out.println("--- 회원가입 내역 End ---");
		
	}

	public void gPrint (Goods getp) {
		System.out.println("--- 상품관리 내역 ---");

		System.out.println(getp.getPcode());
		System.out.println(getp.getUid());
		System.out.println(getp.getPname());
		System.out.println(getp.getPjungsang());
		System.out.println(getp.getPhalin());
		System.out.println(getp.getPsangse());
		System.out.println("--- 상품관리 내역 End ---");
	}

	public void oPrint (Order geto) {

		
		System.out.println("--- 주문관리 내역 ---");
		System.out.println(geto.getOcode());
		System.out.println(geto.getPcode());
		System.out.println(geto.getUid());
		System.out.println(geto.getOcount());
		System.out.println(geto.getOprice());
		System.out.println(geto.getOtotal());
		System.out.println(geto.getOaddress());
		System.out.println("--- 주문관리 내역 End ---");
	}
		
	

}

 

package kr.or.ksmart.A;

public class AA02 {


	public static void main(String[] args) {
		
		TotalPrint prnt = new TotalPrint();
		
		User u01 = new User();
		
		u01.setUid("id001");
		u01.setUpw("pw001");
		u01.setUphone("010-001");
		u01.setUgender('남');
		u01.setUage(20);
		u01.setUlevel("구매자");
		
		prnt.uPrint(u01);
	
	
		User u02 = new User();
		
	
		u02.setUid("id002");
		u02.setUpw ("pw002");
		u02.setUphone ("010-002");
		u02.setUgender ('여');
		u02.setUage (30);
		u02.setUlevel ("판매자");
		
		prnt.uPrint(u02);
		
		Goods g01 = new Goods();
		
		System.out.println(g01);
		
		g01.setPcode ("p001");
		g01.setUid (u02.getUid());
		g01.setPname ("노트북");
		g01.setPjungsang (2000000);
		g01.setPhalin (1000000);
		g01.setPsangse ("SW개발용");
		
		prnt.gPrint(g01);
				
		
		Order o01 =new Order();
		
		System.out.println(o01);
		
		o01.setOcode ("o001");
		o01.setPcode (g01.getPcode());
		o01.setUid (u02.getUid());
		o01.setOcount (3);
		o01.setOprice (g01.getPjungsang());
		o01.setOtotal (o01.getOcount() * o01.getOprice());
		o01.setOaddress ("금암동");
			
		prnt.oPrint(o01);
	
		User u03 = new User();

		
		u03.setUid ("id003");
		u03.setUpw ("pw003");
		u03.setUphone ("010-003");
		u03.setUgender ('여');
		u03.setUage (31);
		u03.setUlevel ("판매자");

		prnt.uPrint(u03);
		
		Goods g02 = new Goods();
		g02.setPcode ("p002");
		g02.setUid (u03.getUid());
		g02.setPname ("선풍기");
		g02.setPjungsang (50000);
		g02.setPhalin (40000);
		g02.setPsangse ("시원해");

		prnt.gPrint(g02);
		
		Order o02 =new Order();
		o02.setOcode("o002");
		o02.setPcode (g02.getPcode());
		o02.setUid (u03.getUid());
		o02.setOcount(5);
		o02.setOprice(g02.getPjungsang());
		o02.setOtotal(o02.getOcount() * o02.getOprice());
		o02.setOaddress ("서신동");
		
		prnt.oPrint(o02);

	
	
		User u04 = new User();

		
		u04.setUid ("id004");
		u04.setUpw ("pw004");
		u04.setUphone ("010-004");
		u04.setUgender ('여');
		u04.setUage (32);
		u04.setUlevel ("관리자");

		prnt.uPrint(u04);
		
		Goods g03 = new Goods();
	
		g03.setPcode ("p003");
		g03.setUid (u04.getUid());
		g03.setPname ("사과박스");
		g03.setPjungsang (100000);
		g03.setPhalin (80000);
		g03.setPsangse ("SW개발용");
		
		prnt.gPrint(g03);
	
		
		Order o03 =new Order();
		
		o03.setOcode ("o003");
		o03.setPcode(g03.getPcode());
		o03.setUid(u04.getUid());
		o03.setOcount(2);
		o03.setOprice (g03.getPjungsang());
		o03.setOtotal (o03.getOcount() * o03.getOprice());
		o03.setOaddress("덕진동");
		
		prnt.oPrint(o03);

	}

	

	
	
	
	

}

 

+ Recent posts