기록

*15일차 실습 (객체배열 실습문제) 본문

학원/실습

*15일차 실습 (객체배열 실습문제)

pringspring 2022. 2. 11. 16:54

[문제 1] 

- 클래스 : com.oop.coffee.model.dto.Coffee.java
 - control 클래스 작성 : com.oop.coffee.controller.CoffeeManager.java
 - 실행클래스 : com.oop.coffee.run.Run.java
=> main() 메소드 포함함
 - Coffee 멤버변수 원산지 origin, 커피명 coffeeName
  기본생성자, 모든필드초기화생성자
  getter, setter
  출력메소드      ()
 - control클래스 구현내용.
1. Coffee 클래스에 대한 객체 배열(세계3대커피정보) 선언함. 배열크기 3.
2. 사용자에게 키보드로 세계3대 커피정보를 입력받아, 각 객체에 기록함
3. 출력 확인함

<세계3대커피>
---------------------------
원산지 커피명
---------------------------
예멘 모카마타리
자메이카     블루마운틴
하와이 코나
---------------------------

 - main() 에서 구현할 내용
1. control클래스 객체생성 및 입력/출력 메소드 호출

 

 

 

package com.oop.coffee.controller;

import java.util.Scanner;

import com.oop.coffee.model.dto.Coffee;

import kh.java.object.array.person.Person;
import kh.java.object.array.student.Student;


public class CoffeeManager {
	private Scanner sc = new Scanner(System.in);
	private static final int LENGTH = 3;
	private int index = 0; 
	private Coffee[] coffee = new Coffee[LENGTH];
	
	
	public void insertCoffee() {
		for(int i=0;i<coffee.length;i++) {
			Coffee cf = new Coffee();
			System.out.println("원산지:  ");
			String origin = sc.next();
			cf.setOrigin(origin);
			System.out.println("커피명:  ");
			String name= sc.next();
			cf.setcoffeeName(name);

			
			coffee[i]=cf;
			index++;
		}
	}
	
	public void printCoffee() {

		System.out.println("\n\n<세계3대커피>");
		System.out.println("-------------------");
		System.out.println("원산지\t커피명");
		
		for(int i=0; i<index;i++) {
			Coffee c = coffee[i];
			System.out.println(c.CoffeePrint());

		}
		
	}

}
--------------------------
package com.oop.coffee.model.dto;

public class Coffee {
	private String origin;
	private String coffeeName;
	
	//기본생성자
	public Coffee() {	
	}
	
	public Coffee(String origin, String coffeeName) {
		this.origin=origin;
		this.coffeeName=coffeeName;
	}
	
	public void setcoffeeName(String coffeeName) {
		this.coffeeName=coffeeName;
	}
	public String getcoffeeName() {
		return coffeeName;
	}
	public void setOrigin(String origin) {
		this.origin=origin;
	}
	public String getOrigin() {
		return origin;
	}
	

	public String CoffeePrint() {
		return origin+"\t\t"+coffeeName;

	}
	
}
---------------------------
package com.oop.coffee.run;

import com.oop.coffee.controller.CoffeeManager;

public class Run {

	public static void main(String[] args) {
		CoffeeManager cm = new CoffeeManager();
		cm.insertCoffee();
		cm.printCoffee();
		
	}

}

 


[문제 2]

- 클래스 : com.oop.person.model.Person.java
 - control 클래스 작성 : com.oop.person.controller.PersonController.java
 - 실행클래스 : com.oop.person.run.Run.java
=> main() 메소드 포함함
 - Person 멤버변수 이름, 나이, 키, 몸무게, 재산
  기본생성자, 모든필드초기화생성자
  getter, setter
  출력메소드 information()
 - control클래스 구현내용.
1. Person 클래스에 대한 객체 배열 5개 선언함
2. 키보드로 5 사람의 정보를 입력받아, 각 객체에 기록함
3. 출력 확인함
4. 5명의 나이, 키, 몸무게, 재산의 평균을 구하여 각각 출력함
 - main() 에서 구현할 내용
1. control클래스 객체생성 및 입력/출력/평균 메소드 호출

 

package com.oop.person.model;

public class Person {
	private String name;
	private int age;
	private double tall;
	private double weight;
	private int money;
	
	//기본생성자
	public Person() {	
	}
	
	public Person(String name,int age,double tall,double weight,int money)
	{
		this.name=name;
		this.age=age;
		this.tall=tall;
		this.weight=weight;
		this.money=money;
	}
	
	public void setName(String name) {
		this.name=name;
	}
	public String getName() {
		return name;
	}
	public void setAge(int age) {
		this.age=age;
	}
	public int getAge() {
		return age;
	}
	public void setTall(double tall) {
		this.tall=tall;
	}
	public double getTall() {
		return tall;
	}
	public void setWeight(double weight) {
		this.weight=weight;
	}
	public double getWeight() {
		return weight;
	}
	public void setMoney() {
		this.money=money;
	}
	public int getMoney() {
		return money;
	}
	
	public String Information() {
		return name+"\t\t"+age
				+"\t\t"+tall+"\t\t"+weight
				+"\t\t"+money;				
	}
}
-------------------------------------------------
package com.oop.person.controller;

