Just expand on my program. Thanks in advance The Admin Menu for our Candy Machin
ID: 3767646 • Letter: J
Question
Just expand on my program. Thanks in advance
The Admin Menu for our Candy Machine and its array of Transactions have been expanded to the following.
public static void adminMenu()
{
System.out.println(" *** Admin Menu ***");
System.out.println("Selections ");
System.out.println("1 Print all transactions");
System.out.println("2 Show Best Selling Item");
System.out.println("3 Show Worst Selling Item");
System.out.println("4 Show statistics by Time");
System.out.println("5 Show total $ amount of sales");
System.out.println("6 Search Sales");
System.out.println("9 to exit");
}//end showSelection
Together in class we made option 1 functional
Create methods that will make the other options functional:
bestSelling(...) -- This should show the best selling item and the number sold.
worstSelling(...) -- This should show the WORST selling item and the number sold.
timeStats(...) -- Give the user the option to search by full time (H:M:S) or individually Hour, Minute or Second.
totalSales(...) -- Just simply let this one show the grand total ($) of all sales (No breakdown necessary)
searchTransactions(...) -- Allow the user to search for any transactions by name (allow wildcards --- see below) or price range.
NOTE: Wildcards means the user could just type in 1 or a few letters of the named item without having to type in the full name and still see results. For example if the user typed in just the letter "c" in their search, then it should show results for anything starting with "c" (i.e. "candy"..."chips") For this you will have to investigate the rich selection of methods that the "String" class provides.
//Program: Candy Machine
import java.util.*;
public class CandyMachine
{
static Scanner console = new Scanner(System.in);
public static void main(String[] args)
{
CashRegister cashRegister = new CashRegister(); //Step 1
Dispenser candy = new Dispenser(100, 50, "candy"); //Step 2
Dispenser chips = new Dispenser(100, 65,"chips"); //Step 2
Dispenser gum = new Dispenser(75, 45,"gum"); //Step 2
Dispenser cookies = new Dispenser(100, 85,"cookies"); //Step 2
Transaction [] transRecord = new Transaction[1000]; //Create room for 1000 transactions
int choice; //variable to hold the selection //Step 3
int transcount=0; //Used as an index for the array of transactions.
//Transaction monday = new Transaction();
showSelection(); //Step 4
choice = console.nextInt(); //Step 5
while (choice != 9) //Step 6
{
switch (choice) //Step 6a
{
case 1:
sellProduct(candy, cashRegister);
transRecord[transcount] = new Transaction();
transRecord[transcount].setItemSold("candy");
transcount++; //Increment array
break;
case 2:
sellProduct(chips, cashRegister);
transRecord[transcount] = new Transaction();
transRecord[transcount].setItemSold("chips");
transcount++; //Increment array
break;
case 3:
sellProduct(gum, cashRegister);
transRecord[transcount] = new Transaction();
transRecord[transcount].setItemSold("gum");
transcount++; //Increment array
break;
case 4:
sellProduct(cookies, cashRegister);
transRecord[transcount] = new Transaction();
transRecord[transcount].setItemSold("cookies");
transcount++; //Increment array
break;
default:
System.out.println("Invalid Selection");
}//end switch
showSelection(); //Step 6b
choice = console.nextInt(); //Step 6c
}//end while
choice = 0; //reset choice
while (choice != 9)
{
adminMenu();
choice = console.nextInt();
switch(choice)
{
case 1: printTrans(transRecord, transcount);
break;
default:
}
}//End while
}//end main
//////////////////////////////////////////////////////////////////////
public static void showSelection()
{
System.out.println("*** Welcome to Shelly's "
+ "Candy Shop ***");
System.out.println("To select an item, enter ");
System.out.println("1 for Candy");
System.out.println("2 for Chips");
System.out.println("3 for Gum");
System.out.println("4 for Cookies");
System.out.println("9 to exit");
}//end showSelection
public static void sellProduct(Dispenser product,
CashRegister cRegister)
{
int price; //variable to hold the product price
int coinsInserted; //variable to hold the amount entered
int coinsRequired; //variable to show the extra amount
//needed
if (product.getCount() > 0) //Step 1
{
price = product.getProductCost(); //Step 1a
coinsRequired = price; //Step 1b
coinsInserted = 0; //Step 1c
while (coinsRequired > 0) //Step 1d
{
System.out.print("Please deposit "
+ coinsRequired
+ " cents: "); //Step 1d.i
coinsInserted = coinsInserted
+ console.nextInt(); //Step 1d.ii
coinsRequired = price
- coinsInserted; //Step 1d.iii
}
System.out.println();
cRegister.acceptAmount(coinsInserted); //Step 1e
product.makeSale(); //Step 1f
System.out.println("Collect your item "
+ "at the bottom and "
+ "enjoy. "); //Step 1g
}
else
System.out.println("Sorry this item "
+ "is sold out. "); //Step 2
}//end sellProduct
public static void adminMenu()
{
System.out.println(" *** Admin Menu ***");
System.out.println("Selections ");
System.out.println("1 Print all transactions");
System.out.println("2 Show Best Selling Item");
System.out.println("3 Show Worst Selling Item");
System.out.println("4 Show statistics by Time");
System.out.println("5 Show total $ amount of sales");
System.out.println("9 to exit");
}//end showSelection
public static void printTrans(Transaction [] transRecord, int transcount)
{
for (int i=0; i<transcount; i++)
{
System.out.println(transRecord[i] + " ");
}
}
}
Explanation / Answer
YOU HAVE NOT PROVIDED THE CLASSES Dispneser and Transaction and CashRegister but I am still trying to guess the methods they might have and writing the code. If you edit your question and add the above, I will edit my answer and make it perfect.
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.omg.CORBA.TRANSACTION_MODE;
public class CandyMachine {
static Scanner console = new Scanner(System.in);
public static void main(String[] args) {
CashRegister cashRegister = new CashRegister(); // Step 1
Dispenser candy = new Dispenser(100, 50, "candy"); // Step 2
Dispenser chips = new Dispenser(100, 65, "chips"); // Step 2
Dispenser gum = new Dispenser(75, 45, "gum"); // Step 2
Dispenser cookies = new Dispenser(100, 85, "cookies"); // Step 2
Dispenser[] allProducts = {candy, chips, gum, cookies}; // For ease of pass of parameters
Transaction[] transRecord = new Transaction[1000]; // Create room for
// 1000 transactions
int choice; // variable to hold the selection //Step 3
int transcount = 0; // Used as an index for the array of transactions.
// Transaction monday = new Transaction();
showSelection(); // Step 4
choice = console.nextInt(); // Step 5
while (choice != 9) // Step 6
{
switch (choice) // Step 6a
{
case 1:
sellProduct(candy, cashRegister);
transRecord[transcount] = new Transaction();
transRecord[transcount].setItemSold("candy");
transcount++; // Increment array
break;
case 2:
sellProduct(chips, cashRegister);
transRecord[transcount] = new Transaction();
transRecord[transcount].setItemSold("chips");
transcount++; // Increment array
break;
case 3:
sellProduct(gum, cashRegister);
transRecord[transcount] = new Transaction();
transRecord[transcount].setItemSold("gum");
transcount++; // Increment array
break;
case 4:
sellProduct(cookies, cashRegister);
transRecord[transcount] = new Transaction();
transRecord[transcount].setItemSold("cookies");
transcount++; // Increment array
break;
default:
System.out.println("Invalid Selection");
}// end switch
showSelection(); // Step 6b
choice = console.nextInt(); // Step 6c
}// end while
choice = 0; // reset choice
while (choice != 9) {
adminMenu();
choice = console.nextInt();
switch (choice) {
case 1:
printTrans(transRecord, transcount);
break;
case 2:
bestSelling(allProducts);
break;
case 3:
worstSelling(allProducts);
break;
case 4:
timeStats(transRecord, transcount);
break;
case 5:
totalSales(allProducts);
break;
case 6:
searchTransactions(transRecord);
break;
default:
}
}// End while
}// end main
// ////////////////////////////////////////////////////////////////////
public static void showSelection() {
System.out.println("*** Welcome to Shelly's " + "Candy Shop ***");
System.out.println("To select an item, enter ");
System.out.println("1 for Candy");
System.out.println("2 for Chips");
System.out.println("3 for Gum");
System.out.println("4 for Cookies");
System.out.println("9 to exit");
}// end showSelection
public static void sellProduct(Dispenser product, CashRegister cRegister) {
int price; // variable to hold the product price
int coinsInserted; // variable to hold the amount entered
int coinsRequired; // variable to show the extra amount
// needed
if (product.getCount() > 0) // Step 1
{
price = product.getProductCost(); // Step 1a
coinsRequired = price; // Step 1b
coinsInserted = 0; // Step 1c
while (coinsRequired > 0) // Step 1d
{
System.out
.print("Please deposit " + coinsRequired + " cents: "); // Step
// 1d.i
coinsInserted = coinsInserted + console.nextInt(); // Step 1d.ii
coinsRequired = price - coinsInserted; // Step 1d.iii
}
System.out.println();
cRegister.acceptAmount(coinsInserted); // Step 1e
product.makeSale(); // Step 1f
System.out.println("Collect your item " + "at the bottom and "
+ "enjoy. "); // Step 1g
} else
System.out.println("Sorry this item " + "is sold out. "); // Step 2
}// end sellProduct
public static void adminMenu() {
System.out.println(" *** Admin Menu ***");
System.out.println("Selections ");
System.out.println("1 Print all transactions");
System.out.println("2 Show Best Selling Item");
System.out.println("3 Show Worst Selling Item");
System.out.println("4 Show statistics by Time");
System.out.println("5 Show total $ amount of sales");
System.out.println("9 to exit");
}// end showSelection
public static void printTrans(Transaction[] transRecord, int transcount) {
for (int i = 0; i < transcount; i++) {
System.out.println(transRecord[i] + " ");
}
}
public static void bestSelling(Dispenser[] allProducts) {
int maxItemsSold = 0;
String maxSellingItem;
for (Dispenser dispenser: allProducts) {
if (dispenser.totalInitialItem() - dispenser.productCount() > maxItemsSold) {
maxItemsSold = dispenser.totalInitialItem() - dispenser.productCount();
maxSellingItem = dispenser.getProductName();
}
}
System.out.println("Best selling item is " + maxSellingItem);
}
public static void worstSelling(Dispenser[] allProducts) {
int minItemsSold = allProducts[0].productCount();
String minSellingItem;
for (Dispenser dispenser: allProducts) {
if (dispenser.totalInitialItem() - dispenser.productCount() < minItemsSold) {
minItemsSold = dispenser.totalInitialItem() - dispenser.productCount();
minSellingItem = dispenser.getProductName();
}
}
System.out.println("Best selling item is " + minSellingItem);
}
public static void timeStats(Transaction[] transRecord, int transcount) {
System.out.println("Do you want to search by full time (H:M:S) or individually Hour, Minute or Second? Choose the corresponding option");
System.out.println("1 By full time (H:M:S)");
System.out.println("2 By Hour");
System.out.println("3 By Minute");
System.out.println("4 By Second");
int choice, sHour, eHour, sMinute, eMinute, sSecond, eSecond;
String startTime, endTime;
choice = console.nextInt();
switch (choice) {
case 1:
System.out.println("Enter Start Hour");
sHour = console.nextInt();
System.out.println("Enter End Hour");
eHour = console.nextInt();
startTime = sHour + ":00:00";
endTime = eHour + ":00:00";
timeStats(startTime, endTime, transRecord, transcount);
break;
case 2:
System.out.println("Enter Start Hour");
sHour = console.nextInt();
System.out.println("Enter End Hour");
eHour = console.nextInt();
System.out.println("Enter Start Minute");
sMinute = console.nextInt();
System.out.println("Enter End Minute");
eMinute = console.nextInt();
startTime = sHour + ":" + sMinute + ":00";
endTime = eHour + ":" + eMinute + ":00";
timeStats(startTime, endTime, transRecord, transcount);
break;
case 3:
System.out.println("Enter Start Hour");
sHour = console.nextInt();
System.out.println("Enter End Hour");
eHour = console.nextInt();
System.out.println("Enter Start Minute");
sMinute = console.nextInt();
System.out.println("Enter End Minute");
eMinute = console.nextInt();
System.out.println("Enter Start Second");
sSecond = console.nextInt();
System.out.println("Enter End Second");
eSecond = console.nextInt();
startTime = sHour + ":" + sMinute + ":" + sSecond;
endTime = eHour + ":" + eMinute + ":" + eSecond;
timeStats(startTime, endTime, transRecord, transcount);
break;
}
}
private static void timeStats(String startTime, String endTime, Transaction[] transRecord, int transcount) {
for (int i = 0; i < transcount; i++) {
if (transRecord[i].getTime() > startTime && transRecord[i].getTime() < endTime) {
System.out.println(transRecord[i] + " ");
}
}
}
public static void totalSales(Dispenser[] allProducts) {
int totalSales = 0;
for (Dispenser product: allProducts) {
totalSales += (product.getInititalItem() - product.productCount())*product.getProductCost();
}
System.out.println("Total Sales = " + totalSales);
}
public static void searchTransactions(Transaction[] transRecord) {
System.out.println("Do you want to search by name or price range");
System.out.println("1 By name");
System.out.println("2 By price range");
int choice = console.nextInt();
if (choice == 1) {
System.out.println("Enter name");
String name = console.nextLine();
for (Transaction transaction: transRecord) {
Pattern r = Pattern.compile(name);
Matcher m = r.matcher(transRecord.getProductName());
if (m.find()) {
System.out.println(transRecord[i] + " ");
}
}
}
else if (choice == 2) {
System.out.println("Enter start price range");
int sRange = console.nextInt();
System.out.println("Enter end price range");
int eRange = console.nextInt();
for (Transaction transaction: transRecord) {
if (transaction.getPrice() > sRange && transaction.getPrice() < eRange) {
System.out.println(transRecord[i] + " ");
}
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.