019-007-002
Transaction and Rollback: Recipe Ingredient Management
Easy
Problem Description
Transaction and Rollback: Recipe Ingredient Management
Learning Objective: Learn to understand transaction and rollback concepts using exception handling
Create program to update recipe ingredient inventory. Execute operations to reduce inventory of multiple ingredients, and rollback all changes if insufficient stock occurs (rollback). Using finally clause, always display final inventory state. This mechanism prevents partial inventory updates, maintaining data consistency.
Input
Line 1: Initial flour stock (g)
Line 2: Initial sugar stock (g)
Line 3: Flour used in recipe (g)
Line 4: Sugar used in recipe (g)
Output
Normal case:
=== Recipe Transaction ===
Flour: [initial]g → [after use]g
Sugar: [initial]g → [after use]g
━━━━━━━━━━━━━━━━
Transaction: Committed
Status: Recipe prepared
```java
Rollback case:
```java
=== Recipe Transaction ===
Flour: [initial]g → Operation failed
Sugar: [initial]g → Not executed
━━━━━━━━━━━━━━━━
Transaction: Rolled back
Flour: [initial]g (restored)
Sugar: [initial]g (restored)
Error: [error message]
Test Cases
※ Output examples follow programming industry standards
Input:
1000 500 300 200
Expected Output:
=== Recipe Transaction === Flour: 1000g → 700g Sugar: 500g → 300g ━━━━━━━━━━━━━━━━ Transaction: Committed Status: Recipe prepared
Input:
200 500 300 200
Expected Output:
=== Recipe Transaction === Flour: 200g → Operation failed Sugar: 500g → Not executed ━━━━━━━━━━━━━━━━ Transaction: Rolled back Flour: 200g (restored) Sugar: 500g (restored) Error: Insufficient flour
Input:
1000 100 300 200
Expected Output:
=== Recipe Transaction === Flour: 1000g → 700g Sugar: 100g → Not executed ━━━━━━━━━━━━━━━━ Transaction: Rolled back Flour: 1000g (restored) Sugar: 100g (restored) Error: Insufficient sugar
❌ Some tests failed
Your Solution
Current Mode:● My Code
99
1
2
3
4
5
6
7
8
9
10
›
⌄
⌄
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Write your code here
sc.close();
}
}
0 B / 5 MB
You have 9 free executions remaining
