Question by Jaix: Create a system in Java for a sales record program that computes and prints the discount for a item?
I am new to java but i am sorta stuck! The program need to choose from four objects. Based on the quantity ordered, if the quantity is < 6 no discount, if it is between 6 and 10 inclusive, 5% discount, between 11 and 20 inclusive, 10% discount, above 20, 15% discount. I've virtually finished the program but I have a logic error the discount method keeps printing 0.00. Will appreciate any help pointing out my mistake. Thanks! Here's my code.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package salesrecord
/**
*
* @author Jikes
*/
public class SalesRecord
private double itemPrice
double discount
public SalesRecord( double price )
setItemPrice( price )
public void setItemPrice( double price )
itemPrice = price
public double getItemPrice()
return itemPrice
public double calculateDiscount( int order )
if( order> 20 )
discount = 15 / 100 * itemPrice
else if( order <= 20 && order>= 11 )
discount = ten / 100 * itemPrice
else if( order <= 10 && order>= 6 )
discount = 5 / 10 * itemPrice
else
discount =
return discount
public double calculateAmount()
double amount
amount = itemPrice – discount
return quantity
The principal method code:
/*
* To alter this template, select Tools | Templates
* and open the template in the editor.
*/
package salesrecord
import java.util.Scanner
/**
*
* @author Jikes
*/
public class SalesRecordTest
/**
* @param args the command line arguments
*/
public static void primary(String[] args)
// TODO code application logic right here
int itemNumber =
int quantity
String itemName = “”
double cost =
Scanner input = new Scanner( Method.in )
Program.out.println( “Latest Objects IN STOCK ARE LISTED Beneath:n” )
System.out.println( “Item Numbert Item Name” )
Technique.out.println( “1)tt Dell 15″ Laptopn2)tt HP Mini NoteBookn3)tt Sony VIAO 12″ Tabletn4)tt Apple MacBook Air” )
Program.out.print( “nEnter Preferred Item Amount: ” )
itemNumber = input.nextInt()
if( itemNumber == 1 )
cost = 500
itemName = “Dell Laptop”
Technique.out.println( “You chosen Dell 15″ Laptopn” )
else if( itemNumber == two )
value = 400
itemName = “HP NoteBook”
System.out.println( “You chosen HP Mini NoteBookn” )
else if( itemNumber == three )
cost = 800
itemName = “Sony Tablet”
System.out.println( “You chosen Sony VIAO 12″ Tabletn” )
else if( itemNumber == 4 )
cost = 1300
itemName = “MacBook Air”
Technique.out.println( “You selected Apple MacBook Airn” )
else
Technique.out.println( “Invalid Item Quantity Entered!” )
Method.out.print( “Enter Preferred Quantity: ” )
quantity = input.nextInt()
SalesRecord mySalesRecord = new SalesRecord( cost )
Program.out.println()
System.out.println( “ITEM NUMBERtITEM NAMEtQUANTITYtPRICE ($ )tDISCOUNT ($ )tAMOUNT ($ )n” )
System.out.printf( “%dtt%st%dtt%.2ft%.2ftt%.2fnn”, itemNumber, itemName, quantity, mySalesRecord.getItemPrice(),
mySalesRecord.calculateDiscount( quantity ), mySalesRecord.calculateAmount() )
Program.out.println( “Thank you for Purchasing with us!” )
Finest answer:
Answer by Sayee
Initial, Thanks for the question! It took about 10 min prior to I spotted the mistake!
when employing double
do not do discount = 5 / ten * itemPrice
USE discount = 5. / ten. * itemPrice
adjust all numbers with an additional . in all the calculation lines related to above and it should work fine!
Hope it helps.
Know better? Leave your own answer in the comments!
^ Jump to Top ^

