对于 "shopping cart" 类型的应用程序,toString 的控制台输出在 Java 中的格式不正确。我错过了一些简单的东西吗?

Console output from toString isn't formatting correctly in Java for a "shopping cart" type application. Am I missing something simple?

堆栈溢出的同胞们,我呼吁您的帮助和大智慧。

问题:driver print 几乎打印了我需要的所有内容,但第一行也在一长行中打印了全部信息。我注意到格式不会保留 copy/pasting 我的控制台输入,所以我会尝试描述它。它打印出来,足够整齐,格式化 table 与我需要的信息。只是顶行也复制了信息。它似乎是完全相同的印刷品,只是没有换行

我有这个购物车应用程序。一切都已完成,现在我正在为在控制台中查看打印输出的收据处理 toString 格式。由于这包含 7 个左右不同的 classes,我不会 post 所有代码,而只是购物车、driver 和 parent class 作为我最好的猜测就是问题的根源。如果需要更多,请告诉我,我可以 post 我有什么。

Copy/paste 控制台输出


[牛肉 2 1 2,名牌 5 2 10,湿粮 2 15 30,猫薄荷 3 2 6,干粮 20 1 20,金鱼 5 真 1 Goldie 真,小 150.5 真 1 Minx 1 4,小 200.28 真 2蓬松 0 3 ]牛肉 2 1 2 名牌 5 2 10 湿粮 2 15 30 猫薄荷 3 2 6 干粮 20 1 20 金鱼 5 对 1 Goldie 对 小 150.5 真 1 Minx 1 4
小 200.28 真 2 毛茸茸 0 3

    public class ShoppingDriver{
                                    
    
        public ShoppingDriver(int max) {
            // TODO Auto-generated constructor stub
        }
    
        public static void main(String[] args) {
            PetStoreItem Beef = new PetFood("Beef", 2.00, 1);
            PetStoreItem Fluffy = new Mammal("Small", 200.275, true, 2, "Fluffy", 0, 3);
            PetStoreItem Minx = new Mammal("Small", 150.50, true, 1, "Minx", 1, 4);
            PetStoreItem Goldie = new Fish("Goldfish", 5.00, true, 1, "Goldie", true);
            PetStoreItem Catnip = new PetFood("Catnip", 3.00, 2);
            PetStoreItem Dryfood = new PetFood("Dryfood", 20.00, 1);
            PetStoreItem Wetfood = new PetFood("Wetfood", 2.00, 15);
            PetStoreItem Nametag = new PetFood("Nametag", 5.00, 2);
            
            Cart one = new Cart(8);
            one.add(Beef);
            one.add(Nametag);
            one.add(Wetfood);
            one.add(Catnip);
            one.add(Dryfood);
            one.add(Goldie);
            one.add(Minx);
            one.add(Fluffy);
            System.out.println(one);
    
        }
    
    }


public class Cart {
    private PetStoreItem[] itemsList;
    private int numItems;

    public Cart(int max) {
        itemsList = new PetStoreItem[max];
        numItems = 0;
    }

    public void add(PetStoreItem itemToAdd) {
        itemsList[numItems++] = itemToAdd;

    }

    public int size() {
        int size = itemsList.length;
        return size;
    }

    @Override
    public String toString() {
        String output = "";
        output += Arrays.toString(itemsList);
        for (int i = 0; i < numItems; i++) {
            output += itemsList[i] + "\n";
        }
        return output;
    }

}


    import java.text.DecimalFormat;
    
    public abstract class PetStoreItem {
        private String description;
        private double price;
        public static final DecimalFormat DF2 = new DecimalFormat("#.##");
        
        public PetStoreItem(String description, double price) {
            this.description = description;
            this.price = price;
        }
        
        public String getDescription() {
            return description;
        }
        public double getPrice() {
            return price;
        }
        
        public void setDescription(String desc) {
            description = desc;
        }
        
        public void setPrice(Double cost) {
            price = cost;
        }
        double itemTotalCost() {
            return price;
        }
        public String toString() {
            String output = "";
            output += description + "\t" + DF2.format(price) + "\t";
            return output;
        }
    }

我还有 4 个 child class(哺乳动物、鱼类、宠物食品、宠物)。如果这些可能有用,我会提供它们。值得注意的是,我确实从购物车 class 中删除了 super.toString,因为它在控制台中产生了类似于“CFS@13756af”的错误。如果我没记错的话,这是因为我试图在没有 toString 的情况下打印 object。无论如何,我从购物车中取出了 super 并修复了它。 我现在正处于最后一步,只是用标签格式化我的 toString 并使它看起来不错,但出于对我的爱,我一直无法弄清楚这一点。任何帮助,将不胜感激。 谢谢!

检查 Cart#toString() 中的这一行,如果删除它有帮助:

输出+=Arrays.toString(itemsList);