Need help in Java!! AGAIN!!

j.kaii

Member
Joined
Sep 7, 2012
Messages
120
Reaction score
0
An electronics shop has announced the following seasonal discounts on the purchase of certain
items.
Purchase Amount Discount on Laptop Discount on Desktop PC
0 – 250 0.0% 5.0%
251 – 570 5.0% 7.6%
571 – 1000 7.5% 10.0%
More than 1000 10.0% 15.0%

Write a program based on the above criteria to input name, address, amount of purchase and
type of purchase (L for Laptop and D for Desktop) by a customer. Compute and print the net
amount to be paid by a customer along with his name and address.
discount = (discount rate/100)* amount of purchase
Net amount = amount of purchase – discount


package Assignment;
import java.util.Scanner;
public class Assignment
{
public static void main(String[] args) throws Exception
{
String name;
String address;
int amount;
char type;
int net = 0;
Scanner keyboard = new Scanner(System.in);

System.out.print("Enter Name : ");
name = keyboard.nextLine();
System.out.print("Enter Address : ");
address = keyboard.nextLine();
System.out.print("Enter Amount of purchase : ");
amount = keyboard.nextInt();
System.out.print("Enter type of purchase : ");
type = (char)System.in.read();
System.in.read();

while(type != 'D' && type!= 'L')
{
System.out.println("Invaild type of purchase");
System.out.print("Enter type of purchase : ");
type = (char)System.in.read();
System.in.read();
}
if(amount<=250)
{
if(type == 'D')
{
net = ((95/100)*amount);
System.out.println("net price : " +net);
}
else if(type == 'L')
{
net = amount;
}
}
{
System.out.println("Name : " +name);
System.out.println("Address : " +address);
System.out.println("Net price : " +amount);
System.out.println("Type of purchase : " +type);

}
}
}

output
run:
Enter Name : ffdhfjf
Enter Address : gstgdgd 435454
Enter Amount of purchase : 200
Enter type of purchase : D
net price : 0
Name : ffdhfjf
Address : gstgdgd 435454
Net price : 200
Type of purchase : D
BUILD SUCCESSFUL (total time: 24 seconds)

Why is my IF part not being able to calculate if my type is D?
 

Swiftbladez

Master Member
Joined
Nov 24, 2006
Messages
4,381
Reaction score
10
It should be an 'or' operator instead.


EDIT: I obviously don't know what I'm saying lol.
 
Last edited:

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,301
An electronics shop has announced the following seasonal discounts on the purchase of certain
items.
Purchase Amount Discount on Laptop Discount on Desktop PC
0 – 250 0.0% 5.0%
251 – 570 5.0% 7.6%
571 – 1000 7.5% 10.0%
More than 1000 10.0% 15.0%

Write a program based on the above criteria to input name, address, amount of purchase and
type of purchase (L for Laptop and D for Desktop) by a customer. Compute and print the net
amount to be paid by a customer along with his name and address.
discount = (discount rate/100)* amount of purchase
Net amount = amount of purchase – discount

ORIGINALLY BADLY FORMATTED CODES

output
run:
Enter Name : ffdhfjf
Enter Address : gstgdgd 435454
Enter Amount of purchase : 200
Enter type of purchase : D
net price : 0
Name : ffdhfjf
Address : gstgdgd 435454
Net price : 200
Type of purchase : D
BUILD SUCCESSFUL (total time: 24 seconds)

Why is my IF part not being able to calculate if my type is D?

Do us a favour and also do yourself a favour at the same time. Please properly format your code for good readability. Your code is already flawed, you don't need to add on the complexity having a human Java syntax parser :)

