getter / setter 는 생략 너무길어짐. 들어가있음.
package fishing;
public class User {
private int num; // 자동생성
private String user_id;
private String passwd;
private String nickname;
private String fishrod;
private int money;
private int bait;
// 기빈
private int baitCnt; // 떡밥구매 수량
private int baitPrice; // 떡밥구매 비용
private int sellNum; // 판매하려는 인벤 번호
private int cost; // 낚싯대 가격
private long tmp;
public User() {}
// 회원가입
public User(String id, String pw, String nickname) {
this.user_id = id;
this.passwd = pw;
this.nickname = nickname;
this.fishrod = "normal";
this.money = 10000;
this.bait = 0;
}
public User(String nickname, int bait) { // 내가 추가한 생성자. 유저의 미끼 수 받을 때 필요함
this.nickname = nickname;
this.bait = bait;
}
// 기빈 시작
// 회원정보
public User(String user_id, String nickname, String fishrod, int money, int bait) {
this.user_id = user_id;
this.nickname = nickname;
this.fishrod = fishrod;
this.money = money;
this.bait = bait;
}
// 떡밥 구매
public User(int num, int baitCnt, int baitPrice) {
this.num = num;
this.baitCnt = baitCnt;
this.baitPrice = baitPrice;
}
// 낚시대 구매
public User(int num, String fishrod, int cost) {
this.num = num;
this.fishrod = fishrod;
this.cost = cost;
}
// 낚싯대 비교
public User(int num, String fishrod) {
this.num = num;
this.fishrod = fishrod;
}
// 기빈 끝
// 낚싯대 구매
public User(int num, long tmp, int cost) {
this.num = num;
this.cost = cost;
this.tmp = tmp;
}
// 선택판매
public User(int num, int sellNum) {
this.num = num;
this.sellNum = sellNum;
}
// 물고기 모두판매 후 금액입금, 잔고확인 후 낚시대 구매 메서드 참고
public User(int num) {
this.num = num;
}
// 기빈 끝
@Override
public String toString() {
return "닉네임 : " + nickname + "\t낚싯대 등급 : " + fishrod + "\t돈 : " + money + "\t소지한 미끼 : " + bait;
}
}
package fishing;
import java.util.Scanner;
public class UserController {
private Scanner scan;
private UserService svc;
private boolean on;
public UserController() {
this.scan = new Scanner(System.in);
this.svc = new UserServiceImpl(); // UserServiceImpl 객체 초기화
this.on = true;
printSignUp();
}
private void printSignUp() {
while(on) {
System.out.println();
System.out.println("\r\n"
+ " _____ ______ _ _ _ \r\n"
+ " | ___| | ___|(_) | | (_) ><(((('> \" ); \r\n"
+ " | |__ ____ ___ _ __ | |_ _ ___ | |__ _ _ __ __ _ \r\n"
+ " | __| |_ / / _ \\| '_ \\ | _| | |/ __|| '_ \\ | || '_ \\ / _` | ><(((('> \" );\r\n"
+ " | |___ / / | __/| | | | | | | |\\__ \\| | | || || | | || (_| |\r\n"
+ " \\____/ /___| \\___||_| |_| \\_| |_||___/|_| |_||_||_| |_| \\__, |\r\n"
+ " __/ |\r\n"
+ " ><(((('> \\\" ); |___/ \r\n"
+ "");
System.out.println();
System.out.println(" ────────────────────────────────────────────────────────");
System.out.println(" │ │");
System.out.println(" │ 1. 로그인 | 2. 회원가입 | 3. 게임종료 │");
System.out.println(" │ │");
System.out.println(" ────────────────────────────────────────────────────────");
System.out.println();
System.out.println("번호를 입력해주세요. ");
int menu = scan.nextInt();
switch (menu) {
case 1:
login();
break;
case 2:
signUp();
break;
case 3:
on = false;
System.out.println(" 게임을 종료합니다. ");
return;
default:
System.out.println(" 잘못된 메뉴를 선택하셨습니다. ");
break;
}
}
}
private void signUp() {
System.out.println("만들 아이디를 입력해주세요.");
String id = scan.next();
if (svc.isUserIdExists(id)) {
System.out.println("이미 존재하는 아이디입니다. 다른 아이디를 입력해주세요.");
return; // 아이디 중복 시 회원가입 종료
}
System.out.println("만들 비밀번호를 입력해주세요.");
String pw = scan.next();
System.out.println("사용할 닉네임을 입력해주세요.");
String nickname = scan.next();
User u = new User(id,pw,nickname);
int signOk = svc.signUp(u);
System.out.println("회원가입 " + ((signOk > 0) ? "성공" : "실패") );
}
private void login() {
System.out.println("아이디를 입력하세요.");
String id = scan.next();
System.out.println("비밀번호를 입력하세요.");
String pw = scan.next();
User u = svc.login(id,pw);
if (u != null) {
System.out.println("로그인 성공! 반갑습니다, " + u.getNickname() + "님.");
System.out.println("소지 금액: " + u.getMoney() + "원");
System.out.println("보유 미끼 수: " + u.getBait() + "개");
on = false;
scan.nextLine();
new StateController(u); // 건희 Controller에 유저 자체를 전달.
} else {
System.out.println("아이디 또는 비밀번호가 틀렸습니다.");
return;
}
}
}
package fishing;
import java.util.List;
public interface UserDAO {
int signUpUser(User u);
User loginUser(String id, String pw);
boolean userIdExists(String id);
// 기빈 시작
// 인벤 리스트
List<Inventory> userInven(int num);
// 떡밥
int userBait(User u);
// 낚시대
int userRod(User u);
// 물고기 선택판매
int userOneMoney(User u);
int sellOne(Inventory i);
// 물고기 모두판매
int userAllMoney(User u);
int sellAll(Inventory i);
User userInfo(int num);
boolean compMoney(User u1);
boolean compRod(User u2);
List<Inventory> userRank(int num);
// 기빈 끝
}
package fishing;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class UserDAOImpl implements UserDAO {
private Connection conn;
private PreparedStatement pst;
private String query="";
public UserDAOImpl() {
DatabaseConnection dbc = DatabaseConnection.getInstance();
conn = dbc.getConnection();
}
@Override
public int signUpUser(User u) {
// System.out.println(" 회원가입 DAOImpl 확인 성공!!");
query = "insert into userlist (user_id, passwd, nickname) values (?, ?, ?)";
try {
pst = conn.prepareStatement(query);
pst.setString(1, u.getUser_id());
pst.setString(2, u.getPasswd());
pst.setString(3, u.getNickname());
return pst.executeUpdate();
} catch (SQLException e) {
System.out.println(" 회원가입 DAOImpl 확인 실패... ");
e.printStackTrace();
}
return 0;
}
@Override
public User loginUser(String id, String pw) {
query = "select * from userlist where user_id = ? AND passwd = ?";
try {
pst = conn.prepareStatement(query);
pst.setString(1, id);
pst.setString(2, pw);
ResultSet rs = pst.executeQuery();
if(rs.next()) {
User u = new User();
u.setNum(rs.getInt("num"));
u.setUser_id(rs.getString("user_id"));
u.setPasswd(rs.getString("passwd"));
u.setNickname(rs.getString("nickname"));
u.setFishrod(rs.getString("fishrod"));
u.setMoney(rs.getInt("money"));
u.setBait(rs.getInt("bait"));
return u;
}
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
@Override
public boolean userIdExists(String id) {
String query = "SELECT COUNT(*) FROM userlist WHERE user_id = ?";
try {
pst = conn.prepareStatement(query);
pst.setString(1, id);
ResultSet rs = pst.executeQuery();
if (rs.next()) {
return rs.getInt(1) > 0;
}
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
// 기빈 시작
@Override
public User userInfo(int num) {
query = "select * from userlist where num = ?";
try {
pst = conn.prepareStatement(query);
pst.setInt(1, num);
ResultSet rs = pst.executeQuery();
if(rs.next()) {
User u = new User(rs.getString("user_id"),
rs.getString("nickname"),
rs.getString("fishrod"),
rs.getInt("money"),
rs.getInt("bait"));
return u;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
public int userBait(User u) {
System.out.println(" 떡밥개수 "+u.getBaitCnt()+" 떡밥값 "+u.getBaitPrice()+" 누구? "+ u.getNum());
query = "update userlist set bait = bait + ?, money = money - ? "
+ "where num = ?";
try {
pst = conn.prepareStatement(query);
pst.setInt(1, u.getBaitCnt());
pst.setInt(2, u.getBaitPrice());
pst.setInt(3, u.getNum());
return pst.executeUpdate();
} catch (SQLException e) {
System.out.println("bait update error");
e.printStackTrace();
}
return 0;
}
@Override
public int userRod(User u) {
System.out.println(u.getNum() +" "+u.getFishrod()+""+ u.getCost());
query = "update userlist set fishrod = ?, money = money - ? "
+ "where num = ?";
try {
pst = conn.prepareStatement(query);
pst.setString(1, u.getFishrod());
pst.setInt(2, u.getCost());
pst.setInt(3, u.getNum());
return pst.executeUpdate();
} catch (SQLException e) {
System.out.println("fishrod update error");
e.printStackTrace();
}
return 0;
}
@Override
public int userOneMoney(User u) {
query = "update userlist "
+ "set money = money + ("
+ "select if(i.fish_size > f.default_size,"
+ "(i.fish_size-f.default_size)*1000 + price, price) as finalprice "
+ "from inventory as i "
+ "inner join fish as f "
+ "on i.fish_no = f.fish_no "
+ "where i.inven_id = ? ) "
+ "where num = ? ";
try {
pst = conn.prepareStatement(query);
pst.setInt(1, u.getSellNum());
pst.setInt(2, u.getNum());
return pst.executeUpdate();
} catch (SQLException e) {
System.out.println("money plus error");
e.printStackTrace();
}
return 0;
}
@Override
public int userAllMoney(User u) {
query = "update userlist "
+ "set money = money + ("
+ "select sum(if(i.fish_size > f.default_size,"
+ "(i.fish_size-f.default_size)*1000 + price, price)) as finalprice "
+ "from inventory as i "
+ "inner join fish as f "
+ "on i.fish_no = f.fish_no "
+ "group by i.num "
+ "having i.num = ? ) "
+ "where num = ? ";
try {
pst = conn.prepareStatement(query);
pst.setInt(1, u.getNum());
pst.setInt(2, u.getNum());
return pst.executeUpdate();
} catch (SQLException e) {
System.out.println("money plus error");
e.printStackTrace();
}
return 0;
}
@Override
public boolean compMoney(User u1) {
query = "select if(money < ?, 0, 1) as bool from userlist where num = ? ";
// 잔고가 낚싯대 가격보다 작으면 0 출력 아니면 1
try {
pst = conn.prepareStatement(query);
pst.setInt(1, u1.getCost());
pst.setInt(2, u1.getNum());
ResultSet rs = pst.executeQuery();
if(rs.next()) {
int res = rs.getInt("bool");
if(res == 0) {
return false;
}
}
} catch (SQLException e) {
System.out.println("money compare error");
e.printStackTrace();
}
return true;
}
@Override
public List<Inventory> userInven(int num) {
query = "select i.inven_id, i.num, i.fish_no, f.fish_name, i.fish_size from inventory as i "
+ "inner join fish as f "
+ "on i.fish_no = f.fish_no "
+ "where i.num = ?";
List<Inventory> list = new ArrayList<>();
try {
pst = conn.prepareStatement(query);
pst.setInt(1, num);
ResultSet rs = pst.executeQuery();
while(rs.next()) {
list.add(new Inventory(rs.getInt("inven_id"),
rs.getString("fish_name"),
rs.getInt("fish_size")));
}
return list;
} catch (SQLException e) {
System.out.println("list error");
e.printStackTrace();
}
return null;
}
@Override
public int sellOne(Inventory i) { // delete from inventory where
query = "delete from inventory "
+ "where num = ? and inven_id = ?";
try {
pst = conn.prepareStatement(query);
pst.setInt(1, i.getNum());
pst.setInt(2, i.getSellno());
return pst.executeUpdate();
} catch (SQLException e) {
System.out.println("inven sell fish error");
e.printStackTrace();
}
return 0;
}
@Override
public int sellAll(Inventory i) {
query = "delete from inventory "
+ "where num = ?";
try {
pst = conn.prepareStatement(query);
pst.setInt(1, i.getNum());
return pst.executeUpdate();
} catch (SQLException e) {
System.out.println("inven sell fish error");
e.printStackTrace();
}
return 0;
}
@Override
public boolean compRod(User u2) {
System.out.println(u2.getNum()+" " + u2.getFishrod());
query = "select if(fishrod = ?, 0, 1) as bool from userlist where num = ?";
try {
pst = conn.prepareStatement(query);
pst.setString(1, u2.getFishrod());
pst.setInt(2, u2.getNum());
ResultSet rs = pst.executeQuery();
if(rs.next()) {
int res = rs.getInt("bool");
if(res == 0) {
return false;
}
}
} catch (SQLException e) {
System.out.println("fishrod compare error");
e.printStackTrace();
}
return true;
}
@Override
public List<Inventory> userRank(int num) {
// TODO Auto-generated method stub
query = "select i.num as num, f.fish_name as fishname, max(i.fish_size) as fishmax "
+ "from inventory as i "
+ "inner join fish as f "
+ "on i.fish_no = f.fish_no "
+ "group by i.num, f.fish_name "
+ "having i.num = ?";
List<Inventory> list = new ArrayList<>();
try {
pst = conn.prepareStatement(query);
pst.setInt(1, num);
ResultSet rs = pst.executeQuery();
while(rs.next()) {
list.add(new Inventory(rs.getString("fishname"),
rs.getInt("fishmax")));
}
return list;
} catch (SQLException e) {
System.out.println("ranking error");
e.printStackTrace();
}
return null;
}
}
package fishing;
import java.util.List; // 기빈
public interface UserService {
int signUp(User u);
User login(String id, String pw);
boolean isUserIdExists(String id);
// 기빈 시작
// 가방 리스트 출력
List<Inventory> getInven(int num);
// 떡밥 구매
int updBait(User u);
// 낚시대 구매
int updRod(User u);
// 물고기 선택판매
int plusOneMoney(User u);
int sellOneFish(Inventory i);
// 물고기 모두판매
int plusAllMoney(User u);
int sellAllFish(Inventory i);
User userInfo(int num);
boolean compMoney(User u1);
boolean compRod(User u2);
List<Inventory> getRank(int num);
// 기빈 끝
}
package fishing;
import java.util.List; // 기빈
public class UserServiceImpl implements UserService {
private UserDAO dao;
public UserServiceImpl() {
dao = new UserDAOImpl();
}
@Override
public int signUp(User u) {
// System.out.println(" 회원가입 서비스 에러 확인 성공 ");
return dao.signUpUser(u);
}
@Override
public User login(String id, String pw) {
// System.out.println("로그인 서비스 에러 확인 성공");
return dao.loginUser(id,pw);
}
@Override
public boolean isUserIdExists(String id) {
// TODO Auto-generated method stub
return dao.userIdExists(id);
}
// 기빈 시작
@Override
public int updBait(User u) {
// System.out.println("떡밥 구매 에러 확인 성공");
return dao.userBait(u);
}
@Override
public int updRod(User u) {
// System.out.println("낚시대 구매 에러확인 성공");
return dao.userRod(u);
}
@Override
public int plusOneMoney(User u) {
// System.out.println("금액 입금 에러확인 성공");
return dao.userOneMoney(u);
}
@Override
public int plusAllMoney(User u) {
// System.out.println("금액 입금 에러확인 성공");
return dao.userAllMoney(u);
}
@Override
public User userInfo(int num) {
// System.out.println("회원정보 출력 에러확인 성공");
return dao.userInfo(num);
}
@Override
public boolean compMoney(User u1) {
// System.out.println("회원 잔고조회 에러확인 성공");
return dao.compMoney(u1);
}
@Override
public int sellOneFish(Inventory i) {
// System.out.println("물고기 판매 에러확인 성공");
return dao.sellOne(i);
}
@Override
public int sellAllFish(Inventory i) {
// System.out.println("물고기 판매 에러확인 성공");
return dao.sellAll(i);
}
@Override
public List<Inventory> getInven(int num) {
// System.out.println("인벤리스트 서비스 에러 확인 성공");
return dao.userInven(num);
}
@Override
public boolean compRod(User u2) {
// System.out.println("낚싯대 비교 에러확인 성공");
return dao.compRod(u2);
}
@Override
public List<Inventory> getRank(int num) {
// System.out.println("랭킹 에러확인 성공.");
return dao.userRank(num);
}
// 기빈 끝
}
'자바 프로젝트(두번째 팀 프로젝트) 관한내용' 카테고리의 다른 글
자바 프로젝트 내용 / 후기 (2) | 2024.10.21 |
---|