0

OHHH lol, that would make sense! ya i did get confused I should have gone back and read what you said again. let me go change that stuff real fast

0

Much better!! thank you once again :P. Could you give me a little guidance as to how to fix the sku or where to start, please and thank you :).

0

OK, then you can keep the current implementation (even though it is not 100% efficient but just for the sake of understanding). What you need to do is to add a part to your toString(). Instead of doing...

out += "SKU: "+sku+"\n";

What you need to do is to convert the sku to string and add leading 0 to it up to 8 digits. You could do it as follows:

String skuStr = Integer.toString(sku);
for (int i=(8-skuStr.length()); i>0; i--) {
  skuStr = "0"+skuStr;
}
...
...
out += "SKU: "+skuStr+"\n";

That's one way to do it. However, this way has a flaw is that when sku has more than 8 digits, it doesn't change the sku at all. Also, if sku is a negative number, it will generate wrong string (i.e. sku is -233 and it will generate "0000-233").

Edited by Taywin: n/a

0

ok, as far as I recall that would be fine so long as the out put displayed the 8 digits. I am going to add it and double check that I don't have to pass it a different way as a requirement.

0

It didn't work, does the for loop close after the "out" declarations? here is how I added it.

public String toString() {
      
      String skuStr = Integer.toString(sku);
        for (int i=(8-skuStr.length()); i>0; i--) {
        skuStr = "0"+skuStr;
}

     
    
    String out = "";
    out += "Item Name: "+name+"\n";
    out += "Department: "+department+"\n";
    out += "SKU: "+sku+"\n";
    out += "Sold: "+sold+"\n";
    out += "Price: $"+price+"\n";
    out += "Total: $"+ CFORMAT.format(getTotal())+"\n";
    out += "Tax: $"+ CFORMAT.format(getTax())+"\n";
    out+= "Overall Total: $"+ CFORMAT.format(getOverallTotal())+"\n";
    
    return out;
  }
0

no error report just not displaying 8 digits, only one digit still.

0

Tell me if this code is correct.

public static int getNextData() {
    int newCode = 0;
    for (int x = 0; x < Data.size(); x++) {
      if (Data.get(x).getiSku() > newCode) {
        newCode = Data.get(x).getiSku();
      }
    }
    return newCode + 1;
  }

the line of code "newCode = Data.get(x).getiSku();" is it pointing to the right object, getiSku?

0

The getiSku() returns an integer which is not a String. If you want, you can create another method call getiSkuStr() and return the String "skuStr" as you did in the toString().

By the way, it will automatically generate a new SKU number and ignore the number entered by the user. In this case, it ensures that the number is automatically generated and will always be unique.

Edited by Taywin: n/a

0

I got it to display 8 digits but it displays 0's for the first 7 and only the first digit from the array sku number. here is what I did.

import java.text.DecimalFormat;

class DataFormat {
    
    final DecimalFormat CFORMAT = new DecimalFormat("#.##");
    
  String name;
  String department;
  int sku;
  int sold;
  float price;
  
  public DataFormat(int code, String[] dat) {
    name = dat[1];
    department = dat[0];
    sku = code;
    sold = Integer.parseInt(dat[3]);
    price = Float.parseFloat(dat[4]);
  }


  public float getTotal() {
    return (price*sold);
  }
  
  public double getTax(){
    return (getTotal()*0.7);
  }

  public double getOverallTotal(){
    return (getTotal()+getTax());
  }

  public float getiPrice() {
    return price;
  }


  public int getiSold() {
    return sold;
  }


  public String getiName() {
    return name;
  }


  public int getiSku() {
    return sku;
  }


  public String getDept() {
    return department;
  }
  
  public String getSkuStr(int sku){
     String skuStr = Integer.toString(sku);
        for (int i=(8-skuStr.length()); i>0; i--) {
        skuStr = "0"+skuStr;
        }
        return skuStr;
  }


