Tips on creating decision statements and loops on this code for a rece

My final consists of applying what we've learned to the first test

Page 1 of 1

2 Replies - 255 Views - Last Post: 09 May 2010 - 08:05 AM Rate Topic: -----

#1 Guest_BLoc*


Reputation:

Tips on creating decision statements and loops on this code for a rece

Posted 09 May 2010 - 06:01 AM

Hello,
My final consists of taking my first test and adding in decision statements and loops. I'm assuming he wants it to run as if you're making multiple transactions and at the end of the day you're going to sum them all up (POS system?).

Here's the initial code from the first test:


/* This is a program that will take customers input and create a receipt */

import javax.swing.JOptionPane; 
public class CrescentCityEats
{
   public static void main(String args[])
   { 
	// Declare variables
		String coffeeSoldString;
		String hurricaneSoldString;
		String beignetSoldString;
		double coffeePrice = 3.95;
		double hurricanePrice = 8.95;
		double beignetPrice = 2.95;
		double salesTax = .09;
		int coffeeSold; 
		int hurricaneSold;
		int beignetSold;
		double coffeeSales;
		double hurricaneSales;
		double beignetSales;
		double subTotalSales;
		double totalSalesTax;
		double totalSales; 
		
	// Get input
		coffeeSoldString = JOptionPane.showInputDialog("Enter How Many Chickory Coffee Sold: ");
		hurricaneSoldString = JOptionPane.showInputDialog("Enter How Many Hurricanes Sold: ");
		beignetSoldString = JOptionPane.showInputDialog("Enter How Many Beignets Sold: ");
	    
	// Convert input to correct data type
		coffeeSold = Integer.parseInt(coffeeSoldString);
		hurricaneSold = Integer.parseInt(hurricaneSoldString);
		beignetSold = Integer.parseInt(beignetSoldString);
			
	// All Calculations go in the below sections
	
	// Figure coffee sales
		coffeeSales = coffeeSold * coffeePrice;
	// Figure hurricane sales
		hurricaneSales = hurricaneSold * hurricanePrice;
	// Figure beignet sales
		beignetSales = beignetSold * beignetPrice;
	// Figure sub-total sales
		subTotalSales = coffeeSales + hurricaneSales + beignetSales;
	// figure out sales tax on the purchase
		totalSalesTax = salesTax * subTotalSales;
	// figure out total sales of purchase
		totalSales = subTotalSales + totalSalesTax;
		

	// receipt output
		System.out.println("\tFat Tuesday Eats\n");
		System.out.println(coffeeSold +"\tChicory Coffee\t"+coffeeSales);
		System.out.println(hurricaneSold +"\tHurricane\t"+hurricaneSales);
		System.out.println(beignetSold +"\tBeignet\t\t"+beignetSales);
		System.out.println("\tSubtotal\t"+subTotalSales);
		System.out.println("\tSales Tax\t"+totalSalesTax);
		System.out.println("\tTotal\t\t"+totalSales);
	
		System.exit(0);//End the method
	}//close the method
}//close the class



