I have been working hard to find a solution, but no dice yet. I am stuck trying to find a way to total of the values of the cases in a switch statement together. I will provide the homework question and my progress. Which will include the class and classTest. Thanks for any suggestions.
THE Homework ?
A large company pays its salespeople on a commission basis. The salespeople receive $200 per week plus 9% of their gross sales for that week. For example, a salesperson who sells $5000 worth of merchandise in a week receives $200 plus 9% of $5000, or a total of $650. You’ve been supplied with a list of the items sold by each salesperson. The values of these items are as follows:
Item Value
1 239.99
2 129.75
3 99.95
4 350.89
Develop a Java application that inputs one salesperson’s items sold for last week and calculates and displays that salesperson’s earnings. There’s no limit to the number of items that can be sold.
*********MY ISSUE:***********************
THE ISSUE ARISES WHEN I ATTEMPT TO ENTER MORE THAN ONE ITEM. THE PROGRAM WORKS WHEN I DO ONLY A SINGLE ITEM. HOWEVER, WHEN I DO MULTIPLES, THE PROGRAM NEVER TOTALS THE VALUES TOGETHER AND THE SALESPERSON EARNINGS ARE OFF
THE CLASS*********
public class SalesCommissionCalculator
{
private String item;
private double value;
public SalesCommissionCalculator()
{
setItem("");
setValue(0.0);
}
public SalesCommissionCalculator(String i, double v)
{
setItem(i);
setValue(v);
}
public void setItem(String i)
{
item = i;
}
public void setValue(double v)
{
value = v;
}
public String getItem()
{
return item;
}
public double getValue()
{
return value;
}
public String toString()
{
return ("Item"+item+"Value"+value);
}
}
************THE ClassTest:
import javax.swing.JOptionPane;
import java.util.Scanner;
public class SalesCommissionCalculatorTest
{
public static void main(String args [])
{
int quantity;
int item;
double salary = 200.00;
double commission = 0.09;
double total= 0;
String msg="";
SalesCommissionCalculator item1 = new SalesCommissionCalculator("1", 239.99);
SalesCommissionCalculator item2 = new SalesCommissionCalculator("2", 129.75);
SalesCommissionCalculator item3 = new SalesCommissionCalculator("3", 99.95);
SalesCommissionCalculator item4 = new SalesCommissionCalculator("4", 350.89);
item=Integer.parseInt(JOptionPane.showInputDialog("Enter item number 1, 2, 3 or 4. Enter -1 to quit."));
while(item != -1)
{
switch(item)
{
case 1: quantity=Integer.parseInt(JOptionPane.showInputDialog("Enter quantity"));
total = (quantity*item1.getValue());
break;
case 2: quantity=Integer.parseInt(JOptionPane.showInputDialog("Enter quantity"));
total= (quantity*item2.getValue());
break;
case 3: quantity=Integer.parseInt(JOptionPane.showInputDialog("Enter quantity"));
total = (quantity*item3.getValue());
break;
case 4: quantity=Integer.parseInt(JOptionPane.showInputDialog("Enter quantity"));
total = (quantity*item4.getValue());
break;
default:
System.out.println("Invalid Item Number");
}
item=Integer.parseInt(JOptionPane.showInputDialog("Enter item number 1, 2, 3 or 4. Enter -1 to quit."));
}
msg = msg + String.format("Your Commission is $%.2f", (total*commission)+salary);
JOptionPane.showMessageDialog(null, msg);
}
}
You aren't accumulating the total in any of the cases. What you have right now (using case 1 as an example)
total = (quantity*item1.getValue());
just sets the total to be equal to quantity*item1.getValue(). What you probably wanted was
total += (quantity*item1.getValue());
instead. (and similarly for the other three cases)
import java.util.*;
public class Sale {
public static void main(String[] args) {
double value;
int item;
double earn= 0.0;
double total=0.0;
Scanner console = new Scanner(System.in);
System.out.print("Enter 1, 2, 3, 4 or \"-1 to quit\": ");
item = console.nextInt();
while(item != -1)
{
switch(item)
{
case 1:
System.out.print("Enter quantity: ");
int quantity= console.nextInt();
total = quantity * 239.99;
break;
case 2:
System.out.print("Enter quantity:");
quantity= console.nextInt();
total = quantity * 129.75;
break;
case 3:
System.out.print("Enter quantity: ");
quantity= console.nextInt();
total= quantity * 99.95;
break;
case 4:
System.out.print("Enter quantity: ");
quantity= console.nextInt();
total= quantity * 350.89;
break;
default: System.out.print("Invalid Item!");
}
if (total>=5000)
{
earn= total * 0.09;
System.out.print("You have earned 9%, Your salary of the week : $"+earn);
}
else
System.out.println("Your salary of the week: $"+total);
System.out.println("");
System.out.print("Enter 1, 2, 3, 4 or \"-1 to quit\": ");
item = console.nextInt();
}
System.out.println("Thank you to visit our shop!");
}
}
Related
//Inventory Items classs
import java.util.Scanner;
public class InventoryItems {
public int sackrice = 4;
public int animalfeed = 12;
public int trayeggs = 15;
public int bottlemilk = 9;
ItemSupplier supple = new ItemSupplier();
public void inventoryItem() {
System.out.println("\nAvailable items:\n");
sackrice = sackrice + supple.getRice();
System.out.println("Sack of rice: " + sackrice);
if(sackrice < 10)
System.out.println("Sack of rice low, please restock");
System.out.println();
System.out.println("Animal feed: " + animalfeed);
if(animalfeed < 10)
System.out.println("Animal feed low, please restock");
System.out.println();
System.out.println("Tray of eggs: " + trayeggs);
if(trayeggs < 15)
System.out.println("Tray of eggs low, please restock");
System.out.println();
System.out.println("Bottle of milk: " + bottlemilk);
if(bottlemilk < 15)
System.out.println("Bottle of milk low, please restock");
System.out.println();
press();
}
public static void press(){
Scanner input = new Scanner(System.in);
System.out.println("Press Enter to continue...");
String enter = input.nextLine();
}
}
//Item Supplier class
import java.util.Scanner;
public class ItemSupplier {
public int z;
Scanner scan = new Scanner(System.in);
public void ricesupplier() {
System.out.println("How many sacks of rice would you like to
order?");
z = scan.nextInt();
}
public int getRice() {
return z;
}
public void feedsupplier() {
}
public void eggsupplier() {
}
public void milksupplier() {
}
}
import java.util.Scanner;
public class InventoryManager{
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
int x;
int y;
do {
System.out.println("Input option:\n" + "\n1. Check inventory" + "\n2. Search item supplier" + "\n3. Exit\n");
x = scan.nextInt();
switch(x) {
case 1:
InventoryItems items = new InventoryItems();
items.inventoryItem();
break;
case 2:
ItemSupplier supply = new ItemSupplier();
do {
System.out.println("\nChoose supplier:\n" + "\n1. Rice supplier\n" + "2. Animal feed supplier\n" + "3. Egg supplier\n" + "4. Milk supplier\n" + "5. Back\n");
y = scan.nextInt();
switch(y) {
case 1:
supply.ricesupplier();
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
default:
System.out.println("Invalid option");
break;
}
break;
} while (y != 5);
break;
case 3:
System.out.println("Program closed");
System.exit(0);
default:
System.out.println("Invalid option");
break;
}
} while(x != 3);
}
}
The "z" I get from getRice() is 0. It only takes the declared but initialized z. How do I get the "z" that was inputed in ricesupplier() method? Specifically, here: System.out.println("How many sacks of rice would you like to order?") and here z = scan.nextInt().
I'm really just a beginner. A lot of parts are still incomplete. I need to finish this problem first before I can proceed.
This won't be a direct answer to your question but here's some hints in order to improve your code and eventually solve your problem.
You should not make a new InventoryItems every time the user's input is 1. This will result into printing the initial inventory items, thus making your user order an item is useless
You should not make a new ItemSupplier every time the user's input is 2.
You don't need ItemSupplier in your InventoryItems
You don't need the variable z in ItemSupplier, you can directly return the input of the user in ricesupplier() method
thus if the user's input is 2 then you can just call ricesupplier() method and add it's return to the current items.sackrice
my instructions on the project were as followed:
Instructions: Use a sentinel value loop. To create a basic Rental Car Calculator
Ask each user for:
Type of vehicle (May use something other than strings, such as: 1 for an economy, 2 for a sedan, etc.) Days rented Calculate the (For each customer):
Rental cost, Taxes, Total Due. There are three different rental options with separate rates: Economy # 31.76, sedan # 40.32, SUV # 47.56. [Note: only whole day units to be considered (no hourly rates)].
Sales tax is = to 6% on the TOTAL.
Create summary data with:
The number of customers Total money collected. Also, Include IPO, algorithm, and desk check values (design documents).
{WHAT I HAVE GOING AND MY QUESTION(S)}
package tests;
import java.util.InputMismatchException;
import java.util.Scanner;
public class Tester {
public static void main(String []args){
int count=0;
int days;
int cus;
int carType;
double dailyFee=0, nonTaxTotal, total,fullTotal=0;
boolean checkRunOrQuit = false, chooseTypeVehicle = false, numberOfDAysChosen = false;
Scanner in=new Scanner(System.in);
while ( !checkRunOrQuit ) {
System.out.print("Press 1 to enter Rental Calculator or else press 0 to quit\n");
System.out.println("Please only enter 1 or 0. Also, please only enter number(s) not letter(s)");
try {
cus=in.nextInt();
switch ( cus ) {
case 0: System.out.println("End of application");
System.exit(0); // This will actually end your application if the user enters 0, no need to verify later on
break;
case 1: checkRunOrQuit = true;
break;
default:
System.out.println("Number must be either 1 or 0");
}
} catch (InputMismatchException ex) {
System.out.println("Invalid entry: ");
in.next();
}
}
while( !chooseTypeVehicle ) { // --> simplified comparison
count++;
System.out.print("What vehical would you like to rent?\n");
System.out.println("Enter 1 for an economy car");
System.out.println("Enter 2 for a sedan car");
System.out.println("Enter 3 for an SUV");
try {
carType = in.nextInt();
chooseTypeVehicle = true;
switch ( carType ) {
case 1: dailyFee = 31.76;
break;
case 2: dailyFee = 40.32;
break;
case 3: dailyFee = 47.56;
break;
default:
System.out.print("Number must be 1-3\n");
System.out.println("Please enter 1 for an economy car");
System.out.println("Enter 2 for a sedan car");
System.out.println("Enter 3 for an SUV");
chooseTypeVehicle = false;
break;
}
} catch (InputMismatchException ex) {
System.out.println("Answer must be a number");
in.next(); // -> you forgot this one.
}
}
while ( !numberOfDAysChosen ) {
try {
System.out.print("Please enter the number of days rented. (Example; 3) : ");
days = in.nextInt();
if (days <= 0) {
System.out.println("Number of days must be more than zero");
} else {
nonTaxTotal = (dailyFee * days);
total = (nonTaxTotal * 1.06);
fullTotal+=total;
numberOfDAysChosen = true;
}
} catch(InputMismatchException ex) {
System.out.println("Answer must be a number");
in.next();
}
}
in.close();
System.out.println("Count of customers : " + count);
System.out.printf("total of the Day : $ %.2f", fullTotal);
}
}
How would I make this program loop back to prompting the user: "Press 1 to enter Rental Calculator or else press 0 to quit\". After the "days rented input is entered?
[Note: Once the days rented is input, I was wanting a total calculation but not a summary. However, I want the summary info when the program is exited.]
My result want me to : when I enter -1 into (Enter the product number) it will directly break and calculate the total , but right now I have to enter -1 into (Enter the product number) and (Enter the quantity) only it will break
import java.util.Scanner;
public class Structuredcontrols {
public static void main(String[]args) {
Scanner input = new Scanner(System.in);
double sum=0;
double sum2=0;
double sum3=0;
int number = 0;
int quantity;
while (number != -1) {
System.out.print("Enter product number: ");
number = input.nextInt();
System.out.print("Enter quantity number: ");
quantity = input.nextInt();
switch (number) {
case 1:
sum += 2.98 * quantity;
break;
case 2:
sum2 += 4.50 * quantity;
break;
case 3:
sum3 += 9.98 * quantity;
break;
}
}
System.out.println("Total value of product 1 is: "+sum);
System.out.println("Total value of product 2 is: "+sum2);
System.out.println("Total value of product 3 is: "+sum3);
}
}
Just add a if statement
if(number == -1) {
break;
}
Here's full code
import java.util.Scanner;
public class structuredcontrols {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double sum = 0;
double sum2 = 0;
double sum3 = 0;
int number = 0;
int quantity;
while (number != -1) {
System.out.print("Enter product number: ");
number = input.nextInt();
if (number == -1)
break;
System.out.print("Enter quantity number: ");
quantity = input.nextInt();
switch (number)
{
case 1:
sum += 2.98 * quantity;
break;
case 2:
sum2 += 4.50 * quantity;
break;
case 3:
sum3 += 9.98 * quantity;
break;
}
}
System.out.println("Total value of product 1 is: " + sum);
System.out.println("Total value of product 2 is: " + sum2);
System.out.println("Total value of product 3 is: " + sum3);
}
}
I am trying to get my output to show the total cost of each item entered and a grand total of the cost of all items, the program runs but when I try to use the end-of-file indicator nothing happens, what am I missing?
package switch1;
import java.util.Scanner;
public class Switch1 {
public static void main(String[] args) {
double total = 0;
int productCounter = 0;
int aProduct = 0;
int bProduct = 0;
int cProduct = 0;
int dProduct = 0;
int eProduct = 0;
Scanner input = new Scanner(System.in);
System.out.printf("%s%n%s%n %s%n %s%n",
"Select your product(1=bread, 2=cheese, 3=meat, 4=msutard, 5=mayo)",
"Type the end-of-file indicator to terminate input:",
"On UNIX/Linux/macOS type <Ctrl> d then press Enter",
"On Windows type <Ctrl> z then press Enter");
while (input.hasNext()) {
int product = input.nextInt();
total += product;
++productCounter;
switch (product) {
case 1:
++aProduct;
break;
case 2:
++bProduct;
break;
case 3:
++cProduct;
break;
case 4:
++dProduct;
break;
case 5:
++eProduct;
break;
}
}
System.out.printf("%nProduct Selection:%n");
if (productCounter != 0) {
double aTotal = (aProduct * 1.89);
double bTotal = (bProduct * 3.99);
double cTotal = (cProduct * 6.99);
double dTotal = (dProduct * 1.99);
double eTotal = (eProduct * 2.99);
total = (((double)aProduct * 1.89) + ((double)bProduct * 3.99) + ((double)cProduct * 6.99) + ((double)dProduct * 1.99) + ((double)eProduct * 2.99));
System.out.printf("You ordered %d gallons of milk totaling: %f%n", aProduct, aTotal);
System.out.printf("You ordered %d loaves of bread totaling: %f%n", bProduct, bTotal);
System.out.printf("You ordered %d packages of cheese totaling: %f%n", cProduct, cTotal);
System.out.printf("You ordered %d pounds of meat totaling: %f%n", dProduct, dTotal);
System.out.printf("You ordered %d bottles of mustard totaling: %f%n", eProduct, eTotal);
System.out.printf("Your total grocery bill is: %d%n", total);
}
else {
System.out.println("No products were entered");
}
}
}
Replace scanner.hasNext with scanner.hasNextLine. If you're on a newline and you hit CTRL-D (linux) or CTRL-Z (Windows), hasNextLine should return false.
Maybe a better question is "how do I find out what am I doing wrong? "
One step would be to warp you code in a try-catch block like:
try {
//the code goes here
}catch(Exception ex) {
ex.printStackTrace();
}
And see the exceptions.
It will indicate a problem in this line
System.out.printf("Your total grocery bill is: %d%n", total);
I am having some trouble getting my sum function to work. I am trying to create a program that asks for a product number and a quantity using switch structure and a sentinel loop. It will run until 0 is entered. It should calculate the total number of products entered and the total value of all products entered. The quantity works just fine. It's the total value that will only work for the 1st product entered. I cannot get the sum for total to keep adding until 0 is pressed. Any help would be greatly appreciated!!
import java.util.Scanner;
public class Mailorder {
public static void main(String[] args) {
//create a scanner
Scanner input = new Scanner(System.in);
//declare variables
double product1 = 3.75;
double product2 = 5.95;
double product3 = 8.75;
double product4 = 6.92;
double product5 = 8.75;
double product6 = 7.87;
double total = 0.00;
//read in product #
System.out.print("Enter a product number: ");
int product = input.nextInt();
//read in quantity sold
System.out.print("Enter quantity sold for 1 day: ");
int quantity = input.nextInt();
//switch case
switch (product)
{
case 1: total = product1 * quantity; break;
case 2: total = product2 * quantity; break;
case 3: total = product3 * quantity; break;
case 4: total = product4 * quantity; break;
case 5: total = product5 * quantity; break;
case 6: total = product6 * quantity; break;
default: System.out.println("ERROR: Invalid product number");
}
//keep reading data until the input is 0
int sum1 = 0;
while (quantity != 0) {
sum1 += quantity;
int sum2 = 0;
while (total != 0) {
sum2 +=total;
}
//read the next data
System.out.print("Enter a product number: ");
product = input.nextInt();
System.out.print("Enter quantity sold for 1 day: ");
quantity = input.nextInt();
}
//print results
System.out.println("The total number of products sold last week " + sum1);
System.out.println("The total retail value of all products sold last week " + sum2);
}
}
There are a couple scoping issues here.
First, you have placed your switch statement outside of your loop. You should place it inside your loop.
Second, there is a scoping problem with sum2. It is declared inside your sentinel loop, but referenced outside. I'm not sure why you have a nested loop adding to sum2. Here is the code with these issues resolved:
public class Mailorder {
public static void main(String[] args) {
//create a scanner
Scanner input = new Scanner(System.in);
//declare variables
double product1 = 3.75;
double product2 = 5.95;
double product3 = 8.75;
double product4 = 6.92;
double product5 = 8.75;
double product6 = 7.87;
//read in product #
System.out.print("Enter a product number: ");
int product = input.nextInt();
//read in quantity sold
System.out.print("Enter quantity sold for 1 day: ");
int quantity = input.nextInt();
//keep reading data until the input is 0
int sum1 = 0;
int sum2 = 0;
while (quantity != 0) {
sum1 += quantity;
double total = 0.00;
//switch case
switch (product)
{
case 1: total += product1 * quantity; break;
case 2: total += product2 * quantity; break;
case 3: total += product3 * quantity; break;
case 4: total += product4 * quantity; break;
case 5: total += product5 * quantity; break;
case 6: total += product6 * quantity; break;
default: System.out.println("ERROR: Invalid product number");
}
sum2 += total;
//read the next data
System.out.print("Enter a product number: ");
product = input.nextInt();
System.out.print("Enter quantity sold for 1 day: ");
quantity = input.nextInt();
}
//print results
System.out.println("The total number of products sold last week " + sum1);
System.out.println("The total retail value of all products sold last week " + sum2);
}
}