package pretzelpalace; /* * PretzelStore.java * * Created on April 9, 2002, 11:43 PM */ //import javax.swing.*; /** * * @author mike * @version */ public class PretzelStore { private int inventory = 0; // current inventory private int numSold = 0; private double salePrice = 0.50; private double profitMargin = .25; private static final int BATCHSIZE = 12; /** * Creates default new PretzelStore */ public PretzelStore() { } /** * Creates new PretzelStore - with non defaults */ public PretzelStore(int inv, double price, double profit) { inventory = inv; salePrice = price; profitMargin = profit; numSold = 0; } /** * report current inventory */ //############# STUDENT CODE HERE ##################### /** * report number sold so far */ //############# STUDENT CODE HERE ##################### /** * report number of pretzels that have ever been in the store */ public int getStart() { int pretzels = inventory + numSold; return pretzels; } /** * report price for each pretzel sold */ public double getSalePrice() { return salePrice; } /** * report amount of profit per pretzel */ public double getMargin() { return profitMargin; } /** * report total profit for the day */ public double getTotalProfit() { double profit = profitMargin * numSold; return profit; } /** * report total sales for the day */ //############# STUDENT CODE HERE ##################### /** * report total inventory value */ public double getTotalInventoryValue() { double value = inventory * salePrice; return value; } /** * Change current inventory levels * Probably should not be allowed to be called directly * So made private !!! * Checks to ensure don't take inventory negative * returns boolean indicating whether successful */ private boolean setCurrInv(int num) { // don't allow negative inventory if (num >= 0) { inventory = num; return true; } else { //JOptionPane.showMessageDialog(null,"ERROR - Inventory remains unchanged after attempt to set to negative"); return false; } } /** * increase current inventory levels by ordering a new batch of pretzels * Pretzels come in batches of 12 * Assume they arrive immediately * Checks to ensure don't order negative batches * returns boolean indicating whether successful */ public boolean orderNewBatch (int numBatches) { // don't allow negative batches if (numBatches >= 0) { // add to inventory - 12 pretzels per batch inventory += numBatches * BATCHSIZE; return true; } else { return false; } } /** * Change number sold * Probably should not be allowed to be called directly * User programmers (clients) must go through sell method * So made private !!! * Checks to ensure don't take num sold negative * returns boolean indicating whether successful */ private void setSold(int num) { // don't allow negative number sold if (num >= 0) { numSold = num; } else { //JOptionPane.showMessageDialog(null,"Number sold remains unchanged after attempt to set to negative"); } } /** * Change profit margin * Checks to ensure don't take margin negative * returns boolean indicating whether successful */ public boolean setMargin(double amt) { // don't allow negative profit margin if (amt >= 0) { profitMargin = amt; return true; } else { //JOptionPane.showMessageDialog(null,"Profit margin remains unchanged after attempt to set to negative"); return false; } } /** * Sell given number of pretzels - reflecting in instance variables * First ensure that that many are available * returns boolean indicating whether successful */ public boolean sell(int num) { // don't allow negative sale if (num <= 0) { //JOptionPane.showMessageDialog(null,"No Pretzels sold due to attempt to sell negative number of pretzels"); return false; // couldn't sell } // dont allow selling more than we have else if (num > inventory) { //JOptionPane.showMessageDialog(null,"No Pretzels sold due to attempt to sell more pretzels than are available"); return false; // couldn't sell } else { // sell them inventory = inventory - num; numSold = numSold + num; return true; } } /** * dummy driver - not currnetly doing anything */ public static void main(String args[]) { PretzelStore myStore = new PretzelStore(); //System.out.println(myStore.getCurrInv()); int x; System.exit(0); } }