All pastes #727923 Raw Edit

Unnamed

public text v1 · immutable
·published 2007-10-06 15:13 UTC
rendered paste body

import java.io.*;

class ComputerBuyer {

    public static void main(String args[]) throws IOException {
        double price = 375.99 + 75.99; // base + 38cm monitor
        String answer = "";
        
        if(getAnswer("Would you like a 43cm monitor for £24 more??"))
            price += 24;
        if(getAnswer("Would you like a CD/DVD writer for £65.99?"))
            price += 65.99;
        if(getAnswer("Would you like a printer for £125?"))
            price += 125;
            
        System.out.println("The total price is £" + price);
        
    }
    
    private static boolean getAnswer(String question) throws IOException {
        String response = "";
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        
        // Keep asking over and over until they give a valid response
        do {
            System.out.print(question);
            System.out.print(" [y/n] ");
            response = br.readLine().trim().toLowerCase();
        } while(!response.equals("y") && !response.equals("n"));
            
        if(response.equals("y"))
            return true;
        else
            return false;
    }
    
}