-
※ 문제. 세계 3대 커피 정보를 입력 받아 출력하는 시스템 설계
Coffee, CoffeeManager, Run 총 세 개의 클래스 만들기
Coffee의 멤버변수 : 원산지, 지역, 기본생성자, 모든 필드 초기화 생성자, getter & setter, 출력메소드
<세계3대커피>
---------------------------
원산지 지역
---------------------------
예멘 모카마타리
자메이카 블루마운틴
하와이 코나
---------------------------더보기123456789101112131415161718192021222324252627282930313233public class Coffee {private String origin;private String location;public Coffee() {}public Coffee(String origin, String location) {this.origin = origin;this.location = location;}public String toString() {return "Coffee [origin=" + origin + ", location=" + location + "]";}public String getOrigin() {return origin;}public void setOrigin(String origin) {this.origin = origin;}public String getLocation() {return location;}public void setLocation(String location) {this.location = location;}}cs 1234567891011121314151617181920212223242526272829303132333435363738public class CoffeeManager {Scanner sc = new Scanner(System.in);private Coffee[] coffees;private int index = 0;public void inputCoffee() {coffees = new Coffee[3];while(index < coffees.length) {Coffee coffee = new Coffee();System.out.print("원산지 입력 > ");coffee.setOrigin(sc.next());System.out.print("지역 입력 > ");coffee.setLocation(sc.next());coffees[index++] = coffee;}System.out.println("커피 입력이 끝났습니다.\n"+ "----------------------------");}public void printCoffee() {String result = "<세계 3대 커피>\n"+ "----------------------------\n"+ "원산지\t지역\n"+ "----------------------------";System.out.println(result);for(int i = 0; i < coffees.length; i++) {Coffee coffee = coffees[i];System.out.println(coffee.getOrigin() + "\t" + coffee.getLocation());}System.out.println("----------------------------");}}cs 객체 배열은 배워도 배워도 헷갈리고 복잡하다...
배열과 객체 이름이 다 비슷비슷해서 그런 것 같기도 하고... 일일이 대조하고 구분하는 게 힘들었다.
더보기1234567891011121314151617181920212223242526public class CoffeeManager {Scanner sc = new Scanner(System.in);public static final int MAX_COFFEE = 3;Coffee[] top3;{top3 = new Coffee[MAX_COFFEE];}public void insertCoffeeData() {for(int i=0; i<top3.length; i++) {Coffee c = new Coffee();System.out.print("커피원산지(국가) 입력 : ");c.setOrigin(sc.next());System.out.print("생산지역 입력 : ");c.setLocation(sc.next());top3[i] = c;}}public void printCoffeeData() {for(int i=0; i<top3.length; i++) {System.out.println(top3[i].information());}}}cs 선생님은 Coffee클래스에 information 메소드를 따로 만들었는데, 이렇게 하니 출력 구문이 훨씬 깔끔하다.
쓰다 보면 for문이 훨씬 편한데 while문도 잘 쓰고 싶어서 결국 번갈아 가면서 쓰게 된다.
※ 문제. 포인트 관리 시스템 구축
Silver, Gold, MemberManager, Run 총 네 개의 클래스 만들기(Has a 포함관계로 만들 것)
Silver, Gold 멤버변수 : 이름, 등급, 포인트
+ 이자포인트 getter 추가할 것(silver = *0.02/ gold = *0.05)
실행클래스 :
123456789101112131415public 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();}}cs
-----------------------<<회원정보>>-------------------------
이름 등급 포인트 이자포인트
-----------------------------------------------------------------
홍길동 Silver 1000 20.00
김말똥 Silver 2000 40.00
고길동 Silver 3000 60.00
김회장 Gold 1000 50.00
이회장 Gold 2000 100.00
오회장 Gold 3000 150.00더보기123456789101112131415161718192021222324252627282930313233343536373839404142public 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 getBonusPoint() {return point*0.02;}}cs Gold 클래스는 거의 흡사하니 생략.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950public class MemberManager {private static final int MAX_MEMBER = 10;Silver[] silvers = new Silver[MAX_MEMBER];Gold[] golds = new Gold[MAX_MEMBER];private int indexS = 0;private int indexG = 0;public void silverInsert(Silver s) {Silver silver = new Silver();silver.setName(s.getName());silver.setGrade(s.getGrade());silver.setPoint(s.getPoint());silvers[indexS++] = silver;}public void goldInsert(Gold g) {Gold gold = new Gold();gold.setName(g.getName());gold.setGrade(g.getGrade());gold.setPoint(g.getPoint());golds[indexG++] = gold;}public void printData() {String result = "-----------------<<회원정보>>------------------\n"+ "이름\t등급\t포인트\t이자포인트\n"+ "---------------------------------------------";System.out.println(result);for(int i = 0; i < indexS; i++) {System.out.println(silvers[i].getName() + "\t"+ silvers[i].getGrade() + "\t"+ silvers[i].getPoint() +"\t"+ silvers[i].getBonusPoint());}for(int i = 0; i < indexG; i++) {System.out.println(golds[i].getName() + "\t"+ golds[i].getGrade() + "\t"+ golds[i].getPoint() +"\t"+ golds[i].getBonusPoint());}}}cs Insert부분이 정말 어려웠다. 어떻게 해야 정보가 원하는 대로 들어가는지 짐작이 안 가서...
일단 프로그램이 돌아가기는 하는데 어떤 원리로 돌아가는 건지 고민이 돼서 혼났다.
더보기1234567891011121314151617181920212223242526272829public class MemberManager {public static final int MAX_MEMBER_COUNT = 10;private Silver[] s= new Silver[MAX_MEMBER_COUNT];int silverIndex = 0;private Gold[] g = new Gold[MAX_MEMBER_COUNT];int goldIndex = 0;public void silverInsert(Silver s) {this.s[silverIndex++] = s;}public void goldInsert(Gold g) {this.g[goldIndex++] = g;}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());}}}cs 이렇게 깔끔할 수가...
Insert에서는 전달 받은 Silver, Gold 객체를 바로 담고 있는 것이라 굳이 getter를 사용할 필요가 없다는 것.
객체 배열을 사용하면 프로그램 코드가 훨씬 깔끔해지긴 하는데 갑자기 100단계 정도로 휙 뛰어 복잡해지니 사용에 어려움이 있다.
주말 동안 다시 풀어봐야겠다.
'혼자 있는 방 > Java' 카테고리의 다른 글
실기_Test(2) (0) 2021.07.06 필기_Test(2) (0) 2021.07.05 Homework(9) (0) 2021.07.01 Homework(8) (0) 2021.06.30 Test(1) (0) 2021.06.28 댓글