Q4.
A shop stocks 4 kinds of chocolate bars: “Mars”, “Snickers”, “Bounty” “Twix”.
This can be declared in a program as:
String[] bars = {“Mars”, “Snickers”, “Bounty” “Twix”}
Another array can be declared which will keep track of the stock of each kind of
chocolate bar, e.g:
int[] stock = new int[4]
Each stock can be initialised to 100 , as below or otherwise:
for(int i=0; i<stock.length; i++){
stock = 100;
}
Write a program which will keep track of the stock of each bar. The program should
have two static methods: Write a driver program which will test both of these
methods.
/* The method below will be called from main when the shop owner buys in stock.
Stock and bars are as before. barType is String read in from the user indicating which
type of bar was bought by shop owner. quantity stores the quantity of bars bought in
by the shop owner. Implement the pseudocode below*/
static void addStock(int[] stock, String[] bars, String
barType, int quantity)
PSEUDOCODE:
BEGIN SWTICH on bartype
Case 1
What is stored in bar type equals “Mars”,
(e.g. barType.equalsIgnorecase(bars[0])
Update position 0 of stock quantity
(e.g. stock(bars[0]) += quantity)
Break out of switch
Case 2
What is stored in bar type equals “Snickers”
Update position 1 of stock with quantity
Bread
Case 3
What is stored in bar type equals “Bounty”
Update position 2 of stock with quantity
Break
Case 4
What is stored in bar type equals “Twix”
Update position 3 of stock with quantity
Break
Default
Incorrect value entered
Inform user
END SWITCH
Got a lab exam tomorrow and cant work out the switch part of this at all, would anyone be able to turn that pseudocode into actual java code for me? Have had a few shots at it, but keep getting lots of errors.
Any help much appreciated.
A shop stocks 4 kinds of chocolate bars: “Mars”, “Snickers”, “Bounty” “Twix”.
This can be declared in a program as:
String[] bars = {“Mars”, “Snickers”, “Bounty” “Twix”}
Another array can be declared which will keep track of the stock of each kind of
chocolate bar, e.g:
int[] stock = new int[4]
Each stock can be initialised to 100 , as below or otherwise:
for(int i=0; i<stock.length; i++){
stock = 100;
}
Write a program which will keep track of the stock of each bar. The program should
have two static methods: Write a driver program which will test both of these
methods.
/* The method below will be called from main when the shop owner buys in stock.
Stock and bars are as before. barType is String read in from the user indicating which
type of bar was bought by shop owner. quantity stores the quantity of bars bought in
by the shop owner. Implement the pseudocode below*/
static void addStock(int[] stock, String[] bars, String
barType, int quantity)
PSEUDOCODE:
BEGIN SWTICH on bartype
Case 1
What is stored in bar type equals “Mars”,
(e.g. barType.equalsIgnorecase(bars[0])
Update position 0 of stock quantity
(e.g. stock(bars[0]) += quantity)
Break out of switch
Case 2
What is stored in bar type equals “Snickers”
Update position 1 of stock with quantity
Bread
Case 3
What is stored in bar type equals “Bounty”
Update position 2 of stock with quantity
Break
Case 4
What is stored in bar type equals “Twix”
Update position 3 of stock with quantity
Break
Default
Incorrect value entered
Inform user
END SWITCH
Got a lab exam tomorrow and cant work out the switch part of this at all, would anyone be able to turn that pseudocode into actual java code for me? Have had a few shots at it, but keep getting lots of errors.
Any help much appreciated.