background: I haven't taken java in a few semesters. It is safe to say that I'm really rusty with it and I don't remember a lot of the fundamentals. This semester I am taking Adv. Java and an Android class. Because my java skills are seriously lacking I've decided to make this thread to ask questions about the code I've written. Also to maybe get help fixing my code. This thread is very similar to those"Make me a better programmer" threads I've seen on these fourms.
Currently for class I'm making a two separate 'tip' calculators using java
The
first tip calculator is to only use the main method, to execute all the code.
While the
second tip calculator is to use a calculation class / printer class alongside the main method.
My teacher said you could just do the tip calculator 'right' (not just main method) and not worry about the first way. But since I feel like I'm relearning all this I decided to start with the main method way. To at least to get my output looking correct.
*Sidenote: I'm currently using notepad++ // cmd prompt for this assignment -- will be looking for a better environment for java after this assignment (any suggestions welcomed) in my Android class we are using android studio -- not sure if I can use that for strictly java assignments (havn't looked into yet)
first tip calculatorCode
import java.text.*;
import java.util.*;
public class week1lab1 {
public static void main(String[] args) {
//General scanner used to allow user input
//(self note: the java.util.* import includes the util.scanner which is needed for this code to run)
Scanner user_input = new Scanner(System.in);
DecimalFormat df = new DecimalFormat("#.##");
//not optional declares, asks for bill amount, than prompts user input.
//in lab2 make it
double currentBillAmount;
System.out.println("Enter current bill amount: ");
currentBillAmount = Double.parseDouble(user_input.next()); //Since user_input is seen as String converted to double
//declares, defines, asks for optional tip amount
int customTipNumber;
customTipNumber = 0; //Bad practice?
System.out.println("Enter custom tip (optional): ");
// Exception handles if a non int was entered
// user_input seems to require some sort of input, hitting enter doesn't do anything.
// Could just ask user to type 'anything' if they don't want a tip
try {
customTipNumber = user_input.nextInt();
}
catch (InputMismatchException e) {
user_input.nextLine();
}
//Basic math below
//variable names kind of unclear good spacing though
double tenPercentTip = (.1 * currentBillAmount);
double tenPercentTotal = (currentBillAmount + tenPercentTip);
double fifteenPercentTip = (.15 * currentBillAmount);
double fifteenPercentTotal = (currentBillAmount + fifteenPercentTip);
double eighteenPercentTip = (.18 * currentBillAmount);
double eighteenPercentTotal = (currentBillAmount + eighteenPercentTip);
double customPercentTip = (customTipNumber * currentBillAmount * .01);
double customPercentTotal = (currentBillAmount + customPercentTip);
//prints output, spacing was added for reading purposes
//DecimalFormat was used -- in order to use you need the import.text.*; (not sure which one specifically)
System.out.println("Bill Total: " + currentBillAmount + "\n");
System.out.println(" 10% 15% 18%");
System.out.println("Tip: " + df.format(tenPercentTip) + " " + df.format(fifteenPercentTip)
+ " " + df.format(eighteenPercentTip));
System.out.println("Total $: " + df.format(tenPercentTotal) + " " + df.format(fifteenPercentTotal)
+ " " + df.format(eighteenPercentTotal) +"\n");
System.out.println("Custom Tip%: " + customTipNumber + "\nTip: " + df.format(customPercentTip)
+ " Total $: " + df.format(customPercentTotal));
}
}
With this it mostly does whats required.
The problem I'm having is with the custom tip being "optional"
With this code if I just hit the 'enter' key after being prompted for user_input in command prompt it does nothing but go down a line, I would like it when the user hits enter for it to react the same way as the exception catch does. Ran out of ideas for that, suggestions?
Also if I had to guess defining my customTipNumber default as 0 is a bad practice. Comments?
is the way I found for 2 decimal places (the df.format) the way you would have choose?
I will be posting my solution for
second tip calculator + probably some questions with it -- tomorrow. HOPEFULLY it will be a lot cleaner / not such an eye sore as this one!
Thanks for your time! and please excuse my ignorance!!