1 Replies - 794 Views - Last Post: 16 February 2010 - 08:29 PM Rate Topic: -----

#1 moman1981  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 43
  • Joined: 17-December 09

retail

Posted 16 February 2010 - 05:12 PM

I have a small favor to ask.... well I hope that it is small.

below is the print console printout of my retail program. if you look closely I have selected item# 1 and entered the max amount of stock. and have done so on both occasions. How do I decrement the amount of stock available every time you select a certain number, and reflect it on the next iteration. you will find all classes and methods below.

Retail Sales Program

Items currently in stock & available for sale:

Description Units on hand Price/unit
Item #1 Jacket 12 59.95
Item #2 Designer Jeans 40 34.95
Item #3 Shirt 20 24.95
Item #4 Silk Tie 15 12.95

Enter item # of product: 1
Enter quatity needed: 12
Any more items to purchase (Y or N): y
Items currently in stock & available for sale:

Description Units on hand Price/unit
Item #1 Jacket 12 59.95
Item #2 Designer Jeans 40 34.95
Item #3 Shirt 20 24.95
Item #4 Silk Tie 15 12.95

Enter item # of product: 1
Enter quatity needed: 12
Any more items to purchase (Y or N): n

Store xyz Sales Receipt
Quantity Description Totals
12 Item #1 Jacket @ $59.95 => $ 719.40
12 Item #1 Jacket @ $59.95 => $ 719.40
-------------------------------------------------------------------
Subtotal: $1438.80
Sales Tax: $ 57.55
-------------------------------------------------------------------
Total: $ 1496.35


public class RetailItem
{

    private String description;
    private int unitsOnHand;
    private double price;
    // no argument constructor:
    public RetailItem() {
        this.description = "empty";
        this.unitsOnHand = 0;
        this.price = 0.0;
    }
    // constructor that 'clones':
    public RetailItem(RetailItem ri) {
        this.description = new String(ri.description);
        this.unitsOnHand = ri.unitsOnHand;
        this.price = ri.price;
    }
    // constructor with arguments:
    public RetailItem(String desc, int units, double price) {
        //  RetailItem build = new RetailItem();
        this.description = new String(desc);
        this.unitsOnHand = units;
        this.price = price;
    }

    public String getDescription() {
        return (new String(this.description));
    }

    public int getUnitsOnHand() {
        return this.unitsOnHand;
    }

    public double getPrice() {
        return this.price;
    }
    // used in PointOfSale to build the currentTransaction vector of 
    // RetailItem objects:
    public void setUnitsOnHand(int numSold) {
        this.unitsOnHand = numSold;
    }
}





import java.util.Vector;
import java.util.Scanner;

public class PointOfSale
{

    private static Scanner kb = new Scanner(System.in);
    
/*
 * **********************************************************************************************************************
 * *********************************************MAIN METHOD**************************************************************
 * **********************************************************************************************************************
 */
 
    public static void main(String[] args) {
        System.out.println("\n\tRetail Sales Program\n");
        // start with no inventory:

        StoreInventory.Inventory.clear();
        // build the inventory using the given products:
        StoreInventory.addToInventory(new RetailItem("Item #1 Jacket", 12, 59.95));
        StoreInventory.addToInventory(new RetailItem("Item #2 Designer Jeans", 40, 34.95));
        StoreInventory.addToInventory(new RetailItem("Item #3 Shirt", 20, 24.95));
        StoreInventory.addToInventory(new RetailItem("Item #4 Silk Tie", 15, 12.95));
        // now get some sales:
        // accumulate sales in a vector:
        Vector<RetailItem> currentTransaction = new Vector<RetailItem>();
        String choice;
        do {
            showItemInventory();
            RetailItem soldItem = enterItemAndQuantity();
            currentTransaction.add(soldItem);
            // take the sold items out of inventory:
            updateInventory(soldItem);
            System.out.print("Any more items to purchase (Y or N): ");
            // clear out the \n left over:
            kb.nextLine();
            choice = kb.nextLine().trim();
        } while (choice.equalsIgnoreCase("Y"));
        SalesTransaction.createSalesReceipt(currentTransaction);

    }

    public static void showItemInventory() {
        System.out.println("Items currently in stock & available for sale:\n");
        System.out.println("Description                Units on hand      Price/unit");
        
        for (int k = 0; k < StoreInventory.Inventory.size(); k++) {
            RetailItem thisOne = StoreInventory.Inventory.elementAt(k);
            // only show items in stock, (unitsOnHand != 0):
            if (thisOne.getUnitsOnHand() != 0) {
                System.out.println(String.format("%-30s  %3d            % 6.2f",
                                   thisOne.getDescription(), thisOne.getUnitsOnHand(),
                                   thisOne.getPrice()));
            }

        }
    }
    // note that for the sole item the unitsOnHand field is the number the 
    // customer wants to buy, it will be subtracted from the Inventory
    public static RetailItem enterItemAndQuantity()
    {


        System.out.print("\nEnter item # of product: ");
        int index = kb.nextInt();
        System.out.print("Enter quatity needed: ");
        int numSold = kb.nextInt();
        // error checking here


        // construct soldItem object:
        RetailItem soldItem = new RetailItem(
                StoreInventory.Inventory.elementAt(index - 1));
        // not really the units on hand but rather the number of units
        // that are sold:
        soldItem.setUnitsOnHand(numSold);
        return soldItem;
    }

    public static void updateInventory(RetailItem sold) {

        // need to search to find item that was sold:
        for (int k = 0; k < StoreInventory.Inventory.size(); k++) {
            RetailItem thisOne = new RetailItem(
                    StoreInventory.Inventory.elementAt(k));
            if (thisOne.getDescription().equals(sold.getDescription())) {

                thisOne.setUnitsOnHand(
                        thisOne.getUnitsOnHand() - sold.getUnitsOnHand());
            }
        }
    }
}






import java.util.Vector;

public class SalesTransaction
{
/**
 * 
 * @param trans
 */
    public static void createSalesReceipt(Vector<RetailItem> trans) {
        /*
        Store xyz Sales Receipt
        Quantity Description Totals
        1 Jacket @ $59.95 each $59.95
        2 Shirt @ $24.95 each $49.90
        Subtotal: $109.85
        Sales Tax: $4.39
        Total: $114.24
         */
        System.out.println("\n\tStore xyz Sales Receipt\n" +
        			       "Quantity     Description                            Totals");
        
        for (int k = 0; k < trans.size(); k++)
        {
            RetailItem thisOne = new RetailItem(trans.elementAt(k));
            System.out.println(String.format("%3d       %-30s  @   $%.2f   =>  $%7.2f",
                               thisOne.getUnitsOnHand(), thisOne.getDescription(),
                               thisOne.getPrice(),
                               thisOne.getPrice() * thisOne.getUnitsOnHand()));
        }
        System.out.println("-------------------------------------------------------------------");
        System.out.println("                                               Subtotal:   " +
                           String.format("$%7.2f", getSubtotal(trans)) + "\n" +
                           "                                              Sales Tax:   " + 
                           String.format("$%7.2f", getTax(trans)));
        
        System.out.println("-------------------------------------------------------------------");
        System.out.println("                                                 Total:  " +
                           String.format("$%9.2f", getTotal(trans)));
    }
/**
 * 
 * @param trans
 * @return
 */
    public static double getSubtotal(Vector<RetailItem> trans) {
        double sum = 0.0;
        for (int k = 0; k < trans.size(); k++) {
            sum += trans.elementAt(k).getUnitsOnHand() * trans.elementAt(k).getPrice();
        }
        return sum;
    }
/**
 * 
 * @param trans
 * @return
 */
   public static double getTax(Vector<RetailItem> trans) {
        return 0.04 * getSubtotal(trans); // 4% sales tax
    }
/**
 * 
 * @param trans
 * @return
 */
   public static double getTotal(Vector<RetailItem>trans)
   {
        return getSubtotal(trans) + getTax(trans);
   }
}








import java.util.Vector;

public class StoreInventory
{

    static Vector<RetailItem> Inventory = new Vector();
/**
 * 
 * @param retIt
 */
    public static void addToInventory(RetailItem retIt)
    {
        Inventory.add(retIt);
    }
/**
 * 
 * @return
 */
    public double getTotalPriceOfInventory() {
        double sum = 0.0;
        for (int k = 0; k < Inventory.size(); k++) {
            RetailItem thisOne = Inventory.elementAt(k);
            sum += thisOne.getUnitsOnHand() *
                    thisOne.getPrice();
        }
        return sum;
    }
/**
 * 
 * @param thisOne
 * @return
 */
    public int getUnitsOnHand(RetailItem thisOne) {
        return thisOne.getUnitsOnHand();
    }
}




Is This A Good Question/Topic? 0
  • +

Replies To: retail

#2 DaneAU  Icon User is offline

  • Great::Southern::Land
  • member icon

Reputation: 286
  • View blog
  • Posts: 1,619
  • Joined: 15-May 08

Re: retail

Posted 16 February 2010 - 08:29 PM

I believe in this section of code
 System.out.print("Enter quatity needed: ");
        int numSold = kb.nextInt();
        // error checking here


        // construct soldItem object:
        RetailItem soldItem = new RetailItem(
                StoreInventory.Inventory.elementAt(index - 1));
        // not really the units on hand but rather the number of units
        // that are sold:
        soldItem.setUnitsOnHand(numSold);



You should not be doing soldItem.setUnitsOnHand( numSold );

Instead you should be retrieving the number of units on hand and subtracking the number that were sold.
eg. soldItem.setUnitsOnHand( getUnitsOnHand() - numSold );
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1