2016-02-28 46 views
1

我開始作爲一個Java開發人員。我正在用java和mysql開發一個股票應用程序。這個班級設計是否錯誤?

我有一個很大的問題。我無法想出我應該如何放置方法來保存數據。我應該爲數據庫操作創建一個新類,還是應該將它們放在庫存類中?

謝謝!

我的類別是:

package inventory; 

public class Item 
{ 
private String bc; // ITEM BARCODE 
private String description; // ITEM DESCRIPTION 
private int nItm; // NUMBER OF ITEMS IN A PACK OR ITEMS 

/** 
* Constructor for objects of class item 
* - Initialise instance variables bc, description to null and nItm to 0 
*/ 
public Item() 
{ 
    // initialise instance variables 
    bc = null; 
    description = null; 
    nItm = 0; 
} 

/** 
* Constructor for objects of class item 
* - Initialise instance variables to a given parameters. 
* @param bc   String item codebar. 
* @param description String description of the item. 
* @param nItm   int items or number of items in a pack 
*/ 
public Item(String bc,String description,int nItm) 
{ 
    this.bc = bc; 
    this.description = description; 
    this.nItm = nItm; 
} 

/** 
* Gets a codebar from the item 
* 
* @return  A String with the codebar item. 
*/ 
public String getBarcode() 
{ 
    // put your code here 
    return bc; 
} 

/** 
* Gets description from the item 
* 
* @return  A String with the description item. 
*/ 
public String getDescription() 
{ 
    return description; 
} 

/** 
* Gets the number of items in stock 
* 
* @return  Number of items in stock. 
*/ 
public int getNumberOfItems() 
{ 
    return nItm; 
} 

/** 
* Set the value for codebar 
* 
* @param bc Barcode item. 
*/ 
public void setBarcode(String bc) 
{ 
    this.bc = bc; 
} 

/** 
* Set the value for description 
* 
* @param description Description item. 
*/ 
public void setDescription(String description) 
{ 
    this.description = description; 
} 

/** 
* Set the value for nItm 
* 
* @param nItm Number of items or items in a pack 
*/ 
public void setNumberOfItems(int nItm) 
{ 
    this.nItm = nItm; 
} 
/** 
* Shows an item with this template "| %-16s | %-44s | %5d |" 
*/ 
public void showItem() 
{ 
    System.out.print("\f"); 
    System.out.printf("+------------------+----------------------------------------------+-------+\n"); 
    System.out.printf("| BARCODE   | DESCRIPTION         | STOCK |\n"); 
    System.out.printf("+------------------+----------------------------------------------+-------+\n"); 
    System.out.printf("| %-16s | %-44s | %5d |",this.bc,this.description,this.nItm); 
    System.out.printf("+-------------------------------------------------------------------------+\n"); 
} 

}

