In my current Computer Science class, we are making a program in which will display a sales report based out of an excel file. A requirement is that we must use a method to form each line of the display, and use JOptionPanes to actually display it. Eg. formDisplay(line1data) "\n" formDisplay(line2data) etc.
The problem that I'm running in to, is that I have to line up the data, based on the periods in the numbers. The numbers are different amounts of characters, so I cannot manually go in and add spaces (which I've tried to do)
Here's all my code for the project, although I know it may not help very much, seeing as you don't have access to the excel files:
import java.io.File;
import java.text.DecimalFormat;
import java.util.Scanner;
import javax.swing.JOptionPane;
public class DailySalesGenerator {
public static String formPaymentSummary( String deptCode, double cashSales, double checkSales, double creditSales) {
String department = "";
if(deptCode.equals("EL")) {
department = "Electronics ";
} else if(deptCode.equals("FU")) {
department = "Furniture ";
} else if(deptCode.equals("MG")) {
department = "Misc. Goods ";
} else if(deptCode.equals("CL")) {
department = "Clothing ";
} else if(deptCode.equals("KG")) {
department = "Kitchen Goods";
}
DecimalFormat money = new DecimalFormat("#0.00#");
String formDisplay = department + " " + money.format(cashSales) + " " + money.format(checkSales) + " " + money.format(creditSales);
return formDisplay;
}
public static boolean isDouble( String input ) {
//Used a Stack Overflow example for integers, and changed it to doubles to work.
try {
Double.parseDouble( input );
return true;
}
catch( Exception e ) {
return false;}
}
public static void displayGreeting() {
JOptionPane.showMessageDialog(null, "Welcome to the Daily Sales Generator. You will choose from a list of already\nformatted excel files, and the Daily Sales report will be returned to you!", "Welcome!", 1);
}
public static void main(String[] args) throws Exception {
//Variable Declarations
String fileName = "";
double ELcash =0;
double FUcash=0;
double MGcash=0;
double CLcash=0;
double KGcash=0;
double ELcredit=0;
double FUcredit=0;
double MGcredit=0;
double CLcredit=0;
double KGcredit=0;
double ELcheck=0;
double FUcheck=0;
double MGcheck=0;
double CLcheck=0;
double KGcheck=0;
//Main Program
displayGreeting();
String fileChoice = JOptionPane.showInputDialog(null, "Which Sales report would you like to use?\nType '1' for DailySales_1.csv\nType '2' for DailySales_2.csv", "Which file?", 1);
if(fileChoice == null) {
JOptionPane.showMessageDialog(null, "Now exiting program!", "Error!", 1);
System.exit(0);
}else if(fileChoice.equals("")) {
JOptionPane.showMessageDialog(null, "Now exiting program!", "Error!", 1);
System.exit(0);
}else if(fileChoice.equals("1")) {
fileName = "C:\\dailysales_1.csv";
}else if(fileChoice.equals("2")) {
fileName = "C:\\dailysales_2.csv";
}else {
JOptionPane.showMessageDialog(null, "Now exiting program!", "Error!", 1);
System.exit(0);
}
//Used TipCalc.Java for reference.
File inFile = new File(fileName);
if(!inFile.exists())
{
JOptionPane.showMessageDialog(null,"File "+fileName+" not found!",
"Error Message", 1);
System.exit(0);
}
Scanner inScan = new Scanner(inFile);
while(inScan.hasNext())
{
String tempCell = inScan.next();
String deptTemp = tempCell;
tempCell = inScan.next();
String typeTemp = tempCell;
tempCell = inScan.next();
String amountTempString = tempCell;
double amountTemp = Double.parseDouble(amountTempString);
if(deptTemp.equals("EL") && typeTemp.equals("cash")) {
ELcash += amountTemp;
} else if(deptTemp.equals("EL") && typeTemp.equals("credit")) {
ELcredit += amountTemp;
} else if(deptTemp.equals("EL") && typeTemp.equals("check")) {
ELcheck += amountTemp;
} else if(deptTemp.equals("CL") && typeTemp.equals("cash")) {
CLcash += amountTemp;
} else if(deptTemp.equals("FU") && typeTemp.equals("cash")) {
FUcash += amountTemp;
} else if(deptTemp.equals("FU") && typeTemp.equals("credit")) {
FUcredit += amountTemp;
} else if(deptTemp.equals("FU") && typeTemp.equals("check")) {
FUcheck += amountTemp;
} else if(deptTemp.equals("MG") && typeTemp.equals("cash")) {
MGcash += amountTemp;
} else if(deptTemp.equals("MG") && typeTemp.equals("credit")) {
MGcredit += amountTemp;
} else if(deptTemp.equals("MG") && typeTemp.equals("check")) {
MGcheck += amountTemp;
} else if(deptTemp.equals("KG") && typeTemp.equals("cash")) {
KGcash += amountTemp;
} else if(deptTemp.equals("KG") && typeTemp.equals("credit")) {
KGcredit += amountTemp;
} else if(deptTemp.equals("KG") && typeTemp.equals("check")) {
KGcheck += amountTemp;
} else if(deptTemp.equals("CL") && typeTemp.equals("credit")) {
CLcredit += amountTemp;
} else if(deptTemp.equals("CL") && typeTemp.equals("check")) {
CLcheck += amountTemp;
} else { }
}
inScan.close();
String titles = " cash check credit totals";
String KGresults = formPaymentSummary("KG", KGcash, KGcheck, KGcredit);
String ELresults = formPaymentSummary("EL", ELcash, ELcheck, ELcredit);
String CLresults = formPaymentSummary("CL", CLcash, CLcheck, CLcredit);
String FUresults = formPaymentSummary("FU", FUcash, FUcheck, FUcredit);
String MGresults = formPaymentSummary("MG", MGcash, MGcheck, MGcredit);
JOptionPane.showMessageDialog(null,titles + "\n" + KGresults + "\n" + CLresults + "\n" + FUresults + "\n" + ELresults + "\n" + MGresults, "Results", 1);
}
}
I understand that my code most likely isn't written in the most efficient way, but we aren't able to use arrays or any complex structures at this point in time. Like I said, the biggest thing I need to know, is how to line up data in JOptionPanes. Preferably, based on decimal points.
EDIT: Here's a sample of the excel files! Everything is one cell a piece:
"FU credit 58.78"
"EL cash 62.2"
"MG cash 223.21"
"MG cash 14.78"
"MG check 166.8"
"CL check 270.35"
EDIT 2: Here's what my output looks like right now:
Electronics: 25.6 1938.0 3920
Furniture: 23.5 697.5 9385
Miscellanious Goods 294.6 295
Here's what I want it to look like:
Electonics: 25.6 1928.0 3920
Furniture 23.5 697.0 9385
etc.