  @Override
  public String toString() {
      
    

     
    
    String out = "";
    out += "Item Name: "+name+"\n";
    out += "Department: "+department+"\n";
    out += "SKU: "+getSkuStr(sku)+"\n";
    out += "Sold: "+sold+"\n";
    out += "Price: $"+price+"\n";
    out += "Total: $"+ CFORMAT.format(getTotal())+"\n";
    out += "Tax: $"+ CFORMAT.format(getTax())+"\n";
    out+= "Overall Total: $"+ CFORMAT.format(getOverallTotal())+"\n";
        
    return out;
       
  }
}

Edited by Blink383: n/a

0

It is not the "first" digit. It is the real integer of what the class is generated. It starts from 1 and increases by 1 every time a new data is entered.

OK, you need to decide what to do with SKU. Currently (and in your original code), you ignore the SKU input and generate a number by your own. Now, I need to ask you again... Do you still want to let the "product" class automatically generates the SKU number or you want only the user to enter the SKU number? If you choose the first, it is already in the code right now. If you choose the latter, you need to modify your DataFormat constructor and also your addData() method in the "product" class.

0

well, technically so long as it's displaying 8 digits I dont see why it would be a problem the way it is now.

0

So, I have made progress and added in some of the functions needed for this week. However I have realized my that, either my calculations or the formatter I have used is wrong. The tax is incorrect for the output. As well as, the Total Product Value, It should be totaling all the tax's + Total's (This could just be because of the wrong calculations above.)

here is the output
SKU Number: 00000003
Farm Name: Star Farm
Item Name: Tomato
Items Sold: 9
Price = $12.95
Total = $116.55
Tax = $81.58
Total + Tax = $198.13

Total Product Value: $422.32

Here is my current code:

import java.text.NumberFormat;
import java.util.Formatter;
import java.util.Locale;
import java.util.ArrayList;

/**
 *
 * @author Ashey Anderson
 */
public class product {
  // Create an array 
  private static ArrayList<DataFormat> Data = new ArrayList<DataFormat>();
  // Keep track of current Data
  private static int currentData = 0;

  // main method begins execution of the Java application;
  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();
    for (int i=0; i<Data.size(); i++) {
      System.out.println(Data.get(i));
    }


    ApplicationGUI appGUI = new ApplicationGUI(); //calls constructor

  } // end main
//Method for calculating total inventory
  public static float inventoryValue() {
    float total = 0;
    for (int x = 0; x < Data.size(); x++) {
         total += Data.get(x).getOrderTotal();
    }// end for loop

    return total;
  }

  // Return a string for the current item
  public static String getCurrentString() {
    if (Data.isEmpty()) {
      return "No data";
    } else {
      StringBuilder sb = new StringBuilder();    
      
      // Send all output to the Appendable object sb
      Formatter formatter = new Formatter(sb, Locale.US);

      // Build Inventory String
      formatter.format(Data.get(currentData).toString());
      formatter.format("\n");
      formatter.format("Total Product Value: %s\n", NumberFormat.getCurrencyInstance().format(inventoryValue()));
      return sb.toString();
    }
  }

  // Method to increment current item
  public static void showNext() {
    if (currentData == (Data.size()-1)) {
      showFirst();
    } else {
      currentData++;
    }
  }
  
  // METHOD TO GO TO PREVIOUS ITEM.
  static void showPrevious() {
        if (currentData == 0) {
			showLast();
		} else {
			currentData--;
		}
	}		

  // Method to go to first item
  public static void showFirst() {
    currentData = 0;
  }

  // Method to go to last item
  public static void showLast() {
    currentData = Data.size() - 1;
  }

  // array of string
  public static String[] getStringArray() {
    String[] currentStringArray = new String[5];

    currentStringArray[0] = Data.get(currentData).getiName();
    currentStringArray[1] = Data.get(currentData).getDept();
    currentStringArray[2] = Integer.toString(Data.get(currentData).getiSold());//convert items to string
    currentStringArray[3] = Double.toString(Data.get(currentData).getiPrice());
   
    return currentStringArray;
  }

  //assigns the Sku number to the current array object
  public static int getNextData() {
    int newCode = 0;
    for (int x = 0; x < Data.size(); x++) {
      if (Data.get(x).getiSku() > newCode) {
        newCode = Data.get(x).getiSku();
      }
    }
    return newCode + 1;
  }

  public static void addNewData(String iName, String Dept, int iSold, double iPrice) {
    String[] newData = new String[5]; //create a String array
    //load the array with the items that passed into the method    
    newData[0] = iName;
    newData[1] = Dept;
    newData[2] = Integer.toString(iSold); //convert the integer pass in to a string
    newData[3] = Double.toString(iPrice);
    
    addData(newData); //method call
    //return;

  }

  // New Method called addData
  public static void addData(String[] data) {
    // Add a new item
    int newCode = getNextData();
    Data.add(new DataFormat(newCode, data));
    showLast();
    dataSort();
    
  }

   private static void dataSort() {
    DataFormat tmp1, tmp2;
    boolean swapped = true;
    int n = Data.size();
      while (swapped){
          swapped = false; 
          for (int i = 1; i < n; i++){
          if (Data.get(i-1).getiName().compareTo(Data.get(i).getiName())>0){
            tmp1 = Data.get(i-1);
            tmp2 = Data.get(i);
            Data.remove(i);
            Data.add(i, tmp1);
            Data.remove(i-1);
            Data.add(i-1, tmp2);
            swapped = true;
          } //END IF
      } //END FOR
      n--;
    }//END WHILE
  } 
} //END MAIN
import java.text.DecimalFormat;

