Ich arbeite an einer Funktion, die beim Löschen eines Artikels den
aktuellen isFree-Status berücksichtigt. Testing.
This commit is contained in:
@@ -60,20 +60,23 @@ public class ArticleController {
|
|||||||
* L<>scht den Artikel mit der gegebenen ID in der Datenbank und aktualisiert die Tabelle.
|
* L<>scht den Artikel mit der gegebenen ID in der Datenbank und aktualisiert die Tabelle.
|
||||||
*
|
*
|
||||||
* @param pRow Row des Artikels, der gel<65>scht werden soll.
|
* @param pRow Row des Artikels, der gel<65>scht werden soll.
|
||||||
* @return true, wenn der Artikel erfolgreich gel<65>scht wurde. false, wenn ein Fehler aufgetreten ist.
|
* @return 0: Artikel konnte erfolgreich gel<65>scht werden
|
||||||
|
* 1: Artikel konnte nicht gel<65>scht werden, unbekannter Fehler (SQL-Fehler)
|
||||||
|
* 2: Artikel konnte nicht gel<65>scht werden, weil er im Moment verliehen ist.
|
||||||
*/
|
*/
|
||||||
public boolean deleteArticle(int pRow) {
|
public int deleteArticle(int pRow) {
|
||||||
|
|
||||||
|
if(!articleTableModel.getArticleByRow(pRow).getIsFree()) return 2;
|
||||||
|
|
||||||
int id = articleTableModel.getArticleByRow(pRow).getId();
|
int id = articleTableModel.getArticleByRow(pRow).getId();
|
||||||
|
int returnCode = dbCon.deleteArticle(id);
|
||||||
|
|
||||||
if(this.dbCon.deleteArticle(id)){
|
if(returnCode == 0){
|
||||||
articleTableModel.updateModel();
|
articleTableModel.updateModel();
|
||||||
freeArticleTableModel.updateModel();
|
freeArticleTableModel.updateModel();
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return returnCode;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,11 +4,13 @@ public class KArticle {
|
|||||||
private int id;
|
private int id;
|
||||||
private String name;
|
private String name;
|
||||||
private String description;
|
private String description;
|
||||||
|
private boolean isFree;
|
||||||
|
|
||||||
public KArticle(int pId, String pName, String pDesc) {
|
public KArticle(int pId, String pName, boolean pFree, String pDesc) {
|
||||||
this.id = pId;
|
this.id = pId;
|
||||||
this.name = pName;
|
this.name = pName;
|
||||||
this.description = pDesc;
|
this.description = pDesc;
|
||||||
|
isFree = pFree;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getId() {
|
public int getId() {
|
||||||
@@ -23,6 +25,10 @@ public class KArticle {
|
|||||||
return this.description;
|
return this.description;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean getIsFree(){
|
||||||
|
return isFree;
|
||||||
|
}
|
||||||
|
|
||||||
public void setName(String pName){
|
public void setName(String pName){
|
||||||
this.name = pName;
|
this.name = pName;
|
||||||
}
|
}
|
||||||
@@ -31,4 +37,8 @@ public class KArticle {
|
|||||||
this.description = pDesc;
|
this.description = pDesc;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setIsFree(boolean pFree){
|
||||||
|
isFree = pFree;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ public interface DbConnector {
|
|||||||
public int editUser(int pId, String pName, String pSurname);
|
public int editUser(int pId, String pName, String pSurname);
|
||||||
public ArrayList<KArticle> getArticleList();
|
public ArrayList<KArticle> getArticleList();
|
||||||
public int createArticle(String pName, String pDesc);
|
public int createArticle(String pName, String pDesc);
|
||||||
public boolean deleteArticle(int id);
|
public int deleteArticle(int id);
|
||||||
public int editArticle(int pId, String pName, String pDesc);
|
public int editArticle(int pId, String pName, String pDesc);
|
||||||
public ArrayList<KArticle> getFreeArticleList();
|
public ArrayList<KArticle> getFreeArticleList();
|
||||||
public ArrayList<KLender> getLenderList();
|
public ArrayList<KLender> getLenderList();
|
||||||
|
|||||||
@@ -51,9 +51,9 @@ public class SqlConnector implements DbConnector{
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean deleteArticle(int id) {
|
public int deleteArticle(int id) {
|
||||||
// TODO Auto-generated method stub
|
// TODO Auto-generated method stub
|
||||||
return false;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -211,11 +211,11 @@ public class SqliteConnector implements DbConnector {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
Statement st = this.connection.createStatement();
|
Statement st = this.connection.createStatement();
|
||||||
String query = "SELECT id, name, description FROM article";
|
String query = "SELECT id, name, is_free, description FROM article";
|
||||||
ResultSet rs = st.executeQuery(query);
|
ResultSet rs = st.executeQuery(query);
|
||||||
|
|
||||||
while (rs.next()){
|
while (rs.next()){
|
||||||
artArr.add(new KArticle(rs.getInt("id"), rs.getString("name"), rs.getString("description")));
|
artArr.add(new KArticle(rs.getInt("id"), rs.getString("name"), rs.getBoolean("is_free"), rs.getString("description")));
|
||||||
}
|
}
|
||||||
|
|
||||||
return artArr;
|
return artArr;
|
||||||
@@ -231,11 +231,11 @@ public class SqliteConnector implements DbConnector {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
Statement st = this.connection.createStatement();
|
Statement st = this.connection.createStatement();
|
||||||
String query = "SELECT id, name, description FROM article WHERE is_free = 1;";
|
String query = "SELECT id, name, is_free, description FROM article WHERE is_free = 1;";
|
||||||
ResultSet rs = st.executeQuery(query);
|
ResultSet rs = st.executeQuery(query);
|
||||||
|
|
||||||
while (rs.next()){
|
while (rs.next()){
|
||||||
artArr.add(new KArticle(rs.getInt("id"), rs.getString("name"), rs.getString("description")));
|
artArr.add(new KArticle(rs.getInt("id"), rs.getString("name"), rs.getBoolean("is_free"), rs.getString("description")));
|
||||||
}
|
}
|
||||||
|
|
||||||
return artArr;
|
return artArr;
|
||||||
@@ -378,18 +378,18 @@ public class SqliteConnector implements DbConnector {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean deleteArticle(int id) {
|
public int deleteArticle(int id) {
|
||||||
try {
|
try {
|
||||||
Statement st = this.connection.createStatement();
|
Statement st = connection.createStatement();
|
||||||
String query = "DELETE FROM article WHERE id = '"+id+"'";
|
String query = "DELETE FROM article WHERE id = '"+id+"'";
|
||||||
|
|
||||||
st.executeUpdate(query);
|
st.executeUpdate(query);
|
||||||
|
|
||||||
return true;
|
return 0;
|
||||||
}
|
}
|
||||||
catch (SQLException e){
|
catch (SQLException e){
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
return false;
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -96,7 +96,7 @@ public class ArticlePanel extends JPanel implements ActionListener, KeyListener
|
|||||||
this.lblArticleStatus = new JLabel("");
|
this.lblArticleStatus = new JLabel("");
|
||||||
lblName.setBounds(10, 30, 70, 20);
|
lblName.setBounds(10, 30, 70, 20);
|
||||||
lblDescription.setBounds(10, 61, 70, 20);
|
lblDescription.setBounds(10, 61, 70, 20);
|
||||||
this.lblArticleStatus.setBounds(90, 145, 250, 14);
|
this.lblArticleStatus.setBounds(90, 145, 390, 14);
|
||||||
|
|
||||||
// Edit: Name-Textfield
|
// Edit: Name-Textfield
|
||||||
this.textFieldArticleName = new JTextField();
|
this.textFieldArticleName = new JTextField();
|
||||||
@@ -232,7 +232,6 @@ public class ArticlePanel extends JPanel implements ActionListener, KeyListener
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void keyPressed(KeyEvent e) {
|
public void keyPressed(KeyEvent e) {
|
||||||
if(e.getKeyCode() == KeyEvent.VK_ENTER) saveButtonPressed();
|
if(e.getKeyCode() == KeyEvent.VK_ENTER) saveButtonPressed();
|
||||||
|
|
||||||
@@ -248,4 +247,18 @@ public class ArticlePanel extends JPanel implements ActionListener, KeyListener
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setDeleteStatusLabel(int pCode){
|
||||||
|
switch(pCode){
|
||||||
|
case 0:
|
||||||
|
lblArticleStatus.setText("Artikel erfolgreich gel<65>scht.");
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
lblArticleStatus.setText("Artikel kann nicht gel<65>scht werden.");
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
lblArticleStatus.setText("Artikel kann nicht gel<65>scht werden, w<>hrend er verliehen ist.");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ public class ArticleDeleteTableButton extends TableButton {
|
|||||||
|
|
||||||
int row = pTable.getSelectedRow();
|
int row = pTable.getSelectedRow();
|
||||||
|
|
||||||
pController.deleteArticle(row);
|
pPanel.setDeleteStatusLabel(pController.deleteArticle(row));
|
||||||
pPanel.resetModeEditArticle();
|
pPanel.resetModeEditArticle();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user