# Calculator help.

• July 28th, 2009, 08:13 AM
Skinnyskinny
Calculator help.
I am new to java, only been taking class for 1 month and i have to come up with this programme.

A possible run of the programme.

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

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 ener detials for item 1
===================================
Item 1's Description is :Belt
Cost per item is :\$30.55
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
Cost for item 2 is = >>>\$21.50<<<

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

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
Cost for item 2 is = >>>\$125.00<<<

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

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

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.
• July 28th, 2009, 08:38 AM
JavaPF
Re: Calculator help.
Hello Skinnyskinny,

Welcome to the Java Programming Forums.

Have you started coding any of this yet?
• July 28th, 2009, 08:46 AM
Skinnyskinny
Re: Calculator help.
Quote:

Originally Posted by JavaPF
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.
• July 28th, 2009, 10:07 AM
JavaPF
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..

Code :

```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); } } }   }```
• July 28th, 2009, 08:08 PM
Skinnyskinny
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.

================================================== ===============
Code :

```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 } }```
• July 29th, 2009, 12:13 AM
helloworld922
Re: Calculator help.
I sent you a message, but i'm going to post the pseudo code here again:
shopcal class:
Code :

```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:
Code :

```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; } }```
• August 1st, 2009, 12:34 PM
MysticDeath
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.