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 7 of 7

Thread: Calculator help.

  1. #1
    Junior Member
    Join Date
    Jul 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Calculator help.

    I am new to java, only been taking class for 1 month and i have to come up with this programme.
    Can anyone please help me please? it stand 20% and i've been cracking my head for serveral nights.
    Really please any kind soul out there please .

    A possible run of the programme.

    *****Shopping Expenditure Calculator****
    1.Calculate Shopping Expenditure
    2.Display Total Expenditure
    3.Exit
    ****************************************

    Please Enter Your Choice : 1

    Please Enter Number of Items You Bought: 2

    Please ener detials for item 1
    ===================================
    Item 1's Description is :Trousers
    Cost per item is :$56.80
    Please enter quantity bought :1
    Please confirm <Y/N> :n



    Please ener detials for item 1
    ===================================
    Item 1's Description is :Belt
    Cost per item is :$30.55
    Please enter quantity bought :1
    Please confirm <Y/N> :Y
    Cost for item 1 is = >>>$30.55<<<

    Please ener detials for item 2
    ===================================
    Item 1's Description is :Shirts
    Cost per item is :$21.50
    Please enter quantity bought :1
    Please confirm <Y/N> :y
    Cost for item 2 is = >>>$21.50<<<

    *****Shopping Expenditure Calculator****
    1.Calculate Shopping Expenditure
    2.Display Total Expenditure
    3.Exit
    ****************************************

    Please Enter Your Choice : 1

    Please Enter Number of Items You Bought: 1

    Please ener detials for item 3
    ===================================
    Item 1's Description is :Table
    Cost per item is :$125
    Please enter quantity bought :1
    Please confirm <Y/N> :y
    Cost for item 2 is = >>>$125.00<<<


    *****Shopping Expenditure Calculator****
    1.Calculate Shopping Expenditure
    2.Display Total Expenditure
    3.Exit
    ****************************************

    Please Enter Your Choice : 2

    Display Item's Individual Cost & Total Cost
    =============================================
    Cost of item 1 Belt is: $30.55
    Cost of item 1 Shirts is: $21.50
    Cost of item 1 Table is: $125.00

    Total Expenditure is = >>> $177.05<<<

    *****Shopping Expenditure Calculator****
    1.Calculate Shopping Expenditure
    2.Display Total Expenditure
    3.Exit
    ****************************************

    Please Enter Your Choice : 3

    Thank you for using Shopping Expenditure Calculator.
    Bye
    Press any key to continure...


    methods include:
    *atleast one static method other than main()
    *must have int,double,string inputs
    *selection (switch-case or if-else)
    *give error message if theres any wrong entry.
    *minimum array size of 10


    I really really really really appreciate if anyone is able to help me.
    I give my thanks.


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Calculator help.

    Hello Skinnyskinny,

    Welcome to the Java Programming Forums.

    Have you started coding any of this yet?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. #3
    Junior Member
    Join Date
    Jul 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Calculator help.

    Quote Originally Posted by JavaPF View Post
    Hello Skinnyskinny,

    Welcome to the Java Programming Forums.

    Have you started coding any of this yet?

    Ty

    I started coding but the more i do the more dead ends i come to.
    I really am giving up else i wont be turning to you guys.

  4. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Calculator help.

    OK well seeing as this is an assignment, i'm not happy about doing all of this for you. You need to learn!!

    I have put together the basic program for you here. You will need to figure out how to add the info to the arrays & do the calculations etc..

    import java.util.Scanner;
    import java.util.ArrayList;
     
    public class ShoppingCalculator {
     
        /**
         * JavaProgrammingForums.com
         */
        static String description, yesOrNo;
        static double cost;
        static int numberOfItems, quantity;
     
        public static ArrayList<String> array = new ArrayList<String>();
     
        // Setup Scanner class for reading user input
        public static Scanner scanner = new Scanner(System.in);
        public static Scanner scanner2 = new Scanner(System.in);
     
        public static void Calculate() {
     
            System.out.println("Please Enter Number of Items You Bought: ");
            numberOfItems = scanner.nextInt();
     
            for(int a = 0; a < numberOfItems; a++){            
     
                    System.out.println("Please Enter Details For Item " + (a+1));
                    System.out.println("===================================");
     
                    System.out.println("Items Description Is: ");
                    description = scanner2.nextLine();                
     
                    System.out.println("Cost Per Item Is: ");
                    cost = scanner2.nextDouble();
     
                    System.out.println("Please Enter Quantity Bought: ");
                    quantity = scanner2.nextInt();
     
                    System.out.println("Please Confirm (y/n): ");
                    yesOrNo = scanner2.next();
     
                    if(yesOrNo.equals("y")){
                        System.out.println("You entered YES");
                        // Array code here
                    }
                    if(yesOrNo.equals("n")){
                        System.out.println("You entered NO");
                        // Possibly call Calculate method again
                    }
     
                    System.out.println("");
            }
     
        }
     
        public static void Total() {
     
            // Code to workout totals here
            System.out.println("Total");
        }
     
        public static void main(String[] args) {
     
            ShoppingCalculator sc = new ShoppingCalculator();
     
            System.out.println("*****Shopping Expenditure Calculator****");
            System.out.println("1.Calculate Shopping Expenditure");
            System.out.println("2.Display Total Expenditure");
            System.out.println("3.Exit");
            System.out.println("");
            System.out.println("Please Enter Your Choice: ");
     
            while (scanner.hasNext()) {
     
                int selection = scanner.nextInt();
     
                if (selection == 1) {
                    sc.Calculate();
                } else if (selection == 2) {
                    sc.Total();
                } else if (selection == 3) {
                    scanner.close();
                    System.out.println("Thank you for using Shopping Expenditure Calculator. Bye!");
                    System.exit(0);
                }
            }
        }
     
    }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  5. #5
    Junior Member
    Join Date
    Jul 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Calculator help.

    thanks for offering ur help. but there are certain things i do not understand about your codes.
    I have not learn about scanner class,shouldnt input be input.nextInt();?


    This is what i've coded so far on my own. I dont know how to continue with it. Please sorry for wasting your effort before, i really appreciate it.


    ================================================== ===============
    import java.util.*;
    import java.text.*;
    public class shopcal
    {
     static String[] itemDes= new String[12];
     static double[] price= new double[12];
     static DecimalFormat fmt=new DecimalFormat("0.00");
     static Scanner input=new Scanner(System.in).useDelimiter("\r\n") ;
      public static void main(String[] args) throws Exception
      {
     
        char choice = 'x'; // dummy initialization
     
        do
         {
          System.out.println("****Shopping EXpenditure Calculator****");
          System.out.println("1. Calculate Shopping Calculator");
          System.out.println("2. Display Total Expenditure");
          System.out.println("3. Exit");
          System.out.println("***************************************");
          System.out.print("Please Enter Your Choice: ");
          choice = (char)System.in.read();
          System.in.read(); // dummy read: reads CR (carriage return)
          System.in.read(); // dummy read: reads LF (linefeed)
          System.out.print("Please Enter Number of Items You Bought: ");
          choice = (char)System.in.read();
          System.in.read(); // dummy read: reads CR (carriage return)
          System.in.read(); // dummy read: reads LF (linefeed)
          switch (choice)
          {
     
            case '1':   for (int i=0; i<2; i++)
             System.out.println("Please enter details for item 1");
               System.out.println("===============================");
               System.out.print("Item 1's Description is :  ");
               itemDes[1]=input.next();
         System.out.print("Cost per item is: ");
         price[1]=input.nextDouble();
                        break;
                        {
                        switch(select) char select='x';
                        {case 'y': 
                         case 'Y': for (int i=0; i<2; i++)
                          System.out.println("cost of item"+i);
                          break;
                        }
     
          }// end of switch-case
         } while (choice != '3'); //end of do-while loop
      } // end of main method
    }
    }

  6. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Calculator help.

    I sent you a message, but i'm going to post the pseudo code here again:
    shopcal class:
    import java.util.*;
    import java.text.*;
     
     
    public class shopcal
    {
        public static void main (String[] args) throws Exception
        {
            Item[] items = new Item[100];
            DecimalFormat fmt = new DecimalFormat("0.00");
            int numItems = 0;
            while (true)
            {
                // Get the top-level menu choice
                String choice = getMenuChoice();
                if (choice.equals("1"))
                {
                    // Calculate shopping calculator choice
                    // get item description
                    String description = getItemDescription(numItems);
                    // get total item price
                    double price = getItemPrice();
                    // get the quantity of that item
                    int number = getItemQuantity();
                    // Set only if confirmation is recieved
                    if (getConfirmation())
                    {
                        // create a new item and add it
                        items[numItems] = new Item(description, price, number);
                        // TODO: increase the number of items
                    }
     
                }
                else if (choice.equals("2"))
                {
                    // display total expenditure choice
                    System.out.println("Display Item's Individual Cost & Total Cost");
                    System.out.println("=============================================");
                    for (int i = 0; i < numItems; i++)
                    {
                        // TODO: what should be printed out for each item?
                        // use items[i].(attribute goes here) to access the item's attributes
                        // ex: items[i].description gets the item's description
                    }
                }
                else if (choice.equals("3"))
                {
                    // quit choice
                    return;
                }
                else
                {
                    // invalid menu choice
                    // TODO: print out an error message if choice was not 1, 2, or 3
                }
            }
        }
     
        public static String getMenuChoice ()
        {
            Scanner input = new Scanner(System.in);
            System.out.println("****Shopping EXpenditure Calculator****");
            System.out.println("1. Calculate Shopping Calculator");
            System.out.println("2. Display Total Expenditure");
            System.out.println("3. Exit");
            System.out.println("***************************************");
            System.out.print("Please Enter Your Choice: ");
            return input.nextLine();
        }
     
        public static String getItemDescription (int itemNum)
        {
            // here's a completed example of how getting each attribute should look like
            Scanner input = new Scanner(System.in);
            System.out.println("Please ener detials for item " + itemNum);
            System.out.println("==================================");
            System.out.print("Item 1's Description is : ");
            return input.nextLine();
        }
     
        public static double getItemPrice ()
        {
            // TODO: finish this method
            Scanner input = new Scanner(System.in);
            double price = 0;
            // TODO: print out what you want to ask the user
            // TODO: read in from scanner
            // TODO: return the price (as double)
            return price;
        }
     
        public static int getItemQuantity ()
        {
            // TODO: finish this method
            Scanner input = new Scanner(System.in);
            int quantity = 0;
            // TODO: print out what you want to ask the user
            // TODO: read in from scanner
            // TODO: return the quantity (as int)
            return quantity;
        }
     
        public static boolean getConfirmation ()
        {
            // TODO: finish this method
            Scanner input = new Scanner(System.in);
            String read = "";
            // TODO: print out what you want to ask the user
            // TODO: read in from scanner
            // TODO: check to see if y/n, return appropriate true (y) or false (n)
            if (read.equals("y"))
            {
                return false;
            }
            else if (read.equals("n"))
            {
                return false;
            }
            else
            {
                // TODO: handle invalid input
                // throw an exception
                throw new RuntimeException();
            }
        }
    }
    Item class:
    public class Item
    {
        public String description;
        public int    numItems;
        public double unitCost;
     
        public Item (String description, double unitCost, int numItems)
        {
            this.description = description;
            this.unitCost = unitCost;
            this.numItems = numItems;
        }
    }

  7. #7
    Member
    Join Date
    Jul 2009
    Posts
    31
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Calculator help.

    Skinny:
    He declares the Scanner "scanner".
    So we would reffer to it by it's assigned name.
    the method scanner.nextInt(); gets user input.
    So something like this:
    System.out.println("How much are you spending");
    int spent = scanner.nextInt();

    The user would see How much are you spending,
    then input a number.
    What they input would be assigned to the int spent.

Similar Threads

  1. Java Calculator
    By helloworld922 in forum Algorithms & Recursion
    Replies: 7
    Last Post: January 10th, 2011, 06:01 AM
  2. Help codiing calculator
    By FM010 in forum Exceptions
    Replies: 7
    Last Post: June 12th, 2009, 02:25 PM
  3. Calculator application using java
    By fabolous04 in forum Paid Java Projects
    Replies: 4
    Last Post: March 25th, 2009, 11:29 AM
  4. Problem of implementing mathematic logic in Java applet
    By AnithaBabu1 in forum Java Applets
    Replies: 0
    Last Post: August 15th, 2008, 11:42 PM