码迷,mamicode.com
首页 > 其他好文 > 详细

COSC1284 Programming Techniques

时间:2019-04-06 00:10:49      阅读:114      评论:0      收藏:0      [点我收藏+]

标签:name   travel   seed   sub   ram   array   lag   class   ast   

package Model;

import error.MyException;
import utils.DateTime;

public class Car {
	private String regNo;
	private String make;
	private String model;
	private String driverName;
	
	private int passengerCapacity;
	private boolean available = false;
	
	private Booking[] currentBookings;
	private Booking[] pastBookings;

	public boolean checkRegNo(String regNo) {
		int LENGTH = 6;
		int len = regNo.length();
		int preLen = 3;
		if(len != LENGTH) {
			return false;
		}
		
		int i = 0;
		while(i < preLen) {
			if(!Character.isLetter(regNo.charAt(i))) {
				return false;
			}
			i++;
		}

		while(i < LENGTH) {
			if(!Character.isDigit(regNo.charAt(i))) {
				return false;
			}
			i++;
		}
		
		return true;
	}
	
	public boolean checkPassengerCapacity(int passengerCapacity) {
		if(passengerCapacity > 0 && passengerCapacity < 10) {
			return true;
		} else {
			return false;
		}
	}
	
	public boolean check(String regNo, int passengerCapacity) {
		if(checkRegNo(regNo) && checkPassengerCapacity(passengerCapacity)) {
			return true;
		} else {
			return false;
		}
		
	}
	
	public Car(String regNo, String make, String model, String driverName, int passengerCapacity) throws MyException {
		boolean flag = check(regNo, passengerCapacity);
		if(flag) {
			this.regNo = regNo;
			this.make = make;
			this.model = model;
			this.driverName = driverName;
			this.passengerCapacity = passengerCapacity;
		} else {
			throw new MyException("invalid input param");
		}

	}
	

	public boolean book(String firstName, String lastName, DateTime required, int numPassengers) {
		
		return available;
	}
	
	
	public String getDetails() {
		StringBuilder sb = new StringBuilder();
		sb.append(String.format("RegNo:        %s\n", this.regNo));
		sb.append(String.format("Make & Model: %s %s\n", this.make, this.model));
		sb.append(String.format("Driver Name:  %s\n", this.driverName));
		sb.append(String.format("Capacity:     %s\n", this.passengerCapacity));
		if(this.available) {
			sb.append(String.format("Available:    %s\n", "YES"));
		} else {
			sb.append(String.format("Available:    %s\n", "NO"));
		}
		
		return sb.toString();
	}
	
	@Override
	public String toString() {
		String format = "%s:%s:%s:%s:%s:%s\n";
		if(this.available) {
			return String.format(format, this.regNo, 
					this.make, 
					this.model, 
					this.driverName, 
					this.passengerCapacity, 
					"YES");
		} else {
			return String.format(format, 
					this.regNo, 
					this.make, 
					this.model, 
					this.driverName, 
					this.passengerCapacity, 
					"NO");
		}
	}
	
	
	public String getRegNo() {
		return regNo;
	}


	public void setRegNo(String regNo) {
		this.regNo = regNo;
	}


	public String getMake() {
		return make;
	}


	public void setMake(String make) {
		this.make = make;
	}


	public String getModel() {
		return model;
	}


	public void setModel(String model) {
		this.model = model;
	}


	public String getDriverName() {
		return driverName;
	}


	public void setDriverName(String driverName) {
		this.driverName = driverName;
	}


	public int getPassengerCapacity() {
		return passengerCapacity;
	}


	public void setPassengerCapacity(int passengerCapacity) {
		this.passengerCapacity = passengerCapacity;
	}


	public boolean isAvailable() {
		return available;
	}


	public void setAvailable(boolean available) {
		this.available = available;
	}


	public Booking[] getCurrentBookings() {
		return currentBookings;
	}


	public void setCurrentBookings(Booking[] currentBookings) {
		this.currentBookings = currentBookings;
	}


	public Booking[] getPastBookings() {
		return pastBookings;
	}


	public void setPastBookings(Booking[] pastBookings) {
		this.pastBookings = pastBookings;
	}


	public static void main(String[] args) throws Exception {
		Car c = new Car("SIM194", "Honda", "Accord Euro", "Henry Cavill", 5);
		System.out.print(c.getDetails());
		System.out.print(c.toString());
		
		System.out.println(c.checkRegNo("a1c12a"));
		System.out.println(Character.isLetter(‘a‘));
	}

}

  

package Model;

import utils.DateTime;

public class Booking {
	
	private String id;
	private double bookingFee;
	private DateTime pickUpDateTime;
	private String firstName;
	private String lastName;
	private int numPassengers;
	private double kilometersTravelled;
	private double tripFee;
	private Car car;
	
	public Booking() {
		
	}
	
	public Booking(String firstName, String lastName, DateTime required, int numPassengers, Car car) {
		this.firstName = firstName;
		this.lastName = lastName;
		this.pickUpDateTime = required;
		this.numPassengers = numPassengers;
		this.car = car;
		
		String format = "%s_%s%s_%s";
		this.id = String.format(format, 
				this.car.getRegNo(), 
				this.firstName.substring(0, 3).toUpperCase(),
				this.lastName.substring(0, 3).toUpperCase(),
				this.pickUpDateTime.getFormattedDate());
	}
	
	
	public void calculateTripFee() {
		Double tripFee = this.bookingFee * 0.3 * this.kilometersTravelled;
		tripFee = Double.valueOf(String.format("%.2f", tripFee));
		this.tripFee = tripFee;
	}
	
	public void increKilometersTravelled(double kilometers) {
		this.kilometersTravelled = this.kilometersTravelled + kilometers;
	}
	
	
	public String getDetails() {
		calculateTripFee();
		StringBuilder sb = new StringBuilder();
		sb.append(String.format("id:          %s\n", this.id));
		sb.append(String.format("Booking Fee: $%s\n", this.bookingFee));
		sb.append(String.format("Pick up Date:%s\n", this.pickUpDateTime));
		sb.append(String.format("Name:        %s %s\n", this.firstName, this.lastName));
		sb.append(String.format("Passengers:  %s\n", this.numPassengers));
		sb.append(String.format("Travelled:   %s\n", this.kilometersTravelled));
		sb.append(String.format("Trip Fee:    %s\n", this.tripFee));
		sb.append(String.format("Car Id:      %s\n", car.getRegNo()));
		
		return sb.toString();
	}
	
	public String toString() {
		calculateTripFee();
		String format = "%s:%s:%s:%s %s:%s:%s:%s:%s";
		return String.format(format, 
				this.id, 
				this.bookingFee, 
				this.pickUpDateTime, 
				this.firstName, this.lastName, 
				this.numPassengers,
				this.kilometersTravelled,
				this.tripFee,
				this.car.getRegNo()
				);
	}
	
	
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public double getBookingFee() {
		return bookingFee;
	}

	public void setBookingFee(double bookingFee) {
		this.bookingFee = bookingFee;
	}

	public DateTime getPickUpDateTime() {
		return pickUpDateTime;
	}

	public void setPickUpDateTime(DateTime pickUpDateTime) {
		this.pickUpDateTime = pickUpDateTime;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public int getNumPassengers() {
		return numPassengers;
	}

	public void setNumPassengers(int numPassengers) {
		this.numPassengers = numPassengers;
	}

	public double getKilometersTravelled() {
		return kilometersTravelled;
	}

	public void setKilometersTravelled(double kilometersTravelled) {
		this.kilometersTravelled = kilometersTravelled;
	}

	public double getTripFee() {
		return tripFee;
	}

	public void setTripFee(double tripFee) {
		this.tripFee = tripFee;
	}

	public Car getCar() {
		return car;
	}

	public void setCar(Car car) {
		this.car = car;
	}

	public static void main(String[] args) throws Exception {

		String firstName = "Jack";
		String lastName = "Tom";
		DateTime dataTime = new DateTime();
		int numPassengers = 4;
		Car c = new Car("SIM194", "Honda", "Accord Euro", "Henry Cavill", 5);
		
		Booking booking = new Booking(firstName, lastName, dataTime, numPassengers, c);
		booking.setBookingFee(1.5);
		booking.setKilometersTravelled(49);
		
		System.out.println(booking.toString());
		System.out.println();
		
		System.out.println(booking.getDetails());
		
	}
	
	
}

  

package main;

public class MenuItem {
	private String desc;
	private String code;
	
	public MenuItem(String desc, String code) {
		this.desc = desc;
		this.code = code;
	}

	public String getDesc() {
		return desc;
	}

	public void setDesc(String desc) {
		this.desc = desc;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}
	
	public String toString() {
		return String.format("%-30s", this.desc) + this.code + "\n";
	}
}

  

package main;

import java.util.ArrayList;

public class Menu {
	ArrayList<MenuItem> menu;
	
	public Menu() {
		menu = new ArrayList<>();
	}
	
	public void loadData() {
		menu.add(new MenuItem("Create Car", "CC"));
		menu.add(new MenuItem("Book Car", "BC"));
		menu.add(new MenuItem("Complete Booking", "CB"));
		menu.add(new MenuItem("Display ALL Cars", "DA"));
		menu.add(new MenuItem("Search Specific Car", "SS"));
		menu.add(new MenuItem("Search available cars", "SA"));
		menu.add(new MenuItem("Seed Data", "SD"));
		menu.add(new MenuItem("Exit Program", "EX"));
		
	}
	
	public void display() {
		System.out.println("*** MiRides System Menu ***");
		for(MenuItem item : menu) {
			System.out.print(item.toString());
		}
		System.out.println("*** ==================== ***");
	}
	
	public static void main(String[] args) {
		Menu m = new Menu();
		m.loadData();
		m.display();
		
	}
}

  

package utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.sql.Date;

/*
 * Class:			DateTime
 * Description:		The class represents a specific date
 * Author:			Rodney Cocker & Charles Thevathayan
 */
public class DateTime
{
	
	private long advance;
	private long time;
	
	public DateTime()
	{
		time = System.currentTimeMillis();
	}
	
	public DateTime(int setClockForwardInDays)
	{
		advance = ((setClockForwardInDays * 24L + 0) * 60L) * 60000L;
		time = System.currentTimeMillis() + advance;
	}
	
	public DateTime(DateTime startDate, int setClockForwardInDays)
	{
		advance = ((setClockForwardInDays * 24L + 0) * 60L) * 60000L;
		time = startDate.getTime() + advance;
	}
	
	public DateTime(int day, int month, int year)
	{
		setDate(day, month, year);
	}
	
	public long getTime()
	{
		return time;
	}
	
	public String toString()
	{
		long currentTime = getTime();
		Date gct = new Date(currentTime);
		return gct.toString();
	}
	
	public static String getCurrentTime()
	{
		Date date = new Date(System.currentTimeMillis());  // returns current Date/Time
		return date.toString();
	}
	
	public String getFormattedDate()
	{
		SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
		long currentTime = getTime();
		Date gct = new Date(currentTime);
		
		return sdf.format(gct);
	}
	
	public String getEightDigitDate()
	{
		SimpleDateFormat sdf = new SimpleDateFormat("ddMMyyyy");
		long currentTime = getTime();
		Date gct = new Date(currentTime);
		
		return sdf.format(gct);
	}
	
	// returns difference in days
	public static int diffDays(DateTime endDate, DateTime startDate)
	{
		final long HOURS_IN_DAY = 24L;
		final int MINUTES_IN_HOUR = 60;
		final int SECONDS_IN_MINUTES = 60;
		final int MILLISECONDS_IN_SECOND = 1000;
		long convertToDays = HOURS_IN_DAY * MINUTES_IN_HOUR * SECONDS_IN_MINUTES * MILLISECONDS_IN_SECOND;
		long hirePeriod = endDate.getTime() - startDate.getTime();
		double difference = (double)hirePeriod / (double)convertToDays;
		int round = (int)Math.round(difference);
		return round;
	}
	
	private void setDate(int day, int month, int year)
	{
		
		Calendar calendar = Calendar.getInstance();
		calendar.set(year, month - 1, day, 0, 0);   

		java.util.Date date = calendar.getTime();

		time = date.getTime();
	}
	
	// Advances date/time by specified days, hours and mins for testing purposes
		public void setAdvance(int days, int hours, int mins)
		{
			advance = ((days * 24L + hours) * 60L) * 60000L;
		}
		
	public static void main(String[] args) {
		DateTime datetime = new DateTime();
		System.out.println(datetime.getFormattedDate());
	}
}

  

public class MyException extends Exception {

	public MyException(String message) {
		super(message);
	}
}

  

COSC1284 Programming Techniques

标签:name   travel   seed   sub   ram   array   lag   class   ast   

原文地址:https://www.cnblogs.com/wylwyl/p/10660177.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!