Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hi i need help to expand my candy machine java project I already did the best se

ID: 3772193 • Letter: H

Question

Hi i need help to expand my candy machine java project I already did the best selling and worst selling and half of the timestats however the timestats doesnt work good and i also need to have the totalsales of all products and searchtransaction here is the description of the assignment that i need to complete i need help with timeStats totalSales and searchTransaction the other answer was wrong and it did not function correctly how can I test the three methods.

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.

                             Prompt the user by allowing he/she to choose a range (i.e. "starting from"..."Ending at")
                             then print any transactions that have occur during that range.

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.

and here is the code

import java.util.Scanner;


public class Admintools
{
   static final int CANDY = 0;
   static final int CHIPS = 1;
   static final int GUM = 2;
   static final int COOKIES = 3;
   static final int PRODCOUNT = 3;
   public static void bestSelling(Transaction [] sales, int transCount)
   {
       int [] itemCount = new int [PRODCOUNT];
       String [] itemName = {"candy","chips","gum","cookies"};
       int best;

       for (int i=0; i<transCount; i++) //Traverse array and count individual sales
       {

           if   (sales[i].getItemSold().equalsIgnoreCase("candy"))
               itemCount[CANDY]++;

           if   (sales[i].getItemSold().equalsIgnoreCase("CHIPS"))
               itemCount[CHIPS]++;

           if   (sales[i].getItemSold().equalsIgnoreCase("gum"))
               itemCount[GUM]++;

           if   (sales[i].getItemSold().equalsIgnoreCase("cookies"))
               itemCount[COOKIES]++;
       }

       //Analyze counts for best selling
       best=itemCount[CANDY];
       int indexBest = CANDY;

       for (int i=0; i < PRODCOUNT; i++)
       {
           if (itemCount[i]>best)
           {
               best = itemCount[i];
               indexBest = i; //Mark the index of this higher number sold
           }
       }
       System.out.println("The best selling item is " + itemName[indexBest] + " with " + best + " sales.");

   }

   public static void worstSelling(Transaction [] sales, int transCount)
   {
       int [] itemCount = new int [PRODCOUNT];
       String [] itemName = {"candy","chips","gum","cookies"};
       int worst = 0;

       for (int i=0; i<transCount; i++) //Traverse array and count individual sales
       {

           if   (sales[i].getItemSold().equalsIgnoreCase("candy"))
               itemCount[CANDY]++;

           if   (sales[i].getItemSold().equalsIgnoreCase("CHIPS"))
               itemCount[CHIPS]++;

           if   (sales[i].getItemSold().equalsIgnoreCase("gum"))
               itemCount[GUM]++;

           if   (sales[i].getItemSold().equalsIgnoreCase("cookies"))
               itemCount[COOKIES]++;
       }

       //Analyze counts for best selling
       worst = itemCount[CANDY];
       int indexworst = CANDY;

       for (int j=0; j > PRODCOUNT; j++)
       {
           if (itemCount[j]>=worst)
           {
               worst = itemCount[j];
               indexworst = j; //Mark the index of this higher number sold
           }
       }
       System.out.println("The worst selling item is " + itemName[indexworst] + " with " + worst + " sales.");

   }  

   public static void timeStats(Transaction[] transRecord, int transcount){
       Scanner scan = new Scanner(System.in);

       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: H:)");
       System.out.println("2. by Hour");
       System.out.println("3. By Minute");
       System.out.println("4. By Second");

       int choice , startHr, endHr, startMin,endMin,startSec,endSec;
       String startTime;
       String endTime;
       choice = scan.nextInt();
       switch (choice)
       {
       case 1:
           System.out.println(" Please Enter the Start Hour that you which to begin ");
           startHr = scan.nextInt();
           System.out.println(" Please Enter the End Hour that you which to stop ");
           endHr = scan.nextInt();
           startTime = startHr + ":00:00";
           endTime = endHr + ":00.00";
           timeStats(transRecord, transcount);
       break;
       case 2:   
           System.out.println(" Please Enter the Start Hour that you which to begin");
           startHr = scan.nextInt();
           System.out.println(" Please Enter the End Hour that you which to stop");
           endHr = scan.nextInt();
           System.out.println(" Please Enter the Start Minute that you which to begin");
           startMin = scan.nextInt();
           System.out.println(" Please Enter the End Minute that you which to stop");
           endMin = scan.nextInt();
           startTime = startHr + ":" + startMin + ":00";
           endTime = endHr + ":" + endMin + ":00";
           timeStats(transRecord, transcount);
           break;
       case 3:
           System.out.println(" Please Enter the Start Hour that you which to begin");
           startHr = scan.nextInt();
           System.out.println(" Please Enter the End Hour that you which to stop");
           endHr = scan.nextInt();
           System.out.println(" Please Enter the End Minute that you which to stop");
           startMin = scan.nextInt();
           System.out.println(" Please Enter the End Minute that you which to stop");
           endMin = scan.nextInt();
           System.out.println("Please Enter the Start Second that you which to begin");
           startSec = scan.nextInt();
           System.out.println("Please Enter End Second that you which to stop");
           endSec = scan.nextInt();
           startTime = startHr + ":" + startMin + ":" + startSec;
           endTime = endHr + ":" + endMin + ":" + endSec;
           timeStats(transRecord, transcount);
       break;
       }
   }
   //public 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] + "startTime"+ " ");
       //else if ("transRecord[i].getTime()>startTime" == "transRecord[i].getTime< endTime")
           //System.out.println(transRecord[i] + "endTime"+ " ");
  
  
      
  
   public static void totalSales(Dispenser[] AllOfItems){
       int totalSale=0;
       for (Dispenser items: AllOfItems)
       {
           totalSale+=(AllOfItems.length-AllOfItems.length)*items.getProductCost();
          
       }
       System.out.println("Total Sales =" + totalSale);
   }

      
   }
  

