-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMenu_Management.java
153 lines (127 loc) · 4.14 KB
/
Menu_Management.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package oodp;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Iterator;
//메뉴 관리 CLASS
class Menu_Management extends Menu{
// function name : makeMenu()
// function role : 메뉴생성
// 파라미터로 넘어온 정보를 menu에 넣고 return 한다.
// menu list가 return 값을 받는다.
public static Menu makeMenu(String section, String list, int price){
// exception handling
if (section==null || list==null || price == 0) throw new NullPointerException("please stop");
Menu menu = new Menu();
menu.menuList = list;
menu.menuPrice = price;
menu.menuSection = section;
Connection conn = null;
Menu val = new Menu();
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/project", "root", "bitnami");
if(!conn.isClosed()){
System.out.println("Successfully connected to MySQL server...");
PreparedStatement s;
s = conn.prepareStatement
("INSERT INTO menu VALUES(?,?,?,?,?)");
s.setString(1, section);
s.setString(2, list);
s.setInt(3, price);
s.setInt(4, 0);
s.setInt(5, 100);
int count = s.executeUpdate();
s.close();
System.out.println(count+"rows were inserted");
}
}
catch(Exception exc)
{
System.err.println("Exception: " + exc.getMessage());
}
return menu;
}
// function name : deleteMenu()
// function role : 메뉴삭제
// delete를 통해 삭제할 값이 넘어온다
// 해당값이 list에 존재하면 지운다
public static int deleteMenu(ArrayList<Menu> mMenuList, Menu delete){
int i;
int result = 0;
Menu menu = new Menu();
Iterator<Menu> iter = mMenuList.iterator();
i = 0;
while (iter.hasNext()) {
menu = (Menu)iter.next();
if(menu.menuList.equals(delete.menuList)&&menu.menuSection.equals(delete.menuSection)
&&menu.menuPrice == delete.menuPrice){
deleteFromDB(delete);
mMenuList.remove(i);
result = 1;
break;
}
i++;
}
System.out.println(delete.menuSection);
return result;
}
private static void deleteFromDB(Menu delete){
Connection conn = null;
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/project", "root", "bitnami");
if(!conn.isClosed()){
System.out.println("Successfully connected to MySQL server...");
PreparedStatement s;
String query = "delete from menu where section = ? and list = ? and price = ?";
PreparedStatement preparedStmt = conn.prepareStatement(query);
preparedStmt.setString(1, delete.menuSection);
preparedStmt.setString(2, delete.menuList);
preparedStmt.setInt(3, delete.menuPrice);
// execute the preparedstatement
preparedStmt.execute();
conn.close();
}
}
catch(Exception exc)
{
System.err.println("Exception: " + exc.getMessage());
}
}
public void modifyMenu(){
}
// menu 초기 설정
public static void initialMenu(ArrayList<Menu> mMenuList){
Connection conn = null;
Menu val = new Menu();
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/project", "root", "bitnami");
if(!conn.isClosed()){
System.out.println("Successfully connected to MySQL server...");
Statement s = conn.createStatement();
s.executeQuery("SELECT *" + "FROM menu");
ResultSet rs = s.getResultSet();
while(rs.next()){
val.menuSection = rs.getString("section");
val.menuList = rs.getString("list");
val.menuPrice = rs.getInt("price");
val.curNum = rs.getInt("cur_num");
val.soldNum =rs.getInt("sold_num");
mMenuList.add(new Menu(val));
} // while end
} // if end
} // try end
catch(Exception exc)
{
System.err.println("Exception: " + exc.getMessage());
}
}
}