Variables aren't getting updated with the proper values - java

I'm making a java program for the system used at Luas Station to purchase tickets. The Luas is a type of train in the Ireland.
I have a few different types of tickets which involves various prices etc. After a customer selects what ticket to buy I give them an option to return to the beginning of the 'chooseTickets' to buy more tickets. At the end, the program I have assigned the value of all of the customer's desired tickets as the completePrice variable. the idea is to allow for the customer to continue adding tickets and for that variable to continuously add the prices of them to the variable until they're ready to pay. It works except when I go back to purchase for tickets and it is the same type, i.e a standard adult ticket twice. The price of 2.50 is not added to another 2.50, it is overwritten by another 2.5. Below is the code for the same:
import java.util.Scanner; //imports the scanner class to allow for user input
public class ticketPurchase {
//Adding global variables to be used by various methods
static Scanner input = new Scanner(System.in);
static String menuChoice;
static double childTicket = 1.20;
static double adultTicket = 2.50;
static double studentTicket = 1.70;
static double adultFlexi = 12.00;
static double childFlexi = 8.00;
static double studentFlexi;
static String ticketType;
static String ticketChoice;
static int adultTicketQty;
static int childTicketQty;
static int studentTicketQty;
static String destinationZone;
static double adultFinalPrice;
static double childFinalPrice;
static double studentFinalPrice;
static double flexiAdultFinalPrice;
static double flexiChildFinalPrice;
static double completePrice;
static String moreTicketChoice2 = "0";
public static void main(String[] args) {
menu(); //calling the menu method within the main method to start the process
}
public static void menu() {
if(moreTicketChoice2.equals("1")){
System.out.println("Press 2 to purchase Standard Tickets");
System.out.println("Press 3 to purchase Flexi tickets");
System.out.println("press 4 to purchase student tickets");
}
else if(moreTicketChoice2.equals("0")) {
System.out.println("Press 1 for information");
System.out.println("Press 2 to purchase Standard Tickets");
System.out.println("Press 3 to purchase Flexi tickets");
System.out.println("press 4 to purchase student tickets");
}
menuChoice = input.next(); //allowing user to choose what tickets to buy or just to see information of services
switch(menuChoice) { //switch statement to record and output information based on users input
case "1":{ //prints information regarding pricing, ticket age restrictions and support
System.out.println("The standard ticket may be a single or return ticket for an adult (16+) or a child");
System.out.println("The flexi ticket covers all journeys for one 24 hour period for either a child or an adult");
System.out.println("A single ticket's price depends on the journey length, single or return and if it is for an adult or a child");
System.out.println("a Flexi ticket for a child costs €8.00 and a Flexi ticket for an adult costs €12.00");
System.out.println("Our Customer Care telephone number for this terminal is 0830462920, please call if further support is required");
menu();
break;
}
case "2":{
ticketChoice = "standard"; //records the value of standard within the ticketChoice global variable
chooseTickets(); //initiates the choose tickets method
break;
}
case "3":{
ticketChoice = "flexi"; //record the value of Flexi within the ticketChoice global variable
chooseTickets();
break;
}
case "4":{
ticketChoice = "student";
chooseTickets();
}
case "a":{ //allows user to enter the admin interface
admin();
break;
}
default:{ //allows for user to input a value outside of the options and notify's them to try again
System.out.println("Invalid choice, please choose from 1 to 3");
break;
}
}
}
public static void chooseTickets() { //payment method to choose quantity, destination zone and final price
System.out.println("You have chosen to purchase " + ticketChoice + " ticket(s)");
if(ticketChoice.equals("student")) { //allows user to purchase student's tickets
ticketType = "student";
System.out.println("Please enter the quantity of tickets: ");
studentTicketQty = input.nextInt();
System.out.print("please enter your destination zone (1, 2, 3, 4): ");
destinationZone = input.next();
ticketChoice = "student";
studentFinalPrice = (studentTicket*studentTicketQty);
switch(destinationZone){ //adjusts price to account for the destination's zone chosen by user
case "1":{
studentFinalPrice = studentFinalPrice + (studentTicketQty*0);
break;
}
case "2":{
studentFinalPrice = studentFinalPrice + (studentTicketQty*0.50);
break;
}
case "3":{
studentFinalPrice = studentFinalPrice + (studentTicketQty*1.0);
break;
}
case "4":{
studentFinalPrice = studentFinalPrice + (studentTicketQty*1.50);
break;
}
default:{
System.out.println("you have entered an invalid choice please choose from 1 to 4");
chooseTickets();
break;
}
}
System.out.print("would you like to purchase more tickets? enter 1 if so, 2 if not: "); //allows user to purchase other tickets
moreTicketChoice2 = input.next();
if(moreTicketChoice2.equals("1")) {
menu();
}
else if(moreTicketChoice2.equals("2")){
System.out.println("you have chosen against purchasing more tickets");
payment();
}
}
else {
System.out.print("please enter 1 for children's tickets and 2 for adult's: ");
String ticketAgeGroup = input.next();
switch(ticketAgeGroup) { //allows user to choose quantity and destination based on choice of adult or child
case "2":{//case for adult tickets
System.out.println("you have chosen adults ticket");
ticketType = "adult";
System.out.print("Please enter the quantity of tickets: ");
adultTicketQty = input.nextInt();
System.out.print("please enter your destination zone (1, 2, 3, 4): ");
destinationZone = input.next();
if(ticketChoice == "flexi") { //if statement to calculate the finalPrice variable value if the ticketChoice is Flexi
flexiAdultFinalPrice = (adultFlexi*adultTicketQty);
}
else {
adultFinalPrice = (adultTicket*adultTicketQty); //else calculates the finalPrice variable value if the ticketChoice is standard
}
switch(destinationZone){ // switch statement to calculate the final price depending on the destination's zone and their extra amount.
case "1":{
adultFinalPrice = adultFinalPrice + (adultTicketQty*0);
break;
}
case "2":{
adultFinalPrice = adultFinalPrice + (adultTicketQty*.50); //calculation to add the extra amount for the destination
break;
}
case "3":{
adultFinalPrice = adultFinalPrice + (adultTicketQty*1.0);
break;
}
case "4":{
adultFinalPrice = adultFinalPrice + (adultTicketQty*1.50);
break;
}
default:{
System.out.println("you have entered an invalid choice please choose from 1 to 4");
break;
}
} //end of the switch statement
System.out.print("Would you like to purchase more tickets? enter 1 if so, 2 if not: "); //allows user to purchase other tickets
moreTicketChoice2 = input.next();
if(moreTicketChoice2.equals("1")) { //if statement to allow a customer to purchase more tickets if required
menu();
}
else {
System.out.println("you have chosen against purchasing more tickets");
payment(); //proceeds to the payment method
}
break;
}
case "1":{ //case for children's tickets
System.out.println("you have chosen children's ticket");
ticketType = "child";
System.out.println("Please enter the quantity of tickets: ");
childTicketQty = input.nextInt();
System.out.print("please enter your destination zone (1, 2, 3, 4): ");
destinationZone = input.next();
if(ticketChoice == "flexi") { //adjusts the price if user chooses the flexi option
flexiChildFinalPrice = (childFlexi*childTicketQty);
}
else {
childFinalPrice = (childTicket*childTicketQty);
}
switch(destinationZone){ //adjusts price to account for the destination's zone chosen by user
case "1":{
childFinalPrice = childFinalPrice + (childTicketQty*0);
break;
}
case "2":{
childFinalPrice = childFinalPrice + (childTicketQty*.50);
break;
}
case "3":{
childFinalPrice = childFinalPrice + (childTicketQty*1.0);
break;
}
case "4":{
childFinalPrice = childFinalPrice + (childTicketQty*1.50);
break;
}
default:{
System.out.println("you have entered an invalid choice please choose from 1 to 4");
chooseTickets();
break;
}
}
System.out.print("would you like to purchase more tickets? enter 1 if so, 2 if not: "); //allows user to purchase other tickets
moreTicketChoice2 = input.next();
if(moreTicketChoice2.equals("1")) {
menu();
}
else if(moreTicketChoice2.equals("2")){
System.out.println("you have chosen against purchasing more tickets");
payment();
}
break;
}
}
}
}
public static void payment() { //method to complete the payment process for the purchase
completePrice = adultFinalPrice + childFinalPrice + studentFinalPrice + flexiAdultFinalPrice + flexiChildFinalPrice;
System.out.println("the total due is €" + completePrice);
}
public static void printTickets() { //method to notify the customer that their tickets are printing
}
public static void admin() { //method to control the admin's interface
}
}
In this photo i want an output of 2.4 not 1.2

Related

How do I loop for only specific "case" in switch/case?

I used switch/case to print yearly earnings depending on the bank account the user choose and I want to make this program, whenever the user enters inappropriate input, print "inappropriate input" message and let the user to re-enter the bank account they want to choose. But, currently my code only prints "inappropriate input" message. How can I fix it?
public static void main(String[] args) throws Exception
{
//initialize variables
String bank = "";
double money;
//asks the user to type the amount of money
Scanner myinput = new Scanner (System.in);
System.out.print("Please enter the amount of money you want in the bank: ");
//if the input is inappropriate, ensures the user to type the correct input
while (true) {
try {
money = myinput.nextDouble();
break;
} catch (Exception e) {
System.out.println("This is inappropriate. Please enter the amount of money you want in the bank: ");
myinput.nextLine();
}
}
//asks user to type the type of account they want
System.out.println("Please enter the type of account you want: ");
bank = myinput.next();
//print yearly earnings depending on the type of account that user chose
switch (bank) {
case "A" , "C": //if the user enters A or C, 1.5% of money will be yearly earnings
System.out.println("You can make $" + (Math.round((0.015 * money)*100)) / 100.0);
break;
case "B": //if the user enters A or C, 2% of money will be yearly earnings
System.out.println("You can make $" + (Math.round((0.02 * money)*100)) / 100.0);
break;
case "X": //if the user enters A or C, 5% of money will be yearly earnings
System.out.println("You can make $" + (Math.round((0.05 * money)*100)) / 100.0);
break;
default:
System.out.println ("This is inappropriate input. Please enter the type of account you want");
bank = myinput.nextLine();
}
//closing scanner
myinput.close();
}//end main
You can have one boolean variable for getting out of while-loop.
Flag is set to false in switch/case because while(false) wont iterate further.
boolean flag = true;
while(flag){
switch(bank){
case "A","C":
System.out.println("You can make $" + (Math.round((0.015 * money)*100)) / 100.0);
flag = false; //put this in all case.
break;
...
}
}
package com.javatutorial;
import java.util.Scanner;
class Main{
public static void main(String[] args) throws Exception
{
String bank = "";
double money = 0;
boolean valid = false;
Scanner myinput = new Scanner (System.in);
System.out.print("Please enter the amount of money you want in the bank: ");
while (true) {
try {
money = myinput.nextDouble();
break;
} catch (Exception e) {
System.out.println("This is inappropriate. Please enter the amount of money you want in the bank: ");
myinput.next();
}
}
do {
System.out.println("Please enter the type of account you want: ");
bank = myinput.next();
switch (bank) {
case "A":
case "C":
System.out.println("You can make $" + (Math.round((0.015 * money) * 100)) / 100.0);
valid = true;
break;
case "B":
System.out.println("You can make $" + (Math.round((0.02 * money) * 100)) / 100.0);
valid = true;
break;
case "X":
System.out.println("You can make $" + (Math.round((0.05 * money) * 100)) / 100.0);
valid = true;
break;
default:
System.out.println("This is inappropriate input. Please enter the type of account you want");
}
}while( !valid);
}
}

Can someone please look my code for atm project?

Currently, I am working on a ATM project. I was able to make it work. However, I'm not sure what to do if the user enters an undercase letter than the uppercase letter on the switch statement. Also, should I use a while loop so the user can able to repeat the steps. Finally, I'm stuck on how to let the user exit the program whenever. Like for instance, exiting after completing their selection.
public static void main(String[] args) {
double checkingAccount = 5000;
double savingAccount = 2000;
Scanner cin = new Scanner(System.in);
//Welcoming the user!
System.out.println("=======================");
System.out.println("Welcome to SoundBank!");
System.out.println("=======================");
// Selections for the user
System.out.println("Please choose: \nA - Checking Account \nB - Savings Account");
String choice1 = cin.nextLine();
System.out.println("Please choose of the following: \nD - Deposit \nW - Withdrawal \nC - Check Balance \nE - Exit");
String choice2 = cin.nextLine();
// It's time for loops
switch (choice2) {
case "D":
// Checking Account
if (choice1.equalsIgnoreCase("A")) {
System.out.println("How much you do you want to deposit?");
double amount = cin.nextDouble();
double total1 = checkingAccount + amount;
System.out.println("You deposited " + amount);
System.out.println("You currently have: " + total1);
} else
// Saving Account
if (choice1.equalsIgnoreCase("B")) {
System.out.println("How much you do you want to deposit?");
double amount2 = cin.nextDouble();
double total2 = savingAccount + amount2;
System.out.println("You deposited " + amount2);
System.out.println("You currently have: " + total2);
}
break;
case "W":
// Checking Account
if (choice1.equalsIgnoreCase("A")) {
System.out.println("How much do you want to withdraw?");
double amount3 = cin.nextDouble();
double total3 = checkingAccount - amount3;
System.out.println("You withdrew " + amount3);
System.out.println("You currently have: " + total3);
} else
// Saving Account
if (choice1.equalsIgnoreCase("B")) {
System.out.println("How much do you want to withdraw?");
double amount4 = cin.nextDouble();
double total4 = savingAccount - amount4;
System.out.println("You withdrew " + amount4);
System.out.println("You currently have: " + total4);
}
break;
case "C":
// Checking Account
if (choice1.equalsIgnoreCase("A")) {
System.out.println("Your current balance is: " + checkingAccount);
} else
// Saving Account
if (choice1.equalsIgnoreCase("B")) {
System.out.println("Your current balance is: " + savingAccount);
}
break;
case "E":
System.out.println("You exited the program.");
break;
default:
System.out.println("Please choose a selction.");
}
}
I would just do
switch(choice2.toUpperCase())
so you don't have to worry about whether they put in upper or lowercase, but you could also do
case "D": // fall through
case "d":
to have two cases for the same block of code.
For existing the program, since it is all in the main function, you can just return
case "E":
System.out.println("You exited the program.");
return;

