Advertisement
Guest User

priceSurvey

a guest
Apr 28th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.54 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. /**
  4.  * Main class for Price Survey logic testing
  5.  */
  6.  
  7. public class priceSurvey {
  8.  
  9.     public static void main(String[] args) {
  10.  
  11.         String price;
  12.         Scanner input = new Scanner(System.in);
  13.  
  14.         System.out.println("Please enter the price: ");
  15.         price = input.nextLine();
  16.  
  17.         if(price.startsWith("0")) {
  18.             //input 0 as a null placeholder
  19.             System.out.println("0");
  20.  
  21.         } else if(price.contains(".") && price.length() > 3  && price.endsWith("9")){
  22.             System.out.println(price);
  23.  
  24.         } else if(price.contains(".") && price.length() > 2) {
  25.             //ensure trailing 9
  26.             price = new StringBuffer(price).insert(price.length(), "9").toString();
  27.  
  28.             System.out.print(price);
  29.  
  30.         } else if(price.length() > 3){
  31.             //add decimal point and ensure trailing 9
  32.             price = price.substring(0, 1) + "." + price.substring(1);
  33.             price = new StringBuffer(price).insert(price.length() - 1, "9").toString();
  34.             price = price.substring(0, price.length() - 1);
  35.  
  36.             System.out.print(price);
  37.  
  38.         } else if(price.length() > 2){
  39.             //add decimal point and ensure trailing 9
  40.             price = price.substring(0, 1) + "." + price.substring(1);
  41.             price = new StringBuffer(price).insert(price.length(), "9").toString();
  42.  
  43.             System.out.print(price);
  44.  
  45.         } else if (price.length() < 3 ) {
  46.             System.out.println("Invalid entry");
  47.         }
  48.  
  49.  
  50.     }
  51.  
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement