Java problem with if stement

enigma1234567890
enigma1234567890 used Ask the Experts™
on
I am trying to write a program in Java  but getting stuck on if statement, can I do an if statement with an and statemernt as in the code below

 if customerType = standard and quatity < 20 then netcosts = quantity * 10



Anyone know how to do this please see some of the code.
// declare variables
	
	int quantity;
	int netCost;
	
	System.out.printIn("Please enter your customer type Standard or Special");
	customerType = sc.nextInt();
	System.out.printIn("Please enter the quantity purchased");
	quantity = sc.nextInt();
	
	// compute costs
		if customerType = standard and quatity < 20 
		netCost = quantity * 10;
	
		else if customerType = standard and quantity > 20
		netCosts = (20 * 10.50) + (quantity - 20) * (10.50 - (10.50 * .005);
		
		else if customerType = Special and quantity < 1000 
		netCost = quantity * 9;
		
		else if customerType = Special and quantity > 1000
		netCost = (1000 * 9.00) + (quantity - 1000) * (10.50 - (10.50 * .02);
 
		System.out.printIn("The total cost is £" + netCost);
		
		}
	}
		
		
	
		System.out.printIn("Total costs are £" + netCosts);
		}
	}

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Jaime OlivaresSoftware Architect
Top Expert 2008
Commented:
Correct statement would be:

if (customerType = standard and quatity < 20)
{
     netCost = quantity * 10;
}      
else if (customerType = standard && quantity > 20)
{
     netCosts = (20 * 10.50) + (quantity - 20) * (10.50 - (10.50 * .005);
}            
else if (customerType = Special && quantity < 1000)
{
     netCost = quantity * 9;
}            

// etcetera
Software Architect
Top Expert 2008
Commented:
sorry, first statement is not correct, it should be:

if (customerType = standard && quatity < 20)
{
     netCost = quantity * 10;
}      
else if (customerType = standard && quantity > 20)
{
     netCosts = (20 * 10.50) + (quantity - 20) * (10.50 - (10.50 * .005);
}            
else if (customerType = Special && quantity < 1000)
{
     netCost = quantity * 9;
}            

// etcetera

also notice you evaluate quantity > 20  and in other cases quantity < 20, so what happends when quantity = 20?
I guess you have to use >=  or  <=  somewhere.
Roger NeymanRetired Software Engineer, Author, Teacher
Commented:
There is another error in jaime's code

use "==" instead of "=" for equality checking :

if ( customerType == standard && ... ) etc.

I also recommend putting the (techically unecessary) parentheses in place, to make things ultra clear. It's a good habit and will occasionally save you a heap of trouble.

if ( (customerType == standard) && (quantity < 20) ) {
.    // do the right thing
}


// you could also nest the conditionals
// sometimes this makes your meaning clearer
 
if ( custmerType == standard ) {
    if ( quantity < 20 ) {
        // standard < 20 processing
    }
    else {
        // other standard processing
    }
}
else {
     // non-standard processing.
}

Open in new window

Author

Commented:
great

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial