019-007-002

Transaction and Rollback: Recipe Ingredient Management

Easy

Problem Description

Transaction and Rollback: Recipe Ingredient Management

In this problem, you will create a program that uses try-catch-finally to update flour and sugar inventory, rolls back all changes when stock is insufficient, and displays the result to standard output.

Learning Objective: Learn to understand transaction and rollback concepts using exception handling

Create a program to update recipe ingredient inventory. Execute operations to reduce the inventory of multiple ingredients, and roll back all changes if insufficient stock occurs mid-way. Using the finally clause, always display the 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

Rollback case:

=== 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

Normal case
Input:
1000
500
300
200
Expected Output:
=== Recipe Transaction ===
Flour: 1000g → 700g
Sugar: 500g → 300g
━━━━━━━━━━━━━━━━
Transaction: Committed
Status: Recipe prepared
Normal case
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

Your Solution

Current Mode: My Code
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 10 free executions remaining