/* * MrsFields.java * * Created on April 24, 2002, 11:43 PM */ import javax.swing.*; /** * * @author mike * @version */ public class MrsFields { private int inventory = 0; private int numSold = 0; private double profitMargin = .50; /** * Creates default new MrsFields */ public MrsFields() { } /** * Creates new MrsFields - with non defaults */ // left for students to do !! } /** * report current inventory */ public int getCurrInv() { return inventory; } /** * report number sold so far */ public int getSold() { return numSold; } /** * report starting inventory */ public int getStart() { int start = inventory + numSold; return start; } /** * report amount of profit per cookie */ public double getMargin() { return profitMargin; } /** * report total profit for the day */ public double getTotalProfit() { double profit = profitMargin * numSold; return profit; } /** * Change current inventory levels * Probably should not be allowed to be called directly * Checks to ensure don't take inventory negative */ private void setCurrInv(int num) { // don't allow negative inventory if (num >= 0) { inventory = num; } else { JOptionPane.showMessageDialog(null,"Inventory remains unchanged after attempt to set to negative"); } } /** * Change number sold * Probably should not be allowed to be called directly * Checks to ensure don't take num sold negative */ 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 */ public void setMargin(double amt) { // don't allow negative profit margin if (amt >= 0) { profitMargin = amt; } else { JOptionPane.showMessageDialog(null,"Profit margin remains unchanged after attempt to set to negative"); } } /** * Sell given number of cookies - reflecting in instance variables * First ensure that that many are available */ public boolean sell(int num) { // don't allow negative sale if (num <= 0) { JOptionPane.showMessageDialog(null,"No Cookies sold due to attempt to sell negative number of cookies"); return false; // couldn't sell } // dont allow selling more than we have else if (num > inventory) { JOptionPane.showMessageDialog(null,"No Cookies sold due to attempt to sell more cookies than are available"); return false; // couldn't sell } else { // sell them inventory = inventory - num; numSold = numSold + num; return true; } } public static void main(String args[]) { MrsFields myStore = new MrsFields(); System.out.println(myStore.getCurrInv()); int x; System.exit(0); } }