I need a pseudocode for this assignment for JAVA . Within a loop, display the me
ID: 3669842 • Letter: I
Question
I need a pseudocode for this assignment for JAVA
.
Within a loop, display the menu. If the user enters an option that is not 1, 2, 3, 4 or 9, display the message “This option is not acceptable” and loop back around to redisplay the menu. Continue in this manner until a correct option is input.
If option 1 is selected, prompt the user for the number of DVDs to buy, and once we have this number, check to see if we can afford the DVDs (based on the formula: number of DVDs to be bought * 9 <= total cash we have)
If we can, then we add the newly purchased DVDs to our current DVD count and reduce our money to whatever is left. We also output the number of DVDs we have (with the prefix text “Number of DVDs:”) and the cash we have (with the prefix text “Cash available:”) after the transaction, and re-display the main menu.
If we cannot, then we display a message saying – “You do not have enough money for this transaction”.
If option 2 is selected, prompt the user for the number of DVDs to sell, and once we have this number, check to see if we have enough to sell
If we can, then we reduce the DVD count by the amount sold and increase the money we have (by number of DVDs sold * 10). We also output the number of DVDs we have left (with the prefix text “Number of DVDs:”) and the cash we have (with the prefix text “Cash available:”) after the transaction, and re-display the main menu.
If we cannot, then we display a message saying – “You do not have enough DVDs for this transaction”.
If option 3 is selected, we output the number of DVDs we have left (with the prefix text “Number of DVDs:”) and re-display the main menu.
If option 4 is selected, we output the cash we have (with the prefix text “Cash available:”)and re-display the main menu.
If option 9 is selected then the Program ends.
Continue this looping until option 9 is selected.
Explanation / Answer
NOTE: This is complete code. For pseudocode just remove some indentation and write some sentences in plain english. Also as it is not mentioned in the question, initial cash available to user is considered as 1000.
import java.io.*;
import java.util.*;
public class Main {
static int numberOfDVDsOwned = 0;
static int cashAvailable = 1000;
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
while (true) {
System.out.println("1. Buy DVDs");
System.out.println("2. Sell DVDs");
System.out.println("3. Print DVDs owned");
System.out.println("4. Print cash available");
int option = reader.nextInt();
switch(option) {
case 1:
System.out.print("Enter number of DVDs to buy: ");
int numberOfDVDsWantsToBuy = reader.nextInt();
if (numberOfDVDsWantsToBuy >= 0) {
if (numberOfDVDsWantsToBuy * 9 <= cashAvailable) {
numberOfDVDsOwned += numberOfDVDsWantsToBuy;
cashAvailable -= numberOfDVDsWantsToBuy * 9;
displayAssets();
} else {
System.out.println("You do not have enough money for this transaction.");
}
} else {
System.out.println("Invalid number of DVDs inserted. Please try again!");
}
break;
case 2:
System.out.print("Enter number of DVDs to sell: ");
int numberOfDVDsToSell = reader.nextInt();
if (numberOfDVDsToSell > 0) {
if (numberOfDVDsToSell <= numberOfDVDsOwned) {
numberOfDVDsOwned -= numberOfDVDsToSell;
cashAvailable += numberOfDVDsToSell * 10;
displayAssets();
} else {
System.out.println("You do have enough DVDs for this transaction.");
}
} else {
System.out.println("Invalid number of DVDs inserted. Please try again!");
}
break;
case 3:
printNumberOfDVDs();
break;
case 4:
printCashAvailable();
break;
case 9:
System.out.println("Thank you!");
return;
default:
System.out.println("This option is not acceptable");
}
}
}
public static void displayAssets() {
printNumberOfDVDs();
printCashAvailable();
}
public static void printNumberOfDVDs() {
System.out.println("Number of DVDs: " + Integer.toString(numberOfDVDsOwned));
}
public static void printCashAvailable() {
System.out.println("Cash available: " + Integer.toString(cashAvailable));
}
}
PSEUDOCODE:
numberOfDVDsOwned = 0;
cashAvailable = 1000;
while (true):
PRINT:
"1. Buy DVDs"
"2. Sell DVDs"
"3. Print DVDs owned"
"4. Print cash available"
READ option;
switch(option):
case 1:
PRINT: "Enter number of DVDs to buy: "
READ: numberOfDVDsWantsToBuy
if (numberOfDVDsWantsToBuy >= 0):
if (numberOfDVDsWantsToBuy * 9 <= cashAvailable):
numberOfDVDsOwned += numberOfDVDsWantsToBuy;
cashAvailable -= numberOfDVDsWantsToBuy * 9;
PRINT: "Number of DVDs: " + numberOfDVDsOwned
PRINT: "Cash available: " + cashAvailable
else:
PRINT: "You do not have enough money for this transaction."
else:
PRINT: "Invalid number of DVDs inserted. Please try again!"
break;
case 2:
PRINT: "Enter number of DVDs to sell: "
READ numberOfDVDsToSell
if (numberOfDVDsToSell > 0):
if (numberOfDVDsToSell <= numberOfDVDsOwned):
numberOfDVDsOwned -= numberOfDVDsToSell;
cashAvailable += numberOfDVDsToSell * 10;
PRINT: "Number of DVDs: " + numberOfDVDsOwned
PRINT: "Cash available: " + cashAvailable
else:
PRINT: "You do have enough DVDs for this transaction."
else:
PRINT: "Invalid number of DVDs inserted. Please try again!"
break;
case 3:
PRINT: "Number of DVDs: " + numberOfDVDsOwned
break;
case 4:
PRINT: "Cash available: " + cashAvailable
break;
case 9:
PRINT: "Thank you!"
EXIT_PROGRAM
default:
PRINT: "This option is not acceptable"
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.