//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;   
case 2: Admintools.bestSelling(transRecord, transcount);
break;   
case 3: Admintools.worstSelling(transRecord, transcount);
break;
case 4: int endTime=0;
int       startTime=0;
           Admintools.timeStats(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] + " ");
}   

}
}

//class cashRegister

public class CashRegister
{
private int cashOnHand; //variable to store the cash
//in the register

//Default constructor to set the cash
//in the register to 500 cents
//Postcondition: cashOnHand = 500
public CashRegister()
{
cashOnHand = 500;
}

//Constructor with parameters to set the cash in
//the register to a specific amount
//Postcondition: cashOnHand = cashIn
public CashRegister(int cashIn)
{
if (cashIn >= 0)
cashOnHand = cashIn;
else
cashOnHand = 500;
}

//Method to show the current amount in the cash register
//Postcondition: The value of the instance variable
// cashOnHand is returned.
public int currentBalance()
{
return cashOnHand;
}

//Method to receives the amount deposited by
//the customer and updates the amount in the register
//Postcondition: cashOnHand = cashOnHand + amountIn
public void acceptAmount(int amountIn)
{
cashOnHand = cashOnHand + amountIn;
}
}

//Program to test the various operations of the class Clock

import java.util.*;

public class TestProgClock
{
static Scanner console = new Scanner(System.in);

public static void main(String[] args)
{
Clock myClock = new Clock(5, 4, 30); //Line 1
Clock yourClock = new Clock(); //Line 2

int hours; //Line 3
int minutes; //Line 4
int seconds; //Line 5

System.out.print("Line 6: myClock: "); //Line 6
myClock.printTime(); //Line 7
System.out.println(); //Line 8

System.out.print("Line 9: yourClock: "); //Line 9
yourClock.printTime(); //Line 10
System.out.println(); //Line 11

yourClock.setTime(5, 45, 16); //Line 12

System.out.print("Line 13: After setting "
+ "the time, yourClock: "); //Line 13
yourClock.printTime(); //Line 14
System.out.println(); //Line 15

if (myClock.equals(yourClock)) //Line 16
System.out.println("Line 17: Both the "
+ "times are equal."); //Line 17
else //Line 18
System.out.println("Line 19: The two "
+ "times are not "
+ "equal."); //Line 19

System.out.print("Line 20: Enter the hours, "
+ "minutes, and seconds: "); //Line 20

hours = console.nextInt(); //Line 21
minutes = console.nextInt(); //Line 22
seconds = console.nextInt(); //Line 23
System.out.println(); //Line 24

myClock.setTime(hours, minutes, seconds); //Line 25

System.out.print("Line 26: New time of "
+ "myClock: "); //Line 26
myClock.printTime(); //Line 27
System.out.println(); //Line 28

myClock.incrementSeconds(); //Line 29

System.out.print("Line 30: After "
+ "incrementing the time by "
+ "one second, myClock: "); //Line 30
myClock.printTime(); //Line 31
System.out.println(); //Line 32

yourClock.makeCopy(myClock); //Line 33
System.out.print("Line 34: After copying "
+ "myClock into yourClock, "
+ "yourClock: "); //Line 34
yourClock.printTime(); //Line 35
System.out.println(); //Line 36
}//end main
}

import java.util.Calendar;


public class Transaction
{
//Data members
   String itemSold; //The name of the item
   Clock dateTime; //Time of sale
  
  
//Constructor
   public Transaction()
   {
       itemSold = "";
       dateTime = new Clock();
   }
  
   public Transaction(String name, Clock time)
   {
       itemSold = name;
       dateTime = time;
   }
  
   public String getItemSold()
   {
   return itemSold;
   }
  
   public void setItemSold(String name)
   {
       itemSold = name;
      
       //Time Stamp the time of the Sale
       Calendar c = Calendar.getInstance();
       dateTime.setTime(c.get(Calendar.HOUR_OF_DAY), c.get(Calendar.MINUTE), c.get(Calendar.SECOND));
      
   }
  
   public Clock getDateTime()
   {

      
   return dateTime;
   }
  
   public void setDateTime(Clock newtime)
   {
   dateTime = newtime;
   }
  
   public String toString()
   {
       return "Item name: " + itemSold + " "
               + "Time sold: " + dateTime;
  
     
}
   }
     

//class Dispenser

