Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: switch statement related exercice

  1. #1
    Junior Member
    Join Date
    Sep 2021
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default switch statement related exercice

    Muvhango runs a small Fruits and Vegetables sphaza shop. He sells oranges, bananas, apples, tomatoes and potatoes. The table 2 below shows the price list for the items sold.

    Code Description Unit price(R)
    A or a Apples 1.50
    B or b Bananas 2.00
    O or o Oranges 2.50
    P or p Potatoes 6.00
    T or t Tomatoes 10.00
    Muvhango gives a 10% discount if the total amount due exceeds R20. Create an application for Muvhango that will allow a customer to select an item to buy, enter the quantity and thereafter be provided with the total amount due. The customer must be given an opportunity to make payment and be given change. Use the switch statement to determine the unit price.

    . here is my code
    package everythingfishyapp;
    import java.util.Scanner;
     
    /**
     *
     * @author SETH
     */
    public class EveryThingFishyApp {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            // declare variables
            char code;
            double dis = 10,totAmtDue,change,price;
            int payment,quantity;
            String choice = "";
            final double PRICE1 = 1.50, PRICE2 = 2.00,PRICE3 = 2.50,PRICE4 = 6.00,PRICE5 = 10.00;
            Scanner sc = new Scanner(System.in);
     
            //get the user input
            System.out.print("Please select an item to buy: " + "\n" +
                             "A/a --> Apples"   + "\n" +
                             "B/b --> Bananas"  + "\n" +
                             "O/o --> Oranges"  + "\n" +
                             "P/p --> Potatoes" + "\n" +
                             "T/t --> Tomatoes" + "\n" +
                             "Your choice: ");
     
     
     
            code = sc.next().charAt(0);
            price = sc.nextDouble();
            quantity = sc.nextInt();
            payment = sc.nextInt();
            totAmtDue = quantity * price;
            change = payment - totAmtDue;
     
     
     
     
             switch(code){
                case'A':
                case'a':
                    System.out.println("the total amount due is: " + totAmtDue);
                    System.out.println("The change is: " + change);;
                    break;
                case'B':
                case'b':
                    System.out.println("the total amount due is: " + totAmtDue);
                    System.out.println("The change is: " + change);
                    break;
                case'O':
                case'o':
                    System.out.println("the total amount due is: " + totAmtDue);
                    System.out.println("The change is: " + change);
                    break;
                case'P':
                case'p':
                    System.out.println("the total amount due is: " + totAmtDue);
                    System.out.println("The change is: " + change);
                    break;
                case'T':
                case't':
                    System.out.println("the total amount due is: " + totAmtDue);
                    System.out.println("The change is: " + change);
                    break;
                default:
                    System.out.println("You entered an invalid code");
            }
            //display outcome 
            System.out.println(choice);
            System.out.println("Please enter a quantity: ");
            System.out.println("Please make a payment: ");
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
        }
    }

    here is the output
    Please select an item to buy: 
    A/a --> Apples
    B/b --> Bananas
    O/o --> Oranges
    P/p --> Potatoes
    T/t --> Tomatoes
    Your choice: a
     
     
     
     
    b
    Exception in thread "main" java.util.InputMismatchException
    	at java.util.Scanner.throwFor(Scanner.java:864)
    	at java.util.Scanner.next(Scanner.java:1485)
    	at java.util.Scanner.nextDouble(Scanner.java:2413)
    	at everythingfishyapp.EveryThingFishyApp.main(EveryThingFishyApp.java:39)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 32 seconds)

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: switch statement related exercice

    Your problem is probably because of how the Scanner class works. There is a problem with calling nextLine after calling other next methods. Read this for an explanation: https://www.geeksforgeeks.org/why-is...ext-functions/
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2021
    Posts
    15
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: switch statement related exercice

    Quote Originally Posted by Seth Lau.20 View Post
    Muvhango runs a small Fruits and Vegetables sphaza shop. He sells oranges, bananas, apples, tomatoes and potatoes. The table 2 below shows the price list for the items sold.

    Code Description Unit price(R)
    A or a Apples 1.50
    B or b Bananas 2.00
    O or o Oranges 2.50
    P or p Potatoes 6.00
    T or t Tomatoes 10.00
    Muvhango gives a 10% discount if the total amount due exceeds R20. Create an application for Muvhango that will allow a customer to select an item to buy, enter the quantity and thereafter be provided with the total amount due. The customer must be given an opportunity to make payment and be given change. Use the switch statement to determine the unit price.

    . here is my code
    package everythingfishyapp;
    import java.util.Scanner;
     
    /**
     *
     * @author SETH
     */
    public class EveryThingFishyApp {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            // declare variables
            char code;
            double dis = 10,totAmtDue,change,price;
            int payment,quantity;
            String choice = "";
            final double PRICE1 = 1.50, PRICE2 = 2.00,PRICE3 = 2.50,PRICE4 = 6.00,PRICE5 = 10.00;
            Scanner sc = new Scanner(System.in);
     
            //get the user input
            System.out.print("Please select an item to buy: " + "\n" +
                             "A/a --> Apples"   + "\n" +
                             "B/b --> Bananas"  + "\n" +
                             "O/o --> Oranges"  + "\n" +
                             "P/p --> Potatoes" + "\n" +
                             "T/t --> Tomatoes" + "\n" +
                             "Your choice: ");
     
     
     
            code = sc.next().charAt(0);
            price = sc.nextDouble();
            quantity = sc.nextInt();
            payment = sc.nextInt();
            totAmtDue = quantity * price;
            change = payment - totAmtDue;
     
     
     
     
             switch(code){
                case'A':
                case'a':
                    System.out.println("the total amount due is: " + totAmtDue);
                    System.out.println("The change is: " + change);;
                    break;
                case'B':
                case'b':
                    System.out.println("the total amount due is: " + totAmtDue);
                    System.out.println("The change is: " + change);
                    break;
                case'O':
                case'o':
                    System.out.println("the total amount due is: " + totAmtDue);
                    System.out.println("The change is: " + change);
                    break;
                case'P':
                case'p':
                    System.out.println("the total amount due is: " + totAmtDue);
                    System.out.println("The change is: " + change);
                    break;
                case'T':
                case't':
                    System.out.println("the total amount due is: " + totAmtDue);
                    System.out.println("The change is: " + change);
                    break;
                default:
                    System.out.println("You entered an invalid code");
            }
            //display outcome 
            System.out.println(choice);
            System.out.println("Please enter a quantity: ");
            System.out.println("Please make a payment: ");
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
        }
    }

    here is the output
    Please select an item to buy: 
    A/a --> Apples
    B/b --> Bananas
    O/o --> Oranges
    P/p --> Potatoes
    T/t --> Tomatoes
    Your choice: a
     
     
     
     
    b
    Exception in thread "main" java.util.InputMismatchException
    	at java.util.Scanner.throwFor(Scanner.java:864)
    	at java.util.Scanner.next(Scanner.java:1485)
    	at java.util.Scanner.nextDouble(Scanner.java:2413)
    	at everythingfishyapp.EveryThingFishyApp.main(EveryThingFishyApp.java:39)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 32 seconds)

    there is a terms exception handling but i have simplified our code very simple way

    now you can check the result

    i have done with Apple only you should use others; the problem was didn't gave the value of "price " .for specifiq way you have put the price1, price 2 in the different case which was fixed.


     
    package com.company;
     
    import java.util.Scanner;
     
    public class Main {
            double num;
        public static void main(String[] args) {
            char code;
            double dis = 10,totAmtDue,change,price;
            int payment,quantity;
            String choice = "";
            final double PRICE1 = 1.50, PRICE2 = 2.00,PRICE3 = 2.50,PRICE4 = 6.00,PRICE5 = 10.00;
            Scanner sc = new Scanner(System.in);
     
            //get the user input
            System.out.print("Please select an item to buy: " + "\n" +
                    "A/a --> Apples"   + "\n" +
                    "B/b --> Bananas"  + "\n" +
                    "O/o --> Oranges"  + "\n" +
                    "P/p --> Potatoes" + "\n" +
                    "T/t --> Tomatoes" + "\n" +
                    "Your choice: ");
            code = sc.next().charAt(0);
            //price = sc.nextDouble();
            System.out.println("how much you want ?1-9kg maximum");
            quantity = sc.nextInt();
     
            switch(code){
                case 'A':
                case 'a':
                    totAmtDue = quantity * PRICE1;
                    System.out.println("the total amount due is: " + totAmtDue);
                    System.out.println("you have paid");
                    payment = sc.nextInt();
                    change = payment - totAmtDue;
                    System.out.println("The change is: " + change);;
                    break;
     
                default:
                    System.out.println("You entered an invalid code");
            }
     
        }
    }



    solving your problem I got stuck with another implementation here. i couldn't able to pass the value of one method to another method. here is the algorithm of the program.

    if anyone can give me a code to create a purchasing menu option i will be glad.



  4. #4
    Member
    Join Date
    Jun 2022
    Posts
    41
    Thanks
    1
    Thanked 3 Times in 2 Posts

    Default Re: switch statement related exercice

    Here's another way to store the data for easy lookup. There's a product code such as 'A' for apple, which can then be used to look up the details of apples.
    import java.util.HashMap;
    public class Scratchpad {
      public static void main(String[] args) {
     
        class Product{
          public String name;
          public Double price;
          Product(String name, Double price){
            this.name = name;
            this.price = price;
          }
          public String toString(){
            return name + ": $" + price;
          }
        }
     
        // load data
        HashMap<Character, Product> productCodes = new HashMap<Character, Product>();
        productCodes.put('A', new Product("Apples", 1.50));
        productCodes.put('B', new Product("Bananas", 2.50));
        productCodes.put('O', new Product("Oranges", 3.50));
        productCodes.put('P', new Product("Potatoes", 4.50));
        productCodes.put('T', new Product("Tomatoes", 5.50));
     
        // display prices
        System.out.println("Choose product " + productCodes);
     
      } // main
    } // scratchpad

    Quote Originally Posted by output
    Choose product {P=Potatoes: $4.5, A=Apples: $1.5, B=Bananas: $2.5, T=Tomatoes: $5.5, O=Oranges: $3.5}
    Last edited by AngleWyrm; July 15th, 2022 at 10:11 PM.

Similar Threads

  1. Replies: 2
    Last Post: December 10th, 2019, 06:17 AM
  2. How to turn my If statement into a case/switch statement?
    By blobby404 in forum What's Wrong With My Code?
    Replies: 23
    Last Post: June 19th, 2014, 03:11 PM
  3. [SOLVED] A Loop statement and a switch statement issue
    By sternfox in forum Loops & Control Statements
    Replies: 13
    Last Post: March 7th, 2013, 04:19 PM
  4. Replacing an If statement with a Switch statement
    By logi in forum Loops & Control Statements
    Replies: 9
    Last Post: February 4th, 2013, 12:21 AM
  5. help with switch statement
    By robertsbd in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 12th, 2010, 12:52 PM