1,105,271 Community Members

Help: Java Retail Sales Application

Member Avatar
Zephnath
Newbie Poster
1 post since Jun 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hello all, I am looking for some help writing an application with the following attribuutes:
An online retailer sells five products whose retail prices are as follows:
1. Product 1, $2.98
2. Product 2, $4.50
3. Product 3, $3.98
4. Product 4, $4.49
5. Product 5, $6.87
Write an application that reads (from the keyboard) a series of pairs of numbers as follows:
1. product number
2. quantity sold
Run your program from a simple menu with the following options
1. Enter products sold
2. Display total retail value
Your program should
1. allow for entry of multiple products without returning to the main menu (use a sentinel-controlled loop)
2. use a switch statement to determine the retail price for each product
3. calculate and display the total retail value of all products sold

Here is the RetailSales class source code I have so far

/**
 * 
 * RetailSales.java
 * Program to record products purchased and display the total value of all purchases
 * 
 * version 1.00 2011/6/22
 */
import java.util.Scanner; // program uses class Scanner


public class RetailSales
{
	private String buyerName; // name of the purchaser
	// int instance variables are initialized to 0 by default
	private int total; // sum of purchase price
	private int purchaseCounter; // number of purchases made
	private int product1; // count of product 1 purchases
	private int product2; // count of product 2 purchases
	private int product3; // count of product 3 purchases
	private int product4; // count of product 4 purchases
	private int product5; // count of product 5 purchases

	// constructor initializes productName
	public RetailSales( String name )
	{
			buyerName = name;// intitializes productName
	}// end contstructor

	// method to set product name
	public void setBuyerName( String name )
	{
		buyerName = name;// store the product name
	}// end method setProductName

	// method to return product name
	public String getBuyerName()
	{
		return buyerName;
	}// end method getProductName

	// display a welcome message to the purchaser
	public void displayMessage()
	{
		// getBuyerName gets name of purchaser
		System.out.printf( "Hello, %s! Please select the products that you wish to purchase.", getBuyerName() );
	}// end method displayMessage

	// inpute arbitrary number for product selection from user
	public void inputPurchase()
	{
		Scanner input = new Scanner( System.in );

		int selection; // product entered by user

		System.out.printf( "%s\n%s\n	%s\n   %s\n",
			"Enter a product selection using an integer in range of 1-5.",
			"Type end-of-line indicator complete purchase",
			"On UNIX/Linux/Mac OS X type <Ctrl> d then press Enter",
         	"On Windows type <Ctrl> z then press Enter" );

		// loop until user enters sentery
		while( input.hasNext() )
		{
			selection = input.nextInt(); // read selection
			total += selection; // add selection to total
			++purchaseCounter; // increment number of selections

			// call the method to increment appropriate counter
			incrementNumberPurchaseCounter( selection );

		}// end method inputPurchase
	}

		// add 1 to appropriate counter for specified counter
		private void incrementNumberPurchaseCounter( int selection )
		{
			// determine which product number was entered
			switch( selection )
			{
				case 1: // selection was product1
					++product1; // increment product1
					break;
				case 2: // selection was product2
					++product2; // increment product2
					break;
				case 3: // selection was product3
					++product3; // increment product3
					break;
				case 4: // selection was product4
					++product4; // increment product4
					break;
				case 5: // selection was product5
					++product5; // increment product5
					break;
			}// end switch
		} // end method incrementNumberPurchaseCounter

		// display a report based on the selections entered
		public void displayPurchaseReport()
		{
			System.out.println( "\nPurchases this session: ");

			// if user entered at leaset on grade...
			if( purchaseCounter != 0 )
			{
				// calculate total of all products entered
				double sum = (double) total + purchaseCounter;

				// output summary of results
				System.out.printf( "Total of the %d products enter is %d\n", purchaseCounter, total );
				System.out.printf( "The total cost is %d\n", sum );
				System.out.printf( "%s\n%s%d\n%s%d\n%s%d\n%s%d\n%s%d\n",
				"Number of each product purchased: ",
				"Product 1: ", product1,
				"Product 2: ", product2,
				"Product 3: ", product3,
				"Product 4: ", product4,
				"Product 5: ", product5 );
			} // end if
			else // no product numbers were entered
				System.out.println( "No products were entered" );
		} // end method displayPurchaseReport

} // end class

And the Main Class source code

/**
 * RetailSalesTest.java
 * version 1.00 2011/6/23
 */

public class RetailSalesTest
{
   public static void main( String[] args )
   {
      // create GradeBook object myGradeBook and
      // pass course name to constructor
      RetailSales myRetailSales = new RetailSales(
         "John" );

      myRetailSales.displayMessage(); // display welcome message
      myRetailSales.inputPurchase(); // read purchase from user
      myRetailSales.displayPurchaseReport(); // display report based on purchases
   } // end main
} // end class GradeBookTest

The program compiles and runs, but I am obviosly missing some stuff so that the app does not satisfy the requirements. Any help or pointers on how I can meet those requirements would be greatly appreciated. (Like assigning prices to the products and adding them for the total purchase value)

Member Avatar
NormR1
Posting Sage
7,723 posts since Jun 2010
Reputation Points: 563 [?]
Q&As Helped to Solve: 793 [?]
Skill Endorsements: 16 [?]
Team Colleague
 
1
 

the app does not satisfy the requirements

Please explain what requirements you are missing.

assigning prices to the products and adding them for the total purchase value

I'm assuming you have a Product class.
Several ways to assign a price: in the constructor when the product object is created.
Have a setPrice() method that sets the price.

To get the total: Have a getPrice() method that returns the price of the product. Call it for each product and add them up

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: