COSC1284 Programming Techniques

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);
	}
}

  

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

时间: 2024-10-15 21:14:11

COSC1284 Programming Techniques的相关文章

The Art of Prolog:Advanced Programming Techniques【译文】

申明:此文为译文,仅供学习交流试用,请勿用作商业用途,造成一切后果本人概不负责,转载请说明.本人英语功力尚浅,翻译大多借助于翻译工具,如有失误,欢迎指正. 逻辑程序简介 逻辑程序是一组公理或规则,定义对象之间的关系.逻辑程序的计算是扣除该计划的后果的.一个程序定义了一组后果,这就是它的意义.逻辑编程的艺术是构建一个具有所需的含义简洁大方的方案. Prolog基本构造 逻辑编程,条款和声明的基本结构,从逻辑继承.有三种基本的语句:事实,规则和查询.有一个单一的数据结构:逻辑术语.最简单的一种说法叫

Java Performance Optimization Tools and Techniques for Turbocharged Apps--reference

Java Performance Optimization by: Pierre-Hugues Charbonneau reference:http://refcardz.dzone.com/refcardz/java-performance-optimization Java is among the most widely used programming languages in the software development world today. Java applications

Core Java Volume I — 4.1. Introduction to Object-Oriented Programming

4.1. Introduction to Object-Oriented ProgrammingObject-oriented programming, or OOP for short, is the dominant programming paradigm these days, having replaced the "structured," procedural programming techniques that were developed in the 1970s.

An Introduction to Lock-Free Programming

original url: http://preshing.com/20120612/an-introduction-to-lock-free-programming/ What Is It? People often describe lock-free programming as programming without mutexes, which are also referred to as locks. That's true, but it's only part of the s

CCN2042 Computer Programming

CCN2042 Computer ProgrammingGroup Project – Cooking Game(Due: 23:59, 18 Apr 2019 (week 12 before Easter))Expected Learning Outcomes familiarise themselves with at least one high level language programming environment. develop a structured and documen

CS2313 Computer Programming

CS2313 Computer ProgrammingAssignment OneDeadline: 2019-Nov-16 11:59pmLate submission: 70% (within 24hrs after the deadline), 0% (after 24hrs)1. Problem descriptionA “lucky sequence” is the sequence of numbers where every number after the first twois

Method, apparatus, and system for speculative abort control mechanisms

An apparatus and method is described herein for providing robust speculative code section abort control mechanisms. Hardware is able to track speculative code region abort events, conditions, and/or scenarios, such as an explicit abort instruction, a

PatentTips - Fair scalable reader-writer mutual exclusion

BACKGROUND The present invention relates generally to multithreaded programming and, more specifically, to mutual exclusion of readers and writers in a multithreaded programming environment. Mutual exclusion is a programming technique that ensures th

Windows Kernel Security Training Courses

http://www.codemachine.com/courses.html#kerdbg Windows Kernel Internals for Security Researchers This course takes a deep dive into the internals of the Windows kernel from a security perspective. Attendees learn about behind the scenes working of va