자바를 사용하여 소프트웨어 개발 과정을 진행 중입니다.메소드 JAX에 컨스트럭터 매개 변수 전달
내가 (외부의 도움을 얻을 수 있도록 허용하고 있음을 염려하지 않는) 프로그램이 나에게 문제를 부여했다. 문제는 내가 어떻게 그 일에 말했는지 이해할 수 없다는 것이다. 손. 여기에 내가있는 곳이있다.
두 변수가있는 생성자를 만든 다음 매개 변수 중 하나를 메서드에 전달해야합니다. 클래스와 생성자는 Order()
이고, 메서드는 testQuantity
입니다. 이것은 내가 이해하지 못하는 것입니다. 첫 번째 매개 변수 productName
은 인스턴스 변수 productName
에 코드 this.productName = productName;
에 의해 할당되지만 두 번째 매개 변수 quantity
에 대해서는 인스턴스 변수 quantity
에 값을 할당하라는 메시지가 표시되지 않고 "매개 변수가 전달됩니다 testQuantity 메소드. "
자, 내가 지금까지 가지고있는 코드는 아직 완성되지 않았으며 아직 시작하지 않은 네 가지 클래스가 있습니다. 길을 따라 가면서 어떤 문제를 지적해도 좋습니다. 모든 도움을 미리 감사드립니다.
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;
}
가 나는 방법에 전달했던 방법이었다 인스턴스 변수 사고에 값을 할당했다, 나는 그것을 제거하는 것을 잊었다 내가 코드를 게시하기 전에 그리고 고마워,이게 내가 뭉친 후 정확히 봤어, 나는 온라인에서 예제를 발견했다. (나는 생각한다.) 그러나 다른 변수들과 방법들 때문에 읽는 것이 까다로웠다. 다시 한번 감사드립니다. – mrkd1991