同一类中如何将两个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
clothesandcostlists inside each add method. That means every time you callcartAddiorcartAddp, 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">orArrayList<"Integer">is incorrect. The type parameter should be the class name without quotes, likeArrayList<String>. - Broken Total Calculation: Your
cartAddpmethod 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 persistentcostlist instead. - Unconnected Receipt Method: The
PrintReciptmethod (note: typo, should bePrintReceipt) 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
- Member Variables:
clothesandcostare now declared at the class level, so every time you callcartAddiorcartAddp, you're adding to the same persistent lists. - Fixed Generics: Removed the quotes around the type parameters to follow valid Java syntax.
- Accurate Total Calculation: The
cartAddpmethod now calculates the total from the fullcostlist, so it returns the running total of all items in the cart. - Receipt Printing: The
PrintReceiptmethod 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
相关产品推荐
相关产品推荐

