Java库存校验与账单计算问题求助:数组匹配与方法实现
Fixing the
finalBillAmount Method for Inventory Check & Total Calculation Hey Ramon, let's break down how to implement your finalBillAmount method properly. The core tasks here are finding the selected item's index (to link it to its inventory and price), validating stock levels, and calculating/printing the total bill (or terminating if stock is insufficient).
Step 1: Clarify What the Method Needs to Do
Your finalBillAmount method has three key jobs:
- Locate the position of
itemAskedin theitemsarray (sinceitems,prices, andinventoryare 1:1 mapped) - Check if the requested
itemAmountexceeds available inventory- If yes: Print the stock shortage message and end the program
- If no: Calculate the total cost and print the bill confirmation
Step 2: Complete the Method Implementation
Here's the full working code for finalBillAmount, with comments explaining each step:
private static double finalBillAmount(String[] items, int[] inventory, double[] prices, String itemAsked, int itemAmount) { // 1. Find the index of the selected item in the items array int itemIndex = -1; for (int i = 0; i < items.length; i++) { if (items[i].equals(itemAsked)) { itemIndex = i; break; } } // Fallback for edge case: item not found (your askUserForItem should prevent this) if (itemIndex == -1) { System.out.println("Error: Selected item not found."); System.exit(0); } // 2. Check if requested amount is within inventory limits int availableStock = inventory[itemIndex]; if (itemAmount > availableStock) { // Print shortage message and terminate program System.out.printf("Sorry we only have Inventory : %d of Item: %s%n", availableStock, itemAsked); System.exit(0); } // 3. Calculate and print total bill if stock is sufficient double totalBill = itemAmount * prices[itemIndex]; System.out.printf("Your total bill is: $ %.2f%n", totalBill); // Return the total (changed return type to double for monetary accuracy) return totalBill; }
Key Adjustments & Notes
- Return Type Update: I changed the method's return type from
inttodoublebecause the total bill is a currency value (e.g., 2 * 1.99 = 3.98, which isn't an integer). Update your main code variable declaration to match:double finalAmount = finalBillAmount(items, inventory, prices, itemAsked, itemAmount); - Added
pricesParameter: Your original method call didn't includeprices, but we need it to calculate the total cost. Make sure to pass this array when calling the method. - Inventory Check Logic: We use the item's index to pull its corresponding inventory value. If the requested amount is too high,
System.exit(0)immediately ends the program as required. - Currency Formatting:
%.2fensures the total is displayed with two decimal places, which is standard for money.
Updated Main Code Flow
Here's how your main code should look after integrating the fixed method:
String[] items = {"Hammer", "Wrench", "Pliers", "Saw", "ScrewDriver" }; double[] prices = {1.99, 2.99, 3.99, 4.99, 5.99}; int[] inventory = {100, 200, 300, 400, 500}; String itemAsked = askUserForItem(items); String itemInfo = showItemInfo(items, prices, inventory, itemAsked); int itemAmount = purchaseAmount(items, inventory, itemAsked, itemInfo); double finalAmount = finalBillAmount(items, inventory, prices, itemAsked, itemAmount);
Test Scenarios to Verify
- Sufficient Stock: If you select "Hammer" and request 50, the output will be
Your total bill is: $ 99.50 - Insufficient Stock: If you select "ScrewDriver" and request 600, the output will be
Sorry we only have Inventory : 500 of Item: ScrewDriverand the program terminates.
内容的提问来源于stack exchange,提问作者Ramon Montoya
相关产品推荐
相关产品推荐

