snippetjavaMinor
Prompting for values to plug into a formula — how to do it elegantly
Viewed 0 times
intopromptingelegantlyforhowvaluesplugformula
Problem
This is my first program in java. I am wondering if there would be a better way to tackle this. I am only allowed to use one class. It's not quite finished, but I am thinking it doesn't look very elegant.
```
import java.io.*;
public class CarHire
{
public static void main(String[] args) throws IOException
{
BufferedReader stdin = new BufferedReader(new InputStreamReader (System.in));
//customer details
System.out.println("Input customer's name: ");
String name = stdin.readLine();
System.out.println("Input customer's address: ");
String address = stdin.readLine();
System.out.println("Input customer's telephone: ");
String telephone = stdin.readLine();
System.out.println("Input customer's licence: ");
String licence = stdin.readLine();
System.out.println("Input customer's credit card number: ");
String creditCard = stdin.readLine();
System.out.println("Input customer's credit card expiry date: ");
String cardExpiry = stdin.readLine();
//days booked for hire
System.out.println("Input number of days for hire: ");
int daysBooked = Integer.parseInt (stdin.readLine());
//car details
System.out.println("Input make/model: ");
String makeModel = stdin.readLine();
System.out.println("Input registration number: ");
String regNumber = stdin.readLine();
System.out.printf("%-20s %n %-40s %n","Select a car size:", "Small (S), Medium (M) or Large (L):");
String carSize = stdin.readLine();
//daily hire rate
double dailyRate;
if (carSize.equalsIgnoreCase("S") == true)
{
dailyRate=80;
}
else if (carSize.equalsIgnoreCase("M") == true)
{
dailyRate=110;
}
else
{
dailyRate=140;
}
//Calculate stage 1 hire
double basicH
```
import java.io.*;
public class CarHire
{
public static void main(String[] args) throws IOException
{
BufferedReader stdin = new BufferedReader(new InputStreamReader (System.in));
//customer details
System.out.println("Input customer's name: ");
String name = stdin.readLine();
System.out.println("Input customer's address: ");
String address = stdin.readLine();
System.out.println("Input customer's telephone: ");
String telephone = stdin.readLine();
System.out.println("Input customer's licence: ");
String licence = stdin.readLine();
System.out.println("Input customer's credit card number: ");
String creditCard = stdin.readLine();
System.out.println("Input customer's credit card expiry date: ");
String cardExpiry = stdin.readLine();
//days booked for hire
System.out.println("Input number of days for hire: ");
int daysBooked = Integer.parseInt (stdin.readLine());
//car details
System.out.println("Input make/model: ");
String makeModel = stdin.readLine();
System.out.println("Input registration number: ");
String regNumber = stdin.readLine();
System.out.printf("%-20s %n %-40s %n","Select a car size:", "Small (S), Medium (M) or Large (L):");
String carSize = stdin.readLine();
//daily hire rate
double dailyRate;
if (carSize.equalsIgnoreCase("S") == true)
{
dailyRate=80;
}
else if (carSize.equalsIgnoreCase("M") == true)
{
dailyRate=110;
}
else
{
dailyRate=140;
}
//Calculate stage 1 hire
double basicH
Solution
Since you can only use this one class, the key to having better, more beautiful code is to encapsulate logic in reusable and well-named methods. Below is a modified version of the code which follows some basic design principles, which I will explain piece by piece. Also, you have a lot of variables that you declare and never use, but I left them there under the assumption that they were going to be used in a future release.
So let's walk through it!
I changed your
```
//Customer Details
String name = getInput("customer's name");
String address = getInput("customer's address");
String telephone = getInput("customer's telephone");
String license = getInput("customer's license");
String creditCard = getInput("customer's credit card number");
String c
import java.util.Scanner;
public class CarHire {
private static final Scanner STDIN = new Scanner(System.in);
public static void main(String[] args) {
//Customer Details
String name = getInput("customer's name");
String address = getInput("customer's address");
String telephone = getInput("customer's telephone");
String license = getInput("customer's license");
String creditCard = getInput("customer's credit card number");
String cardExpiry = getInput("customer's credit card expiry date");
//Days Booked for Hire
int daysBooked = getIntegerInput("number of days for hire");
//Car Details
String makeModel = getInput("make/model");
String regNumber = getInput("registration number");
String carSize = getInput("car size");
//Daily Hire Rate
int dailyRate = 0;
if(carSize.equalsIgnoreCase("s")) {
dailyRate = 80;
}
else if(carSize.equalsIgnoreCase("m")) {
dailyRate = 110;
}
else if(carSize.equalsIgnoreCase("l")) {
dailyRate = 140;
}
else {
//TODO - what will you do if this is invalid?
}
//Calculate Stage 1 Hire
int basicHire = dailyRate * daysBooked;
System.out.println("Basic Hire is: " + basicHire);
int daysHired = getIntegerInput("number of days hired");
int lateDays = daysHired - daysBooked;
int lateFee = lateDays > 0 ? (2 * dailyRate * lateDays) : 0;
//My Test Line
int subTotal = basicHire + lateFee;
double finalHire = processFinalHire(basicHire, lateFee);
System.out.println("Final hire: " + finalHire);
}
private static double processFinalHire(double basicHire, double lateFee) {
String input = "";
String damage, infringement;
double repairTotal = 0.0;
double finesTotal = 0.0;
while(!input.equalsIgnoreCase("x")) {
input = getInput("(Damage = D, Infringements = F, Exit = X");
if(input.equalsIgnoreCase("d")) {
damage = getInput("damage description");
repairTotal += getDoubleInput("repair costs");
}
else if(input.equalsIgnoreCase("f")) {
infringement = getInput("infringement type");
finesTotal += getDoubleInput("fine");
}
else if(!input.equalsIgnoreCase("x")) {
System.out.println("Please enter a valid option.");
}
}
return lateFee + basicHire + repairTotal + finesTotal;
}
private static String getInput(String prompt) {
System.out.print("Input " + prompt + ": ");
return STDIN.nextLine().trim();
}
private static int getIntegerInput(String prompt) {
Integer returnValue = null;
while(returnValue == null) {
System.out.print("Input " + prompt + ": ");
try {
returnValue = Integer.parseInt(STDIN.nextLine().trim());
}
catch(NumberFormatException nfe) {
System.out.println("Please input a valid integer.");
}
}
return returnValue;
}
private static double getDoubleInput(String prompt) {
Double returnValue = null;
while(returnValue == null) {
String input = getInput(prompt);
try {
returnValue = Double.parseDouble(input);
}
catch(NumberFormatException nfe) {
System.out.println("Please input a valid decimal.");
}
}
return returnValue;
}
}So let's walk through it!
private static final Scanner STDIN = new Scanner(System.in);I changed your
BufferedReader to a Scanner in order to avoid your having to handle IOExceptions in its initialization (or ignore them by having that ugly throws IOException clause on your main method, haha). This is just an easier API to use at this level. I've also moved it to be a private static final field for the entire CarHire class. This will allow it to be referenced from all of the methods we create from here on out.```
//Customer Details
String name = getInput("customer's name");
String address = getInput("customer's address");
String telephone = getInput("customer's telephone");
String license = getInput("customer's license");
String creditCard = getInput("customer's credit card number");
String c
Code Snippets
import java.util.Scanner;
public class CarHire {
private static final Scanner STDIN = new Scanner(System.in);
public static void main(String[] args) {
//Customer Details
String name = getInput("customer's name");
String address = getInput("customer's address");
String telephone = getInput("customer's telephone");
String license = getInput("customer's license");
String creditCard = getInput("customer's credit card number");
String cardExpiry = getInput("customer's credit card expiry date");
//Days Booked for Hire
int daysBooked = getIntegerInput("number of days for hire");
//Car Details
String makeModel = getInput("make/model");
String regNumber = getInput("registration number");
String carSize = getInput("car size");
//Daily Hire Rate
int dailyRate = 0;
if(carSize.equalsIgnoreCase("s")) {
dailyRate = 80;
}
else if(carSize.equalsIgnoreCase("m")) {
dailyRate = 110;
}
else if(carSize.equalsIgnoreCase("l")) {
dailyRate = 140;
}
else {
//TODO - what will you do if this is invalid?
}
//Calculate Stage 1 Hire
int basicHire = dailyRate * daysBooked;
System.out.println("Basic Hire is: " + basicHire);
int daysHired = getIntegerInput("number of days hired");
int lateDays = daysHired - daysBooked;
int lateFee = lateDays > 0 ? (2 * dailyRate * lateDays) : 0;
//My Test Line
int subTotal = basicHire + lateFee;
double finalHire = processFinalHire(basicHire, lateFee);
System.out.println("Final hire: " + finalHire);
}
private static double processFinalHire(double basicHire, double lateFee) {
String input = "";
String damage, infringement;
double repairTotal = 0.0;
double finesTotal = 0.0;
while(!input.equalsIgnoreCase("x")) {
input = getInput("(Damage = D, Infringements = F, Exit = X");
if(input.equalsIgnoreCase("d")) {
damage = getInput("damage description");
repairTotal += getDoubleInput("repair costs");
}
else if(input.equalsIgnoreCase("f")) {
infringement = getInput("infringement type");
finesTotal += getDoubleInput("fine");
}
else if(!input.equalsIgnoreCase("x")) {
System.out.println("Please enter a valid option.");
}
}
return lateFee + basicHire + repairTotal + finesTotal;
}
private static String getInput(String prompt) {
System.out.print("Input " + prompt + ": ");
return STDIN.nextLine().trim();
}
private static int getIntegerInput(String prompt) {
Integer returnValue = null;
while(returnValue == null) {
System.out.print("Input " + prompt + private static final Scanner STDIN = new Scanner(System.in);//Customer Details
String name = getInput("customer's name");
String address = getInput("customer's address");
String telephone = getInput("customer's telephone");
String license = getInput("customer's license");
String creditCard = getInput("customer's credit card number");
String cardExpiry = getInput("customer's credit card expiry date");private static String getInput(String prompt) {
System.out.print("Input " + prompt + ": ");
return STDIN.nextLine().trim();
}//Days Booked for Hire
int daysBooked = getIntegerInput("number of days for hire");Context
StackExchange Code Review Q#31666, answer score: 4
Revisions (0)
No revisions yet.