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

1. ## 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.

2. ## Re: Calculator help.

Hello Skinnyskinny,

Welcome to the Java Programming Forums.

Have you started coding any of this yet?

3. ## Re: Calculator help.

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.

4. ## 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();

quantity = scanner2.nextInt();

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("");

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);
}
}
}

}```

5. ## 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 Number of Items You Bought: ");
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. ## 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
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
{
// TODO: print out an error message if choice was not 1, 2, or 3
}
}
}

{
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("***************************************");
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);
// 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)
{
return false;
}
{
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. ## 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.