import java.util.Calendar;
import java.text.SimpleDateFormat;
import java.util.Scanner;
public class carwip
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int sedan, truck, suv, comm1, comm2, comm3;
float base_salary=3200.45f;
float tax=.08f;
char answer;
Calendar cal = Calendar.getInstance();
int currentMonth = cal.get(Calendar.MONTH) + 1;
int currentYear = cal.get(Calendar.YEAR);
System.out.println("Please enter your name.");
String name=sc.nextLine();
System.out.println("How many sedans have you sold this month?");
sedan=sc.nextInt();
System.out.println("How many trucks have you sold this month?");
truck=sc.nextInt();
System.out.println("How many SUVs have you sold this month?");
suv=sc.nextInt();
do{
if(sedan<=10)
{
comm1=85*sedan;
}
else if(sedan<=20 && sedan>10)
{
comm1=120*sedan;
}
else
{
comm1=180*sedan;
}
if(truck<=10)
{
comm2=100*truck;
}
else if(truck<=20 && truck>10)
{
comm2=120*truck;
}
else
{
comm2=180*truck;
}
if(suv<=10)
{
comm3=100*suv;
}
else if(suv<=20 && suv>10)
{
comm3=150*suv;
}
else
{
comm3=250*suv;
}
System.out.println("How many sedans you have sold this month: " +sedan);
System.out.println("Your commission for the sedan sale is: $" +comm1);
System.out.println("How many trucks you have sold this month: " +truck);
System.out.println("Your commission for the truck sale is: $" +comm2);
System.out.println("How many SUV you have sold this month: " +suv);
System.out.println("Your commission for the SUV sale is: $" +comm3);
System.out.println((name) + ", your gross salary for before deduction is: $" + (base_salary + comm1 + comm2 + comm3));
float gross= base_salary + comm1 + comm2 + comm3;
System.out.println((name) + ", your net salary for(Month/Year) after tax deduction is: $" + (gross - (gross * 0.08)));
System.out.println("Do you want to calculate salary for(Month/Year) another person? Type 'Y' for yes, and 'N' for no.");
answer = sc.next().charAt(0);
System.out.println("Thanks for using this system.");
}while(answer == 'Y' || answer == 'y');
}
}
The output that I get is correct up until the net salary prints out, which comes out to be $3533.213955078125 as an example. I'd like to know how to get it to just be $3533.21, and I've tried NumberFormat, but wasn't sure how that works. I've imported calendar and some variables but how can I correctly put it into my code where it says (Month/Year where Month is the actual month name, not the number)? Lastly, when I input Y/y at the end to loop, it only repeats the previous output and doesn't loop back to the beginning.
import java.text.DecimalFormat
import java.util.Calendar;
import java.text.SimpleDateFormat;
import java.util.Scanner;
import java.text.DecimalFormat;
public class carwip
{
private static DecimalFormat df = new DecimalFormat("0.00");
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int sedan, truck, suv, comm1, comm2, comm3;
float base_salary=3200.45f;
float tax=.08f;
char answer;
Calendar cal = Calendar.getInstance();
int currentMonth = cal.get(Calendar.MONTH) + 1;
int currentYear = cal.get(Calendar.YEAR);
System.out.println("Please enter your name.");
String name=sc.nextLine();
System.out.println("How many sedans have you sold this month?");
sedan=sc.nextInt();
System.out.println("How many trucks have you sold this month?");
truck=sc.nextInt();
System.out.println("How many SUVs have you sold this month?");
suv=sc.nextInt();
do{
if(sedan<=10)
{
comm1=85*sedan;
}
else if(sedan<=20 && sedan>10)
{
comm1=120*sedan;
}
else
{
comm1=180*sedan;
}
if(truck<=10)
{
comm2=100*truck;
}
else if(truck<=20 && truck>10)
{
comm2=120*truck;
}
else
{
comm2=180*truck;
}
if(suv<=10)
{
comm3=100*suv;
}
else if(suv<=20 && suv>10)
{
comm3=150*suv;
}
else
{
comm3=250*suv;
}
System.out.println("How many sedans you have sold this month: " +sedan);
System.out.println("Your commission for the sedan sale is: $" +comm1);
System.out.println("How many trucks you have sold this month: " +truck);
System.out.println("Your commission for the truck sale is: $" +comm2);
System.out.println("How many SUV you have sold this month: " +suv);
System.out.println("Your commission for the SUV sale is: $" +comm3);
System.out.println((name) + ", your gross salary for before deduction is: $" + df.format((base_salary + comm1 + comm2 + comm3)));
float gross= base_salary + comm1 + comm2 + comm3;
System.out.println((name) + ", your net salary for(Month/Year) after tax deduction is: $" + df.format((gross - (gross * 0.08))));
System.out.println("Do you want to calculate salary for(Month/Year) another person? Type 'Y' for yes, and 'N' for no.");
answer = sc.next().charAt(0);
System.out.println("Thanks for using this system.");
}while(answer == 'Y' || answer == 'y');
}
}
You can use BigDecimal
:
public static void main(String[] args) {
BigDecimal bd = new BigDecimal("123456789.0123456890");
bd = bd.setScale(2, BigDecimal.ROUND_DOWN);
System.out.println(bd);
}
Result
run: 123456789.01 BUILD SUCCESSFUL (total time: 1 second)
While others have answered the part about the two decimals, here's the way to print the current month with month name.
DateTimeFormatter monthFormatter = DateTimeFormatter.ofPattern("MMMM/uuuu", Locale.ENGLISH);
YearMonth thisMonth = YearMonth.now(ZoneId.of("Australia/Hobart"));
String formattedMonth = thisMonth.format(monthFormatter);
System.out.println("How many sedans have you sold in " + formattedMonth + "?");
When I ran the code today, the output was:
How many sedans have you sold in March/2020?
(I hope they were not many, the world needs us to drive less.) Please insert your desired time zone where I put Australia/Hobart since the month doesn't change at the same time in all time zones.
In the code MMMM/uuuu
is a format pattern string . MMMM
means that we want full month name (no abbreviation) and uuuu
is the year (in the case of the current year you can use uuuu
and yyyy
interchangeably). And Locale.ENGLISH
is the language in which we want the month name. Java knows the month names in about any language.
I am using and recommending java.time, the modern Java date and time API. The Calendar
class that you used and even more the SimpleDateFormat
class that you imported are poorly designed and long outdated. There's no reason why we should struggle wth those in 2020.
Lastly, when I input Y/y at the end to loop, it only repeats the previous output and doesn't loop back to the beginning.
You need to put the 8 lines from System.out.println("Please enter your name.");
inside your loop, that is, after the line do{
. Once you've tried it, I think that you will understand why it fixes the problem.
Oracle tutorial: Date Time explaining how to use java.time.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.