📄 dish.java
字号:
package beans;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.Vector;
import util.AddZeroAhead;
import util.BaseSQL;
public class Dish {
private String dishID = ""; //菜的ID
private String dishName = ""; //菜的名字
private String type = ""; //菜的种类
private String dishSort = ""; //菜的菜系
private float price = 0; //菜的价格
private Vector materials = new Vector(); //菜用到的主料
private String description = ""; //对菜的描述
public String getDishID() {
return dishID;
}
public void setDishID(String dishID) {
this.dishID = AddZeroAhead.addZeroAhead(dishID, 8);
}
public String getDishName() {
return dishName;
}
public void setDishName(String dishName) {
this.dishName = dishName;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getDishSort() {
return dishSort;
}
public void setDishSort(String dishSort) {
this.dishSort = dishSort;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public Vector getMaterials() {
return materials;
}
public void setMaterials(Vector materials) {
this.materials = materials;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
//开始编写
//增加主料
public String addMaterial(Material addThing){
Iterator iter = materials.iterator();
while(iter.hasNext()){
Material m = (Material)iter.next();
if(m.equals(addThing.getMaterialID())){
materials.remove(m);
materials.add(addThing);
return "此主料已存在!替换!";
}
}
materials.add(addThing);
System.out.println(materials.size());
return "成功增加!";
}
public String deleteMaterial(Material delete){
Iterator<Material> iter = materials.iterator();
while(iter.hasNext()){
Material m = iter.next();
if(m.equals(delete.getMaterialID())){
materials.remove(m);
return "成功删除!";
}
}
return "删除失败!此主料不存在!";
}
//保存菜谱
public String persist(){
BaseSQL b = new BaseSQL();
try {
b.connect("eatery", "root", "123456");
ResultSet rs = b.statement.executeQuery(
"select dishID from dish");
dishID = "00000001";
while(rs.next()){
if(Integer.parseInt(dishID) == Integer.parseInt(rs.getString(1))){
dishID = Integer.parseInt(dishID)+1+"";
}
}
dishID = AddZeroAhead.addZeroAhead(dishID, 8);
b.connection.setAutoCommit(false);
b.statement.executeUpdate("insert into dish values ('"
+dishID+"','"+dishName+"','"+price+"','"+type+"','"+dishSort+"','"+description+"')");
Iterator iter = materials.iterator();
Material material;
while(iter.hasNext()){
material = (Material)iter.next();
if(b.statement.executeQuery(
"select materialID from material where materialID = '"
+material.getMaterialID()+"'").next()){
b.statement.executeUpdate("insert into dish_material values ('"
+dishID+"','"+material.getMaterialID()+"',"+material.getWeight()+")");
}else{
b.connection.rollback();
dishID = "";
return "主料不存在,无法添加!";
}
}
b.connection.commit();
} catch (SQLException e) {
// TODO Auto-generated catch block
try {
b.connection.rollback();
} catch (SQLException e1) {
// TODO Auto-generated catch block
}
dishID = "";
return "数据库操作出错!";
} catch (Exception e) {
// TODO Auto-generated catch block
try {
b.connection.rollback();
} catch (SQLException e1) {
// TODO Auto-generated catch block
}
dishID = "";
return "出现未知错误!";
} finally {
try {
b.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
}
}
return null;
}
//修改菜谱
public String updateDish(){
BaseSQL b = new BaseSQL();
try {
b.connect("eatery", "root", "123456");
ResultSet rs = b.statement.executeQuery(
"select * from dish where dishID = '"+dishID+"'");
if(rs.next()){
b.connection.setAutoCommit(false);
b.statement.executeUpdate("update dish set dishID = '"
+dishID+"',dishName = '"+dishName+"',price = '"+price+"',type = '"+type+
"',dishSort = '"+dishSort+"',descrip = '"+description+"'where dishID ='"+dishID+"'");
}else{
return "数据库中不存在该项!";
}
Iterator iter = materials.iterator();
b.statement.executeUpdate("delete from dish_material where dishID ='"+dishID+"'");
while(iter.hasNext()){
Material material = (Material)iter.next();
if(b.statement.executeQuery(
"select materialID from material where materialID = '"
+material.getMaterialID()+"'").next()){
b.statement.executeUpdate("insert into dish_material values ('"
+dishID+"','"+material.getMaterialID()+"',"+material.getWeight()+")");
}else{
b.connection.rollback();
return "主料不存在,无法修改!";
}
}
b.connection.commit();
} catch (SQLException e) {
// TODO Auto-generated catch block
try {
b.connection.rollback();
} catch (SQLException e1) {
// TODO Auto-generated catch block
}
return "数据库操作出错!";
} catch (Exception e) {
// TODO Auto-generated catch block
try {
b.connection.rollback();
} catch (SQLException e1) {
// TODO Auto-generated catch block
}
return "出现未知错误!";
} finally {
try {
b.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
}
}
return null;
}
public String deleteDish(){
BaseSQL b = new BaseSQL();
try {
b.connect("eatery", "root", "123456");
ResultSet rs = b.statement.executeQuery(
"select * from dish where dishID = '"+dishID+"'");
if(rs.next()){
b.connection.setAutoCommit(false);
b.statement.executeUpdate("delete from dish where dishID = '"+dishID+"'");
b.connection.commit();
}else{
return "数据库中不存在该项!";
}
} catch (SQLException e) {
// TODO Auto-generated catch block
try {
b.connection.rollback();
} catch (SQLException e1) {
// TODO Auto-generated catch block
}
return "数据库操作出错!";
} catch (Exception e) {
// TODO Auto-generated catch block
try {
b.connection.rollback();
} catch (SQLException e1) {
// TODO Auto-generated catch block
}
return "出现未知错误!";
} finally {
try {
b.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
}
}
return null;
}
//
public String getDetailInfo(String dishID){
dishID = AddZeroAhead.addZeroAhead(dishID, 8);
BaseSQL b = new BaseSQL();
try {
if(getMainInfo(dishID) == null){
b.connect("eatery", "root", "123456");
materials = new Vector();
ResultSet rs = b.statement.executeQuery(
"select * from dish_material where dishID = '"+dishID+"'");
while(rs.next()){
Material material = new Material();
material.getData(dishID, rs.getString(2));
materials.add(material);
}
}else{
return "此菜不存在!";
}
} catch (SQLException e) {
// TODO Auto-generated catch block
return "数据库操作出错!";
} catch (Exception e) {
// TODO Auto-generated catch block
return "出现未知错误!";
} finally {
try {
b.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
}
}
return null;
}
public String getMainInfo(String dishID){
dishID = AddZeroAhead.addZeroAhead(dishID, 8);
BaseSQL b = new BaseSQL();
try {
b.connect("eatery", "root", "123456");
ResultSet rs = b.statement.executeQuery(
"select * from dish where dishID = '"+dishID+"'");
if(rs.next()){
this.dishID = rs.getString("dishID");
this.dishName = rs.getString("dishName");
this.description = rs.getString("descrip");
this.price = rs.getFloat("price");
this.dishSort = rs.getString("dishSort");
this.type = rs.getString("type");
}else{
return "此菜不存在!";
}
} catch (SQLException e) {
// TODO Auto-generated catch block
return "数据库操作出错!";
} catch (Exception e) {
// TODO Auto-generated catch block
return "出现未知错误!";
} finally {
try {
b.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
}
}
return null;
}
public float queryMaterial(String materialID){
materialID = AddZeroAhead.addZeroAhead(materialID, 8);
if(materials.size() == 0){
return 0;
}
for(int i = 0 ; i < materials.size(); i++){
Material temp = (Material)materials.get(i);
if(temp.equals(materialID)){
return temp.getWeight();
}
}
return 0;
}
public boolean equals(String compare){
if(Integer.parseInt(dishID)==Integer.parseInt(compare)){
return true;
}else{
return false;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -