В этом java проекте у меня есть три класса с именами Query, QueryBuilder и Test, которые состоят из основного метода.
Query. java
public class Query {
private String SELECT;
private String FROM;
private String WHERE;
private String ORDERBY;
public void printQuery() {
if (WHERE == null && ORDERBY == null)
System.out.println("SELECT " + SELECT + "\nFROM " + FROM + "\n");
if (WHERE == null && ORDERBY != null)
System.out.println("SELECT " + SELECT + "\nFROM " + FROM + "\nORDER BY " + ORDERBY + "\n");
if (WHERE != null && ORDERBY == null)
System.out.println("SELECT " + SELECT + "\nFROM " + FROM + "\nWHERE " + WHERE + "\n");
if (WHERE != null && ORDERBY != null)
System.out.println("SELECT " + SELECT + "\nFROM " + FROM + "\nWHERE " + WHERE + "\nORDER BY " + ORDERBY + "\n");
}
public String getSELECT() {
return SELECT;
}
public void setSELECT(String SELECT) {
this.SELECT = SELECT;
}
public String getFROM() {
return FROM;
}
public void setFROM(String FROM) {
this.FROM = FROM;
}
public String getWHERE() {
return WHERE;
}
public void setWHERE(String WHERE) {
this.WHERE = WHERE;
}
public String getORDERBY() {
return ORDERBY;
}
public void setORDERBY(String ORDERBY) {
this.ORDERBY = ORDERBY;
}
}
QueryBuilder. java
public class QueryBuilder {
private Query query;
public QueryBuilder() {
query = new Query();
}
public QueryBuilder setSelect(String selectString) {
query.setSELECT(selectString);
return this;
}
public QueryBuilder setFrom(String fromString) {
query.setFROM(fromString);
return this;
}
public QueryBuilder setWhere(String whereString) {
query.setWHERE(whereString);
return this;
}
public QueryBuilder setOrderBy(String orderByString) {
query.setORDERBY(orderByString);
return this;
}
public Query build() {
query.printQuery();
if (query.getFROM() == null && query.getSELECT() == null)
throw new IllegalStateException("Query must have a SELECT and a FROM");
if (query.getSELECT() == null)
throw new IllegalStateException("Query must have a SELECT");
if (query.getFROM() == null)
throw new IllegalStateException("Query must have a FROM");
return query;
}
}
Тест. java
public class Test {
public static void main(String[] args) {
Query query1 = new QueryBuilder().setSelect("name").setFrom("student").build();
System.out.println(query1.toString());
Query query2 = new QueryBuilder().setSelect("name").setFrom("student").setWhere("name = 'Name1'").build();
System.out.println(query2.toString());
Query query3 = new QueryBuilder().setSelect("name").setWhere("name = 'Name1'").build();
System.out.println(query3.toString());
}
}
Выход - [1]: https://i.stack.imgur.com/TNadC.png
SELECT name
FROM student
Query@2a139a55
SELECT name
FROM student
WHERE name = 'Name1'
Query@15db9742
SELECT name
FROM null
WHERE name = 'Name1'
Exception in thread "main" java.lang.IllegalStateException: Query must have a FROM
at QueryBuilder.build(QueryBuilder.java:38)
at Test.main(Test.java:9)
I Нужно избавиться от распечатки памяти объектов в выходных данных. (Query@2a139a55 и Query@15db9742).
Заранее спасибо!