Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 这个班级设计错了吗?_Java_Mysql_Class - Fatal编程技术网

Java 这个班级设计错了吗?

Java 这个班级设计错了吗?,java,mysql,class,Java,Mysql,Class,我开始是一名java开发人员。我正在用java和mysql开发一个股票应用程序 我有个大问题。我想不出应该在哪里放置方法来保存数据。我应该为DB operatios创建一个新类,还是应该将它们放在inventory类中 谢谢 我的课程是: package inventory; public class Item { private String bc; // ITEM BARCODE private String description; // ITEM DESCRIPTION private

我开始是一名java开发人员。我正在用java和mysql开发一个股票应用程序

我有个大问题。我想不出应该在哪里放置方法来保存数据。我应该为DB operatios创建一个新类,还是应该将它们放在inventory类中

谢谢

我的课程是:

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");
}
}


}

快速回答是的,这是一个糟糕的设计对不起,下面是我要做的改进:

  • 尝试实现类之间的松散耦合,每个类都应该是一个接口的实现

    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会有这样一个引用(可以注入):

  • 尝试遵循驼峰案例实践()

  • 基于属性名称,属性和关联方法应该共享相同的名称

    private String barCode;
    
    public String getBarCode(){
        return this.barCode;
    }
    
    public void setBarCode(final String barCode){
        this.barCode = barCode;
    }
    
  • 遵循DAO模式(),基本上您需要创建一个类名ItemDao,它将负责“Item”的DB的所有操作

  • 清单应该重命名为ItemService,并且它不应该只有DAO才能访问DB,因此如果您需要实现另一个DAO,则可以很容易地替换它

  • 不要过多地使用System.out.print,而是将类配置为使用输出流,这样就可以轻松地使用文件或字符串输出流进行配置

  • 要将数据注入数据库,请使用prepared语句,而不是在查询()中连接数据


快速回答是的,这是一个糟糕的设计对不起,下面是我要做的改进:

  • 尝试实现类之间的松散耦合,每个类都应该是一个接口的实现

    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会有这样一个引用(可以注入):

  • 尝试遵循驼峰案例实践()

  • 基于属性名称,属性和关联方法应该共享相同的名称

    private String barCode;
    
    public String getBarCode(){
        return this.barCode;
    }
    
    public void setBarCode(final String barCode){
        this.barCode = barCode;
    }
    
  • 遵循DAO模式(),基本上您需要创建一个类名ItemDao,它将负责“Item”的DB的所有操作

  • 清单应该重命名为ItemService,并且它不应该只有DAO才能访问DB,因此如果您需要实现另一个DAO,则可以很容易地替换它

  • 不要过多地使用System.out.print,而是将类配置为使用输出流,这样就可以轻松地使用文件或字符串输出流进行配置

  • 要将数据注入数据库,请使用prepared语句,而不是在查询()中连接数据


寻找Dao模式:请学习Java编码标准并遵循它们。方法名称应以小写字母开头。对于有经验的Java开发人员来说,您的代码更难阅读。可读性非常重要。还要考虑命名。你的名字不太好。寻找Dao模式:请学习Java编码标准并遵循它们。方法名称应以小写字母开头。对于有经验的Java开发人员来说,您的代码更难阅读。可读性非常重要。还要考虑命名。你的名字不好。谢谢你的回答。没问题,我知道我的代码不是很好,哈哈哈,这是关于改进的。我还有一个问题。为什么更好地实现接口??我实现的每个类都应该有一个相关联的接口??不客气,这样做更好,因为它会使代码松散耦合。每个类都不知道其他类的实现。他们所知道的是,他们正在谈论的实现类尊重接口契约。因此,例如,如果您需要更改与SQL Server的连接,您需要做的唯一理论上的更改就是添加新的ConnectionManager实现并使用它。谢谢您的回答。没问题,我知道我的代码不是很好,哈哈哈,这是关于改进的。我还有一个问题。为什么更好地实现接口??我实现的每个类都应该有一个相关联的接口??不客气,这样做更好,因为它会使代码松散耦合。每个类都不知道其他类的实现。他们所知道的是,他们正在谈论的实现类尊重接口契约。因此,例如,如果您需要更改与SQL Server的连接,您需要做的唯一理论上的更改就是添加新的ConnectionManager实现并使用它。
private String barCode;

public String getBarCode(){
    return this.barCode;
}

public void setBarCode(final String barCode){
    this.barCode = barCode;
}