Created
October 20, 2013 18:37
-
-
Save benjic/7073493 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* AlternativeTrip.java | |
* Date: 10/17/13 | |
* Author: Benjamin Campbell <bc223913@umconnect.umt.edu> | |
* Abstract: A simple application that allows users to submit travel | |
* mechanisms that implement the BUsWalkBike class. | |
*/ | |
package com.benjica.csci135.hw4; | |
import java.text.ParseException; | |
import java.text.SimpleDateFormat; | |
import java.util.ArrayList; | |
import java.util.Date; | |
import java.util.Scanner; | |
import com.benjica.csci135.hw4.BusWalkBike.TravelMode; | |
public class AlternativeTrip { | |
public static void main(String[] args) { | |
// Initialize utility classes | |
ArrayList<BusWalkBike> list = new ArrayList<BusWalkBike>(); | |
Scanner scan = new Scanner(System.in); | |
SimpleDateFormat df = new SimpleDateFormat("mm/dd/yyyy"); | |
// Start iterating over user input for creating new routes | |
do { | |
// Reinitialize key variables for testing | |
TravelMode mode = null; | |
Date date = null; | |
// Determine mode of transportation and reiterate until valid data. | |
do { | |
// Prompt user for input | |
System.out.println("What mode of transportation did you use[bus|walk|bike]?"); | |
// Iterate over response | |
switch(scan.nextLine().toLowerCase()) { | |
case "bus": // Set mode to BUS | |
mode = TravelMode.BUS; | |
break; | |
case "walk": // set mode to WALK | |
mode = TravelMode.WALK; | |
break; | |
case "bike": // set mode to BIKE | |
mode = TravelMode.BIKE; | |
break; | |
default: | |
// Let user know the input was not reconized as valid | |
System.out.println("Your mode was unreconized please enter bus, walk, or bike."); | |
break; | |
} | |
// Test to see if mode was set and if not resubmit | |
} while (!(mode != null)); | |
// Input route name | |
System.out.println("Please enter your route name:"); | |
String name = scan.nextLine(); | |
// Input distance | |
System.out.println("How far did you travel in miles?"); | |
float distance = scan.nextFloat(); | |
// Input duration | |
System.out.println("How long did your route take in hours? (0.0)"); | |
float duration = scan.nextFloat(); | |
// Input date | |
System.out.println("Please enter the date of your route.[mm/dd/yyyy]"); | |
String dateInput = scan.next(); | |
// Try to parse date | |
try { | |
date = df.parse(dateInput); | |
} catch (ParseException e) { | |
// TODO Reiterate over input if bad date is found | |
e.printStackTrace(); | |
} | |
// Add a new BusWakBike to list | |
list.add(new BusWalkBike(mode, name, distance, duration, date)); | |
System.out.println("Route added!\n\n"); | |
// Prompt if user has more data | |
System.out.println("Would you like to enter another entry? [Yn]"); | |
// test to add another route | |
} while (scan.next().toLowerCase().charAt(0) != 'n'); | |
// Close of resources | |
scan.close(); | |
// Iterate over list | |
for( BusWalkBike bwb : list) { | |
// Printout overview | |
System.out.println(bwb.toString()); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* File: BusWalkBike.java | |
* Date: 10/17/13 | |
* Author: Benjamin Campbell <bc223913@umconnect.umt.edu> | |
* Comments: BusWalkBike class abstraction. | |
* | |
*/ | |
package com.benjica.csci135.hw4; | |
import java.text.SimpleDateFormat; | |
import java.util.Date; | |
import java.util.Formatter; | |
public class BusWalkBike { | |
// Create constants for magic numbers | |
private final double GAS_SAVED_COEFFICENT = 0.437; | |
/** | |
* A simple enumeration to differentiate different modes of transportation | |
* @author benjic | |
* | |
*/ | |
public enum TravelMode { | |
WALK, | |
BIKE, | |
BUS | |
} | |
// Create properties for the class as specified by Micheal Cassesn | |
private TravelMode mMode; | |
private String mRoute; | |
private float mDistanceTraveled; | |
private float mDuration; | |
private Date mDate; | |
// A constructor that takes values as specified by Micheal Cassens | |
public BusWalkBike(TravelMode mode, String routeName, float distance, float duration, Date when ) { | |
mMode = mode; | |
mRoute = routeName; | |
mDistanceTraveled = distance; | |
mDuration = duration; | |
mDate = when; | |
} | |
/** | |
* calculates the amount of gas saved by taking this alternative route | |
* @return a double representing the number of gallons saved | |
*/ | |
public double gasSaved() { | |
return mDistanceTraveled * GAS_SAVED_COEFFICENT; | |
} | |
/** | |
* A string value of the class | |
*/ | |
public String toString() { | |
StringBuilder sb = new StringBuilder(); | |
Formatter fmt = new Formatter(sb); | |
SimpleDateFormat dateFMT = new SimpleDateFormat("mm/dd/yyyy"); | |
fmt.format("You used the %s mode on %s using the %s route for a distance of %.2f, which took %f.2 hours, which saved %.3f gallons of gas.", | |
mMode, | |
dateFMT.format(mDate), | |
mRoute, | |
mDistanceTraveled, | |
mDuration, | |
gasSaved() | |
); | |
fmt.close(); | |
return sb.toString(); | |
} | |
// All the getters and setters as prescribed | |
public TravelMode getMode() { | |
return mMode; | |
} | |
public void setMode(TravelMode mode) { | |
this.mMode = mode; | |
} | |
public String getRoute() { | |
return mRoute; | |
} | |
public void setRoute(String route) { | |
this.mRoute = route; | |
} | |
public float getDistanceTraveled() { | |
return mDistanceTraveled; | |
} | |
public void setDistanceTraveled(float distanceTraveled) { | |
this.mDistanceTraveled = distanceTraveled; | |
} | |
public float getDuration() { | |
return mDuration; | |
} | |
public void setDuration(float duration) { | |
this.mDuration = duration; | |
} | |
public Date getDate() { | |
return mDate; | |
} | |
public void setDate(Date date) { | |
this.mDate = date; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment