I am stuck on homework assignment Commission Calculation - java

I need to compare the total annual sales of at least three people. I need my app to calculate the additional amount that each must achieve to match or exceed the highest earner. I figured out most of it and know how to do it if there were only two people in the scenario, but getting third into the equation is throwing me for a loop! Any help is appreciated and thanks in advance! Here's what I have so far, but obviously at the end where the calculations are not going to be right.
package Commission3;
import java.util.Scanner;
public class MainClass {
public static void main(String[] args) {
// Create a new object AnnualCompensation
Commission3 salesPerson[] = new Commission3[2];
// creat two object
salesPerson[0] = new Commission3();
salesPerson[1] = new Commission3();
salesPerson[2] = new Commission3();
//new scanner input
Scanner keyboard = new Scanner(System.in);
//get salesperson1 name
System.out.println("What is your first salesperson's name?");
salesPerson[0].name = keyboard.nextLine();
//get salesperson1 sales total
System.out.println("Enter annual sales of first salesperson: ");
double val = keyboard.nextDouble();
salesPerson[0].setAnnualSales(val);
//get salesperson2 name
System.out.println("What is your second salesperson's name?");
salesPerson[1].name = keyboard.next();
//get salesperson2 sales total
System.out.println("Enter annual sales of second salesperson: ");
val = keyboard.nextDouble();
salesPerson[1].setAnnualSales(val);
//get salesperson3 name
System.out.println("What is your third salesperson's name?");
salesPerson[2].name = keyboard.next();
//get salesperson3 sales total
System.out.println("Enter annual sales of third salesperson: ");
val = keyboard.nextDouble();
salesPerson[2].setAnnualSales(val);
double total1, total2, total3;
total1 = salesPerson[0].getTotalSales();
System.out.println("Total sales of " + salesPerson[0].name +" is: $" + total1);
total2 = salesPerson[1].getTotalSales();
System.out.println("Total sales of " + salesPerson[1].name +" is: $" + total2);
total3 = salesPerson[2].getTotalSales();
System.out.println("Total sales of " + salesPerson[2].name +" is: $" + total3);
if (total1 > total2) {
System.out.print("Salesperson " + salesPerson[2].name + "'s additional amount
of sales that he must " + " achieve to match or exceed the higher of the
salesperson " + salesPerson[0].name);
System.out.println(" $" + (total1 - total2));
} else if (total2 > total1) {
System.out.print("Salesperson " + salesPerson[0].name + "'s additional amount
of sales that he must " + " achieve to match or exceed the higher of the
salesperson " + salesPerson[1].name);
System.out.println(" $" + (total2 - total1));
} else {
System.out.println("Both have same compensation $" + total1);
}
}
}

When you take the input from the user, keep track of the highest sales thus far, and the name of the salesperson with the most sales.
Then, instead of checking total1 and total2, you can loop through all three, and compare them to the max. If the current total is less than the max, then calculate the difference. Otherwise, the current total is equal to the max, and you don't need to do the calculation.
I'll leave the actual code for you to figure out.

Related

The operator * is undefined for the argument type(s) int, String / Type mismatch: cannot convert from double to int

import java.util.Scanner;
public class Asgn1 {
//comment practice
/*multi-line comment practice
* no text fill
*/
public static void main(String[] args) {
//user prompted inputs for future calculations
Scanner in = new Scanner(System.in);
System.out.println("The following information is required:");
System.out.println("Enter customer ID: ");
String customerId = in.nextLine();
System.out.println("Enter unit price in decimal format (up to two decimals, e.g. 3.5): ");
String unitPrice = in.nextLine();
System.out.println("Enter quantity (whole numbers only): ");
String orderQuantity = in.nextLine();
System.out.println("Enter product description, (e.g. 'whole wheat bread'): ");
String productDescription = in.nextLine();
System.out.println("Enter discount in decimal format (e.g. .05 = 5%): ");
String appliedDiscount = in.nextLine();
//confirm order data details and display to user
System.out.println("Your order data is as follows: ");
System.out.println("Customer ID: " + customerId);
System.out.println("Unit Price: " + unitPrice);
System.out.println("Order Quantity: " + orderQuantity );
System.out.println("Product Description: " + productDescription);
System.out.println("Applied Discount: " + appliedDiscount);
//calculation formulas based on users input
int beforeDiscount = (Integer.parseInt(unitPrice) * Integer.parseInt(orderQuantity));
int afterDiscount = 1 - (Integer.parseInt(unitPrice) * Integer.parseInt(orderQuantity)) * (appliedDiscount);
//totals before and after discount
System.out.println("Your Order Totals");
System.out.println("Before Discount: ");
System.out.println("After Discount: ");
}
}
I have this java code I want to take the unit price and multiply that by the order quantity, then apply the discount so I can display a before and after discount price.
Originally, when I entered this, I figured out I had to parse the strings for unitPrice and orderQuantity as ints, but when I tried that with the double, I got this message as well on the same line: "Type mismatch: cannot convert from double to int".
I tried looking around at other answers but could not find something that would fix this issue so I'm asking for help, please. What would be the best way to solve this?
In the future, should I try to alter it before it comes in, maybe where they input it, or do I wait until I get the values and then alter that? What would convention dictate?
Thank you for your consideration and assistance.
I change some things on the code... first, the type of variables of unit price and appliedDiscount into double. And also I change the formula to calculate price after discount.
public static void main(String[] args) {
//user prompted inputs for future calculations
Scanner in = new Scanner(System.in);
Scanner in2 = new Scanner(System.in);
System.out.println("The following information is required:");
System.out.println("Enter customer ID: ");
String customerId = in.nextLine();
System.out.println("Enter unit price in decimal format (up to two decimals, e.g. 3.5): ");
double unitPrice = in.nextDouble();
System.out.println("Enter quantity (whole numbers only): ");
int orderQuantity = in.nextInt();
System.out.println("Enter product description, (e.g. 'whole wheat bread'): ");
String productDescription = in2.nextLine();
System.out.println("Enter discount in decimal format (e.g. .05 = 5%): ");
double appliedDiscount = in.nextDouble();
//confirm order data details and display to user
System.out.println("Your order data is as follows: ");
System.out.println("Customer ID: " + customerId);
System.out.println("Unit Price: " + unitPrice);
System.out.println("Order Quantity: " + orderQuantity );
System.out.println("Product Description: " + productDescription);
System.out.println("Applied Discount: " + appliedDiscount);
//calculation formulas based on users input
double beforeDiscount = (unitPrice * orderQuantity);
double afterDiscount = beforeDiscount - (beforeDiscount * (appliedDiscount));
//totals before and after discount
System.out.println("Your Order Totals" );
System.out.println("Before Discount: "+ beforeDiscount);
System.out.println("After Discount: " + afterDiscount);
}

Program repeats user input even when an equivalency condition / the first if-statement has been met

I need an instance in my program to work in that if the user inputs a payment number (payment) that is equal to the total price of a checkout with taxation (price3), the program will just list 0.00 as change and not repeat user input as if the user's payment is less than the price. However, when payment equals price3 (payment - price3 == 0), the program goes to the else-if statement. How do I fix this?
Example: price3 == 28, payment == 28, the output after payment input is "You still owe..." and so on instead of "Your change is $0.00".
I think it is skipping the first if-statement in the while loop, but I have no idea why. I already tried moving around the if-statements in the while-loop to no avail.
There are no error messages.
Note: I am still trying to learn Java. Just started recently.
The program code which my question references is displayed below:
import java.util.Scanner;
public class Checkout
{
public static void main(String [] args)
{
Scanner input = new Scanner(System.in);
System.out.println("How many items are you purchasing?");
int count = input.nextInt();
double price1 = 0;
double price2 = 0;
String userItem = "";
for(int i = 1; i<=count; i++)
{
System.out.println("Please enter the name of the item:");
input.nextLine();
userItem = input.nextLine();
System.out.println("Please enter the price of the item:");
price1 = input.nextDouble();
System.out.println();
System.out.printf("Your item #" + i + " is " + userItem + " with a price of $" + "%.2f", price1);
System.out.println();
price2 = price2 + price1;
}
System.out.println();
System.out.printf("Your total amount is: $" + "%.2f", price2);
double price3 = price2 + (price2 * 0.06);
System.out.println();
System.out.printf("Your total amount with tax is: $" + "%.2f", price3);
System.out.println();
System.out.println("I need a payment!");
double payment = input.nextDouble();
boolean condition = false;
while(condition == false)
{
if(payment - price3 == 0)
{
condition = true;
}
else if(payment < price3)
{
System.out.println();
System.out.printf("You still owe " + "%.2f", (price3-payment));
System.out.println();
System.out.println("I need a better payment!");
payment = input.nextDouble();
}
else
{
condition = true;
}
}
double change = payment - price3;
System.out.println();
System.out.printf("Your change is: $" + "%.2f", change);
}
}
The core of your problem lies in (1) expecting exact equality of floating-point value, and (2) displaying quantities to 2 decimal places.
Given the user is told the amount to pay (price3) using 2 places of decimals, even if he enters that exact same value as payment, it may not match the true value of price3.
Ideally you should do all calculation in pennies (or whatever the smaller unit of this currency is). Failing that, your criterion for having paid the right amount should be something like the difference between price and payment is less than 0.01.
In the stated case
Example: price3 == 28, payment == 28, the output after payment input
is "You still owe..." and so on instead of "Your change is $0.00".
if the price before tax is 26.415 it makes the price after tax 27.9999, which displays as 28.00 but is not equal to 28.00. Neither 26.41 nor 26.42 get you to an after-tax displayed price of 28.00.
that is happening because of price3=price2+(price2*0.06). So, when it is comparing payment with price3, it is always less. See below

How do I delimit 2 words with "#" symbol?

I have a task from my university where I should prompt the user for 2 numbers one integer the other decimal and print their product in money format. The program should also take in two words delimited by # symbol. I'm struggling to figure out the last portion of the task (two words delimited by # symbol).
Everything else I understand fine.
This is the exercise
Sample run 1:
Enter a whole number: 4
Enter a decimal number: 6.854
Enter two words delimitated by # symbol: Mango#15
Output:
The product of the 2 numbers: 27.416
The product in money format is: N$ 27.42
Assuming the user bought 4 Mango(s) costing N$ 6.85
The VAT to be charged is 15%, hence total due to be paid is N$ 31.53
This is my code.
import java.util.Scanner;
public class Lab02_Task4 {
public static void main(String[]args){
Scanner info = new Scanner(System.in);
int whole;
System.out.println("Enter a whole number: ");
whole = info.nextInt();
double decimal;
System.out.println("Enter a decimal number: ");
decimal = info.nextDouble();
String item;
System.out.println("Enter two words delimitated by # symbol: ");
item = info.nextLine();
String item2 = "Mango";
double total = whole * decimal;
double vatIncluded = (total * 0.15) + total;
String s=String.valueOf(total);
System.out.println("The product of the 2 numbers: " + total);
String total2 = String.format("%.2f", total);
System.out.println("The product in money format is: N$ " + (total2));
String vatIncluded2 = String.format("%.2f", vatIncluded);
System.out.println("Assuming the user bought " + whole + " " + item2 + "(s) " + "costing N$ " + total2 +
" The VAT to be charged is 15%, hence total due to be paid is N$ " + vatIncluded2);
}
}
You can use split to separate the two values like this:
public static void main(String[] args) {
String string = "value#anothervalue";
String[] arr = string.split("#");
System.out.print(Arrays.toString(arr)); //[value, anothervalue]
}
This will do the trick:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int wholenumber = Integer.parseInt(input.nextLine());
double decimal = Double.parseDouble(input.nextLine());
String[] deli = input.nextLine().split("#");
String item = deli[0];
int tax = Integer.parseInt(deli[1]);
double product = decimal*wholenumber;
NumberFormat formatter = NumberFormat.getCurrencyInstance();
String rounded = formatter.format(product).substring(1);
double finalprice = product*tax/100+product;
System.out.println("The product of the 2 numbers: "+product);
System.out.println("The product in money format is: N$"+rounded);
System.out.println("Assuming the user bought "+wholenumber+" "+item+"s costing N$"+formatter.format(decimal).substring(1));
System.out.println("The VAT to4 be charged is "+tax+"%, hence the total due to the paid is N$"+finalprice);
//The VAT to be charged is 15%, hence total due to be paid is N$ 31.53
}
Sample Run
4
6.854
Mango#15
The product of the 2 numbers: 27.416
The product in money format is: N$27.42
Assuming the user bought 4 Mangos costing N$6.85
The VAT to4 be charged is 15%, hence the total due to the paid is N$31.5284
With user prompts
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a number: ");
int wholenumber = Integer.parseInt(input.nextLine());
System.out.print("Enter a decimal number: ");
double decimal = Double.parseDouble(input.nextLine());
System.out.print("Enter two words seperated by # symbol: ");
String[] deli = input.nextLine().split("#");
String item = deli[0];
int tax = Integer.parseInt(deli[1]);
double product = decimal*wholenumber;
NumberFormat formatter = NumberFormat.getCurrencyInstance();
String rounded = formatter.format(product).substring(1);
double finalprice = product*tax/100+product;
System.out.println("The product of the 2 numbers: "+product);
System.out.println("The product in money format is: N$"+rounded);
System.out.println("Assuming the user bought "+wholenumber+" "+item+"s costing N$"+formatter.format(decimal).substring(1));
System.out.println("The VAT to4 be charged is "+tax+"%, hence the total due to the paid is N$"+finalprice);
//The VAT to be charged is 15%, hence total due to be paid is N$ 31.53
}
Sample Run
Enter a number: 4
Enter a decimal number: 6.854
Enter two words seperated by # symbol: Mango#15
The product of the 2 numbers: 27.416
The product in money format is: N$27.42
Assuming the user bought 4 Mangos costing N$6.85
The VAT to4 be charged is 15%, hence the total due to the paid is N$31.5284

while and for loops

How can I add up 3 inputs from the scanner using one variable, and while & for loop only (no array)?CLICK THIS LINK TO SEE IMAGE INCLUDING INSTRUCTIONS
HERE IS THE CODE THE NEEDE TO COMPLETE THE TASK IN THE IMAGE.
import java.util.Scanner;
public class Ass1b
{
public static void main (String[]args)
{
String taxPayerName;
int totalInc;
double totalTax;
Scanner inText = new Scanner(System.in);
System.out.print("Please enter the name of the tax payer==> ");
taxPayerName = inText.nextLine();
Scanner inNumber = new Scanner(System.in);
System.out.print("Enter the income for "+ taxPayerName +" ==> " );
totalInc = inNumber.nextInt();
if (totalInc < 18200)
{
totalTax = 0;
System.out.print("The tax that " + taxPayerName + " has to pay is $"+ totalTax);
}
else if(totalInc < 37000)
{
totalTax=((totalInc - 18200)* 0.19);
System.out.print("The tax that " + taxPayerName + " has to pay is $"+ totalTax);
}
else if(totalInc < 87000)
{
totalTax=(3572 +(totalInc - 37000)* 0.325);
System.out.print("The tax that " + taxPayerName + " has to pay is $"+ totalTax);
}
else if(totalInc < 180000)
{
totalTax=(19822 +(totalInc - 87000)* 0.37);
System.out.print("The tax that " + taxPayerName + " has to pay is $"+ totalTax);
}
else
{
totalTax = (54232 + (totalInc - 180000)*0.47);
System.out.print("The tax that " + taxPayerName + " has to pay is $"+ totalTax);
}
}
}
So the main questions seems to be: How can I add up 3 inputs from the scanner?
One scanner can be used multiple times, for example:
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < 5; i++){
System.out.println("value : " + scanner.nextInt());
}
}
This code would a for loop and one scanner object to ask the user five times for a integer. With this you should be able to complete the exercise. Now you just need to have an additional variable which keeps track of the total tax and then finally calculate the average tax.
In the future try to ask a more specific question, instead of just asking for the answer, because it seems this is a homework assignment. So I tried to help you out with general code, which doesn't answer the question directly.

Creating trip cost calulator

I am working on an assignment for school and I have a basic program already that calculates the total cost of a trip by asking the total mileage, average mpg of car, and cost per gallon of gas. This works great but I also need to add a few more items and I am unsure of how to do so. First I need to include a menu of some sort giving the option to either calculate the trip cost or exit the program. Next I need to incorporate somewhere in the program a bit asking if the user wants an oil change or not and then based on the answer adding this to the total.
import java.util.Scanner;
public class GasCalculator {
public static void main (String args[])
{
Scanner scan = new Scanner(System.in);
System.out.println("How many miles do you plan to travel?");
int miles = scan.nextInt();
System.out.println("So you will drive " + miles +" miles.");
System.out.println("What is the price of gas?");
double gas = scan.nextDouble();
System.out.println("Ok, so gas costs $" + gas +" per gallon.");
System.out.println("What is the average MPG of your car? Use the nearest whole number.");
int mpg = scan.nextInt();
System.out.println("So, you get " + mpg +" miles per gallon.");
System.out.println("Would you like an oil change? Enter Y or N");
double cost = (miles / mpg * gas + oil);
System.out.println("Below is your total cost");
System.out.println(String.format("Your trip will cost $" + cost + "."));
}
}
As you can see I added a little bit asking if they want an oil change. My vision of doing it would be to create a variable for either the y or n answer and then an if else statement based on whether y or n. If y it will add 39.99 to a new variable "oil". If n it will make the variable 0. Oil has been incorporated into the final equation regardless of it's value for ease.
I am not looking for anyone to do my assignment for me. I guess I am looking to see what this would look like or if anyone has any input as far as how I should tackle this. Thank you for any help you can provide!
First I need to include a menu of some sort giving the option to
either calculate the trip cost or exit the program.
You can use a switch statement.
//ask for user to enter 0 to exit, 1 to calculate the trip
switch(answer) {
case 0 : System.exit(0);
break;
case 1 : //calculate cost trip here
break;
default : System.exit(0);
}
Next I need to incorporate somewhere in the program a bit asking if
the user wants an oil change or not and then based on the answer
adding this to the total
Well you can get the value of the user with your Scanner object like you did and write an if statement to check this value.
System.out.println("Would you like an oil change? Enter Y or N");
//here get the value of the user using your scanner object
double oil = 0;
if(/*test if value is equals to y*/)
oil += 39.99;
Hints :
to avoid testing if the value is "y" or "Y", use the method equalsIgnoreCase of the String class.
when this will works you can wrap the functionnality of calculating the trip cost in a method and call this method in the case 1 of the switch statement.
Edit
import java.util.Scanner;
public class GasCalculator {
public static void main (String args[])
{
double total = 0;
Scanner scan = new Scanner(System.in);
System.out.println("How many miles do you plan to travel?");
int miles = scan.nextInt();
System.out.println("So you will drive " + miles +" miles.");
System.out.println("What is the price of gas?");
double gas = scan.nextDouble();
System.out.println("Ok, so gas costs $" + gas +" per gallon.");
System.out.println("What is the average MPG of your car? Use the nearest whole number.");
int mpg = scan.nextInt();
System.out.println("So, you get " + mpg +" miles per gallon.");
System.out.println("Would you like an oil change? Enter Y or N");
char oil = scan.next().charAt(0);
if (Character.toUpperCase(oil) == 'Y'){
System.out.println("Cost of oil change id 39.99);
System.out.println("39.99 will be added to your total cost");
total += 39.99;
}
double cost = (miles / mpg * gas);
total += cost;
String menu = "Pick a menu option: \n"
+ "1. Calculate total \n"
+ "2. exit";
System.out.println(menu);
int choice = scan.nextInt();
if (choice == 1){
System.out.println("Below is your total cost");
System.out.println(String.format("Your trip will cost $" + total + "."));
} else {
System.exit(0);
}
}
}
You can implement exit option like this:
System.out.println("Choose your desired option:\n1)Calculate Trip Cost\n2)Exit");
int answer = scan.nextInt();
if (answer==1){
// the rest of your program
System.out.println("How many miles do you plan to travel?");
int miles = scan.nextInt();
System.out.println("So you will drive " + miles +" miles.");
System.out.println("What is the price of gas?");
double gas = scan.nextDouble();
System.out.println("Ok, so gas costs $" + gas +" per gallon.");
System.out.println("What is the average MPG of your car? Use the nearest whole number.");
int mpg = scan.nextInt();
System.out.println("So, you get " + mpg +" miles per gallon.");
System.out.println("Would you like an oil change? Enter Y or N");
double cost = (miles / mpg * gas + oil);
System.out.println("Below is your total cost");
System.out.println(String.format("Your trip will cost $" + cost + "."));
}

Categories