10.3 Write a program that shows the cost of goods in a fluctuating inventory, wi
ID: 3688245 • Letter: 1
Question
10.3
Write a program that shows the cost of goods in a fluctuating inventory, with inflation, assuming
LIFO inventory evaluation. In an inflating economy, LIFO inventory evaluation reduces income
taxes. To model a LIFO inventory, we use a stack. Assume that the first item purchased for inventory
costs 200, and each additional item costs two percent more than the previous item. To model
inflation, create a class variable called cost. Initialize it at 200, and after pushing the cost of each
newly purchased item onto the stack, increase the value in this class variable by two percent in
preparation for the next push. To represent a sale, pop an item off the stack and display its cost as
“cost of goods sold.” Start by pushing five items onto the stack. Then use println to display the
inventory. Then use a peek method call to read the cost of the item currently on the top of the stack.
(Java API documentation says ArrayDeque’s peek method “retrieves, but does not remove, the
head of the queue represented by this deque, or returns null if this deque is empty.”) Then do three
pops, and print each popped value. Then do two more pushes, followed by another display of the
inventory. Make your program produce the following output:
Sample session:
after five pushes: [216, 212, 208, 204, 200]
peek: 216
cost of sale = 216
cost of sale = 212
cost of sale = 208
after two more pushes: [224, 220, 204, 200]
Explanation / Answer
import java.util.*;
public class StackDemo
{
static void main(String[] args)
{
int n=200;
Stack stack = new Stack();
stack.push(n);
for(int i=0;i<4;i++)
{
n=n+(n*2/100);
stack.push(n);
}
System.out.println(stack.pop());
System.out.println(stack.pop());
System.out.println(stack.pop());
stack.push(220);
stack.push(224);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.