I need help on a candy machine program i did the best selling with my instructor
ID: 3768272 • Letter: I
Question
I need help on a candy machine program i did the best selling with my instructor at my college but i need help for the worst selling timestats and totalsales and searchtransaction here is my program 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 [] worstsales, int transCount)
{
int [] itemCount = new int [PRODCOUNT];
String [] itemName = {"candy","chips","gum","cookies"};
int worst;
for (int i=0; i<transCount; i++) //Traverse array and count individual sales
{
if (worstsales[i].getItemSold().equalsIgnoreCase("candy"))
itemCount[CANDY]++;
if (worstsales[i].getItemSold().equalsIgnoreCase("CHIPS"))
itemCount[CHIPS]++;
if (worstsales[i].getItemSold().equalsIgnoreCase("gum"))
itemCount[GUM]++;
if (worstsales[i].getItemSold().equalsIgnoreCase("cookies"))
itemCount[COOKIES]++;
}
//Analyze counts for best selling
worst=itemCount[CANDY];
int indexWorst = CANDY;
for (int i=0; i < PRODCOUNT; i++)
{
if (itemCount[i]>worst)
{
worst = itemCount[i];
indexWorst = i; //Mark the index of this higher number sold
}
}
System.out.println("The worst selling item is " + itemName[indexWorst] + " with " + worst + " sales.");
}
}
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--;
}
}
//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);
case3: Admintools.worstSelling(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
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[] worstsales, int transCount) {
int[] itemCount = new int[PRODCOUNT];
String[] itemName = {"candy", "chips", "gum", "cookies"};
int worst;
for (int i = 0; i < transCount; i++) //Traverse array and count individual sales
{
if (worstsales[i].getItemSold().equalsIgnoreCase("candy")) {
itemCount[CANDY]++;
}
if (worstsales[i].getItemSold().equalsIgnoreCase("CHIPS")) {
itemCount[CHIPS]++;
}
if (worstsales[i].getItemSold().equalsIgnoreCase("gum")) {
itemCount[GUM]++;
}
if (worstsales[i].getItemSold().equalsIgnoreCase("cookies")) {
itemCount[COOKIES]++;
}
}
//Analyze counts for best selling
worst = itemCount[CANDY];
int indexWorst = CANDY;
for (int i = 0; i < PRODCOUNT; i++) {
if (itemCount[i] < worst) {
worst = itemCount[i];
indexWorst = i; //Mark the index of this lowest number sold
}
}
System.out.println("The worst selling item is " + itemName[indexWorst] + " with " + worst + " sales.");
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------
import java.text.ParseException;
import java.util.Calendar;
import java_samples.Clock;
public class Transaction {
//Data members
String itemSold; //The name of the item
Clock dateTime; //Time of sale
//Constructor
public Transaction() throws ParseException {
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;
}
}
-------------------------------------------------------------------------------------------------------------------------------------------------------------
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
static int totalSales = 0;
//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;
totalSales = totalSales + (setCost * setNoOfItems);
}
//Method to show the number of items in the dispenser
//Postcondition: The value of the instance variable
// numberOfItems is returned.
public int getSales(){
return totalSales;
}
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--;
}
}
--------------------------------------------------------------------------------------------------------------------------------------------------
import java.text.ParseException;
import java.util.*;
import java_samples.CashRegister;
public class CandyMachine {
static Scanner console = new Scanner(System.in);
public static void main(String[] args) throws ParseException {
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);
case3:
Admintools.worstSelling(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] + " ");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.