Basic Computer Ordering – Java
Hey guys,
Well I’ve got another example for you. This example allows for a user to buy a computer, and then select a monitor and then allows them to select the a special promotional item to go with it. The Java coding uses an If…else and Switch statement to get the desired result.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
import java.util.*; //Basic order system with multiple suggestions - upsell
public class ComputerOrder {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner keyboard = new Scanner(System.in);
double total, computer, screen1, screen2, selection, dvd, printer;
computer = 375.99; //cost of computer
screen1 = 75.99; //cost of screen
screen2 = 99.99; //cost of screen
total = 0;
System.out.println("You have ordered the basic model costing £" + computer);
System.out.println("If you would you like a 38cm screen? Press 1: Costing: £" + screen1);
System.out.println("If you woud like 43cm screen? Press 2: Costing: £" + screen2);
selection = keyboard.nextDouble(); //input users selection
if ( selection == 1) // if 1 do this
{
System.out.println("You have selected the 38cm screen");
total = computer + screen1; // work out the cost
}
else
{
if (selection == 2) // work out selection 2
{
System.out.println("You have selected the 43cm");
total = computer + screen2; // cost of selection
}
}
System.out.println("**Extra Items**");
System.out.println("DVD/CD Write - Cost 65.99 - Press A");
System.out.println("Printer - 125.00 - Press B");
dvd = 65.99; //declare cost and dvd
printer = 125.00; // declare printer and cost
char upsell; // used for switch
upsell = keyboard.next().charAt(0);
switch(upsell)
{
case 'A' : total = total + dvd; System.out.println("The total cost is £" + total);
break;
case 'B' : total = total + printer; System.out.println("The total cost is £" + total);
break;
default : System.out.println("Error - We could not find yoru suggestion!");
}
}
}
So as you can see, it first of all asks the user to select which monitor they would like, and then which add-on (printer or dvd player) and then displays the total price. A good programme that actually has some real world application!
Related posts (By Relevance):









