I am taking java class and I had to do an assignment which reads as follows:
Create a class named Purchase. Each Purchase contains an invoice number, amount of sale, and amount of sales tax. Include set methods for the invoice number and sale amount. Within the set() method for the sale amount, calculate the sales tax as 7.5% (using a static filed in the Purchase class) of the sale amount. Also include a display method that displays a purchase's details in a well formatted output display. Save the file as Purchase.java. Compile and run your programs until it works and the output looks nice. Add the necessary documentation as described in Course Documents, and then attach your .java files to this assignment
My solution was as follows:
import java.util.*;
public class Purchase {
//Properties of Purchase class - static
private static int invoiceNumber;
private static double salesAmount;
private static double salesTax;
//setter for invoiceNumber
public static void setInvoiceNum(int invNo){
invoiceNumber = invNo;
}
//setter for salesAmount
public static void setSalesAmount(double salesAmnt){
salesAmount = salesAmnt;
salesTax = 0.075*salesAmnt;
}
//public static method to display purchase info
public static void displaySalesInfo(){
System.out.println("Invoice Number: " + Purchase.invoiceNumber);
System.out.println("Sales Amount: " + Purchase.salesAmount);
System.out.println("Sales Tax: " + Purchase.salesTax);
}
//main method that makes use of the static properties and display method
public static void main (String[] args) {
//ask user to input purchase details
Scanner scan = new Scanner(System.in);
System.out.println("Enter your invoice Number:" );
int inv = scan.nextInt();
System.out.println("Enter your Sales Amount:");
double sales = scan.nextDouble();
// send the user submitted purchase details to the setter methods and call display method
setInvoiceNum(inv);
setSalesAmount(sales);
displaySalesInfo();
}
}
And here is my teacher's comments: "For this assignment you were to provide a sales tax of 7.5% using a static field in the Purchase class. In the code you submitted you used a numeric literal, which most will consider a very bad programming practice. You did set the static variable salesTax, however the value you give it is based on the instance method parameter, which is a logic error. Only the tax rate was to be static, all other fields should not be, otherwise each purchase would be the same regardless of what was purchased. The assignment code submitted indicated you do not understand the concept of static fields."
I just don't understand?? He says I am not understanding static fields.. Am I that ignorant? This is simply embarrassing..Please shed some light.