I am working on a program that tracks items and their costs as you add them to you bag. I have switch statement to give options for 1. adding new items, 2. printing the totals, and 3. ending the program.
For some reason when I select case 1 it also prints the totals using my toString method. But I only have the toString method in case 2.
Can anyone explain why this would be happening?
Here is my main
import java.util.Scanner;
public class ShoppingBagTracker {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
float taxRate, cost;
int items, newItems, choice;
String receipt;
String menu = ("1. Add items" +
"2. Get receipt"
+ "3. Exit");
System.out.print("Enter the sales tax rate: ");
taxRate = in.nextFloat();
ShoppingBag myBag = new ShoppingBag(taxRate);
items = 0;
do{
System.out.println("What would you like to do?");
System.out.println(menu);
choice = in.nextInt();
switch(choice){
case 1:
System.out.print("Enter cost of item: ");
cost = in.nextFloat();
System.out.print("Enter number of items: ");
newItems = in.nextInt();
items = items + newItems;
myBag.place(items, cost);
myBag.getItems();
myBag.getCost();
myBag.getTotal();
myBag.getAverage();
case 2:
receipt = myBag.toString();
System.out.println(receipt);
case 3:
break;
default:
System.out.println("That is not an option");
}
}while(choice != 3);
}
}
Here is my Shopping Bag class
public class ShoppingBag {
public float taxRate;
public int items;
public float cost;
public float average;
public float totalCost;
public float finalCost;
public ShoppingBag(float taxRate)
{
this.taxRate = taxRate;
}
public void place(int newitems, float newcost)
{
items = newitems;
cost = newcost;
cost = items * cost;
}
public int getItems()
{
return items;
}
public float getCost()
{
return cost;
}
public float getTotal()
{
finalCost = cost + (cost * taxRate);
return finalCost;
}
public float getAverage()
{
average = finalCost/items;
return average;
}
#Override
public String toString()
{
return("Items: " + items + " Cost: " + cost + " Total cost: " + finalCost + " Average cost: " + average);
}
}
A switch case isn't restrict to execute one 'case'. It execute all code from the matching case down to a break; or to the end of the switch.
In your case add a break; before the case 2
You need to have break in switch-cases.
switch(something){
case 1:
// do something
break;
case 2:
//do something
break;
default:
// do something
break;
}
If there is no break, that will execute all cases here.
switch-cases in Java.
Conclusion.
If there is no return in cases, you should use break for every case.
Following case you don't need break.
switch (cases){
case "a":
return "a";
case "b":
return "b";
default:
return "default";
}
You need to add a break statement inside the case 1 body
case 1:
System.out.print("Enter cost of item: ");
cost = in.nextFloat();
System.out.print("Enter number of items: ");
newItems = in.nextInt();
items = items + newItems;
myBag.place(items, cost);
myBag.getItems();
myBag.getCost();
myBag.getTotal();
myBag.getAverage();
break;
Here is the official tutorial
You should either have a break for all the cases or have a return statements in all cases. Otherwise when Java will execute all the cases which are after the matching case.
swicth(urSwitch){
case 1:// logic
break; // or return something
case 2:// logic
break; // or return something
case 3:// logic
break; // or return something
}
Related
I'm writing a program that displays a fast food menu. The user selects an item, then enters the quantity of that item, and can continue selecting items with specific quantities until done. I have to use several methods. What I'm having trouble with is calculating a running total. This is my first Java class so I only know the basics. I put the running total in a while loop so it'll keep adding a subtotal to it, but when I call done() the runningTotal is 0. What's the best way to keep track of the running total while using multiple methods? Also, I'm open to any criticism or clean up in my code. Thank you.
import java.util.Scanner;
public class Menu {
public double subTotal;
public static double runningTotal;
private static double itemPrice;
static boolean ordering = true;
static Scanner input = new Scanner(System.in);
public static void menu() {
System.out.println("Welcome \n1. Burger ($2.00) \n2. Fries ($1.50)\n3. Soda ($1.00) \n4. Done");
}
public static double itemPrice(int foodItem) {
if (foodItem == 1) {
// burger= $2.00
System.out.println("You've ordered a burger");
itemPrice = 2.00;
}
if (foodItem == 2) {
// fries = $1.50
System.out.println("You've ordered fries");
itemPrice = 1.50;
}
if (foodItem == 3) {
// soda = $1.00
System.out.println("You've ordered a soda");
itemPrice = 1.00;
}
quantity();
return itemPrice;
}
public static double quantity() {
System.out.println("Enter quantity");
double quantity = input.nextDouble();
subTotal(quantity, itemPrice);
return quantity;
}
public static double subTotal(double quantity, double itemPrice) {
double subTotal = quantity * itemPrice;
System.out.println("Subtotal: " + subTotal);
return subTotal;
}
public static void done(double runningTotal) {
ordering = false;
System.out.println(runningTotal);
System.out.println("Enjoy your meal");
}
public static void main(String[] args) {
int menuOption;
int foodItem = 0;
input = new Scanner(System.in);
do {
double runningTotal = 0;
menu();
menuOption = input.nextInt();
switch (menuOption) {
case 1:
foodItem = 1;
itemPrice(foodItem);
break;
case 2:
foodItem = 2;
itemPrice(foodItem);
break;
case 3:
foodItem = 3;
itemPrice(foodItem);
break;
case 4:
done(runningTotal);
break;
default:
System.out.println("Invalid option.");
}
} while (ordering);
{
subTotal(quantity(), itemPrice(foodItem));
runningTotal = runningTotal + subTotal(quantity(), itemPrice(foodItem));
}
}
}
You are resetting the double runningTotal=0; in the while loop. Also the price returned by the itemPrice needs to be added into the runningTotal variable;
This is how your main method should look like. You are not required to call the subTotal method, once the user is done.
public static void main(String[] args) {
int menuOption;
int foodItem = 0;
input = new Scanner(System.in);
double runningTotal=0;
do{
menu();
menuOption = input.nextInt();
switch(menuOption){
case 1:
foodItem = 1;
runningTotal += itemPrice(foodItem);
break;
case 2:
foodItem = 2;
runningTotal += itemPrice(foodItem);
break;
case 3:
foodItem = 3;
runningTotal += itemPrice(foodItem);
break;
case 4:
done(runningTotal);
break;
default:
System.out.println("Invalid option.");
}
} while(ordering);
System.out.println("Total amount: " + runningTotal);
}
Output:
Welcome
1. Burger ($2.00)
2. Fries ($1.50)
3. Soda ($1.00)
4. Done
1
You've ordered a burger
Enter quantity
2
Subtotal: 4.0
Welcome
1. Burger ($2.00)
2. Fries ($1.50)
3. Soda ($1.00)
4. Done
2
You've ordered fries
Enter quantity
1
Subtotal: 1.5
Welcome
1. Burger ($2.00)
2. Fries ($1.50)
3. Soda ($1.00)
4. Done
4
3.5
Enjoy your meal
Total amount: 3.5
You reset runningTotal to 0 every iteration. I have attached a working example. I moved the total calculation to your subTotal method where it adds every subTotal as they come in.
import java.util.Scanner;
public class menu {
public double subTotal;
public static double runningTotal;
private static double itemPrice;
static boolean ordering = true;
static Scanner input = new Scanner(System.in);
public static void menu(){
System.out.println("Welcome \n1. Burger ($2.00) \n2. Fries ($1.50)\n3. Soda ($1.00) \n4. Done");
}
public static double itemPrice(int foodItem) {
if (foodItem == 1) {
//burger= $2.00
System.out.println("You've ordered a burger");
itemPrice = 2.00;
}
if (foodItem == 2) {
//fries = $1.50
System.out.println("You've ordered fries");
itemPrice = 1.50;
}
if (foodItem == 3) {
//soda = $1.00
System.out.println("You've ordered a soda");
itemPrice = 1.00;
}
quantity();
return itemPrice;
}
public static double quantity() {
System.out.println("Enter quantity");
double quantity = input.nextDouble();
subTotal(quantity, itemPrice);
return quantity;
}
public static double subTotal(double quantity, double itemPrice) {
double subTotal = quantity*itemPrice;
System.out.println("Subtotal: "+ subTotal);
runningTotal += subTotal;
return subTotal;
}
public static void done(){
ordering = false;
System.out.println(runningTotal);
System.out.println("Enjoy your meal");
}
public static void main(String[] args) {
int menuOption;
int foodItem = 0;
input = new Scanner(System.in);
do{
double runningTotal=0;
menu();
menuOption = input.nextInt();
switch(menuOption){
case 1:
foodItem = 1;
itemPrice(foodItem);
break;
case 2:
foodItem = 2;
itemPrice(foodItem);
break;
case 3:
foodItem = 3;
itemPrice(foodItem);
break;
case 4:
done();
break;
default:
System.out.println("Invalid option.");
}
} while(ordering); {
}
}
}
This will bring the exact output.
import java.util.Scanner;
public class Resteraunt
{
public double subTotal;
public static double runningTotal;
private static double itemPrice;
static boolean ordering = true;
static Scanner input = new Scanner(System.in);
static double j=0.0;
public static void main(String[] args) {
int menuOption;
int foodItem = 0;
input = new Scanner(System.in);
double runningTotal=0;
while(ordering)
{
menu();
menuOption = input.nextInt();
switch(menuOption){
case 1:
foodItem = 1;
runningTotal += ItemPrice(foodItem);
break;
case 2:
foodItem = 2;
runningTotal += ItemPrice(foodItem);
break;
case 3:
foodItem = 3;
runningTotal += ItemPrice(foodItem);
break;
case 4:
done(runningTotal);
break;
default:
System.out.println("Invalid option.");
}
}
System.out.println("Total amount: $" + runningTotal);
}
public static void menu() {
System.out.println("Welcome \n1. Burger ($2.00) \n2. Fries ($1.50)\n3. Soda
($1.00) \n4. Done");
}
public static double ItemPrice(int foodItem) {
if (foodItem == 1) {
// burger= $2.00
System.out.println("You've ordered a burger");
itemPrice = 2.00;
}
if (foodItem == 2) {
// fries = $1.50
System.out.println("You've ordered fries");
itemPrice = 1.50;
}
if (foodItem == 3) {
// soda = $1.00
System.out.println("You've ordered a soda");
itemPrice = 1.00;
}
quantity();
return j;
}
public static double quantity() {
System.out.println("Enter quantity");
double quantity = input.nextDouble();
subTotal(quantity, itemPrice);
return quantity;
}
public static double subTotal(double quantity, double itemPrice) {
double subTotal = quantity * itemPrice;
System.out.println("Subtotal: $" + subTotal);
j=subTotal;
return subTotal;
}
public static void done(double runningTotal) {
ordering = false;
System.out.println("Enjoy your meal");
}
}
I am working on a program for my CS class that involves methods and variables. I know it's a problem with the scope, or at least I think, but not sure how to solve. I can't use objects. The code isn't complete yet, because I'm currently just focused on this one problem.
import java.util.Scanner;
public class MethodBankingSystem {
Scanner input = new Scanner(System.in);
public static double Deposit(double userDeposit, double userBalance) {
System.out.print("Enter the amount you would like to deposit: ");
userDeposit = input.nextDouble(); //Create variable userDeposit for amount entered
userBalance = userBalance + userDeposit; //Replace value for userBalance
System.out.printf("New balance after deposit is: $" + "%.2f \n\n",userBalance );
return userBalance;
}
public static double Withdraw(double userWithdraw, double userBalance) {
System.out.print("Enter the amount you would like to withdraw: ");
userWithdraw = input.nextDouble(); //Create variable userWithdraw for amount entered
if (userWithdraw > userBalance) { //Create if/else statement to make sure enough funds are available
System.out.printf("Funds are not available for this withdraw \n\n");
}
else {
userBalance = userBalance - userWithdraw; //Replace value for userBalance
System.out.printf("New balance after withdraw is: $" + "%.2f \n\n",userBalance );
}
return userBalance;
}
public static void CurrentBalance(double userBalance) {
System.out.printf("Current balance is $" + "%.2f \n\n",userBalance );
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double userBalance = 100.00;
double userDeposit;
double userWithdraw;
double userInput;
String cardNumber;
String menu = "Welcome to Mustang Bank\nPlease choose from the options below:\n1 - Make a Deposit\n" +
"2 - Make a Withdraw\n3 - Check your Current Balance\n9 - Exit the Program\nEnter Option:\n";
do {
System.out.print(menu);
userInput = input.nextDouble();
switch (userInput) {
case '1':
Deposit(userDeposit, userBalance);
break;
case '2':
Withdraw(userWithdraw, userBalance);
break;
case '3':
CurrentBalance(userBalance);
break;
case '4':
CurrentBalance(userBalance);
break;
default:
System.out.println("This option is not acceptable.");
}
} while (userInput !=9);
}
}
Basically the problem is this:
static means that there is only one instance of that field per class (not instance). The field is essentially shared between all instances; modifying it in one instance will affect it in all instances. You can reference static fields within instances but you cannot statically reference instance members.
Either make input static (not really recommended):
static Scanner input = new Scanner(System.in);
Or make an instance of MethodBankingSystem and remove the static modifiers from your methods (this is what I think you are trying to do):
import java.util.Scanner;
public class MethodBankingSystem {
Scanner input = new Scanner(System.in);
public double Deposit(double userDeposit, double userBalance) {
System.out.print("Enter the amount you would like to deposit: ");
userDeposit = input.nextDouble(); //Create variable userDeposit for amount entered
userBalance = userBalance + userDeposit; //Replace value for userBalance
System.out.printf("New balance after deposit is: $" + "%.2f \n\n",userBalance );
return userBalance;
}
public double Withdraw(double userWithdraw, double userBalance) {
System.out.print("Enter the amount you would like to withdraw: ");
userWithdraw = input.nextDouble(); //Create variable userWithdraw for amount entered
if (userWithdraw > userBalance) { //Create if/else statement to make sure enough funds are available
System.out.printf("Funds are not available for this withdraw \n\n");
}
else {
userBalance = userBalance - userWithdraw; //Replace value for userBalance
System.out.printf("New balance after withdraw is: $" + "%.2f \n\n",userBalance );
}
return userBalance;
}
public void CurrentBalance(double userBalance) {
System.out.printf("Current balance is $" + "%.2f \n\n",userBalance );
}
public static void main(String[] args) {
new MethodBankingSystem();
}
MethodBankingSystem () {
Scanner input = new Scanner(System.in);
double userBalance = 100.00;
double userDeposit;
double userWithdraw;
double userInput;
String cardNumber;
String menu = "Welcome to Mustang Bank\nPlease choose from the options below:\n1 - Make a Deposit\n" +
"2 - Make a Withdraw\n3 - Check your Current Balance\n9 - Exit the Program\nEnter Option:\n";
do {
System.out.print(menu);
userInput = input.nextDouble();
switch (userInput) {
case '1':
Deposit(userDeposit, userBalance);
break;
case '2':
Withdraw(userWithdraw, userBalance);
break;
case '3':
CurrentBalance(userBalance);
break;
case '4':
CurrentBalance(userBalance);
break;
default:
System.out.println("This option is not acceptable.");
}
} while (userInput !=9);
}
}
I FIXED YOUR CODE GET IT BEFORE THE QUESTION GETS DELETED
Because I'm a nice guy I fixed your code. There were all sorts of issues, please read up on them. You create an instance of input twice, you are switching on a double you are calling Deposit(userDeposit, userBalance); without initialisinguserDeposit, you declare cardNumber but never instantiate or even try to use it anywhere, you have issues with static scoping.
import java.util.Scanner;
public class MethodBankingSystem {
public static void main(String[] args) {
new MethodBankingSystem();
}
Scanner input;
double userBalance = 100.00;
MethodBankingSystem() {
input = new Scanner(System.in);
int userInput;
String menu = "Welcome to Mustang Bank\nPlease choose from the options below:\n" + "1 - Make a Deposit\n"
+ "2 - Make a Withdraw\n" + "3 - Check your Current Balance\n"
+ "9 - Exit the Program\nEnter Option:\n";
do {
System.out.print(menu);
userInput = input.nextInt();
switch (userInput) {
case 1:
Deposit();
break;
case 2:
Withdraw();
break;
case 3:
CurrentBalance();
break;
case 9:
break;
default:
System.out.println("This option is not acceptable.");
}
} while (userInput != 9);
input.close();
}
public double Deposit() {
System.out.print("Enter the amount you would like to deposit: ");
double userDeposit = input.nextDouble(); // Create variable userDeposit for amount entered
userBalance = userBalance + userDeposit; // Replace value for userBalance
System.out.printf("New balance after deposit is: $" + "%.2f \n\n", userBalance);
return userBalance;
}
public double Withdraw() {
System.out.print("Enter the amount you would like to withdraw: ");
double userWithdraw = input.nextDouble(); // Create variable userWithdraw for amount entered
if (userWithdraw > userBalance) { // Create if/else statement to make sure enough funds are available
System.out.printf("Funds are not available for this withdraw \n\n");
} else {
userBalance = userBalance - userWithdraw; // Replace value for userBalance
System.out.printf("New balance after withdraw is: $" + "%.2f \n\n", userBalance);
}
return userBalance;
}
public void CurrentBalance() {
System.out.printf("Current balance is $" + "%.2f \n\n", userBalance);
}
}
This code converts from meters to feet/inches/kilometers. Sorry, I know this is basic, I'm new to Java. When prompted for a number of meters, if 4 or over is entered it doesn't convert and ends the program. Does anyone know what I did wrong?
import javax.swing.JOptionPane;
public class Measurements
{
public static void main(String[] args)
{
// Have user make selection
String input;
int selectedNum; // number chosen
float numMeters; // number of meters
input = JOptionPane.showInputDialog("Enter a distance in meters.");
numMeters = Float.parseFloat(input);
menu();
selectedNum = Integer.parseInt(input);
// Selection results
if (selectedNum < 1 )
{ errorMessage(); }
if (selectedNum == 1)
{ showKilometers(numMeters); }
if (selectedNum == 2)
{ showInches(numMeters); }
if (selectedNum == 3)
{ showFeet(numMeters); }
if (selectedNum == 4)
{
exitSystem();
}
}
/** errorMessage method */
public static double errorMessage()
{
double selectedNum;
String input;
input = JOptionPane.showInputDialog("That is not a valid selection. Please enter 1, 2, 3, or 4.");
selectedNum = Integer.parseInt(input);
return selectedNum;
}
/** menu method */
public static String menu()
{
String input;
input = JOptionPane.showInputDialog("Choose a selection:\n1. Convert to kilometers\n2. Convert to inches\n3. Convert "
+ "to feet\n4. Quit the program.");
return input;
}
/** showKilometers method */
public static void showKilometers(double numMeters)
{
double result;
result = numMeters*.001;
JOptionPane.showMessageDialog(null, numMeters + " meters is " + result + " kilometers.");
}
/** showInches method */
public static void showInches(double numMeters)
{
double inches;
inches = numMeters*(39.37);
JOptionPane.showMessageDialog(null, numMeters + " meters is " + inches + " inches.");
}
/** showFeet */
public static void showFeet(double numMeters)
{
double awesome;
awesome = (3.281*numMeters);
JOptionPane.showMessageDialog(null, numMeters + " meters is " + awesome + " feet.");
}
public static void exitSystem()
{
System.exit(0);
}
}
The String input in menu() is a different variable from the String input in main.
The menu function should be returning the value for selectedNum to use. Once that fix is made,
menu();
selectedNum = Integer.parseInt(input);
should be changed to:
selectedNum = Integer.parseInt(menu());
The input return from menu() is not the input in main, the input in main is always refer to the input of "Enter a distance in meters.", that is the reason why when user enter number of meters >=4 the program either exit or show error. Besides, I would prefer using switch than if else for condition switching scenario;
public static void main(String[] args)
{
double numMeters = Double.parseDouble(JOptionPane.showInputDialog("Enter a distance in meters."));
switch(menu()) {
case 1:
showKilometers(numMeters);
break;
case 2:
showInches(numMeters);
break;
case 3;
showFeet(numMeters);
break;
case 4;
exitSystem();
break;
default;
errorMessage();
break;
}
}
just use this
selectedNum = menu();
instead of
menu();
selectedNum = Integer.parseInt(input);
since the menu() function is already return integer value, no need to parse it.
however you can use switch statement as in this code
switch (selectedNum){
case 1: showKilometers(numMeters);
case 2: showInches(numMeters);
case 3: showFeet(numMeters);
case 4: exitSystem();
default: errorMessage();
}
String item1="Burger";
String item2="Softdrink";
int burger_price=40, sofdrink_price=20;
int x=0;
while(x==0){
System.out.println("Select item [1] burger [2] sofdrink");
int select=scan.nextInt();
switch(select){
case 1:
System.out.println("Enter Quantity: ");
int qty=scan.nextInt();
int total=qty*burger_price;
System.out.println("Item: Burger");
System.out.println("Quanity: "+qty);
System.out.println("Total: "+total);
x=1;
break;
case 2:
System.out.println("Enter Quantity: ");
int qty2=scan.nextInt();
int total2=qty2*sofdrink_price;
System.out.println("Item: Softdrink");
System.out.println("Quanity: "+qty2);
System.out.println("Total: "+total2);
x=1;
break;
default: System.out.println("Select [1] and [2] only");
}
}//end while`
MadProgrammer gave a really great explanation. I just put it together as a code. I tried to make minimum changes to your original code so that you understand better.
import java.util.Scanner;
public class BugerCheese {
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
int burger_price=40, sofdrink_price=20;
int qty1=0, qty2=0;
while(true){
System.out.println("Select item [1] burger [2] sofdrink");
int select=scan.nextInt();
switch(select){
case 1:
System.out.println("Enter Quantity: ");
qty1 += scan.nextInt();
break;
case 2:
System.out.println("Enter Quantity: ");
qty2 += scan.nextInt();
break;
default: System.out.println("Select [1] and [2] only");
}
System.out.println("Enter 9 to add more items. Enter any other key to calculate total");
if(9!=scan.nextInt()){
break;
}
}
if(qty1>0){
int total=qty1*burger_price;
System.out.println("Item: Burger");
System.out.println("Quanity: "+qty1);
System.out.println("Total: "+total);
}
if(qty2>0){
int total2=qty2*sofdrink_price;
System.out.println("Item: Softdrink");
System.out.println("Quanity: "+qty2);
System.out.println("Total: "+total2);
}
System.out.println("GrandTotal: "+(qty1*burger_price+qty2*sofdrink_price));
}
}
First, you need some kind of loop...
boolean done = false;
do {
//...
} while (!done);
Then you need to provide some way for the user to exit the loop
boolean done = false;
do {
System.out.println("Select item [1] burger [2] sofdrink or [0] when you're done");
//...
switch(select){
case 0:
done = true;
break;
}
} while (!done);
Now, you need someway to keep a running tally of what's been ordered, since you only have two items, you could use two variables...
int burgerCount = 0;
int softDrinkCount = 0;
boolean done = false;
do {
//...
Now you can just increment the counters based on the user selection...
Now, automatically, you need to ask the user for two things, what they want and how much they want, you could simplify the process a bit by been a little clever...
System.out.println("Select item [1] burger [2] sofdrink or [0] when you're done");
int select=scan.nextInt();
scan.nextLine();
switch (select) {
case 0:
done = true;
break;
case 1:
case 2:
System.out.println("Enter Quantity: ");
int qty=scan.nextInt();
scan.nextLine();
switch (select) {
case 1:
burgerCount += qty;
break;
case 1:
softDrinkCount += qty;
break;
}
break;
}
// Show details
Make sure, you call nextLine after you read the nextInt, as there is still a carriage return in the buffer ;)
Then you can display running a tally...
//...
System.out.println("You've ordered...");
double burgerTally = burger_price * burgerCount;
double drinkTally = burger_price * drinkTally;
System.out.println(burgerCount + " burgers # $" + burger_price + " for a sub total of $" + burgerTally);
System.out.println(softDrinkCount + " soft drinks # $" + sofdrink_price + " for a sub total of $" + drinkTally);
System.out.println("For a total of $" + (burgerTally + drinkTally));
} while (!done);
Because of the way the code is laid out, the tally will be displayed regardless of what you pick...
Now, if you have more items, then an array is going to come in very handy and will reduce the over all amount of code you might need...
So I just learned about Constructors. I wrote this program myself, and I want to incorporate constructors into this program(AS it could use it!). Also I get null instead of a symbol type but other than that the program runs fine(Thinking maybe constructors can fix the null).
My main issue currently is that I don't want to take out the class called "Options"(or is it called a method class?). But yet I still want to use the constructor, to get rid of "choice" and "getOperatives".
While I'm aware my "Options" class(Should it be capitalized), can just simply go in main, as it's only going to be used once, I'm focusing on understanding the concepts not so much it being robust(but still being robust).
//Call objects
Scanner scan = new Scanner(System.in);
css cssAccess = new css();
//Call Variables
int choice;
int x,y;
int total;
String type;
String str;
//Give Choices & Input Choice
choice = scan.nextInt();
//Get 2 itmes to operate on
System.out.println("Please enter two items to operate on");
x = scan.nextInt();
y = scan.nextInt();
//Pass in operatives AND then choice
cssAccess.getOperatives(x,y);
cssAccess.arithmeticDecision(choice);
//Grab total
total = cssAccess.answer();
//Grab type of operation user entered
type = cssAccess.arithmeticSymbol();
//Output Results
System.out.println(x + type + y + " = " + total);
And Here is the file with all my classes in it
public class css {
//Declare Variables
private int getChoice;
private int input1,input2;
private int total;
private String symbol;
int refchoice;
public void choice(int choice){
getChoice = choice;
}
public void getOperatives(int x, int y){
input1 = x;
input2 = y;
}
public void Options(){
System.out.println("Would you like to add(1)");
System.out.println("Would you like to subtract(2)");
System.out.println("Would you like to multiply(3)");
System.out.println("Would you like to Divide(4)");
System.out.println("Or would you like to find the remainder(5)");
}
public void arithmeticDecision(int choice){
choice = refchoice;
switch (refchoice){
case 5:total = input1 % input2;
break;
case 4:total = input1 / input2;
break;
case 3:total = input1 * input2;
break;
case 2:total = input1 - input2;
break;
case 1:total = input1 + input2;
break;
}
}
public int answer(){
return total;
}
public String arithmeticSymbol(){
switch (Integer.toString(refchoice)){
case "5":symbol = "%";
break;
case "4":symbol = "/";
break;
case "3":symbol = "*";
break;
case "2":symbol = "-";
break;
case "1":symbol = "+";
break;
}
return symbol;
}
}