class DataFormat extends Data{
    
    final DecimalFormat CFORMAT = new DecimalFormat("#.##");
    
  String name;
  String department;
  int sku;
  int sold;
  float price;
  
  public DataFormat(int code, String[] dat) {
    name = dat[1];
    department = dat[0];
    sku = code;
    sold = Integer.parseInt(dat[2]);
    price = Float.parseFloat(dat[3]);
  }


  public float getOrderTotal() {
    return (price*sold);
  }
  
  public double getTax(){
    return (getOrderTotal()*0.7);
  }

  public double getOverallTotal(){
    return (getOrderTotal()+getTax());
  }

   
    @Override
  public float getiPrice() {
    return price;
  }


    @Override
  public int getiSold() {
    return sold;
  }


    @Override
  public String getiName() {
    return name;
  }


    @Override
  public int getiSku() {
    return sku;
  }


    @Override
  public String getDept() {
    return department;
  }
  
  public String getSkuStr(int sku){
     String skuStr = Integer.toString(sku);
        for (int i=(8-skuStr.length()); i>0; i--) {
        skuStr = "0"+skuStr;
        }
        return skuStr;
  }


  @Override
  public String toString() {
    
    String out = "";
    out += "SKU Number: "+getSkuStr(sku)+"\n";
    out += "Farm Name: "+department+"\n";
    out += "Item Name: "+name+"\n";
    out += "Items Sold: "+sold+"\n";
    out += "Price = $"+price+"\n";
    out += "Total = $"+ CFORMAT.format(getOrderTotal())+"\n";
    out += "Tax = $"+ CFORMAT.format(getTax())+"\n";
    out+= "Total + Tax = $"+ CFORMAT.format(getOrderTotal()+getTax())+"\n";
        
    return out;
       
  }
}

My last issue is my logo in the GUI. It seems like I have added it correctly, the display makes space for it, but it will not display the image.

