gpt4 book ai didi

java - 我没有从我的 java 应用程序中得到预期的输出

转载 作者:太空宇宙 更新时间:2023-11-04 11:01:44 24 4
gpt4 key购买 nike

我有 5 个类,其中 4 个正在按预期工作。我是java的绝对初学者,我希望有人能为我提供帮助。这是一个用于下订单和捆绑订单的小应用程序。我的问题是类bundleItem。我希望获得所有带有折扣的商品的值(value),但我总是得到负的返回值。

这是我的类(class)

类订单

package testateins;

import java.util.ArrayList;

public class Order {
private ArrayList<Item> allOrders;
public Order(){
this.allOrders = new ArrayList<Item>();
}


public void addItem(Item item) {
this.allOrders.add(item);
}

public void removeItem(Item item) {
this.allOrders.remove(item);
}

public double getTotalprice(){
double totalprice = 0;
for(Item element : allOrders) {
totalprice += element.getPrice();
}
return totalprice;

}

public void printItems() {
for(Item element : allOrders) {
element.print();
}
}

}

类项目

package testateins;

public abstract class Item{
String description;
double price;
public Item(String description) {
this.description = description;

}

public abstract double getPrice();

public void setItem() {

}

public void print() {
System.out.println("Beschreibung " + description);
}


}

产品项目类

package testateins;

public class ProductItem extends Item{
public double pricePerUnit;
public double amount;
public double price;

public ProductItem(String description, int pricePerUnit, int amount) {
super(description);
this.pricePerUnit = pricePerUnit;
this.amount = amount;
this.price = amount * pricePerUnit;

}

public void print() {
System.out.println("Beschreibung " + description);
System.out.println("menge " + amount);
System.out.println("Preis pro Einheit " + pricePerUnit);
System.out.println("Gesammtpreis " + price);
}

public double getPrice() {
return this.price;
}
}

服务项目类

package testateins;

public class ServiceItem extends Item{
public double price;
public ServiceItem(String description, Integer packageprice) {
super(description);
this.price = packageprice;
}

public void print() {
System.out.println("Beschreibung " + description);
System.out.println("Servicepreis " + price);
}

public double getPrice() {
return this.price;
}

}

类 BundleItem(我的问题类)

package testateins;

import java.util.ArrayList;

public class BundleItem extends Item {

private double rabatt;
private ArrayList<Item> allOrders;

public BundleItem(String description, double rabatt) {
super(description);
this.allOrders = new ArrayList<Item>();
setrabatt(rabatt);
}
public void setrabatt(double rabatt) {
this.rabatt = rabatt;
}
public double getrabatt() {
return this.rabatt;
}
public void addItem(Item item) {
if(item instanceof ProductItem || item instanceof ServiceItem) {
this.allOrders.add(item);
}
}
@Override
public double getPrice() {
double result = 0;
double sum = 0;
double discount = (rabatt/100);
for(Item element : allOrders) {
result+=element.getPrice();
}
return sum - (result * discount);
}

public void print() {
super.print();
System.out.println(rabatt);
for(Item element: allOrders) {
element.print();

}
}
}

我的测试类(class)返回负折扣,重要的部分是在“第2部分”之后

package testateins;


public class Testing {

public static void main(String[] args) {



ServiceItem wischen = new ServiceItem("wischen", 20);
System.out.println(wischen.description);
System.out.println(wischen.price);
System.out.println("___________________________");

ProductItem kuchen = new ProductItem("kuchen", 3, 15);
System.out.println(kuchen.description);
System.out.println(kuchen.amount);
System.out.println(kuchen.pricePerUnit);
System.out.println(kuchen.price);
System.out.println("___________________________");

Order simpleOrder = new Order();
simpleOrder.addItem(wischen);
simpleOrder.addItem(kuchen);
simpleOrder.addItem(kuchen);

simpleOrder.printItems();
System.out.println("___________________________");
System.out.println("totalpreis:");
System.out.println(simpleOrder.getTotalprice());


//part 2
System.out.println("--------uebung 2------------.-");
Order BundleOrder = new Order();

ProductItem heft = new ProductItem("Heft", 1, 10);
ProductItem stift = new ProductItem("Stift", 1, 10);

BundleItem Discounteins = new BundleItem("Discounteins(50)", 50);
BundleItem Discountzwei = new BundleItem("Discountzwei(50)", 50);
BundleItem Discountdrei = new BundleItem("Discounteins(10)", 10);

Discounteins.addItem(heft);
Discounteins.addItem(stift);

Discountzwei.addItem(Discounteins);
Discountzwei.addItem(wischen);
Discountzwei.addItem(wischen);

Discountdrei.addItem(wischen);
Discountdrei.addItem(wischen);

BundleOrder.addItem(Discountzwei);
BundleOrder.addItem(Discounteins);

BundleOrder.printItems();
System.out.println(BundleOrder.getTotalprice());

System.out.println("---------------------------.-");

Discounteins.setrabatt(50);

System.out.println(Discounteins.getrabatt());

BundleOrder.printItems();
System.out.println("---------------------------.---");
System.out.println(BundleOrder.getTotalprice());

System.out.println("---------------------------.----");
}

}

最佳答案

您的总和始终为零,这就是为什么您总是得到负值。

我想你打算做这样的事情:

@Override
public double getPrice() {
double sum = 0;
double discount = (rabatt / 100);

for(Item element : allOrders) {
sum += element.getPrice();
}

return sum - (sum * discount);
}

关于java - 我没有从我的 java 应用程序中得到预期的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46824712/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com