package inventory; 
import javax.swing.JTable; 
import javax.swing.table.DefaultTableModel; 
import DataBases.*; 
import java.util.Scanner; 
import java.sql.*; 
public class Inventory 
{ 
// instance variables - replace the example below with your own 
private Item[] inventory; // array of items 
public int nreg; // NUMBER OF ITEMS STORED 

/** 
* Constructor for objects of class Inventory 
*/ 
public Inventory() throws SQLException,ClassNotFoundException,InstantiationException,IllegalAccessException 
{ 
    // initialise instance variables 

    this.inventory = new Item[100]; 
    loadData(); 
} 

/** 
* An example of a method - replace this comment with your own 
* 
* @param y a sample parameter for a method 
* @return  the sum of x and y 
*/ 
public void addItem(String bc,String description,int nItm) 
{ 
    // put your code here 
    Item item; 
    item = new Item(bc,description,nItm); 
    inventory[nreg] = item; 
    nreg++; 
} 

/** 
* An example of a method - replace this comment with your own 
* 
* @param y a sample parameter for a method 
* @return  the sum of x and y 
*/ 
public void loadData() throws SQLException,ClassNotFoundException,InstantiationException,IllegalAccessException 
{ 
    Mysql c = new Mysql(); 
    ResultSet rs; 

    c.On(); 
    rs = c.ExeGet("SELECT * FROM item"); 
    while(rs.next()) 
    { 
     inventory[nreg] = new Item(rs.getString("barcode"),rs.getString("description"),rs.getInt("nItems")); 
     this.nreg++; 
    } 
    c.Off(); 
} 

/** 
* An example of a method - replace this comment with your own 
* 
* @param y a sample parameter for a method 
* @return  the sum of x and y 
*/ 
public void searchItem() 
{ 
    System.out.print("\f"); 
    int op; 
    System.out.printf("\tSEARCH MENU\n"); 
    System.out.printf("\t===========\n\n"); 
    System.out.printf("\t1.BY BARCODE.\n"); 
    System.out.printf("\t2.BY ITEM.\n"); 
    System.out.printf("\t3.BY NUMBER OF ITEMS.\n"); 
    System.out.printf("\t0.EXIT.\n\n"); 
    System.out.printf("\tOPTION: "); 

    Scanner reader = new Scanner(System.in); 
    op = reader.nextInt(); 
    switch(op) 
    { 
     case 0: break; 
     //case 1: searchByBarcode();break; 
     //case 2: searchByItem();break; 
     case 3: searchByNumberOfItems();break; 
     default: break; 
    } 
} 

/** 
* An example of a method - replace this comment with your own 
* 
* @param y a sample parameter for a method 
* @return  the sum of x and y 
*/ 
/* 
private void searchByBarcode() 
{ 
    int op; 
    int flag = 0; 
    Item reg = new Item(); 

    do{ 
     System.out.print("\f"); 
     System.out.println("OPTION 1. OK"); 
     System.out.printf("BARCODE: "); 
     String bc; 
     Scanner reader = new Scanner(System.in); 
     bc = reader.nextSt(); 
    // AQUI VA UN TRY COMO UNA CASA 
     for(int i=0;i<nreg;i++) 
     { 
      if (inventory[i].getBarcode() == bc) 
      { 
       reg = inventory[i]; 
       flag = 1; 
       break; 
      } 
     } 
     if (flag == 1) 
     { 
      reg.showItem(); 
     } 
     else 
     { 
      System.out.printf("I CAN NOT FIND %d",bc); 
     } 
     System.out.println("CONTINUE SEARCHING (0/1): "); 
     op = reader.nextInt(); 

    }while(op != 1); 
} 
*/ 

/** 
* An example of a method - replace this comment with your own 
* 
* @param y a sample parameter for a method 
* @return  the sum of x and y 
*/ 
private void searchByNumberOfItems() 
{ 
    System.out.print("\f"); 
    System.out.println("OPTION 3. OK"); 
} 

/** 
* An example of a method - replace this comment with your own 
* 
* @param y a sample parameter for a method 
* @return  the sum of x and y 
*/ 
public void addNewItem(String barcode,String description,int nItm) throws SQLException,ClassNotFoundException,InstantiationException,IllegalAccessException 
{ 
    Mysql c = new Mysql(); 
    Item i = new Item(barcode,description,nItm); 
    String q = "INSERT INTO inventory.item (barcode,description,nItems) VALUES ('" + i.getBarcode() + "','" 
       + i.getDescription() + "'," + i.getNumberOfItems() + ");"; 

    c.On(); 
    c.Exe(q); 
    c.Off(); 
    nreg++; 
} 

/** 
* An example of a method - replace this comment with your own 
* 
* @param y a sample parameter for a method 
* @return  the sum of x and y 
*/ 
public void listItems() throws SQLException,ClassNotFoundException,InstantiationException,IllegalAccessException 
{ 
    Mysql c = new Mysql(); 
    ResultSet rs; 

    c.On(); 
    rs = c.ExeGet("SELECT * FROM item"); 
    System.out.print("\f"); 
    System.out.printf("+------------------+----------------------------------------------+-------+\n"); 
    System.out.printf("| BARCODE   | DESCRIPTION         | STOCK |\n"); 
    System.out.printf("+------------------+----------------------------------------------+-------+\n"); 
    while(rs.next()) 
    { 
     //System.out.println("" + rs.getString("barcode") + "  " + rs.getString("description") + "  " + rs.getInt("nItems")); 

     System.out.printf("| %-16s | %-44s | %5d |\n",rs.getString("barcode"),rs.getString("description"),rs.getInt("nItems")); 
    } 
    System.out.printf("+-------------------------------------------------------------------------+\n"); 
    c.Off(); 
} 

public void listArray() 
{ 
    System.out.print("\f"); 
    System.out.printf("+------------------+----------------------------------------------+-------+\n"); 
    System.out.printf("| BARCODE   | DESCRIPTION         | STOCK |\n"); 
    System.out.printf("+------------------+----------------------------------------------+-------+\n"); 
    for(int i = 0;i<nreg;i++) 
    { 
     System.out.printf("| %-16s | %-44s | %5d |\n",inventory[i].getBarcode(),inventory[i].getDescription(),inventory[i].getNumberOfItems()); 
    } 
    System.out.printf("+-------------------------------------------------------------------------+\n"); 
    System.out.printf("| NUMBER OF ITEMS: %3d             |\n",nreg); 
    System.out.printf("+-------------------------------------------------------------------------+\n"); 
} 

}

