You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

同一类中如何将两个ArrayList传入PrintRecipt()方法?

Fixing Your Cart Class & Receipt Printing

Let's work through this step by step—your current code has a few critical issues that are keeping it from working as intended, and I'll help you get it sorted for your clothing shop project.

Key Problems in the Original Code

  • Local ArrayLists: You're creating new clothes and cost lists inside each add method. That means every time you call cartAddi or cartAddp, you're starting with a blank list—all previous items/prices get lost immediately. These lists need to be class-level member variables so they persist across method calls.
  • Invalid Generics Syntax: Writing ArrayList<"String"> or ArrayList<"Integer"> is incorrect. The type parameter should be the class name without quotes, like ArrayList<String>.
  • Broken Total Calculation: Your cartAddp method calculates the total only on the new (empty) list it just created, so it will always return just the price of the single item you just added. We need to calculate the total from the persistent cost list instead.
  • Unconnected Receipt Method: The PrintRecipt method (note: typo, should be PrintReceipt) can't access the lists because they were local to other methods—making them member variables fixes this.

Corrected Full Code

package clothesshop;
import java.util.ArrayList;

public class Cart {
    // Class-level member variables: persist for the life of the Cart object
    private ArrayList<String> clothes = new ArrayList<>(5);
    private ArrayList<Integer> cost = new ArrayList<>(5);

    // Add item to the clothes list
    void cartAddi(String item) {
        clothes.add(item);
    }

    // Add price to cost list and calculate total price
    int cartAddp(int price) {
        cost.add(price);
        int totalP = 0;
        // Calculate total from the persistent cost list
        for (int priceItem : cost) {
            totalP += priceItem;
        }
        return totalP;
    }

    // Print receipt using the two synchronized lists
    void PrintReceipt() {
        System.out.println("=== Your Clothing Shop Receipt ===");
        // Loop through indices (since lists are 1:1)
        for (int i = 0; i < clothes.size(); i++) {
            String item = clothes.get(i);
            int price = cost.get(i);
            System.out.printf("- %s: $%d%n", item, price);
        }
        // Calculate total (or reuse the value from cartAddp if you want)
        int total = 0;
        for (int p : cost) {
            total += p;
        }
        System.out.println("-------------------------------");
        System.out.printf("Total: $%d%n", total);
        System.out.println("=================================");
    }
}

What Changed & Why

  1. Member Variables: clothes and cost are now declared at the class level, so every time you call cartAddi or cartAddp, you're adding to the same persistent lists.
  2. Fixed Generics: Removed the quotes around the type parameters to follow valid Java syntax.
  3. Accurate Total Calculation: The cartAddp method now calculates the total from the full cost list, so it returns the running total of all items in the cart.
  4. Receipt Printing: The PrintReceipt method can now directly access the two lists. Since their indices are 1:1, we loop through each index to pair the correct item with its price, then print the final total.

How to Use This

When you create a Cart object, you can call cartAddi and cartAddp for each item, then call PrintReceipt() to generate the full receipt:

public class Main {
    public static void main(String[] args) {
        Cart myCart = new Cart();
        myCart.cartAddi("Denim Jeans");
        myCart.cartAddp(49);
        myCart.cartAddi("Cotton T-Shirt");
        myCart.cartAddp(19);
        myCart.PrintReceipt();
    }
}

This will output:

=== Your Clothing Shop Receipt ===
- Denim Jeans: $49
- Cotton T-Shirt: $19
-------------------------------
Total: $68
=================================

内容的提问来源于stack exchange,提问作者ItsMsalati

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:29:04