aspose file tools*
The moose likes Beginning Java and the fly likes Understanding selection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Understanding selection" Watch "Understanding selection" New topic
Author

Understanding selection

Chris Caretti
Greenhorn

Joined: Jul 15, 2008
Posts: 5
I am very early into studying Java and using a book called "Java in Two Semesters - 2nd Ed". The early exercises don't have answers and I have been attempting a short programming exercise without success. I do not understand how to build in selection that can be pulled together as a total at the end. The chapter was covering if..else and switch.

Could someone please provide the pseudocode to the following as this might help me work it out?

"Write a program to take an order for a computer system. The basic system costs 375.99. The user then has to choose from a 38cm screen (75.99) or a 43cm screen (99.99). There are also two optional extras DVD/CD Writer (65.99); Printer (125.00). Allow the user to select from these extras and then display the final cost of the order."
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38769
    
  23
Welcome to JavaRanch

We don't simply hand out answers; read this FAQ. It looks a rather awkward example to me, but I think you want an if-else for the printer and if-else for the DVD writer, and you could consider a switch for the screen.
In the switch, how about some sort of error message for the "default."
Chris Caretti
Greenhorn

Joined: Jul 15, 2008
Posts: 5
Thank you for directing me to the Beginners FAQ. Quite right too! I understand that you wish to see whether I have spent some time on this and tried to construct it myself.

Below is approx. my 8th attempt. I certainly don't wish anyone to provide me with the answer, just some clarity on working through this kind of problem.

I have decided that I want to try and make it work with if..else (and will then use switch once I have understood), so I learn to break down the logical steps properly.

I have been attempting as below. But each time I try it, I can't get it to work so that a value is retained and then all added together at the end.


<blockquote>code:
<pre name="code" class="core">package ordersystem;

import java.util.*;

public class OrderSystem {

public static void main(String[] args) {

int screens;
double priceDVD, pricePrinter, price38, price43, subTotal, priceTotal;
char reply;

Scanner sc = new Scanner(System.in);
System.out.println("Choose your screen size (38/43) ");
screens = sc.nextInt();

price38 = 451.98;
price43 = 475.98;


if (screens == 38) {
System.out.println("Thank you, your subtotal is:� " + 451.98);
} else if (screens == 43) {
System.out.println("Thank you, your subtotal is:� " + 475.98);
}

System.out.println("Would you like extras?(y/n)?");
reply = sc.next().charAt(0);

priceDVD = 65.99;
pricePrinter = 125.00;

if (reply == 'y') {
System.out.println("Please select from the following");
System.out.println("[1] DVD at �65.99");
System.out.println("[2] Printer at �125.00");
System.out.println("[3] DVD and Printer at �190.99");

reply = sc.next().charAt(0);

if (reply == '1') {
System.out.println(" ")
}
}

}
}
</pre>
</blockquote>
[edit]Add Code Tags. CR[/edit]
[ July 16, 2008: Message edited by: Campbell Ritchie ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38769
    
  23
It is (as I said before) awkward; I think this is intentional to force you to use both if-elses and switches.

You seem to be making good progress however.

For your latter selection where you have 1 2 3 you might find the Scanner#nextInt method easier to use. After that selection, you are not printing out the total price. But the 1 2 3 format lends itself to a switch.

I suggest you add an else to the bit about screen size for what happens if you don't enter 38 or 43.
You will have problems with Exceptions if you enter something which isn't a number, but you will probably study Exceptions in a few weeks' time.
Chris Caretti
Greenhorn

Joined: Jul 15, 2008
Posts: 5
Thank you for your advice so far, and I have been attempting to understand where I am going wrong. Below is my latest attempt. I am using both if..else and switch, but am not sure if I have set the costs in the best way. Also it does not end the right way on some selections. It is going back to the earlier question on extras.

I guess my two questions here are, how should I be incorporating the else part so the total cost is delivered at the end? And secondly, is there a better way to establish those costs in the first place?



Nathan Leniz
Ranch Hand

Joined: Nov 26, 2006
Posts: 132
A question. Since the user must select a screen size, wouldn't it be easier to ask the questions about the extras only once? Also, instead of defining all the ending values, couldn't you have a working total variable that you alter based on input, then display that total?


The very existence of flamethrowers proves that at some time, some where, some place, someone once said to themselves "I'd really like to set those people on fire over there, but I just can't get close enough".
Chris Caretti
Greenhorn

Joined: Jul 15, 2008
Posts: 5
Yes, your questions mirror my desire exactly. However, my ability so far and lack of understanding have led me to follow the wordy route I've currently arrived at!
Nathan Leniz
Ranch Hand

Joined: Nov 26, 2006
Posts: 132
I'm not saying that my thinking is the best way, not even remotely, but this is how I'd think about solving the problem.
Chris Caretti
Greenhorn

Joined: Jul 15, 2008
Posts: 5
Many thanks for your help. It really helped me think it through more clearly. Below is where I have got to. Do you think this is succinct enough based on the parameters I was required to work within?

One final thing, I have noticed that if the user enters an incorrect choice, then whilst a message can be displayed it doesn't return them to the preceding question? is this because I'm missing something, or the way the if..else is nested?



Nathan Leniz
Ranch Hand

Joined: Nov 26, 2006
Posts: 132
The only thing I can see is you could easily cut down on your println() statements. In both your if/else and switch blocks, no matter what you alter the value with, you still print the user's subtotal.

Ah, forgot to add...

If you are expecting only two inputs and nothing else, you can catch that input before executing through the rest of your program, and repeat the catching until the user enters something valid.

[ July 21, 2008: Message edited by: Nathan Leniz ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Understanding selection