- 질문 게시판입니다.
Date | 17/10/19 15:33:52 |
Name | 二ッキョウ니쿄 |
Subject | 자바 질문입니다 |
package com.ex11market; [메인클래스] public class MarketMain { static Goods[] g = { new Goods("g01", "레쓰비", 25, 900), new Goods("g02", "박카스", 25, 700), new Goods("g03", "ABC초콜릿", 25, 1600), new Goods("g04", "크리넥스", 25, 2000), new Goods("g05", "전자담배", 25, 90000) }; static Customer[] c = { new Customer("010-1111-1111", "서울이", "10-10"), new Customer("010-1212-1212", "고려놈", "10-18"), new Customer("010-1313-1313", "연세놈", "01-11"), new Customer("010-1414-1414", "서강이", "02-27"), new Customer("010-1515-1515", "세종이", "06-30") }; public static void main(String[] args) { sell("g05", "1515", 3); sell("g02", "1212", 5); } private static void sell(String gCode, String subTel, int su) { int i, gIndex, cIndex; for (i = 0; i < g.length; i++) { if (g[i].getgCode().equals(gCode)) { break; } } if (i == g.length) { System.out.println("상품코드가 잘못 되었습니다"); return; } gIndex = i; // 상품이 가르키는 곳의 인덱스 for (i = 0; i < c.length; i++) { String tel = c[i].getTel();// 010-9999-9999 if (tel.substring(9).equals(subTel)) { break; } } if (i == c.length) { System.out.println("전화번호가 잘못 되었습니다. 회원가입하시죠"); return; } cIndex = i; // 고객을 가르키는 곳의 인덱스 int tempSu = g[gIndex].getStock() - su; if (tempSu < 0) { System.out.println("재고량이 부족합니다. 죄송합니다."); System.out.println("사장님!" + g[gIndex].getgName() + "얼른 주문하세요"); return; } g[gIndex].setStock(tempSu); String gName = g[gIndex].getgName(); int price = g[gIndex].getPrice(); c[cIndex].buy(gName, price, su); } } [상품클래스] public class Goods { private String gCode; private String gName; private int stock; private int price; public Goods() { } public Goods(String gCode, String gName, int stock, int price) { this.gCode = gCode; this.gName = gName; this.stock = stock; this.price = price; } public String getgCode() { return gCode; } public void setgCode(String gCode) { this.gCode = gCode; } public String getgName() { return gName; } public void setgName(String gName) { this.gName = gName; } public int getStock() { return stock; } public void setStock(int stock) { this.stock = stock; } public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } } [고객클래스] import java.text.SimpleDateFormat; import java.util.Date; public class Customer { private String tel; private String cName; private int money; private int point; private String birth; private boolean vip; public Customer() { } public Customer(String tel, String cName, String birth) { this.tel = tel; this.cName = cName; this.birth = birth; money = 0; point = 0; } public void changedTel(String cName,String tel) { this.tel = tel; } public void buy(String gName, int price, int su) { if(price<0) { System.out.println("구매금액이 잘못되었습니다"); return; } money += price*su; int thispoint = (int)(price*su*0.1); point += thispoint; if(money>=1000000 && vip == false) { vip = true; System.out.println("vip가 되셨습니다"); } msgBirth(); System.out.println(gName+""+su+"개 *"+price+"원 = "+(price*su)); System.out.println("이번 누적 포인트는 "+thispoint+"원 입니다"); System.out.println(cName+"고객님 포인트 합계 : "+point+"원"); System.out.println("★ ★ ★ 감 사 합 니 다 ★ ★ ★"); } public void msgBirth() { Date now = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("MM-dd"); String today = sdf.format(now); if(birth.equals(today)) { System.out.println(cName+"님 생일 축하드립니다"); point+=3000; System.out.println("생일 기념으로 3000포인트가 적립됩니다"); } } public String getTel() { return tel; } public void setTel(String tel) { this.tel = tel; } public String getcName() { return cName; } public void setcName(String cName) { this.cName = cName; } public int getMoney() { return money; } public void setMoney(int money) { this.money = money; } public int getPoint() { return point; } public void setPoint(int point) { this.point = point; } public String getBirth() { return birth; } public void setBirth(String birth) { this.birth = birth; } public boolean isVip() { return vip; } public void setVip(boolean vip) { this.vip = vip; } } [콘솔창 결과물] 전자담배3개 *90000원 = 270000 이번 누적 포인트는 27000원 입니다 세종이고객님 포인트 합계 : 27000원 ★ ★ ★ 감 사 합 니 다 ★ ★ ★ 박카스5개 *700원 = 3500 이번 누적 포인트는 350원 입니다 고려놈고객님 포인트 합계 : 350원 ★ ★ ★ 감 사 합 니 다 ★ ★ ★ 안녕하세요 자바 4주차 공부중인 코린이입니다. 슈퍼마켓에서 배열등을 이용해 콘솔창에서 물건을 사고 재고랑 가격, 포인트 등을 만드는 코드를 짰는데요 제가 배운 영역에서 어떻게 더 리팩토링해서 보기 좋고 더 나은 ? 상태로 만들어야 할지 더 알수가 없어서 질문드립니다. 혹시 어떤식으로 고치는게 좋을까요? 제반지식은 본문에 쓰인 코드랑 싱글턴, 스트레터지 패턴, 대표적인 api 몇가지랑 추상화,인터페이스, 상속, 접근제한자 및 기본적인 변수 연산자 제어문 배열정도입니다.. 깔끔하게 올릴 방법을 몰라서 이렇게 올리는데 도움 주시면 감사드리겠습니다. 0
이 게시판에 등록된 二ッキョウ니쿄님의 최근 게시물
|
간단한 프로그램이라 리팩토링할 거리가 별로 없어보이기는 하는데
1) 변수명..
private static void sell(String gCode, String subTel, int su) ~{
이런식으로 쓰지 마시구요
private static void sellGoods(String goodsCode, String lineNumber, int buyingCount)
리팩토링은 함수명 변수명만 잘지어도 반은 먹고들어갑니다.
2)
int i, gIndex, cIndex;
for (i = 0; i <... 더 보기
1) 변수명..
private static void sell(String gCode, String subTel, int su) ~{
이런식으로 쓰지 마시구요
private static void sellGoods(String goodsCode, String lineNumber, int buyingCount)
리팩토링은 함수명 변수명만 잘지어도 반은 먹고들어갑니다.
2)
int i, gIndex, cIndex;
for (i = 0; i <... 더 보기
간단한 프로그램이라 리팩토링할 거리가 별로 없어보이기는 하는데
1) 변수명..
private static void sell(String gCode, String subTel, int su) ~{
이런식으로 쓰지 마시구요
private static void sellGoods(String goodsCode, String lineNumber, int buyingCount)
리팩토링은 함수명 변수명만 잘지어도 반은 먹고들어갑니다.
2)
int i, gIndex, cIndex;
for (i = 0; i < g.length; i++) ~{
if (g[i].getgCode().equals(gCode)) ~{
break;
}
}
if (i == g.length) ~{
System.out.println("상품코드가 잘못 되었습니다");
return;
}
gIndex = i; // 상품이 가르키는 곳의 인덱스
이렇게 하지 마시고.. for문 iterator로 쓰는 변수를 for문 밖에서 쓰는건 좋은 습관은 아니에요.
아마 문제에서 번호가 겹치는 데이터가 2개 이상 있지는 않다고 가정되어있나 보네요. 아니면 겹치는 데이터들이 있으면 가장 먼저 등록된 걸 리턴하거나..
int i;
int goodsIndexFound = -1;
for (int i=0; i<g.length; i++) ~{
if (g[i].getgCode().equals(gCode)) ~{
goodsIndexFound = i;
break;
}
}
if (goodsIndexFound == -1) ~{
throw new GoodsNotFoundException("입력하신 코드에 맞는 상품이 없습니다!");
}
3)
if(money>=1000000 && vip == false) ~{
상수 쓰지마시고..
private static final int VIP_POINT_THRESHOLD = 1000000;
if(money >= VIP_POINT_THRESHOLD && isVIP == false) ~{
이런식으로..
1) 변수명..
private static void sell(String gCode, String subTel, int su) ~{
이런식으로 쓰지 마시구요
private static void sellGoods(String goodsCode, String lineNumber, int buyingCount)
리팩토링은 함수명 변수명만 잘지어도 반은 먹고들어갑니다.
2)
int i, gIndex, cIndex;
for (i = 0; i < g.length; i++) ~{
if (g[i].getgCode().equals(gCode)) ~{
break;
}
}
if (i == g.length) ~{
System.out.println("상품코드가 잘못 되었습니다");
return;
}
gIndex = i; // 상품이 가르키는 곳의 인덱스
이렇게 하지 마시고.. for문 iterator로 쓰는 변수를 for문 밖에서 쓰는건 좋은 습관은 아니에요.
아마 문제에서 번호가 겹치는 데이터가 2개 이상 있지는 않다고 가정되어있나 보네요. 아니면 겹치는 데이터들이 있으면 가장 먼저 등록된 걸 리턴하거나..
int i;
int goodsIndexFound = -1;
for (int i=0; i<g.length; i++) ~{
if (g[i].getgCode().equals(gCode)) ~{
goodsIndexFound = i;
break;
}
}
if (goodsIndexFound == -1) ~{
throw new GoodsNotFoundException("입력하신 코드에 맞는 상품이 없습니다!");
}
3)
if(money>=1000000 && vip == false) ~{
상수 쓰지마시고..
private static final int VIP_POINT_THRESHOLD = 1000000;
if(money >= VIP_POINT_THRESHOLD && isVIP == false) ~{
이런식으로..
목록 |
|