Q: Doing multiple loops and multiple if-statements and if-else-statements | RENTAL CAR CALCULATOR PROJECT

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.]

How to make the switch statement more accurate?

We were ask to make a program using the switch statement..
Here is my code:
double price = 0, totalPrice;
System.out.print("Enter the number of your choice: ");
int optionNumber = Integer.parseInt(kb.readLine());
switch(optionNumber)
{
case 1:
price = 190.00;
break;
case 2:
price = 410.00;
break;
default:
System.out.println("ERROR: Invalid number!");
break;
}
System.out.print("Enter quantity: ");
int quantity = Integer.parseInt(kb.readLine());
totalPrice = price * quantity;
So basically, the user will input a certain number and it will have different prices... inside the switch statements.
but if the user inputs a wrong number, it will display an error message and i dont want the user to enter the quantity which will be executed after the switch statement.
we are not allowed to use any methods or functions and i dont want to code repeatedly like this:
System.out.print("Enter the number of your choice: ");
int optionNumber = Integer.parseInt(kb.readLine());
switch(optionNumber)
{
case 1:
price = 190.00;
System.out.print("Enter quantity: ");
int quantity = Integer.parseInt(kb.readLine());
totalPrice = price * quantity;
System.out.print("Total price: " + totalPrice);
break;
case 2:
price = 410.00;
System.out.print("Enter quantity: ");
int quantity = Integer.parseInt(kb.readLine());
totalPrice = price * quantity;
System.out.print("Total price: " + totalPrice);
break;
default:
System.out.println("ERROR: Invalid number!");
break;
}
is there any other way not to use if else, methods, functions or coding repeatedly?
ANY HELP WILL BE APPRECIATED.
You can use a boolean flag and make it false if invalid option is selected.
Then only ask user further if flag is true.
System.out.print("Enter the number of your choice: ");
int optionNumber = Integer.parseInt(kb.readLine());
boolean flag = true;
switch (optionNumber) {
case 1:
price = 190.00;
break;
case 2:
price = 410.00;
break;
default:
flag = false;
System.out.println("ERROR: Invalid number!");
break;
}
if (flag) {
System.out.print("Enter quantity: ");
int quantity = Integer.parseInt(kb.readLine());
totalPrice = price * quantity;
System.out.print("Total price: " + totalPrice);
}
Use as this:
while(!valid option)
//do this stuff
Use a flag and set it to true if the number entered is valid, so it will go to your next instruction; else ask again for input.
Throw an exception
default:
throw new InvalidArgumentException("Invalid number!");
See also InvalidArgumentException vs UnexpectedValueException
You could just remove default from the switch statement and check to see if the price is equal to 0 after the switch statement
double price = 0, totalPrice;
System.out.print("Enter the number of your choice: ");
int optionNumber = Integer.parseInt(kb.readLine());
switch(optionNumber)
{
case 1:
price = 190.00;
break;
case 2:
price = 410.00;
break;
}
if (price == 0)
{
System.out.println("ERROR: Invalid number!");
}
else
{
System.out.print("Enter quantity: ");
int quantity = Integer.parseInt(kb.readLine());
totalPrice = price * quantity;
System.out.print("Total price: " + totalPrice);
}
This keeps you from adding unnecessary variables (like a boolean flag) when you already have one (price) with a default value of 0 that you can check against.

Need assistence displaying value from a different private class

Okay, first off I am very, very new to java. For this project I need to design a program that takes a product number, an amoutn sold, calculates the total, and then displays it. However, I need to to display when I select option 2, which is a seperate private class, to be honest I don't even know where to begin. Any help would be appreciated.
import java.util.Scanner;
public class Attempt1
{
//method to pause until a key is pressed
public static void pause()
{
try
{
System.out.print("Press <Enter> to continue...");
System.in.read();
}
catch (Exception e)
{
System.err.printf("Error %s%c\n",e.getMessage(),7);
}
}//end pause
public static void main(String[] args)
{
//variables to capture keyboard input
Scanner keyBd = new Scanner( System.in );
char selection;
//int selection;
do{//display menu
System.out.println("\n--------------");
System.out.println("Retail Sales\n");
System.out.println("1. Enter Products Sold");
System.out.println("2. Display Total Retail Sales");
System.out.println("3. Exit\n");
System.out.print ("Selection: ");
//get menu selection
selection = keyBd.next().charAt(0);
//selection = keyBd.nextInt();
//process menu selection
switch (selection){
case '1':
enterProducts();
break;
case '2':
displaySales();
break;
case '3':
//recognize as valid selection but do nothing
break;
default :
//System.out.printf("%c\n",7);
System.out.println("Invalid Selection");
}//end switch
}while( selection != '3');
}//end main()
private static void enterProducts()
{
Scanner inp = new Scanner(System.in);
int product,quantity;
double total = 0.00;
System.out.print("Enter product #(1-5)(0 to stop): ");
product=inp.nextInt();
while(product !=0)
{
System.out.print("Enter quantity: ");
quantity=inp.nextInt();
switch( product ){
case 1:total+=quantity*2.98;
break;
case 2:total+=quantity*4.50;
break;
case 3:total+=quantity*3.98;
break;
case 4:total+=quantity*4.49;
break;
case 5:total+=quantity*6.87;
break;
default:System.out.println("Invalid Product Number");
System.out.println("Product Number Does not Exist");
if(product<0 && product>=6)
{
System.out.print("Enter product #(1-5)(0 to stop): ");
product=inp.nextInt();
System.out.print("Enter quantity: ");
quantity=inp.nextInt();
}
break;
}
System.out.print("Enter product #(1-5)(0 to stop): ");
product=inp.nextInt();
}
pause();
}
private static void displaySales()
{
System.out.println( "The total retail value was: " + total );
pause();
}
}//end MenuDemo
Here is an algorithm for improving your code:
At the beginning of your your main add a variable total and initialize it with 0: double total=0;
Change the enterProducts method's return type to double: private static double enterProducts() and return the local variable total at the end from this method after the call to pause: return total;
In the case for the input of 1 add the returned value from enterProducts to the current value of total (it's the total inside your main): total += enterProducts();
Add to the method displaySales an double argument: private static void displaySales(double total) and change the call to it in the main's case for 2 to displaySales(total);
I think you mean private method. You could pass the total like so:
private static void displaySales(double total) {
...
total is defined in enterProducts but not in the main method where the loop is displayed, so you could return this:
double enterProducts() {
...
return total;
}
so that you can pass it to displaySales.
The problem with the code is that you're trying to access a local variable, declared in the static enterProducts() method, inside of the static displaySales() method.
The code below solves that "problem".
With that being said, I recommend that you work through some Java tutorials to understand why the code now works... Have a look at Variable Scope.
public class Attempt1
{
//use a static variable to store the total
static double total = 0.00;
//method to pause until a key is pressed
public static void pause()
{
try
{
System.out.print("Press <Enter> to continue...");
System.in.read();
}
catch (Exception e)
{
System.err.printf("Error %s%c\n",e.getMessage(),7);
}
}//end pause
public static void main(String[] args)
{
//variables to capture keyboard input
Scanner keyBd = new Scanner( System.in );
char selection;
//int selection;
do{//display menu
System.out.println("\n--------------");
System.out.println("Retail Sales\n");
System.out.println("1. Enter Products Sold");
System.out.println("2. Display Total Retail Sales");
System.out.println("3. Exit\n");
System.out.print ("Selection: ");
//get menu selection
selection = keyBd.next().charAt(0);
//selection = keyBd.nextInt();
//process menu selection
switch (selection){
case '1':
enterProducts();
break;
case '2':
displaySales();
break;
case '3':
//recognize as valid selection but do nothing
break;
default :
//System.out.printf("%c\n",7);
System.out.println("Invalid Selection");
}//end switch
}while( selection != '3');
}//end main()
private static void enterProducts()
{
Scanner inp = new Scanner(System.in);
int product,quantity;
System.out.print("Enter product #(1-5)(0 to stop): ");
product=inp.nextInt();
while(product !=0)
{
System.out.print("Enter quantity: ");
quantity=inp.nextInt();
switch( product ){
case 1:total+=quantity*2.98;
break;
case 2:total+=quantity*4.50;
break;
case 3:total+=quantity*3.98;
break;
case 4:total+=quantity*4.49;
break;
case 5:total+=quantity*6.87;
break;
default:System.out.println("Invalid Product Number");
System.out.println("Product Number Does not Exist");
if(product<0 && product>=6)
{
System.out.print("Enter product #(1-5)(0 to stop): ");
product=inp.nextInt();
System.out.print("Enter quantity: ");
quantity=inp.nextInt();
}
break;
}
System.out.print("Enter product #(1-5)(0 to stop): ");
product=inp.nextInt();
}
pause();
}
private static void displaySales()
{
System.out.println( "The total retail value was: " + total );
pause();
}
}//end MenuDemo

Categories

Resources