grahil24 grahil24 - 5 months ago 7
Java Question

New to Java and having issues writing and example

The book I am working from is called 'Java in 2 semesters- third addition' On chapter 3 I am doing an example (number 5) which I cannot make work so my understanding and logic must be all wrong!

The question wants me to write a program to take an order for a computer system. the basic system is for example £300. The user then has to choose from two different monitors at 2 separate prices. Then the user is prompted for optional extras say a dvd at £65.99 or a printer at £125. the user needs to select from these options then the program will display the total amount of the overall order.

I have tried writing this using nested if else.. and using switch. I am having issues with the selection of the multiple items storing the calculations and the final calculation of ALL items selected.

I split the program in two using price1 then price 2 but it will not compile as i have variables that need initializing. I understand why but have no thoughts on a better solution or things to try :(

I am not looking at any giving me the answer straight away in code I want to understand the logic and program flow so I can learn from what I am clearly doing wrong!!

This is a basic example and nothing fancy as it is early in the book and has only just gone through keyboard inputs if else.. case and switch so one of these must be the solution if I can think this through correctly.

Thanks

--- Code ---

import java.util.*;

public class orderComputer
{

public static void main(String[] args)
{
double baseunit = 375.99;
double screen1 = 75.99;
double screen2 = 99.99;
double dvd = 65.99;
double printer = 125.0;
int total1;
int total2;
double price1;
double price2;

Scanner keyboard = new Scanner(System.in);

System.out.println("************************************************************");
System.out.println("* This is a computer order system *");
System.out.println("* You have already selected the Base Unit at £375.99 *");
System.out.println("* 1. For the 38cm Screen *");
System.out.println("* 2. For the 48cm Sceen £99.99 *");
System.out.println("************************************************************");
total1 = keyboard.nextInt();

switch (total1)
{
case 1: price1 = baseunit + screen1;
System.out.println("The price is currently £" + price1);
break;
case 2: price1 = baseunit + screen2;
System.out.println("The price is currently £" + price1);
break;
default: System.out.println("You did not select a screen!!");
}

System.out.println("************************************************************");
System.out.println("* This is a computer order system *");
System.out.println("* You have already selected the Base Unit at £375.99 *");
System.out.println("* 1. DVD at £65.99 *");
System.out.println("* 2. For a printer £125.00 *");
System.out.println("************************************************************");
total2 = keyboard.nextInt();

switch (total2)
{
case 1: price2 = total1 + dvd;
System.out.println("The price is currently £" + price2);
break;
case 2: price2 = total1 + printer;
System.out.println("The price is currently £" + price2);
break;
default: System.out.println("You did not select a screen!!");
}
}


}

Answer

Regarding the "uninitialized variables":
The simplest fix is to initialize the variables to some value of your choice, e.g.:

double price1 = 0.0;
double price2 = 0.0;

Though your code compiles just fine on my machine (java 1.8.0_91).

But be careful how the values are initialized/changed during program flow. E.g. and invalid input in one of the two switch-statements would cause your program to spit out an incorrect value.

Instead use an approach that removes redundant variables. E.g.: why use multiple variables for summing up the total price? Simply reuse a single variable for all additions:

double price = baseunit; //we know for sure the user selected the base-unit

...

switch(total1){
    case 1:
        price += screen1;
        break;
    ...
}

...

Don't create variables for single-use values. Eliminate the usage of total1 and total2:

double baseunit = 375.99;
double screen1 = 75.99;
double screen2 = 99.99;
double dvd = 65.99;
double printer = 125.0;
double price1;
double price2;

...

switch(keyboard.nextInt()){
     ...
}

...

Simply continuing a program, after the user entered an invalid input won't produce any useful/correct value. Notify him about the fact that the value he entered was useless and afterwards either terminate or ask again:

switch(keyboard.nextInt()){
     ...

     default:
         System.out.println("Invalid input");
         return;     //invalid input was entered, program is terminated
}