我有 3 个课程Order
:InternalOrder
和OrderTester
。我已经搜索了一段时间,并且我一直在寻找的东西我只是无法尝试将这些示例更改为我需要的内容。
所以我在InternalOrder
和OrderTester
课程方面遇到了麻烦。到目前为止,我的代码都是...
public class InternalOrder extends Order {
public static final int DISCOUNT = 40/100;
public InternalOrder(String productName, int quantity) {
super(productName, quantity, DISCOUNT);
}
public void printInternalReport() {
System.out.println("Printing internal report...");
}
}
和
public class OrderTester {
public static void main(String[] args) {
testCase1();
testCase2();
testCase3();
testCase4();
testCase5();
testCase6();
testCase7();
testCase8();
testCase9();
testCase10();
}
public static void testCase1() {
Order ord = new Order();
System.out.println("Test Number : " + Order.orderNum + " Type of Order : Normal, " + "Product Name : " + "Order Quantity : " + "Discount : ");
}
public static void testCase2() {
Order ord = new Order();
System.out.println("Test Number : " + Order.orderNum + " Type of Order : Normal, " + "Product Name : " + "Order Quantity : " + "Discount : ");
}
好吧,它要测试 10,但目前一切都一样。
这是我需要的东西:
该InternalOrder
课程将包含 Stellar Stationary 员工的逻辑,他们在内部订购固定库存作为其工作要求的一部分。
内部订单自动获得 40% 的折扣。
- 类是对
InternalOrder
类的扩展Order
。 - 是包含一个
final static
名为DISCOUNT
. 此字段用作员工收到的折扣率的常数,应设置为 40%。
现在,我有这两个部分,但我不完全确定下一部分。
- 该类将包含一个构造函数。构造函数是接收两个参数productName和quantity。构造函数是将这些参数传递给它的超类(Order)以及作为
DISCOUNT
第三个参数的常量。
我是否需要在超类中添加任何内容,因为这让我很困惑。有了OrderTester
,我有一个很难导入的表,所以我将只生成几行。
该类OrderTester
用于启动程序并测试Order
和
InternalOrder
类以确保其正常工作。
有十个测试要运行,每个测试都应该在它自己的静态方法中,并且应该testCase1()
通过testCase10()
. 每个测试应按照下表进行测试:
Test Number Type of Order Product Name Order Quantity Discount
1 "Normal" "N/A" "N/A" "N/A"
有了这个测试。当我的 Quantity 和 Discount 是整数时,我不确定如何产生“N/A”。
如果您需要我的其他代码,我将在下面发布。
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;
public static int orderNum = 0;
public Order() {
isValidOrder = false;
message = "**ERROR** Order number cannot be totalled as no details have been supplied.";
orderNum++;
}
public Order(String productName, int quantity){
this.productName = productName;
this.quantity = quantity;
getPrice(this.productName);
if(isValidOrder != false){
calculate();
}
orderNum++;
}
public Order(String productName, int quantity, int discount){
this.productName = productName;
testQuantity(quantity);
getPrice(productName);
if(isValidOrder != false){
calculate();
}
orderNum++;
}
private String getOrderDetails(){
message = message;
if(isValidOrder == true && isDiscounted == false){
message = "Order Number: " + quantity + "\n" + "Product Name; " + productName + "\n" + "Product Price: $" + price + "\n" + "Order Quantity: " + quantity + "\n" + "Total Price: $" + total;
} else if(isValidOrder == true && isDiscounted == true){
message = "Order Number: " + quantity + "\n" + "Product Name; " + productName + "\n" + "Product Price: $" + price + "\n" + "Order Quantity: " + quantity + "\n" + "Total Price: $" + total;
} else {
return message;
}
return message;
}
private void calculate(){
if(this.isDiscounted == false){
total = quantity * price;
} else {
total = quantity * price - quantity * price * (discount / 100 );
}
}
private void getPrice(String productName){
switch(productName){
case "Pencil":
this.price = 0.6;
break;
case "Pen":
this.price = 0.3;
break;
case "Ruler":
this.price = 1.2;
break;
case "Pencil Sharpener":
this.price = 0.3;
break;
case "Compass":
this.price = 4.5;
break;
case "Erasor":
this.price = 4.5;
break;
case "Scissors":
this.price = 2.5;
break;
case "Pencil Case":
this.price = 10.0;
break;
default:
this.price = 0.0;
this.isValidOrder = false;
this.message = "**ERROR**: Invalid product name";
break;
}
}
private void testDiscount(int discount) {
if (discount <=0) {
message = "**ERROR**: The discount rate cannot be lower than or equal to 0.";
}
else if (discount >50) {
message = "**ERROR**: The discount rate cannot be higher than 50.";
} else {
this.discount = discount;
this.isDiscounted = true;
}
}
private void testQuantity(int quantity){
if(quantity <=0) {
isValidOrder = false;
message = ("**ERROR**: Invalid quantity. Quantity cannot be 0 or less.");
message = message + "messagehere";
message += "messagehere";
}
else if (quantity >1000) {
isValidOrder = false;
message = ("**ERROR**: Invalid quantity. Quantity cannot be greater than 1000.");
} else {
isValidOrder = true;
this.quantity = quantity;
}
}
}