1

I have searched so long to clear my console in java after I have finished with a 'customer'. What I'm talking about is I made code where I can have someone 'buy' something using my java code, but I can't clear the console or start my scanners again. After they are done using it.
My code is here:

package restaurantmanagement;

import static java.lang.System.in;
import static java.lang.System.out;
import java.util.Scanner;

public class RestaurantManagement {
    public static void main(String[] args) {
        Scanner sc = new Scanner(in);
        double beefsandwhichcombo = 9.00;
        double chickensandwhichcombo = 10.00;
        double frenchfries = 2.99;
        double eggsandwhich = 6.99;
        double sDrink = 1.50;
        double mDrink = 2.50;
        double lDrink = 3.50;
        out.println("Our menu:");
        out.println("1. Beef Sandwhich Combo - " + beefsandwhichcombo);
        out.println("2. Chicken Sandwhich Combo - " + chickensandwhichcombo);
        out.println("3. French Fries - " + frenchfries);
        out.println("4. Egg Sandwhich - " + eggsandwhich);
        out.println("5. Small Drink - " + sDrink);
        out.println("6. Medium Drink - " + mDrink);
        out.println("7. Large Drink - " + lDrink);
        double Total = 0;
        String whatbuy;
        out.println("What do you want to buy?");
        whatbuy = sc.nextLine();
        if (null != whatbuy) switch (whatbuy) {
            case "1":
                Total = Total + beefsandwhichcombo;
                break;
            case "2":
                Total = Total + chickensandwhichcombo;
                break;
            case "3":
                Total = Total + frenchfries;
                break;
            case "4":
                Total = Total + eggsandwhich;
                break;
            case "5":
                Total = Total + sDrink;
                break;
            case "6":
                Total = Total + mDrink;
                break;
            case "7":
                Total = Total + lDrink;
                break;
            case "No":
                Total = Total;
                break;
            default:
                break;
        }
        String anyelse;
        out.println("Do you want anything else?");
        anyelse = sc.nextLine();
        if (null != anyelse) switch (anyelse) {
            case "1":
                Total = Total + beefsandwhichcombo;
                break;
            case "2":
                Total = Total + chickensandwhichcombo;
                break;
            case "3":
                Total = Total + frenchfries;
                break;
            case "4":
                Total = Total + eggsandwhich;
                break;
            case "5":
                Total = Total + sDrink;
                break;
            case "6":
                Total = Total + mDrink;
                break;
            case "7":
                Total = Total + lDrink;
                break;
            case "No":
                Total = Total;
                break;
            default:
                break;
        }
        String anyelse1;
        out.println("Do you want anything else?");
        anyelse1 = sc.nextLine();
        if (null != anyelse1) switch (anyelse1) {
            case "1":
                Total = Total + beefsandwhichcombo;
                break;
            case "2":
                Total = Total + chickensandwhichcombo;
                break;
            case "3":
                Total = Total + frenchfries;
                break;
            case "4":
                Total = Total + eggsandwhich;
                break;
            case "5":
                Total = Total + sDrink;
                break;
            case "6":
                Total = Total + mDrink;
                break;
            case "7":
                Total = Total + lDrink;
                break;
            case "No":
                Total = Total;
                 break;
            default:
                 break;
        }
        String coupon;
        out.println("Do you have a coupon? Y/N");
        coupon = sc.nextLine();
        if (null != coupon) switch (coupon) {
            case "Y":
                Total = Total / 1.5;
                break;
            case "N":
                Total = Total;
                break;
        }
        double tax = 1.24;
        Total = Total * tax;
        double Totald1 = Total;
        double w1 = Totald1;
        double m1 = w1;
        double y1 = m1;
        out.println("Your total : $" + Total);
        Total = 0;
    }

}

And what I'm getting:

Our menu:
1. Beef Sandwhich Combo - 9.0
2. Chicken Sandwhich Combo - 10.0
3. French Fries - 2.99
4. Egg Sandwhich - 6.99
5. Small Drink - 1.5
6. Medium Drink - 2.5
7. Large Drink - 3.5
What do you want to buy?
6
Do you want anything else?
2
Do you want anything else?
3
Do you have a coupon? Y/N
Y
Your total : $12.805066666666667

But what I want is so that if I finish this, my java code will see that it said Your total and will keep it up there for a few seconds, and then reset. Any idea how to do this?
EDIT:
Also, to hit 2 birds with one stone, any idea how to make java test for if a double has more than 2 decimal places and then make it round to the second one, for example, 13.424 would become 13.42.
BTW, I'm using NetBeans and Eclipse.

BlazeDaBlur2
  • 43
  • 1
  • 12
  • @Carcigenicate I had already looked there, no luck for what I'm trying to do. – BlazeDaBlur2 Aug 22 '17 at 21:57
  • And you can't necessarily round a double to a certain number of places. You can however have a string representation of a number rounded to a certain number of places. – Carcigenicate Aug 22 '17 at 21:57
  • How doesn't it answer your question? – Carcigenicate Aug 22 '17 at 21:58
  • @Carcigenicate It doesn't talk about **after something happens** like after the `Your total` it will delete that stuff and next person comes along and it starts again. – BlazeDaBlur2 Aug 22 '17 at 21:59
  • 1
    When it happens is an unimportant detail to *how* it's done. Just run the "cls" command (from the first answer) after something happens, when you want to clear the screen. In this case, you could just use `Thread.sleep` after the total is shown, then run the "cls" command. The `sleep` method just adds a delay for a certain number of milliseconds. – Carcigenicate Aug 22 '17 at 22:01
  • And for your second question, read https://stackoverflow.com/questions/153724/how-to-round-a-number-to-n-decimal-places-in-java – Carcigenicate Aug 22 '17 at 22:07
  • I can just print a lot of whitespace to the screen. – BlazeDaBlur2 Mar 20 '18 at 12:05

4 Answers4

1

If you use linux, you can do that command Runtime.getRuntime().exec("clear");

And if you want set Timer. Read about Timer and TimerTask classes

1

Using Runtime#exec on Windows will not work, as it creates a seperate process, outside of your terminal.
It will do absolutely nothing.

If your terminal supports ANSI sequences, you can simply print this String: "\u001Bc".
Most terminals will recognize this sequence, and clear itself.

mid
  • 421
  • 5
  • 20
1

You can use this maybe:

System.out.println("\u000c");
Pronoy999
  • 645
  • 6
  • 25
1

You can use this for altering to 2 decimal numbers.

DecimalFormat df = new DecimalFormat("##.##");
total = Double.parseDouble(df.format(total));
Mandeep Singh
  • 91
  • 1
  • 1
  • 8