on 03-02-2007 12:33 AM
Hi. I am in a beginner Java Programming class and I am having trouble setting up the following problem:
A mail-order house sells five products whose retail prices are as follows: Product 1, $2.98 product 2, $4.50; product 3, $9.98; product 4, $4.49; product 5, $6.87. Write an application that reads a series of pairs of numbers as follows:
a) product number
b) quantity sold
Your program should use a <i>switch</i> statement to determine the retail price for each product. It should calculate and display the total retail value of all products sold. Use a sentinel-controlled loop to determine when the program should stop looping and display the final results.
Any kind of help would be great!
Thanks,
James
Hi James,
[code]
import java.util.Scanner;
public class Calculator {
private static final String LS = System.getProperty("line.separator", "\r\n");
private static final String SENTINEL = "x";
public static void main(String[] args) {
usage();
Scanner in = new Scanner(System.in);
double sum = 0;
for (; in.hasNextInt();) {
int productNumber = in.nextInt();
double productPrice = getRetailPrice(productNumber);
if (productPrice == -1) {
System.out.println(LS + "Sorry, '" + productNumber
+ "' is not a valid product code. Bye!");
return;
} else if (in.hasNextInt()) {
sum += in.nextInt() * productPrice;
} else {
String invalidQuantity = in.next();
System.out.println(LS + "Sorry, '" + invalidQuantity
+ "' is not a valid quantity. Bye!");
return;
}
}
String sentinel = in.next();
if (SENTINEL.equals(sentinel)) {
System.out.println(LS + "Congratulations! The total retail value of all"
+ " products sold is $"
+ sum + ".");
} else {
System.out.println(LS + "Sorry, '" + sentinel
+ "' doesn't equal '" + SENTINEL + "'. Bye!");
}
}
private static double getRetailPrice(int productNumber) {
switch (productNumber) {
case 1:
return 2.98;
case 2:
return 4.50;
case 3:
return 9.98;
case 4:
return 4.49;
case 5:
return 6.87;
default:
return -1;
}
}
private static void usage() {
System.out.println("Please enter a series of pairs of numbers as follows:");
System.out.println(" product number: 1 to 5");
System.out.println(" quantity sold");
System.out.println("Enter '" + SENTINEL
+ "' to calculate the total retail value of all products sold." + LS);
}
}
[/code]
Enjoy your java programming class!
Kind regards,
Sigiswald
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi everyone,
to the owner of this thread, please <b>respect</b> the fact that this is a forum for professional SAP developers and <b>not</b> a school forum. Please refrain from posting questions that are irrelevant. It clutters up the forum and stops people with real development problems from getting responses from other fellow developers. There are plenty of other forums out there suitable for these types of questions.
To the developers who replied, please do not take this the wrong way but if you answer these questions you encourage such posts and further undemine the value of this forum. I realise you're only trying to help and that is admirable, but there already is a huge problem in some of the forums with posts that add no value whatsoever and make it difficult for developers to find solutions to their problem, as well as discourage people from answering.
Thanks.
Regards,
Dion
Sorry for posting a java question on a java forum. Thanks to everyone who tried to help and reply. I don't see how my post hurt any other post's chance to get a reply, because my post didn't get a reply that actually helped until several weeks after I posted it. It turns out that I figured out the program on my own, shortly after I posted the original post. Sorry for not replying but I have been busy with school and 60 hours of work every week. With my experiences so far, I have found that Java is a easy language to learn.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi James,
I'll continue you further with the switch statement:
I am editing your code:
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in );
double prod1 = 2.98;
double prod2 = 4.50;
double prod3 = 9.98;
double prod4 = 4.49;
double prod5 = 6.87;
int prodnum;
int quansold;
<i>double retprice = 0;</i>
System.out.print( "Enter a product number. ");
prodnum = input.nextInt();
System.out.print( "Enter the quantity sold. ");
quansold = input.nextInt();
<i>switch(prodnum)
{
case 1:
retprice += prod1*quansold;
break;
case 2:
retprice += prod2*quansold;
break;
case 3:
retprice += prod3*quansold;
break;
case 4:
retprice += prod4*quansold;
break;
case 5:
retprice += prod5*quansold;
break;
}
System.out.println("The total retail value is: "+retprice);</i>
}
Hope i have answered your query.
Regards,
Prashil
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Heres what I have so far:
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in );
double prod1 = 2.98;
double prod2 = 4.50;
double prod3 = 9.98;
double prod4 = 4.49;
double prod5 = 6.87;
int prodnum;
int quansold;
System.out.print( "Enter a product number. ");
prodnum = input.nextInt();
System.out.print( "Enter the quantity sold. ");
quansold = input.nextInt();
}
}
I'm having difficulty implementing the switch statement, the textbook I'm using isn't very clear on it. By the way, I'm using Java version jdk 1.5.0_11
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
James,
please go through the following link to know the systex of Switch Statement.
http://www.macs.hw.ac.uk/~pjbk/pathways/cpp1/node105.html
just make the necessary changes and implement the same.
regards
Anil
User | Count |
---|---|
90 | |
10 | |
10 | |
10 | |
7 | |
7 | |
6 | |
5 | |
4 | |
3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.