0

I just started learning Java and I'm trying to write a program based on an assignment sheet (gave this sheet at bottom of the post). However, I really don't quite understand how to use methods all that well. I've written my methods in the "Customer.java" class, and I'm trying to use them in my "TestCustomer.java" class. However, since I really don't know how to do this, it has turned out horribly. I've searched for information on this, but I just seem to keep making myself more confused. Is there any chance you guys could show me the correct way to use these methods, or at least point me in the right direction? Thank you a ton for any help you can provide.

Customer class

import java.util.Scanner;

import javax.swing.JOptionPane;


public class Customer {
public static double taxRate = 0.00;
public static double saleRate = 0.00;
String customerName;
double listSaleAmount;
double saleDiscount = 0;
double netSaleAmount;
double taxAmount;
double saleTotal;
boolean taxable;

public Customer (String CustomerName, boolean taxable) {

}

public double calculateTax (double listSaleAmount) {
    saleDiscount = listSaleAmount*saleRate;
    netSaleAmount = listSaleAmount-saleDiscount;
    if (taxable == true) {
    taxAmount = netSaleAmount*taxRate;
    }
    else {
    taxAmount = 0.00;
    }
    saleTotal = listSaleAmount + taxAmount;


    return saleTotal;

}

public String printRecord; {
    System.out.println("Customer is " + customerName);
    System.out.println("Sale amount is $" + listSaleAmount);
    System.out.println("Discount amount is $" + saleDiscount);
    System.out.println("Net Sale Amount is $" + netSaleAmount);
    System.out.println("Tax amount is $" + taxAmount);
    System.out.println("Total Sale Amount is $" + saleTotal);
}

public static double changeTaxAmount (double taxRate) {
    Scanner input = new Scanner(System.in);
    double userTaxAmount = Double.parseDouble(JOptionPane.showInputDialog("What is the Tax Rate? (8.25 & 8.50 for testing)"));
    taxRate = userTaxAmount;
    return taxRate;

}

public static double changeSaleRate (double saleRate) {
    Scanner input = new Scanner(System.in);
    double userSaleAmount = Double.parseDouble(JOptionPane.showInputDialog("What is the Sale Discount Rate? (0.00 & 7.50 for testing)"));
    saleRate= userSaleAmount;
    return saleRate;
}

public static String printTaxRate; {
    System.out.println("Tax Rate is" + taxRate + "%.");
}

public static String printSaleRate; {
    System.out.println("The Sale Rate is" + saleRate + ".");
}
}

TestCustomer class

import java.math.BigDecimal;
public class TestCustomer {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

Customer customer1 = new Customer("Annie Smith", true);
Customer customer2 = new Customer("Bob Wilson", false);
Double totalOfAllSales = 0.00;


//I have no clue how to actually use the methods I created in the Customer class!
//These are my best guesses, which are obviously wrong
//Any help here would be greatly appreciated!
Customer.changeTaxAmount(taxRate);

Customer.printTaxRate;

Customer.changeSaleRate(saleRate);

Customer.printSaleRate;

customer1.listSaleAmount = 65.00;
customer2.listSaleAmount = 52.00;

totalOfAllSales += customer1.calculateTax;
totalOfAllSales += customer2.calculateTax;

customer1.printRecord;
customer2.printRecord;

Customer.changeTaxAmount(taxRate);
Customer.printTaxRate;
Customer.changeSaleRate(saleRate);
Customer.printSaleRate;

customer1.listSaleAmount = 84.00;
customer2.listSaleAmount = 105.00;
totalOfAllSales += customer1.calculateTax;
totalOfAllSales += customer2.calculateTax;
customer1.printRecord;
customer2.printRecord;

System.out.println("The total of all sales is $" + totalOfAllSales);
    }

}

Assignment sheet (Not worrying about printing to a file right now, just want the main mechanics to work) enter image description here enter image description here

2
  • Please consolidate your post. We only need to see the parts of your code that are relevant to the problem at hand, not the entire program. Commented Oct 30, 2013 at 0:24
  • I'm getting all kinds of errors trying to call the methods. And Silvio, I wasn't really sure what parts were relevant and what wasn't :/ Commented Oct 30, 2013 at 0:24

1 Answer 1

2

You seem to be confused about the syntax for calling a method. The syntax is as follows:

object.method(arguments)

If there are no arguments it looks like this:

object.method()

Also, you need to use accessor and mutator methods instead of directly setting instance variables like you do here:

customer1.listSaleAmount = 65.00;

You should implement methods like these:

public void setListSaleAmount(double lsa) {
    listSaleAmout = lsa;
}

public double getListSaleAmount() {
    return listSaleAmount;
}

and make listSaleAmount private.

Problem #2: The syntax for defining the methods. You are using this code to define a method:

public static String printTaxRate; {
    System.out.println("Tax Rate is" + taxRate + "%.");
}

You should be using this code:

public static String printTaxRate() {
    System.out.println("Tax Rate is" + taxRate + "%.");
}

The problem is the weirdly placed semicolon inside the method header.

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you for taking a look at this. Is there any chance you could give me an example by calling one of my methods? Both of these still give errors and don't work correctly. customer1.printTaxRate(); Customer.printTaxRate();
There's another problem. I'll update the answer to include that.
@user2774647 take a look at your Customer class. the method printTaxRate is incorrectly written as printTaxRate;. Change it to printTaxRate()
Thank you guys! Not sure why I wrote them like that. Like I said, I'm very new to this. I've got all of the print methods functioning now. I'm now trying to get the others working and I will report back. Thanks again.
Finally got rid of all the errors, but now it prints 0 or null for everything. Only thing it actually prints right are the sale amounts :( Now to just figure this part out... lol. Regardless, thanks again for help with the methods.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.