COSC1284 Programming Techniques
Posted wylwyl
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 ", this.regNo)); sb.append(String.format("Make & Model: %s %s ", this.make, this.model)); sb.append(String.format("Driver Name: %s ", this.driverName)); sb.append(String.format("Capacity: %s ", this.passengerCapacity)); if(this.available) { sb.append(String.format("Available: %s ", "YES")); } else { sb.append(String.format("Available: %s ", "NO")); } return sb.toString(); } @Override public String toString() { String format = "%s:%s:%s:%s:%s:%s "; 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 ", this.id)); sb.append(String.format("Booking Fee: $%s ", this.bookingFee)); sb.append(String.format("Pick up Date:%s ", this.pickUpDateTime)); sb.append(String.format("Name: %s %s ", this.firstName, this.lastName)); sb.append(String.format("Passengers: %s ", this.numPassengers)); sb.append(String.format("Travelled: %s ", this.kilometersTravelled)); sb.append(String.format("Trip Fee: %s ", this.tripFee)); sb.append(String.format("Car Id: %s ", 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 + " "; } }
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的主要内容,如果未能解决你的问题,请参考以下文章
讲解COSC1295 Advanced Programming
4COSC001W: Programming Principles