I had to reconstruct my program from the last time I had posted for help. My instructor enlightened me that I needed to follow her example to the tea, even though I had the same output that was required, I did not have all concepts required meet.
So, after spending two days getting the program back on track, I am now having a problem getting the array items to pass through my constructor and the result is null outputs. I have scanned the code so many times and can't see the error. If someone could take the time to thoroughly look over my main java doc and it's corresponding pages that hold my super classes and constructor for the array it would be greatly appreciated.
Page 1 main java doc.
import java.text.NumberFormat;
import java.util.Formatter;
import java.util.Locale;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Ashley Anderson
*/
public class sales {
static dataFormat [] Data = new dataFormat [0];
static int currentData = 0;
public static void main (String args[])
{
//loading the array object
addNewData("Fresh Farm","Lemon", 8, 10.78);
addNewData("Dale Farm","Lettuce", 6, 8.99);
addNewData("Star Farm","Tomato", 9, 12.95);
addNewData("Apple Acers","Apple", 12, 12.55);
addNewData("Orangeland Farms","Orange", 1, 14.99);
dataSort();
ApplicationGUI appGui = new ApplicationGUI();
}
public static float inventoryValue(){
float total = 0;
for (int x = 0; x < Data.length; x++){
total += Data[x].getSalesValue();
}
return total;
}
public static void dataSort() {
dataFormat tmp1, tmp2;
;
for (int i=0; i <Data.length; i++){
for (i = currentData + 1; i< Data.length; i++){
String s1 = Data[i-1].getName();
String s2 = Data[i].getName();
if(s1.compareTo(s2) > 0) {
tmp1 = Data[i-1];
tmp2 = Data[i]; Data[currentData] = Data[i];
Data[i] = tmp1;
Data[i-1] = tmp2;
}
}
}
}
public static String getInventoryString (){
StringBuilder sb = new StringBuilder();
Formatter formatter = new Formatter(sb, Locale.US);
for (int x = 0; x < Data.length; x++){
formatter.format(Data[x].toString());
formatter.format("\n");
}
formatter.format("Total Inventory Value: %s\n", NumberFormat.getCurrencyInstance().format(inventoryValue()));
return sb.toString();
}
public static String getCurrentString(){
if(Data.length == 0){
return "No Data!";
}else{
StringBuilder sb = new StringBuilder();
Formatter formatter = new Formatter(sb, Locale.US);
formatter.format(Data[currentData].toString());
formatter.format("\n");
formatter.format("Total Inventory Value: %s\n", NumberFormat.getCurrencyInstance().format(inventoryValue()));
return sb.toString();
}
}
public static String[] getStringArray(){
String[] currentStringArray = new String[4];
currentStringArray[0] = Data[currentData].getName();
currentStringArray[1] = Data[currentData].getFarm();
currentStringArray[2] = Integer.toString(Data[currentData].getSold());
currentStringArray[3] = Double.toString(Data[currentData].getPrice());
return currentStringArray;
}
public static void showNext(){
if (currentData == Data.length -1){
showFirst();
}else{
currentData ++;
}
}
public static void showLast() {
currentData = Data.length -1;
}
public static void showFirst() {
currentData = 0;
}
public static void searchData(String name){
for(int searchData = 0; searchData < Data.length; searchData++) {
if (name.compareToIgnoreCase(Data[searchData].getName())== 0){
currentData = searchData;
return;
}
}
// ApplicationGUI.searchFailed();
}
public static void modifyData(String [] newData){
Data[currentData].setName(newData[0]);
Data[currentData].setName(newData[1]);
Data[currentData].setSold(Integer.valueOf(newData[2]));
Data[currentData].setPrice(Double.valueOf(newData[3]));
dataSort();
}
public static int getNextData(){
int newCode = 0;
for (int x = 0; x < Data.length; x++){
if (Data[x].getNumSku() > newCode){
newCode = Data[x].getNumSku();
}
}
return newCode +1;
}
private static void addNewData(String name, String farm, int sold, double price) {
String [] newData = new String [4];
newData[0] = name;
newData[1] = farm;
newData[2] = Integer.toString(sold);
newData[3] = Double.toString(price);
addData(newData); //CALLS METHOD
}
public static void addData(String[] newData) {
dataFormat iDataFormat = new dataFormat(getNextData(), newData[0], Integer.valueOf(newData[2]), Double.valueOf(newData[3]), newData[1]);
Class elementType = Data.getClass().getComponentType();
dataFormat[] newArray = (dataFormat[])java.lang.reflect.Array.newInstance(elementType, Data.length +1);
System.arraycopy(Data, 0, newArray, 0, Data.length);
newArray[newArray.length -1] = iDataFormat;
Data = newArray;
showLast();
dataSort();
}
public static void deleteData(){
int dataToDelete = currentData;
Class elementType = Data.getClass().getComponentType();
dataFormat[] newArray = (dataFormat[]) java.lang.reflect.Array.newInstance(elementType, Data.length -1);
System.arraycopy(Data, 0, newArray, 0, dataToDelete);
System.arraycopy(Data, dataToDelete+1, newArray, dataToDelete, Data.length - dataToDelete -1);
Data = newArray;
showPrevious();
dataSort();
}
public static void saveData(){
for(int x = 0; x < Data.length; x++){
if (Data[x].saveData()){
}else{
ApplicationGUI.saveFailed();
return;
}
}
ApplicationGUI.saveSuccessful();
return;
}
static int getDataSize() {
return Data.length;
}
static void showPrevious() {
if (currentData == 0){
showLast();
}else{
currentData--;
}
}
}
Page two, original constructor for array and is inherited by the dataformat java doc.
/**
*
* @author Ashley
*/
public class Data {
private int numSku;
private String name;
private int sold;
private double price;
public Data(int numSku, String name, int sold, double price){
numSku = numSku;
name = name;
sold = sold;
price = price;
}
public Data(String name){
numSku = 0;
name = name;
sold = 0;
price = 0;
}
public Data (){
numSku = 0;
name = "";
sold = 0;
price = 0;
}
/**
* @return the numSku
*/
public int getNumSku() {
return numSku;
}
/**
* @param numSku the numSku to set
*/
public void setNumSku(int numSku) {
this.numSku = numSku;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the sold
*/
public int getSold() {
return sold;
}
/**
* @param sold the sold to set
*/
public void setSold(int sold) {
this.sold = sold;
}
/**
* @return the price
*/
public double getPrice() {
return price;
}
/**
* @param price the price to set
*/
public void setPrice(double price) {
this.price = price;
}
// calculation for sales value
public double getSalesValue(){
return price * sold;
}
}
the dataformat java doc extends on data.java, it is unnecessary for it be this way but is required for the class.
class dataFormat extends Data{
private String farm;
dataFormat(int numSku, String name, Integer sold, Double price, String farm) {
super(numSku, name, sold, price);
farm = farm;
}
public dataFormat(String name){
super(name);
}
public String getFarm(){
return farm;
}
public void setFarm(String farm){
farm = farm;
}
@Override
public double getSalesValue(){
return super.getSalesValue();
}
public double getTax(){
return super.getSalesValue() * 07;
}
public String toString() {
StringBuilder sb = new StringBuilder();
// Send all output to the Appendable object sb
Formatter formatter = new Formatter(sb, Locale.US);
// Explicit argument indices may be used to re-order output.
formatter.format("Salesman Number: %d\n", getNumSku());
formatter.format("Salesman Name: %s\n", getName());
formatter.format("Department: %s\n", getFarm());
formatter.format("Number of Widgets Sold: %d\n", getSold());
formatter.format("Widget Price: %s\n", NumberFormat.getCurrencyInstance().format(getPrice()));
formatter.format("Total Sales: %s\n", NumberFormat.getCurrencyInstance().format(getSalesValue()));
formatter.format("Sales Bonus: %s\n", NumberFormat.getCurrencyInstance().format(getTax()));
formatter.format("Total value plus Fee: %s\n", NumberFormat.getCurrencyInstance().format(getSalesValue() + getTax()));
return sb.toString();
}
public Boolean saveData() {
StringBuilder fileLine = new StringBuilder();
FileWriter fstream = null;
// Create file
try {
// Create the file if it doesn't exist, make the file appendable
fstream = new FileWriter("c:\\data\\sales.dat", true);
}
catch (Exception e) {
// Directory doesn't exist or some other error
// Check if directory exists
File file=new File("C:\\data");
boolean exists = file.exists();
// If not, create it or else we have some sort of other error, so quit
if (!exists) {
boolean success = (new File("C:\\data")).mkdir();
// We the directory is created, try save again
if (success) {
return saveData();
}
}
System.err.println("Error: " + e.getMessage());
return false;
}
try {
BufferedWriter out = new BufferedWriter(fstream);
fileLine.append(String.valueOf(getNumSku()));
fileLine.append(";");
fileLine.append(getName());
fileLine.append(";");
fileLine.append(getFarm());
fileLine.append(";");
fileLine.append(String.valueOf(getSold()));
fileLine.append(";");
fileLine.append(String.valueOf(getPrice()));
out.append(fileLine.toString());
out.newLine();
out.close();
}
catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
return false;
}
return true;
}
}