Here's what I've done so far, it's now a loop allowing you to calculate another transaction:
/* This is a program that will take customers input and create a receipt */

   import javax.swing.JOptionPane; 
    public class FINALpossibilitiesRETRY
   {
       public static void main(String args[])
      { 
      // Declare variables
         String coffeeSoldString;
         String hurricaneSoldString;
         String beignetSoldString;
         double coffeePrice = 3.95;
         double hurricanePrice = 8.95;
         double beignetPrice = 2.95;
         double salesTax = .09;
         int coffeeSold; 
         int hurricaneSold;
         int beignetSold;
         double coffeeSales;
         double hurricaneSales;
         double beignetSales;
         double subTotalSales;
         double totalSalesTax;
         double totalSales; 
      
         String enterTransaction;
         String moreOrders;
      // Prime the loop.
         enterTransaction = JOptionPane.showInputDialog("Welcome to Crescent City Eats Are you ready to order? Enter Y or N");
      
                // Validate input.
         while(enterTransaction.compareTo("Y")!=0&&enterTransaction.compareTo("N")!=0)
         {
            enterTransaction = JOptionPane.showInputDialog("Invalid response. Please enter capital Y or N");
         }
      // Enter loop if they want to play.
         while(enterTransaction.compareTo("Y") == 0)
         {
         
         	// Get input
            coffeeSoldString = JOptionPane.showInputDialog("Enter How Many Chickory Coffee Sold: ");
            hurricaneSoldString = JOptionPane.showInputDialog("Enter How Many Hurricanes Sold: ");
            beignetSoldString = JOptionPane.showInputDialog("Enter How Many Beignets Sold: ");
         
         // Convert input to correct data type
            coffeeSold = Integer.parseInt(coffeeSoldString);
            hurricaneSold = Integer.parseInt(hurricaneSoldString);
            beignetSold = Integer.parseInt(beignetSoldString);
         	
            coffeeSales = coffeeSold * coffeePrice;
            hurricaneSales = hurricaneSold * hurricanePrice;
            beignetSales = beignetSold * beignetPrice;
            subTotalSales = coffeeSales + hurricaneSales + beignetSales;
            totalSalesTax = salesTax * subTotalSales;
            totalSales = subTotalSales + totalSalesTax;
         	
            System.out.println("\tFat Tuesday Eats\n");
            System.out.println(coffeeSold +"\tChicory Coffee\t"+coffeeSales);
            System.out.println(hurricaneSold +"\tHurricane\t"+hurricaneSales);
            System.out.println(beignetSold +"\tBeignet\t\t"+beignetSales);
            System.out.println("\tSubtotal\t"+subTotalSales);
            System.out.println("\tSales Tax\t"+totalSalesTax);
            System.out.println("\tTotal\t\t"+totalSales);
         
         	
            enterTransaction = JOptionPane.showInputDialog("Would you like to make another? Y or N");
         	
            while(enterTransaction.compareTo("Y")!=0&&enterTransaction.compareTo("N")!=0)
            {
               enterTransaction = JOptionPane.showInputDialog("Invalid response. Please enter capital Y or N");
            }
            if(enterTransaction.compareTo("Y")==0)
            {
               enterTransaction ="Y";
            }
           	
            if(enterTransaction.compareTo("N") == 0)      
            {
               System.out.println("Thanks for shopping.");
            }	              
          	
         
         } // End of while loop.
         System.exit(0);
      } // End of main() method.
   
   } // End of GuessNumber class.


I'm really not sure what he's going to ask us to do. If you have any guesses I would surely appreciate it.
I know we're going to have "if, if else, etc" and "do, do while, etc" that will be incorporated...I just dont completely know where. I know I'll need a counter to sum all of it up for daily total sales, but I dont know where to insert it

Forgive my lack of Java knowledge, it's the very first java class and balancing school and full-time work has been difficult.

Thanks in advance!

Is This A Good Question/Topic? 0

Replies To: Tips on creating decision statements and loops on this code for a rece

#2 Luckless  Icon User is offline

  • </luck>
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,141
  • Joined: 31-August 09

Re: Tips on creating decision statements and loops on this code for a rece

Posted 09 May 2010 - 06:43 AM

This isn't very much to go on, but as far as a counter, that is really easy. There are many ways to go about it, but you will want to make a double variable, then you can either store each sale in an array, then create a for loop at the end of the program to sum all the sales to the double variable or you can just add the sales to the double variable right after they are "sold"
Was This Post Helpful? 0
  • +
  • -

#3 Guest_Bloc*


Reputation:

Re: Tips on creating decision statements and loops on this code for a rece

Posted 09 May 2010 - 08:05 AM

Thank you for the tips. I'm sorry I don't have much more to go on...as I don't know exactly what he wants the program to do.

I guess I should put it this way, if you were the teacher what would you have us do to this code after being taught the basic decision statements and loops? He said we're going to turn this receipt in to something a business would use.
Was This Post Helpful? 0

Page 1 of 1