import java.util.Scanner;

import com.oop.person.model.Person;

import kh.java.object.array.student.Student;

public class PersonController {
	private Scanner sc = new Scanner(System.in);
	private static final int LENGTH = 5;
	private int index = 0; // students의 인덱스 관리
	private Person[] persons = new Person[LENGTH];

	public void insertData() {
		System.out.println(">> 개인정보입력. . .  ");
		while (index<LENGTH) {
			System.out.println(">> 멤버 " + (index + 1));
			System.out.print("이름:  ");
			String name = sc.next();
			System.out.print("나이:  ");
			int age = sc.nextInt();
			System.out.print("키:  ");
			double tall = sc.nextDouble();
			System.out.print("몸무게:  ");
			double weight = sc.nextDouble();
			System.out.print("재산:  ");
			int money = sc.nextInt();

			persons[index++] = new Person(name, age, tall, weight, money);
			System.out.println();
			System.out.println(">> 입력완료!");
		}

	}

	public void avg() {
		int sum_age = 0;
		double sum_tall = 0;
		double sum_weight = 0;
		int sum_money = 0;

		for (int i = 0; i < index; i++) {
			Person p = persons[i];
			sum_age += persons[i].getAge();
			sum_tall += persons[i].getTall();
			sum_weight += persons[i].getWeight();
			sum_money += persons[i].getMoney();
		}
		int avg_age = sum_age / index;
		double avg_tall = sum_tall / index;
		double avg_weight = sum_weight / index;
		int avg_money = sum_money / index;

		System.out.println();
		System.out.printf("평균 나이: %d%n",avg_age);
		System.out.printf("평균 키: %.2f%n",avg_tall);
		System.out.printf("평균 몸무게: %.2f%n",avg_weight);
		System.out.printf("평균 재산: %d%n",avg_age);
	}

	public void printData() {
		for (int i = 0; i < index; i++) {
			Person p = persons[i];
			System.out.println(p.Information());
		}
	

	}
}
--------------------------------------------------
package com.oop.person.run;

import com.oop.person.controller.PersonController;

public class Run {
	public static void main(String[] args) {
		PersonController pc = new PersonController();
		pc.insertData();
		pc.printData();
		pc.avg();
	}
}

 

 

[문제 1]

Has A  포함관계 기법을 활용하여 포인트 관리 시스템을 만들어라
1. Has a 포함 관계로 만들것
2. Entity  와 Control 클래스를 명확히 구분할것
* entity 클래스 이름 : member.model.vo.Silver , member.model.vo.Gold
* control 클래스 이름 ; member.controller.MemberManager

Silver,Gold 멤버변수 : 이름 name,등급 grade ,포인트 point
Silver,Gold 멤버함수 : 컨스트럭터(생성자) , setter/getter(이자포인트 getter추가)
---> 실버 등급은 2% 를 곱한값이 이자 포인트
---> 골드 등급은 5% 를 곱한값이 이자 포인트
Control 클래스 이름 : MemberManager
MemberManager 멤버변수 : Silver 등급을 최대 10명, Gold 등급을 최대 10명 관리할수 있는  객체배열 , 인덱스 추가
MemberManager 멤버메소드 : silverInsert, goldInsert, printData 

주의할점 : 적절한 접근제어 지시자 사용 (private,public)

실행클라스 : member.run.Run 

public class Run {

public static void main(String[] args){
MemberManager m = new MemberManager();
m.silverInsert(new Silver("홍길동", "Silver",1000));
m.silverInsert(new Silver("김말똥", "Silver",2000));
m.silverInsert(new Silver("고길동", "Silver",3000));
m.goldInsert(new Gold("김회장", "Gold",1000));
m.goldInsert(new Gold("이회장", "Gold",2000));
m.goldInsert(new Gold("오회장", "Gold",3000));
m.printData();
}

}

출력결과 :

---------------------------<<회원정보>>---------------------------
이름              등급              포인트             이자포인트          
-----------------------------------------------------------------
홍길동             Silver          1000            20.00          
김말똥             Silver          2000            40.00          
고길동             Silver          3000            60.00          
김회장             Gold            1000            50.00          
이회장             Gold            2000            100.00         
오회장             Gold            3000            150.00  

 

 

 

package member.model.vo;

public class Silver {
	private String name;
	private String grade;
	private int point;

	public Silver() {}
	public Silver(String name, String grade, int point) {
		this.name = name;
		this.grade = grade;
		this.point = point;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getGrade() {
		return grade;
	}
	public void setGrade(String grade) {
		this.grade = grade;
	}
	public int getPoint() {
		return point;
	}
	public void setPoint(int point) {
		this.point = point;
	}
	public double getEjapoint() {
		return point*0.02;
	}
}

-----------------------------------------
package member.model.vo;

public class Gold {
	private String name;
	private String grade;
	private int point;
	
	public Gold() {}
	public Gold(String name, String grade, int point) {
		this.name = name;
		this.grade = grade;
		this.point = point;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getGrade() {
		return grade;
	}
	public void setGrade(String grade) {
		this.grade = grade;
	}
	public int getPoint() {
		return point;
	}
	public void setPoint(int point) {
		this.point = point;
	}
	public double getEjapoint() {
		return point*0.05;
	}
}

 

[문제 2]

회원관리에 vip, vvip등급을 추가.
---> vip 등급은 10% 를 곱한값이 이자 포인트
---> vvip 등급은 15% 를 곱한값이 이자 포인트

실행클라스 : member.run.Run.main메소드에 추가

//vip추가
m.vipInsert(new Vip("이부자", "Vip",10000));
//vvip추가
m.vvipInsert(new VVip("김갑부", "VVip",100000));

 

package member.model.vo;

public class Vip {
	private String name;
	private String grade;
	private int point;
	
	public Vip() {}
	public Vip(String name, String grade, int point) {
		this.name = name;
		this.grade = grade;
		this.point = point;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getGrade() {
		return grade;
	}
	public void setGrade(String grade) {
		this.grade = grade;
	}
	public int getPoint() {
		return point;
	}
	public void setPoint(int point) {
		this.point = point;
	}
	public double getEjapoint() {
		return point*0.1;
	}
}
-----------------------------------------------
package member.model.vo;

public class VVip {
	private String name;
	private String grade;
	private int point;
	
	public VVip() {}
	public VVip(String name, String grade, int point) {
		this.name = name;
		this.grade = grade;
		this.point = point;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getGrade() {
		return grade;
	}
	public void setGrade(String grade) {
		this.grade = grade;
	}
	public int getPoint() {
		return point;
	}
	public void setPoint(int point) {
		this.point = point;
	}
	public double getEjapoint() {
		return point*0.15;
	}
}
---------------------------------------------
package member.run;

import member.controller.MemberManager;
import member.model.vo.Gold;
import member.model.vo.Silver;
import member.model.vo.VVip;
import member.model.vo.Vip;

public class Run {
	public static void main(String[] args){
		MemberManager m = new MemberManager();
		m.silverInsert(new Silver("홍길동", "Silver",1000));
		m.silverInsert(new Silver("김말똥", "Silver",2000));
		m.silverInsert(new Silver("고길동", "Silver",3000));
		m.goldInsert(new Gold("김회장", "Gold",1000));
		m.goldInsert(new Gold("이회장", "Gold",2000));
		m.goldInsert(new Gold("오회장", "Gold",3000));
		
		//vip추가
		m.vipInsert(new Vip("이부자", "Vip",10000));
		
		//vvip추가
		m.vvipInsert(new VVip("김갑부", "VVip",100000));
		
		m.printData();
	}
}
-------------------------------------------------------------
package member.controller;

import member.model.vo.Gold;
import member.model.vo.Silver;
import member.model.vo.VVip;
import member.model.vo.Vip;

public class MemberManager {

	public static final int MAX_MEMBER_COUNT = 10;
	Silver[] s= new Silver[MAX_MEMBER_COUNT];
	Gold[] g = new Gold[MAX_MEMBER_COUNT];
	Vip[] v = new Vip[MAX_MEMBER_COUNT];
	VVip[] vv = new VVip[MAX_MEMBER_COUNT];
	
	int silverIndex = 0;
	int goldIndex = 0;
	int vipIndex = 0;
	int vvipIndex = 0;
	
	
	public void silverInsert(Silver s) {
		this.s[silverIndex++] = s;
	}
	public void goldInsert(Gold g) {
		this.g[goldIndex++] = g;
	}
	public void vipInsert(Vip v){
		this.v[vipIndex++] =  v;
	}
	public void vvipInsert(VVip vv){
		this.vv[vvipIndex++] =  vv;
	}
	
	public void printData() {
		System.out.println("----------------------------------------<<회원정보>>-----------------------------------------");
		System.out.printf("%-15s %-15s %-15s %-15s\n", "이름","등급","포인트","이자포인트");
		System.out.println("------------------------------------------------------------------------------------------------");
		for(int i=0; i<silverIndex;i++) {
			System.out.printf("%-15s %-15s %-15d %-15.2f\n", s[i].getName(), s[i].getGrade(), s[i].getPoint(), s[i].getEjapoint());			
		}
		for(int i=0; i<goldIndex;i++) {
			System.out.printf("%-15s %-15s %-15d %-15.2f\n", g[i].getName(), g[i].getGrade(), g[i].getPoint(), g[i].getEjapoint());
		}
		for(int i=0; i<vipIndex; i++){
			System.out.printf("%-15s %-15s %-15d %-15.2f\n", v[i].getName(), v[i].getGrade(), v[i].getPoint(), v[i].getEjapoint());
		}
		for(int i=0; i<vvipIndex; i++){
			System.out.printf("%-15s %-15s %-15d %-15.2f\n", vv[i].getName(), vv[i].getGrade(), vv[i].getPoint(), vv[i].getEjapoint());
		}
	}
}

'학원 > 실습' 카테고리의 다른 글

*17일차 실습  (0) 2022.02.15
*16일차 실습  (0) 2022.02.14
*14일차 실습  (0) 2022.02.10
*13일차 실습  (0) 2022.02.09
*12일차 실습  (0) 2022.02.08