To print the data separated by ;
, considering the search of the data of the database done by the colleague up there
After you have searched the data in your database or another, and have the data in a collection in java (ArrayList), just iterate the array like this:
public void printReport(Date initialDate, Date endDate) {
List<Sale> sales = getSales(initialDate, endDate);//chamando getSales
StringBuilder sb = new StringBuilder();
for(Sales sale: sales){
sb.append(sale.getStoreName() + ";" + sale.getVisa() + ";"+sale.getMaster() + ";" + sale.getDiners() + ";" + sale.getAmex() + ";" + sale.getTotal());
}
}
System.out.println(sb.toString());
It may be even easier if you override the toString method of the Sales class
@override
public String toString(){
return storeName+";"+visa+";"+master+";"+dinners+";"+amex+";"+total;
}
Then you would do it:
for(Sales sale: sales){
sb.append(sale.toString());
}
Edit: Consider the Store object as an attribute of the class Sale (Code snippets of other answers in this question)
Store class.
public class Store{
private int id;
private String name;
//gets and sets
}
Class Sale
public class Sale {
//por questões didáticas coloquei como String a maioria.
private Store store;
private String visa ;
private String master ;
private String diners;
private String amex ;
private Double total;
//getts e setts
}
An example of Select that would bring the DB data
private static final String SALES_BY_DATES_SQL = ""
+ "SELECT store.name store_name, store.id store_id, visa, master, diners, amex "
+ "FROM Sale, Store "
+ "WHERE date >= ? AND date <= ?"
+ " and sale.store_id = store.id";
Method that retrieves DB data
public List<Sale> getSales(Date startDate, Date endDate) {
try (
Connection c = params.conectar();
PreparedStatement ps = c.prepareStatement(SALES_BY_DATES_SQL);
)
{
ps.setDate(1, startDate);
ps.setDate(2, endDate);
try (ResultSet rs = ps.executeQuery()) {
List<Sale> sales = new ArrayList<>();
while (rs.hasNext()) {
Sale s = new Sale(
//carregando o objeto store.
Store store = new Store();
store.setId(rs.getInt("store_id"));
store.setName(rs.getString("store_name"));
sale.setStore(store);
rs.getInt("visa"),
rs.getInt("master"),
rs.getInt("diners"),
rs.getInt("amex"));
sales.add(s);
}
return sales;
}
}
}
Then to create the layout:
for(Sales sale: sales){
sb.append(sale.getStore().getName() + ";" + sale.getVisa() + ";"+sale.getMaster() + ";" + sale.getDiners() + ";" + sale.getAmex() + ";" + sale.getTotal());
}
or:
@override
public String toString(){
return getStore().getName()+";"+visa+";"+master+";"+dinners+";"+amex+";"+total;
}
Edit: Whereas data is already coming right from the database:
method that formats the printout:
public StringBuilder formatarLayout(ArrayList<Sales> sales){
StirngBuilder sb = new StringBuilder();
for(Sales sale: sales){
sb.append(sale.getStore().getName() + ";" + sale.getVisa() + ";"+sale.getMaster() + ";" + sale.getDiners() + ";" + sale.getAmex() + ";" + sale.getTotal());
}
return sb;
}
printReport method
public void printReport(Date initialDate, Date endDate) {
List<Sale> sales = getSales(initialDate, endDate);
//isso ja retorna seus dados formatados
String layout = formatarLayout(sales).toString();
// agora pode fazer o que quiser com eles
}