package DataBases; 
import java.sql.*; 


public class Mysql 
{ 
// instance variables - replace the example below with your own 
private String host; 
private String user; 
private String pass; 
private String db; 
private int port; 
private Connection connection; 

/** 
* Constructor for objects of class Mysql 
*/ 
public Mysql()throws ClassNotFoundException,InstantiationException,IllegalAccessException 
{ 
    // initialise instance variables 
    this.host = "localhost"; 
    this.user = "inventory"; 
    this.pass = "123456"; 
    this.port = 3306; 
    this.db = "inventory"; 
    this.Init(); 
} 

public Mysql(String host,String user,String pass,int port,String db) throws ClassNotFoundException,InstantiationException,IllegalAccessException 
{ 
    this.host = host; 
    this.user = user; 
    this.pass = pass; 
    this.port = port; 
    this.db = db; 
    this.Init(); 
} 


public void Init() throws ClassNotFoundException,InstantiationException,IllegalAccessException 
{ 
    // put your code here 
    Class.forName("com.mysql.jdbc.Driver").newInstance(); 
} 

public void On() throws SQLException 
{ 

    this.connection = DriverManager.getConnection("jdbc:mysql://"+ this.host +"/" + this.db,this.user,this.pass); 
} 

public void Off() throws SQLException 
{ 

    this.connection.close(); 
} 

public ResultSet ExeGet(String Query) throws SQLException 
{ 
    Statement st = this.connection.createStatement(); 
    return (ResultSet) st.executeQuery(Query); 
} 

public int Exe(String Query) throws SQLException 
{ 

    Statement st = this.connection.createStatement(); 
    return st.executeUpdate(Query); 
} 

}

+1

找DAO模式:http://stackoverflow.com/questions/12812256/how-do -i-implement -a-dao-manager-using-jdbc-and-connection-pools – MGorgon

+0

請學習Java編碼標準並遵循它們。您的方法名稱應以小寫字母開頭。您的代碼對於有經驗的Java開發人員來說更難閱讀。可讀性很重要。也考慮命名。你的名字不好。 – duffymo

回答

0

要快速回答是這是一個可怕的設計對不起,這裏是我會改善我t:

  • 試着實現你的類之間的鬆耦合,每個類都應該是一個接口的實現。

    public interface ConnectionManager { 
        void On() throws SQLException; 
        void Off() throws SQLException; 
        ResultSet ExeGet(String Query) throws SQLException; 
        int Exe(String Query) throws SQLException; 
    } 
    
    public class MysqlConnectionManager implements ConnectionManager { 
        @Override 
        public void On() throws SQLException { 
        } 
        @Override 
        public void Off() throws SQLException { 
        } 
        @Override 
        public ResultSet ExeGet(String Query) throws SQLException { 
        } 
        @Override 
        public int Exe(String Query) throws SQLException { 
        } 
    } 
    

然後你的DAO將有一個這樣的引用(這可能注入):

ConnectionManager connectionManager = new MysqlConnectionManager(); 
  • 嘗試按照駱駝的情況下實踐(Wiki source

  • 您的屬性和關聯方法應根據您的屬性名稱共享相同的名稱。

    private String barCode; 
    
    public String getBarCode(){ 
        return this.barCode; 
    } 
    
    public void setBarCode(final String barCode){ 
        this.barCode = barCode; 
    } 
    
  • 按照DAO模式(Wiki source),基本上你需要創建一個類名ItemDao會照顧所有操作與DB的「項目」。

  • 該庫存應該重命名爲ItemService,並且它不應該訪問只有DAO纔會訪問的數據庫,所以如果您需要實現另一個DAO,則很容易將其替換。

  • 不要過多地使用System.out.print,而是將您的類配置爲使用輸出流,因此可以使用文件或字符串輸出流輕鬆進行配置。

  • 要注入數據到數據庫使用準備語句而不是你的查詢中串聯數據(SQL injection

+0

感謝您的回答。沒問題,我知道我的代碼不是很好哈哈哈,這有關改善。我還有其他問題。爲什麼更好的實現接口?我實現的每個類都應該有一個相關的接口? –

+0

不客氣,最好是因爲它讓你的代碼鬆散耦合每個類對其他類的實現一無所知他們知道的是他們正在說的實現類尊重接口契約。因此,如果您需要例如更改連接到SQL Server,則需要做的唯一理論更改是添加ConnectionManager的新實現並使用它。 –