Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- /**
- * Main class for Price Survey logic testing
- */
- public class priceSurvey {
- public static void main(String[] args) {
- String price;
- Scanner input = new Scanner(System.in);
- System.out.println("Please enter the price: ");
- price = input.nextLine();
- if(price.startsWith("0")) {
- //input 0 as a null placeholder
- System.out.println("0");
- } else if(price.contains(".") && price.length() > 3 && price.endsWith("9")){
- System.out.println(price);
- } else if(price.contains(".") && price.length() > 2) {
- //ensure trailing 9
- price = new StringBuffer(price).insert(price.length(), "9").toString();
- System.out.print(price);
- } else if(price.length() > 3){
- //add decimal point and ensure trailing 9
- price = price.substring(0, 1) + "." + price.substring(1);
- price = new StringBuffer(price).insert(price.length() - 1, "9").toString();
- price = price.substring(0, price.length() - 1);
- System.out.print(price);
- } else if(price.length() > 2){
- //add decimal point and ensure trailing 9
- price = price.substring(0, 1) + "." + price.substring(1);
- price = new StringBuffer(price).insert(price.length(), "9").toString();
- System.out.print(price);
- } else if (price.length() < 3 ) {
- System.out.println("Invalid entry");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement