8 Replies - 832 Views - Last Post: 03 July 2012 - 11:51 AM Rate Topic: -----

#1 r19ecua  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 6
  • Joined: 02-July 12

Online Ordering System assignment

Posted 02 July 2012 - 10:03 PM

Hello everyone! I'm new here and I'll do my best to explain my assignment as well as show you what I've done!

Assignment: Online ordering System
Now it has been suggested that I use ArrayList, but unfortunately that's not an option as I haven't gotten to it yet :/ It seems like I have little choice, so I'll just jump ahead (my qualm with this is that it's 200 pages after Arrays).

import java.util.Scanner; 
import java.util.*;
public class OnlineOrderingSystem {
    
    public static void main(String[] args) {
    	
    	String product[] = { "PD1", "PD2","PD3","PD4","PD5","PD6","PD7","PD8","PD9","PD10"};
    	double price[] = {5.99,10.99,32.99,3.99,54.99,64.99,29.99,19.99,23.99,84.99};
    	int stock[] = {32,28,10,104,95,14,2,0,14,3};
    	Scanner kb = new Scanner(System.in);
    	int i,p,number;
    	String storage[] = new String[10];
    	boolean found = false;
    	String answer;
    	
    	System.out.println("Welcome to the Online Ordering System!\nTake a look at our product!");
    	for(String s : product)
    		System.out.print(s + " ");
    		
    	for(i = 0; i<storage.length;i++){
    	
    		System.out.println("\n\nWhich product would you like to order?");
    		storage[i] = kb.next();
    			for(p = 0; p < product.length - 1; p++) {
    				if(product[p].toLowerCase().equals(storage[i]))
    				found = true;
    				break;
    			}
    		
    		if(found) 
    		
    			System.out.println("We carry it!");
    	
    		
    		System.out.print("How many do you want?");
    		number = kb.nextInt();
    		for(number : )
    	}
    }
   }


Now I know what this code says: if storage[index] = product[index] then found = true. This doesn't help. After realizing that if I type anything other then PD1, I'll always get a false. Why isn't product incrementing >< What I want to do is get the arrays to compare to each other. I just can't seem to get that to happen.

Here's what I tried:
	System.out.println("\n\nWhich product would you like to order?");
    		storage[i] = kb.next();
    		for(storage[i] : product){
    			if(storage[i] == product){
    				found = true;
    				break;
    			}
    		}
    		if(found)
    			System.out.println("We carry it!");
    			else
    				System.out.println("We do not carry that item, sorry!");
    				System.exit(0); 	//temporary exit point NOT PERMANENT
    	}


This doesn't work because the storage array became a problem (please understand it's late and I've been at this for hours so hopefully after a nights rest things will come to me).
The code I was trying to mimic came from my text which was;
for(int x : nums) {
if(x == val){
found = truel
break;
}
}
if (found)
System.out.println("Value found!");



OK these are my goals. To get the program to verify that the product is carried, in stock and how many the customer wants. As I type this I'm not even sure why I wrote this code like this, I think it's best if I create sub-classes. What I really want to know though at this moment is why I can't compare the storage array and the product array.

Is This A Good Question/Topic? 1
  • +

Replies To: Online Ordering System assignment

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 3872
  • View blog
  • Posts: 11,405
  • Joined: 18-April 07

Re: Online Ordering System assignment

Posted 02 July 2012 - 10:39 PM

Lets take a look at this piece of code...


for(p = 0; p < product.length - 1; p++) {
   if(product[p].toLowerCase().equals(storage[i]))
      found = true;
      break;
}



In this loop what do you think happens if the if statement fails? Do you think it goes to the next product by incrementing "p" and going through the if statement again?

Nope. It compares the first one (PD1) with storage[i] and if they match it returns true and breaks the for loop.

But lets say we have "PD2". It won't match the first item so "found" will not be set to true. It also won't go around again because.... the break statement is not part of the if statement! Since you didn't use curly braces in your if, the if statement turns into a one line if statement. It will only consider the "found = true" line as part of the if. The break is thus part of the for loop. So no matter the outcome of the if statement, the break statement will always be executed after the first iteration and terminates the loop.

I don't know why new people to the language always do these one line if statements and one line loops. I say you should get in the habit of always using opening and closing curly braces so you can control what goes in the if statement or while loop.

Here is a more correct version...


for(p = 0; p < product.length - 1; p++) {
   if(product[p].toLowerCase().equals(storage[i])) {
      found = true;
      break;
   }
}



Now with the code above, the break is part of the if statement. It will only break the loop if the product is found. Otherwise it will loop through the entire list.

This should solve your problem of finding the product in the list of products.

:)

This post has been edited by Martyr2: 02 July 2012 - 10:41 PM

Was This Post Helpful? 1
  • +
  • -

#3 r19ecua  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 6
  • Joined: 02-July 12

Re: Online Ordering System assignment

Posted 03 July 2012 - 05:31 AM

Thanks for that, your suggestion worked. After going over the code however, I've determined that this was not the best way to approach this project. Could you offer some suggestion? I can re-write this program into 2 classes but my main issue is relating the arrays to each other. After the loop, say PD4 or index 3 was selected; how can I say that since index 3 was chosen, all the other index 3's are related to it as well. So PD4 has an inventory and a price attached with it, I want to pull all this information at once. If you tell me to read my book, kindly tell me what section :) As I post this, I'm just going to jump ahead and read up on ArrayLists since that's what was first suggested.
Was This Post Helpful? 0
  • +
  • -

#4 r19ecua  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 6
  • Joined: 02-July 12

Re: Online Ordering System assignment

Posted 03 July 2012 - 08:10 AM

OK gang, so here's my update!

String product[] = { "PD1", "PD2","PD3","PD4","PD5","PD6","PD7","PD8","PD9","PD10"};
	        double price[] = {5.99,10.99,32.99,3.99,54.99,64.99,29.99,19.99,23.99,84.99};
	        int stock[] = {32,28,10,104,95,14,2,0,14,3};
	        Scanner kb = new Scanner(System.in);
	        int i,p,number,quantity;
 	        String storage[] = new String[10];
 	        boolean found = false;
 	        String answer;
 	         
 	        System.out.println("Welcome to the Online Ordering System!\nTake a look at our product!");
 	        for(String s : product)
 	            System.out.print(s + " ");
 	             
 	        for(i = 0; i<storage.length;i++){
 	         
 	            System.out.println("\n\nWhich product would you like to order?");
 	            storage[i] = kb.next();
 	                for(p = 0; p < product.length; p++) {
  	                    if(product[p].toLowerCase().equals(storage[i])){
 	                    found = true;
 	                    break;
  	                    }
  	                    
  	                    
 	                }
 	             number = p;
 	             System.out.println(p);
 	            if(found) {
 	             
 	                System.out.println("We carry it!");
 	            
 	            }
 	            	
 	                System.out.println("How many do you want?");
 	                quantity = kb.nextInt();
 	                if(number <= stock[p]) {
 	                
 	                	System.out.println("We have your desired quantity!");
 	                	break;
 	                }      
 	                else
 	                
 	                	System.out.println("we don't have");
 	               
 	             
 	                }
 	        }
 	    }
 	 


So I found a way to link the arrays. But now the stock array doesn't want to play nice and give me the appropriate values.. I found this interesting chapter on mapping, but I fear I'll get confused by skipping to that chapter without reading the chaps. in between.
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9037
  • View blog
  • Posts: 33,518
  • Joined: 27-December 08

Re: Online Ordering System assignment

Posted 03 July 2012 - 08:17 AM

Design a Product class to encapsulate the price, name, and quantity of a single item, then store a Product[]. This is a much cleaner, more Object-Oriented approach than using parallel arrays. And you don't have to worry about "linking" your arrays in this way.

My tutorial Moving Away From Parallel Arrays covers this in more detail.
Was This Post Helpful? 1
  • +
  • -

#6 r19ecua  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 6
  • Joined: 02-July 12

Re: Online Ordering System assignment

Posted 03 July 2012 - 08:27 AM

View Postmacosxnerd101, on 03 July 2012 - 08:17 AM, said:

Design a Product class to encapsulate the price, name, and quantity of a single item, then store a Product[]. This is a much cleaner, more Object-Oriented approach than using parallel arrays. And you don't have to worry about "linking" your arrays in this way.

My tutorial Moving Away From Parallel Arrays covers this in more detail.


I think I love you, I need to digest this.
Was This Post Helpful? 1
  • +
  • -

#7 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9037
  • View blog
  • Posts: 33,518
  • Joined: 27-December 08

Re: Online Ordering System assignment

Posted 03 July 2012 - 08:30 AM

Glad I could help! :)
Was This Post Helpful? 0
  • +
  • -

#8 r19ecua  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 6
  • Joined: 02-July 12

Re: Online Ordering System assignment

Posted 03 July 2012 - 11:45 AM

OK, here's the "mock" program complete. Now, I now plan to break it up and split it into 3 classes to make it easier to read. Hopefully I can finish before I have to submit it. :rolleyes2:


	import java.util.Scanner;
	import java.util.*;
	public class OnlineOrderingSystem {
	     
	    public static void main(String[] args) {
	         																					// parallel array hell
	        String product[] = { "PD1", "PD2","PD3","PD4","PD5","PD6","PD7","PD8","PD9","PD10"};
	        double price[] = {5.99,10.99,32.99,3.99,54.99,64.99,29.99,19.99,23.99,84.99};
	        int stock[] = {32,28,10,104,95,14,2,0,14,3};										
	        String storage[] = new String[10];													// stores the products chosen
	        int Item[] = new int[10];															// item array that stored number of items purchased
	        double subtotal[] = new double[10];													// sub total array
	        
	        Scanner kb = new Scanner(System.in);	
	        int i,p,number,quantity,q,z,zip;													// loop variables and value place holders
	        int counter = 0;																	// counts number of times the i loop iterates

 	        double sum = 0;
 	        double tax;
 	        double snh = 4.50;																	// shipping and handling
 	        boolean found = false;
 	        
 	        String answer,address,city;															
 	         
 	        System.out.println("Welcome to the Online Ordering System!\nTake a look at our product!");
 	        for(String s : product)																// re-read for-each loops, THIS NEEDS A NOTE!
 	            System.out.print(s + " ");
 	             
 	        for(i = 0; i<storage.length; i++){
 	         
 	            System.out.println("\n\nWhich product would you like to order?");
 	            storage[i] = kb.next();
 	                for(p = 0; p < product.length; p++) {
  	                    if(product[p].toLowerCase().equals(storage[i])){						//testing equality between user input and product array
 	                    found = true;
 	                    break;
  	                    }  
 	                }
 	             number = p;																	// LINK between the pre-defined array to user-defined array
 	             
 	            if(found) {
 	             
 	                System.out.println("We carry it!");
 	            
 	            }
 	            	
 	                System.out.println("How many do you want?");
 	                quantity = kb.nextInt();
 	            	
 	                if(quantity <= stock[p]) {											//inventory
 	                
 	                	System.out.println("We have your desired quantity!");
 	           			
 	                }      
 	                else
 	                
 	                	System.out.println("we don't have");
 	                
 	                Item[i] = quantity; 
 	                															//System.out.println("Item[i] = " + Item[i]);
 	                															//System.out.println("Quantity: " + quantity);
 	                															//System.out.println("Price: " + price[p]);
 	                subtotal[i] = Item[i] * price[p];							//use these to test variables
 	                															//System.out.println("Subtotal" + subtotal[i]);
 	                	
 	               	counter++;
 	                	
 	                	
 	             	System.out.println("Would you like to purchase more?");
 	             	answer = kb.next();
 	             	
 	             	if(!answer.toLowerCase().equals("yes")){  
						break;
 	             	}
 	
 	                }
 	                for(z = 0; z < counter; z++){
 	                
 	             	System.out.println("Checkout:  Your cart consists of: " + Item[z] + " " + storage[z] + " at a subtotal of $" + subtotal[z]);
 	             	sum += subtotal[z];
 	             	System.out.println("Your subtotal is: $" + sum);
 	                }
 	             	System.out.println("Please enter your address: ");
 	             		address = kb.nextLine();
 	             	System.out.println("Please enter your city: ");	
 	             		city = kb.next();
 	             	System.out.println("Please enter your zip code: ");
 	             		zip = kb.nextInt();
 	        
 	        			tax = sum * 0.0960;													// average sales tax rate
 	        			sum += tax;
 	        		
 	        		int y;
 	        		
 	        		System.out.println("\n\nThis is your invoice: please print a copy for your records.");
 	        		System.out.println(" ");
 	        		for(y = 0; y < counter; y++){
 	        			System.out.printf("\n" + storage[y] + "\t\t" +Item[y] + "\t$" + subtotal[y]);		
 	        		}
 	        		System.out.println("\nThese items will be shipped to " + "\n" + address + "\n" + city + "\n" + zip + 
 	        							"\nand will arrive in 3 business days.");
 	        		System.out.printf("Total cost: $" + (sum + snh));
 	    }
	}
	
 	   



After I finish that, I plan to recreate this using Macosxnerd101's method. I would've used your non-parallel array technique but I was so close to finishing this that I just had to end it lol..

Thank you all for the help :) I hope you guys criticize me!

I'm trying to adopt a style of programming where I do the sub-class(es) first and THEN the main class, but because I love the pressure I didn't have time to do this as planned.
Was This Post Helpful? 0
  • +
  • -

#9 r19ecua  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 6
  • Joined: 02-July 12

Re: Online Ordering System assignment

Posted 03 July 2012 - 11:51 AM

Oh! and before you bash me on indenting, know this! I properly indented immediately after I posted :sweatdrop:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1