我正在使用toString()
方法编写一个子类,当在数组列表中读取该子类的项时,我希望将其打印出来,但是当该类型的项目将添加到arraylist。这是子类的代码:
toString()
这是超类package shop;
import java.text.DecimalFormat;
public class MultiBuyProduct extends Product{
private int minDiscountedQuantity;
private int discountPercent;
public MultiBuyProduct(String name, double price, int quantity, int minDiscountedQuantity, int discountPercent) {
super(name, price, quantity);
this.minDiscountedQuantity = minDiscountedQuantity;
this.discountPercent = discountPercent;
}
//getters and setters
public double getTotalPrice() {
if (getQuantity() >= getMinDiscountedQuantity()) {
double total = getPrice() * getQuantity();
double discountedTotal = total - ((discountPercent/100) * total);
return discountedTotal;
}
return getPrice() * getQuantity();
}
public double discount() {
double total = getPrice() * getQuantity();
double discount = (discountPercent/100) * total;
return discount;
}
@Override
public String toString() {
DecimalFormat format = new DecimalFormat("#.00");
return String.format("%s,\n%20s%5.2f)", super.toString(), format.format(getTotalPrice()), "(Multibuy Discount: GBP ", discount());
}
}
Products
我在这里是包含主要方法public String toString() {
DecimalFormat format = new DecimalFormat("#.00");
return String.format("%3d * GBP %5s %-20s= GBP %7s", quantity, format.format(price),
name, format.format(getTotalPrice()));
}
的类的一部分:
ShoppingCart
打印:
public class ShoppingCart {
private ArrayList<Product> cart;
public ShoppingCart() {
cart = new ArrayList<>();
}
@Override
public String toString() {
double total = 0;
StringBuilder sb = new StringBuilder();
for (Product p : cart) {
sb.append(p.toString()).append("\n");
total += p.getTotalPrice();
}
sb.append(String.format("%48s \n%40s%8.2f", "------------", "TOTAL GBP", total));
return sb.toString();
}
public static void main(String[] args) {
ShoppingCart newCart = new ShoppingCart();
Product apple, milk, caulk, ice, snakes;
MultiBuyProduct snakesMulti;
apple = new Product("Apples (4 pack)", 1.20, 1);
milk = new Product("Milk (1l)", 0.75, 1);
caulk = new Product("Caulk (1l)", 6.84, 1);
ice = new Product("Ice (1kg)", 4.30, 1);
snakes = new Product("Snake (5m)", 32.0, 1);
snakesMulti = new MultiBuyProduct("Snakes", 30.0, 12, 3, 20);
newCart.add(apple);
newCart.add(apple);
newCart.add(apple);
newCart.add(caulk);
newCart.add(milk);
newCart.add(milk);
newCart.add(snakes);
newCart.add(ice);
newCart.add(ice);
newCart.add(snakesMulti);
System.out.println(newCart);
}
但它应该打印:
3 * GBP 1.20 Apples (4 pack) = GBP 3.60
1 * GBP 6.84 Caulk (1l) = GBP 6.84
2 * GBP .75 Milk (1l) = GBP 1.50
1 * GBP 32.00 Snake (5m) = GBP 32.00
2 * GBP 4.30 Ice (1kg) = GBP 8.60
12 * GBP 30.00 Snakes = GBP 360.00
------------
TOTAL GBP 412.54
我是否需要 3 * GBP 1.20 Apples (4 pack) = GBP 3.60
1 * GBP 6.84 Caulk (1l) = GBP 6.84
2 * GBP .75 Milk (1l) = GBP 1.50
1 * GBP 32.00 Snake (5m) = GBP 32.00
2 * GBP 4.30 Ice (1kg) = GBP 8.60
12 * GBP 30.00 Snakes = GBP 288.00
(Multibuy Discount: GBP 72.00
------------
TOTAL GBP 340.54
本身的主要方法,还是可以使用MultiBuyProduct
中的主要方法?如果需要,我可以为上下文提供更多代码。
编辑:我已经找到问题的根源。在ShoppingCart
中,我检查该项目,如果它不在arraylist中,它将创建该项目的副本并将其添加到arraylist中:
ShoppingCart.add()
public void add(Product p) {
if (cart.size() > 0) {
for (Product i : cart) {
if (i.getName().equals(p.getName())
&& i.getPrice() == p.getPrice()) {
i.setQuantity(i.getQuantity() + p.getQuantity());
return;
}
}
cart.add(new Product(p)); //this is done because if it were just cart.add(p), it would change the product being assigned as well as the product in the arraylist
} else {
cart.add(new Product(p));
}
}
在Product (p)
中定义为
Product
这意味着public Product(Product p) {
this.name = p.name;
this.price = p.price;
this.quantity = p.quantity;
}
类型的所有项目都会丢失其MultiBuyProduct
和minDiscountedQuantity
的值。我不确定如何解决此问题,因为我无法将discountPercent
扩展到public Product(Product p)
答案 0 :(得分:0)
这是一些示例代码。请查看这是否是您的应用程序试图执行的操作(这是行为)吗?
该示例具有Animal
类。 Dog
类扩展了 Animal
。两者都覆盖了java.lang.Object
的{{1}}方法。
toString
输出:
class Animal {
private String name;
public Animal(String s) {
name = s;
}
public String toString() {
return "Animal name: " + name;
}
}
class Dog extends Animal {
private String name;
public Dog(String s) {
super(s);
name = s;
}
public String toString() {
return "Dog Name: " + name + ", " + super.toString();
}
}
Public class TestingInheritance {
public static void main(String [] args) {
Animal d0 = new Animal("just animal");
Animal d1 = new Dog("mutt");
Dog d2 = new Dog("pup");
System.out.println(d0);
System.out.println(d1);
System.out.println(d2);
System.out.println("");
List<Animal> anims = Arrays.asList(d0, d1, d2);
for (Animal a : anims) {
System.out.println(a.toString());
}
}
}