public class Dispenser
{
private int numberOfItems; //variable to store the number of
//items in the dispenser
private int cost; //variable to store the cost of an item
private String itemname; //Name of Item

//Default constructor to set the cost and number of
//items to the default values
//Postcondition: numberOfItems = 50; cost = 50;
public Dispenser()
{
numberOfItems = 50;
cost = 50;
itemname = "";
}

//Constructor with parameters to set the cost and number
//of items in the dispenser specified by the user
//Postcondition: numberOfItems = setNoOfItems;
// cost = setCost;
public Dispenser(int setNoOfItems, int setCost, String name)
{
if (setNoOfItems >= 0)
numberOfItems = setNoOfItems;
else
numberOfItems = 50;

if (setCost >= 0)
cost = setCost;
else
cost = 50;
itemname = name;
}

//Method to show the number of items in the dispenser
//Postcondition: The value of the instance variable
// numberOfItems is returned.
public int getCount()
{
return numberOfItems;
}

//Method to show the cost of the item
//Postcondition: The value of the instance
// variable cost is returned.
public int getProductCost()
{
return cost;
}

//Method to reduce the number of items by 1
//Postcondition: numberOfItems = numberOfItems - 1
public void makeSale()
{
numberOfItems--;
}

  
  
}

Explanation / Answer

Here your methods

public static void timeStats(Transaction[] transRecord, int transcount) {
Scanner scan = new Scanner(System.in);
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: H:)");
System.out.println("2. by Hour");
System.out.println("3. By Minute");
System.out.println("4. By Second");
int choice, startHr, endHr, startMin, endMin, startSec, endSec;
String startTime;
String endTime;
choice = scan.nextInt();
switch (choice) {
case 1:
System.out.println(" Please Enter the Start Hour that you which to begin ");
startHr = scan.nextInt();
System.out.println(" Please Enter the End Hour that you which to stop ");
endHr = scan.nextInt();
startTime = startHr + ":00:00";
endTime = endHr + ":00.00";
for (int i = 0; i < transRecord.length; i++) {
int start = transRecord[i].dateTime.toString().compareTo(startTime);
int end = transRecord[i].dateTime.toString().compareTo(endTime);
if (start == -1 && end == 1) {
System.out.println(transRecord[i]);
}
}
break;
case 2:
System.out.println(" Please Enter the Start Hour that you which to begin");
startHr = scan.nextInt();
System.out.println(" Please Enter the End Hour that you which to stop");
endHr = scan.nextInt();
System.out.println(" Please Enter the Start Minute that you which to begin");
startMin = scan.nextInt();
System.out.println(" Please Enter the End Minute that you which to stop");
endMin = scan.nextInt();
startTime = startHr + ":" + startMin + ":00";
endTime = endHr + ":" + endMin + ":00";
for (int i = 0; i < transRecord.length; i++) {
int start = transRecord[i].dateTime.toString().compareTo(startTime);
int end = transRecord[i].dateTime.toString().compareTo(endTime);
if (start == -1 && end == 1) {
System.out.println(transRecord[i]);
}
}
break;
case 3:
System.out.println(" Please Enter the Start Hour that you which to begin");
startHr = scan.nextInt();
System.out.println(" Please Enter the End Hour that you which to stop");
endHr = scan.nextInt();
System.out.println(" Please Enter the End Minute that you which to stop");
startMin = scan.nextInt();
System.out.println(" Please Enter the End Minute that you which to stop");
endMin = scan.nextInt();
System.out.println("Please Enter the Start Second that you which to begin");
startSec = scan.nextInt();
System.out.println("Please Enter End Second that you which to stop");
endSec = scan.nextInt();
startTime = startHr + ":" + startMin + ":" + startSec;
endTime = endHr + ":" + endMin + ":" + endSec;
for (int i = 0; i < transRecord.length; i++) {
int start = transRecord[i].dateTime.toString().compareTo(startTime);
int end = transRecord[i].dateTime.toString().compareTo(endTime);
if (start == -1 && end == 1) {
System.out.println(transRecord[i]);
}
}
break;
}
}

public static void totalSales(Dispenser[] AllOfItems) {
int totalSale = 0;
for (int i = 0; i < AllOfItems.length; i++) {
totalSale += AllOfItems[i].getCount() * AllOfItems[i].getProductCost();
}
System.out.println("Total Sales =" + totalSale);
}

public static void searchTransactions(Dispenser[] AllOfItems) {
Scanner sc = new Scanner(System.in);
System.out.println("1. search by name 2.search by price");
int choice = sc.nextInt();
//searching by name
if (choice == 1) {
sc.nextLine();
System.out.println("Enter name to search");
String name = sc.nextLine();
for (int i = 0; i < AllOfItems.length; i++) {
if(AllOfItems[i].getItemname().equals(name)){
System.out.println(AllOfItems[i]);
}
}
//searching by cost
} else {
System.out.println("Enter name to search");
int cost = sc.nextInt();
for (int i = 0; i < AllOfItems.length; i++) {
if(AllOfItems[i].getProductCost() == cost){
System.out.println(AllOfItems[i]);
}
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote