I am undertaking a course in software development, at the moment I am working through Java.
The program I am working on (which don't worry, I am allowed to get external help with) has given me a problem, well more so the problem is I don't understand how they have worded the task at hand. Here's where I am at.
What I am required to do is create a constructor with two variables, then pass one of the parameters to a method. The class and constructor are called Order(), the method is called testQuantity. This is what I do not understand. The first parameter productName has it's value assigned to an instance variable productName by the code this.productName = productName; but for the second parameter quantity it doesn't ask me to assign the value to the instance variable quantity but rather "The quantity parameter is to be passed to the testQuantity method."
Ok, here's the code I have so far, it's far from complete and there are four other classes I haven't began yet. Also feel free to point out any issues you spot along the way. Thanks in advance for any help.
package JaveGUIPrototype;
import java.util.Arrays;
/**
*
* @author User
*/
public class Order {
private String productName;
private double price;
private int discount;
private int quantity;
private double total;
private String message;
private boolean isDiscounted;
private boolean isValidOrder;
private static int orderNum = 0;
public Order() {
// sets validity to false, sets message, increments orderNum by 1
isValidOrder = false;
message = "**ERROR**: Order number cannot be totalled as no details have been supplied";
orderNum = orderNum + 1;
}
public Order(String productName, int quantity) {
// sets productName variable to productName parameter value
this.productName = productName;
this.quantity = quantity;
}
public void testQuantity() {
// tests the value of the quantity variable
// quantity is 0 or less, order isn't valid, message explains why
if(quantity <=0){
isValidOrder = false;
message = "**ERROR**: Invalid quantity. Quantity cannot be 0 or less";
}
// quantity is greater than 1000, order isn't valid, message explains why
else if(quantity >1000) {
isValidOrder = false;
message = "**ERROR**: Invalid quantity. Quantity cannot be greater than 1000";
}
// quantity is valid, quantity instance variable assigned value of parameter variable
else {
this.quantity = quantity;
}
}
public void testDiscount(int discount) {
// tests the value of the discount variable
// discount is 0 or less, order isn't valid, message explains why
if(discount <=0) {
isValidOrder = false;
message = "**ERROR**: The discount rate cannot be lower than or equal to 0";
}
// discount is greater than 50, order isn't valid, message explains why
else if(discount >50) {
isValidOrder = false;
message = "**ERROR**: The discount rate cannot be greater than 50";
}
// discount is valid, discount instance variable assigned value of parameter variable
// isDiscounted variable set to true
else {
this.discount = discount;
isDiscounted = true;
}
}
String[] products = {"Compass",
"Eraser",
"Pen",
"Pencil",
"Pencil Case",
"Ruler",
"Scissors"};
double[] prices = {4.5, 0.5, 0.3, 0.6, 10, 0.3, 1.2, 2.5};
int indexPos;
double indexVal;
public void getPrice(String productName) {
// searches products array by value of productName variable
indexPos = Arrays.binarySearch(products, productName);
// tests for validity of result
if (indexPos >-1) {
// assigns value of prices array by index position
indexVal = prices[indexPos];
// assigns prices array value to price variable
price = indexVal;
}
else {
// product name not valid, not a valid order, message explains why
isValidOrder = false;
message = "**ERROR**: Invalid product name";
}
}
public void calculate() {
// tests for validity
if(isValidOrder == true) {
// tests for discount
if(isDiscounted == false) {
// equats total without discount
total = quantity * price;
}
else {
// equats total with discount
total = quantity * price - quantity * price * (discount/100);
}
}
}
public String getOrderDetails() {
//tests for validity
if(isValidOrder == true) {
//tests for discount
if(isDiscounted == false) {
// sets non discounted message details
message = "Order number: " + orderNum +
"Product name: " + productName +
"Product price: $" + price +
"Order quantity: " + quantity +
"Total price: $" + total;
}
else {
// sets discounted message details
message = "Order number: " + orderNum +
"Product name: " + productName +
"Product price: $" + price +
"Order quantity: " + quantity +
"Discount: " + discount + "%" +
"Total price: $" + total;
}
}
return message;
}