here is the GUI code: (The image is in the project folder>build> classes>farmersMarket>apple.gif. Not sure if this is correct or not

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;

/**
 *
 * @author Ashley Anderson
 */
class ApplicationGUI implements ActionListener
{
        static JFrame jfrMain;
	// Declare 4 buttons
	static JButton jbnFirst, jbnNext, jbnPrevious, jbnLast;

	// Declare a panel to draw on - holds labels
	static JPanel jplNavigation;

	// Declare Advanced panel for additional buttons - holds buttons
	static JPanel jplAdvanced;

	// Declare a text area
	static JTextArea jtxtArea;

	// Declare a Label
	static JLabel jlbLogo, jlbLabel;

	public ApplicationGUI(){  //constructor
	    // Create and set up the window.
	    jfrMain = new JFrame("Ander Farms\n Sales Receipt");
	    jfrMain.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

	    // Add the text area
	    jtxtArea = new JTextArea();
	    jtxtArea.append(product.getCurrentString());
	    jfrMain.getContentPane().add(jtxtArea);

	    // Create Panel for label
	    jplNavigation = new JPanel();
            jlbLabel = new JLabel();
            jlbLabel.setText("Sales Receipt Program");

	    // Create Advanced panel for additional buttons
	    jplAdvanced = new JPanel();
            
            // Create First button
	    jbnFirst = new JButton("First");
	    jbnFirst.addActionListener(this);
	    jbnFirst.setActionCommand("first");

	    // Create Previous button
	    jbnPrevious = new JButton("Previous");
	    jbnPrevious.addActionListener(this);
	    jbnPrevious.setActionCommand("previous");


	  
	    // Create Next button
	    jbnNext = new JButton("Next");
	    jbnNext.addActionListener(this);
	    jbnNext.setActionCommand("next");
            
            // Create Last button
	    jbnLast = new JButton("Last");
	    jbnLast.addActionListener(this);
	    jbnLast.setActionCommand("last");


	   

	    // Add Title label to the panel
            jplNavigation.add(jlbLabel);


	    // Add buttons to Advanced Panel           
	    jplAdvanced.add(jbnNext);
            jplAdvanced.add(jbnFirst);
	    jplAdvanced.add(jbnPrevious);
	    jplAdvanced.add(jbnNext);
	    jplAdvanced.add(jbnLast);
	   
            jlbLogo = new JLabel (createImageIcon("apple.gif"));

	    // Add panel to frame
	    jfrMain.getContentPane().add(jplNavigation, BorderLayout.PAGE_START);
	    jfrMain.getContentPane().add(jtxtArea, BorderLayout.WEST);
	    
	    // Add Advanced panel to frame
	    jfrMain.getContentPane().add(jplAdvanced, BorderLayout.PAGE_END);

	    //Display the window.
	    jfrMain.pack();
	    jfrMain.setVisible(true);

	}

	private static void showCurrentData(){
		jtxtArea.setText(product.getCurrentString());
	}
        
        //tells the program where to find the image file
	private static ImageIcon createImageIcon(String fileName) {
		java.net.URL imgURL = ApplicationGUI.class.getResource(fileName);
		return new ImageIcon(imgURL);
	}	

	public static void enableButtons(boolean enable) {
		jbnFirst.setEnabled(enable);
		jbnNext.setEnabled(enable);
		jbnPrevious.setEnabled(enable);
		jbnLast.setEnabled(enable);
		
		return;
	}

    @SuppressWarnings("static-access")
    @Override
	public void actionPerformed(ActionEvent e) {
		// Auto-generated method stub
		if  ("first".equalsIgnoreCase(e.getActionCommand())) {
			product.showFirst();
			showCurrentData();
                }
		else if ("previous".equalsIgnoreCase(e.getActionCommand())) {
			product.showPrevious();
			showCurrentData();
		} else if ("next".equalsIgnoreCase(e.getActionCommand())) {
			product.showNext();
			showCurrentData();
		} else if ("last".equalsIgnoreCase(e.getActionCommand())) {
			product.showLast();
			showCurrentData();
		} 
	}
}
0

In line 27 for your DataFormat class, it should be multiplied with 0.07 (if it is 7%). Sorry for the mistype the decimal value.

For the logo, you created it but you didn't add it to the display???

0

I thought I did add it. I put it in the classes folder netbeans and I created the logo method in the GUI doc. it has a place for it but it's not displaying in the output.

0

Sorry, I wasn't clear. You created an object of image icon but didn't add the image to a display panel. As a result, no image is displayed. I am not sure how you would want to add it to. If you want to add it to the frame, do it as...

jfrMain.setIconImage(createImageIcon("apple.gif"));
// and then you don't need this line
// jlbLogo = new JLabel (createImageIcon("apple.gif"));
0

Ya I have that in the code. Could it be that the image is bigger than the area it created for the image? I need to turn it in but I will need it working for my final due this Sunday.

I am so appreciative of your help, I really can't thank you enough I would still be struggling through the assignment if not for you :)

0

I'm silly! I forgot to define the folder.... Thanks a ton for your help :)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.