Issue
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.
Solution
You can use this for altering to 2 decimal numbers.
DecimalFormat df = new DecimalFormat("##.##");
total = Double.parseDouble(df.format(total));
Answered By - Mandeep Singh