PHP:
package Assignment;
import java.util.Scanner;
public class Assignment
{
    public static void main(String[] args) throws Exception
    {
        String name;
        String address;
        int amount;
        char type;
        int net = 0;
        Scanner keyboard = new Scanner(System.in);
        
        System.out.print("Enter Name : ");
        name = keyboard.nextLine();
        System.out.print("Enter Address : ");
        address = keyboard.nextLine();
        System.out.print("Enter Amount of purchase : ");
        amount = keyboard.nextInt();
        System.out.print("Enter type of purchase : ");
        type = (char)System.in.read();
        System.in.read();
        
        while(type != 'D' && type!= 'L')
        {
            System.out.println("Invaild type of purchase");
            System.out.print("Enter type of purchase : ");
            type = (char)System.in.read();
            System.in.read();
        }
        if(amount<=250)
        {
            if(type == 'D')
            {
                net = ((95/100)*amount);
                System.out.println("net price : " +net);
            }
            else if(type == 'L')
            {
                net = amount;
            }
        }
        {
            System.out.println("Name : " +name);
            System.out.println("Address : " +address);
            System.out.println("Net price : " +amount);
            System.out.println("Type of purchase : " +type);
            
        }
    }
}
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,301
ykgoh has more or less pointed out your issue.

I will just give you a bit more glimpse into how you can code better
PHP:
package Assignment;
import java.util.Scanner;
public class Assignment
{
    public static void main(String[] args) throws Exception
    {
        String name;
        String address;
        int amount;
        char type;
        int net = 0;
        Scanner keyboard = new Scanner(System.in);
        
        System.out.print("Enter Name : ");
        name = keyboard.nextLine();
        System.out.print("Enter Address : ");
        address = keyboard.nextLine();
        System.out.print("Enter Amount of purchase : ");
        amount = keyboard.nextInt();
        
        //////////////////////////////////////////////////////////////
        // I PURPOSELY ISOLATED THIS FRAGMENT OF CODES TO GUIDE YOU
        // ON BETTER LOGIC LOOPING CONCEPT
        /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
           set input condition to be false, so while loop will enter
           while (input condition false) {
             (1) read input from user
             if (input is true) {
               optionally perform your necessary post processing here
               and break out of the while loop
             }
             // reaching here means while loop condition has to be re-evaluated
           }
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
        System.out.print("Enter type of purchase : ");
        type = (char)System.in.read();
        System.in.read(); // <-- Why is there an extra read in here ?
                          //     Would your user knows the need of an
                          //     extra RETURN keypress ?
        
        while(type != 'D' && type!= 'L')
        {
            System.out.println("Invaild type of purchase");
            System.out.print("Enter type of purchase : ");
            type = (char)System.in.read();
            System.in.read();
        }
        ///////////////////////////////////////////////////////////////
        
        
        if(amount<=250)
        {
            if(type == 'D')
            {
                net = ((95/100)*amount);
                System.out.println("net price : " +net);
            }
            else if(type == 'L') // <-- is there any possibility for type to be
                                 //     other than L and D ?
            /*
            You can take this approach for such cases
            else // if (type == 'L')
            it serves as comment and also save evaluation.
             */
            {
                net = amount;
            }
        }
        // <-- is there something missing here ?
        // Why an extra block for the codes below ?
        {
            System.out.println("Name : " +name);
            System.out.println("Address : " +address);
            System.out.println("Net price : " +amount);
            System.out.println("Type of purchase : " +type);
            
        }
    }
}
 

ykgoh

Master Member
Joined
Jan 1, 2000
Messages
2,782
Reaction score
0
You might want to point out why is it zero ? :)

Source: Beginning Java - Unit 2 Math Operators

Confusing DIVISIONS

Be careful when performing integer division. When dividing an integer by an integer, the answer will be an integer (not rounded).

Compare these divisions: (5 is an integer while 5.0 is a double)

Integer division 8 / 5 = 1
Double division 8.0 / 5.0 = 1.6
Mixed division 8.0 / 5 = 1.6

TS do you see the issue now?
 
Important Forum Advisory Note
This forum is moderated by volunteer moderators who will react only to members' feedback on posts. Moderators are not employees or representatives of HWZ Forums. Forum members and moderators are responsible for their own posts. Please refer to our Community Guidelines and Standards and Terms and Conditions for more information.
Top