Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Noobie issues?

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Noobie issues?

    I'm having a few issues with my program. First off, for some reason when I do setBasePrice, it doesn't return as a -1 nor 1, depending on what I do. As well, for calculateTotalPrice, it doesn't return as -1 when under 0 and over 1 and my program overall gives the wrong value (for instance for a Refrigerated item with the price of $5.60 and taxRate of .06, it's supposed to be $7.562 and it's not), but I think if I figure out the return issue, it will help with that problem.

    Could someone please help me figure out my errors?

    Thank you.

    public class GroceryItem {
     
          private String groceryName, groceryCategory;
          public final double minPrice = 0, maxPrice = 500, taxHigh = 1;
          private static final String GENERAL = "General", PRODUCE = "Produce",
          REFRIGERATED = "Refrigerated", FROZEN = "Frozen";
          private double basePrice = 0, localRate = 0, totalPrice = 0;
     
     
          public GroceryItem(String name, String category) {
             groceryName = name;
             groceryCategory = category;
             category = GENERAL;
             basePrice = 0;
          }
     
     
          public String setName(String itemName) {
             if (itemName == null || itemName.trim().equals("")) {
                itemName = "No Name";
                groceryName = itemName;
                return itemName;
             }
             else {
                groceryName = itemName;
                return itemName;
             }
          }
     
          public String getName() {
             return groceryName;
          }
     
     
          public int setBasePrice(double base) {
             basePrice = base;
             if (basePrice > minPrice || base <= maxPrice) {
                return 1;
             }
             else if (basePrice > maxPrice) {
                return 0;
             }
             else {
                return -1;
             }
          }
     
     
          public double getBasePrice() {
             return basePrice;
          }
     
     
          public double calculateTotalPrice(double taxRate) {
             localRate = taxRate;
             if (taxRate < minPrice || taxRate > taxHigh) {
                return -1;
             }
             else if (groceryCategory == GENERAL) {
                totalPrice = (basePrice + taxRate);
             }
             else if (groceryCategory == PRODUCE) {
                totalPrice = (basePrice + (basePrice * 0.05));
             }
             else if (groceryCategory == REFRIGERATED) {
                totalPrice = ((basePrice + 1.50) + taxRate);
             }
             else if (groceryCategory == FROZEN) {
                totalPrice = ((basePrice + 3.00) + taxRate);
             }
     
             return totalPrice;
          }
     
          public boolean setCategory(String cat) {
             groceryCategory = cat;
             if (cat == GENERAL) {
                return true;
             }
             else if (cat == PRODUCE) {
                return true;
             }
             else if (cat == REFRIGERATED) {
                return true;
             }
             else if (cat == FROZEN) {
                return true;
             }
             else {
                return false;
             }
          }
     
          public String getCategory() {
             return groceryCategory;
          }
     
          public String toString() {
             String output = "Grocery Item: " 
                + groceryName + "\n"
                + "Category: " 
                + groceryCategory + "\n"
                + "Base Price of Grocery Item: "
                + basePrice + "\n";
     
             return output;
          }
       }


  2. #2
    Member
    Join Date
    Aug 2011
    Posts
    55
    Thanks
    5
    Thanked 3 Times in 3 Posts

    Default Re: Noobie issues?

    Hello,
    Is you main method in a different class? Also you have parameters for your methods that are not declared or initialized. Again, that may be because its in a different class.You should include that code also.

  3. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Noobie issues?

    Oh, yes! I'm sorry, here's the main method, but I am also having issues with that, but I am trying to figure them out before throwing in the towel.
       import java.util.Scanner;
       import java.util.ArrayList;
       import java.io.IOException;
       import java.io.File;
     
       public class GroceryList {
     
          public static void main(String[] args) throws IOException
     
     
          {
     
             String file = "", line = "", grocery = "", category = "";
             double tax;
             ArrayList<GroceryItem> groceries = new ArrayList<GroceryItem>();
             Scanner fileScan = new Scanner(new File(groceries.txt));
     
             GroceryItem groceryInfo = new GroceryItem(grocery, category);
     
             System.out.println("Insert file name: ");
             file = fileScan.nextLine();
     
             while(fileScan.hasNext())
     
     
             {
                grocery = fileScan.nextLine();
     
                Scanner lineScan = new Scanner(line);
                while (lineScan.hasNext()) {
                   groceries.add(new GroceryItem(grocery, category));
                }
             }
             fileScan.close();
     
             System.out.println("\n\nGrocery List");
             System.out.println("------------");
     
             if (category == "General") {
                System.out.println("General:\n"
                   + groceries);
             }
             else if (category == "Produce") {
                System.out.println("Produce: \n"
                   + groceries);
             }
             else if (category == "Refrigerated") {
                System.out.println("Refrigerated:\n"
                   + groceries);
             }
             else if (category == "Frozen") {
                System.out.println("Frozen:\n"
                   + groceries);
             }
     
             int index = 0;
             while (index < groceries.size()) {
                System.out.print(groceries.get(index) + " ");
                index++;
     
             }
          }
     
       }

  4. #4
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Noobie issues?

    Do you think, your existing code is working fine without errors?
    First, remove all the syntax errors from your code.
    Later, tell us, where exactly the problem, you want to discuss.
    Sorry to say but no one will even try to read the whole code and try to understand what you want to do.
    State your problem clearly so that we could help you.
    Thanks

Similar Threads

  1. [SOLVED] Replace String Method? Noobie :/
    By Usoda in forum What's Wrong With My Code?
    Replies: 7
    Last Post: September 18th, 2011, 10:58 AM
  2. Event Issues
    By fractalorbit in forum AWT / Java Swing
    Replies: 3
    Last Post: September 2nd, 2011, 07:49 PM
  3. Alignment issues
    By fride360 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: March 10th, 2011, 02:58 PM
  4. ResultSet issues
    By _lithium_ in forum JDBC & Databases
    Replies: 3
    Last Post: March 4th, 2011, 03:20 PM
  5. Noobie to Java questions!! :D
    By chansey123 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 17th, 2010, 11:53 AM