- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的程序遇到问题,该程序要求我比较对 Date 对象的引用。该场景是一个车辆陈列室,车辆存储在数组列表中。
我有一个车辆类别
import java.text.DecimalFormat;
import java.util.*;
import java.util.Date;
public class Vehicle {
private String manufacturer;
private String model;
//private String customerNameSold = null;
private String vehicleID_VIN;
private String dateManufacture;
private String dateSold;
private boolean beenSold;
private char taxBand_A_M;
private double vehicleCost;
private String emissions;
private Customer customerNameSold;
private Date dom;
//private String manuDate;
private Date saleDate;
//Constructor 7
public Vehicle (String manu, String mod, String vin, String dateManu, char tax, double cost){
manufacturer = manu;
model = mod;
vehicleID_VIN = vin;
dateManufacture = dateManu;
//Add String to Date
dom = new Date(dateManu);
taxBand_A_M = tax;
vehicleCost = cost;
dateSold = null;
customerNameSold = null;
beenSold = false;
}
public String toString() {
DecimalFormat df = new DecimalFormat("#.00");
//method calls - do they need this. then method call?
String s = "\nManufacturer: " + getManu()
+ " \nModel: " + getModel()
+ " \nVIN: " + getVin()
+ " \nDate of Manufacture: " + getManuDate()
+ " \nDate of Manufacture (String to Date): " + getManuDate2()
+ " \nAge of Vehicle: " + getAgeOfVehicle() + " (in WEEKS)"
+ " \nTax Band: " + getTax()
+ " \nEmissions: " + cO2()
+ " \nCost: £" + df.format(getCost())
+ " \nHas Vehicle Been Sold: " + getBeenSold()
+ " \nCustomer: " + getCust()
+ " \nDate Sold: " + getDateSold()
+ " \nDate Sold (String to Date): " + getDateSold2();
return s;
}
public String getManu() {
return manufacturer;
}
public String getModel() {
return model;
}
//// public String getCust() {
////
//// return customerNameSold;
//// }
public Customer getCust() {
return customerNameSold;
}
public String getVin() {
return vehicleID_VIN;
}
public String getManuDate(){
return dateManufacture;
}
public Date getManuDate2() {
return dom;
}
public String getDateSold(){
return dateSold;
}
public Date getDateSold2() {
return saleDate;
}
//CONVENTION SUGGESTS SHOULD BE 'isSold()'
public boolean getBeenSold() {
return beenSold;
}
public char getTax() {
return taxBand_A_M;
}
public double getCost() {
//how to format to 2 decimal places?
return vehicleCost;
}
public void buyVehicle(String sale, Customer cust) {
customerNameSold = cust;
//Add String to Date
saleDate = new Date(sale);
dateSold = sale;
beenSold = true;
}
public String cO2() {
switch (taxBand_A_M) {
case 'A':
emissions = "0-100";
break;
case 'B':
emissions = "101-110";
break;
case 'C':
emissions = "111-120";
break;
case 'D':
emissions = "121-130";
break;
case 'E':
emissions = "131-140";
break;
case 'F':
emissions = "141-150";
break;
case 'G':
emissions = "151-160";
break;
default:
emissions = null;
break;
}
return emissions;
}
public int getAgeOfVehicle() {
Date now = new Date();
long diff = now.getTime() - dom.getTime();
long age = (diff / (1000L * 60 * 60 * 24 * 7));
return (int) age;
}
}
陈列室类(class)
import java.util.*;
public class Showroom {
private String showroomName;
private ArrayList<Vehicle> theVehicles;
private Vehicle currVeh = null;
private ArrayList<Vehicle> recentlySold;
private Date dateSold;
private long diff;
private long age;
//Constructor Method - Takes the name of the Showroom Object & Creates the array list of vehicles
public Showroom(String name) {
showroomName = name;
theVehicles = new ArrayList<Vehicle>();
}
public String getName() {
return showroomName;
}
public void setName(String name) {
//this.showroomName if passed in parameter was named showroomName also
showroomName = name;
}
public boolean addVehicle(Vehicle newVehicle) {
theVehicles.add(newVehicle);
currVeh = newVehicle;
return true;
}
public boolean addVehicleAfterCurrent(Vehicle newVehicle) {
theVehicles.add(theVehicles.indexOf(currVeh) + 1, newVehicle);
currVeh = newVehicle;
return true;
}
public Vehicle findVehicle(String vehicleVIN) {
for (Vehicle v : theVehicles) {
if (v.getVin().equalsIgnoreCase(vehicleVIN)) {
System.out.println("Vehicle Found:\n"
+ v.getManu() + "\n"
+ v.getModel() + "\n"
+ v.getVin() + "\n"
+ theVehicles.indexOf(v) + "\n");
return v;
//OR CALL THE toString() METHOD - v.toString()
}
}
System.out.println("Sorry - The Vehicle was not found in the Showroom!\n"
+ theVehicles.indexOf(vehicleVIN) + "\n");
return null;
}
public Vehicle setCurrentVehicle(Vehicle cv) {
currVeh = cv;
return currVeh;
}
public Vehicle getCurrentVehicle() {
System.out.println("\nCurrentVehicle: " + currVeh);
return currVeh;
}
public Vehicle nextVehicle() {
int index = theVehicles.indexOf(currVeh);
if (index < 0 || index + 1 == theVehicles.size()) {
System.out.println("\nEnd of the list");
return null;
}
Vehicle v = theVehicles.get(index + 1);
setCurrentVehicle(v);
System.out.println("\nThe Former Next Vehicle & Now Current Vehicle: " + v);
return currVeh;
}
public Vehicle previousVehicle() {
int index = theVehicles.indexOf(currVeh);
if (index <= 0) {
System.out.println("\nNegative Index -1 Before Start of List");
return null;
}
Vehicle v = theVehicles.get(index - 1);
setCurrentVehicle(v);
System.out.println("\nThe Former Previous Vehicle & Now Current Vehicle: " + v);
return currVeh;
}
public void outputArray() {
for (Vehicle nextVehicle : theVehicles) {
System.out.println(nextVehicle.getModel() + "\n" + theVehicles.indexOf(nextVehicle));
}
}
public void outputShowroomDetails() {
System.out.println("\nSHOWROOM NAME: " + showroomName);
//output each vehicle in turn
System.out.println("THE VEHICLES IN THE SHOWROOM:");
if (theVehicles.isEmpty()) {
System.out.println("\n*** There are no Vehicles in the Showroom! ***");
} else {
for (Vehicle nextVehicle : theVehicles) {
System.out.println(nextVehicle.toString() + "\n" + theVehicles.indexOf(nextVehicle));
}
}
}
public Vehicle setCurrentVehicleByVIN(String vin) {
System.out.println("\n*** ATTEMPTING TO SET CURRENT VEHICLE BY VIN:\n"
+ vin);
Vehicle v = findVehicle(vin);
if (v != null) {
System.out.println("\nTHE CURRENT VEHICLE: " + "\nARRAY LIST INDEX: " + theVehicles.indexOf(v)
+ v.toString());
}
currVeh = v;
return currVeh;
}
public boolean deleteVehicle(String vin) {
System.out.println("\nATTEMPTING TO DELETE VEHICLE:\n"
+ "VEHICLE VIN to DELETE: " + vin);
Vehicle v = findVehicle(vin);
if (v != null) {
theVehicles.remove(v);
System.out.println("VEHICLE *** " + vin + " *** REMOVED!");
return true;
}
return false;
}
//Method not working - null pointer 'long diff' line
public ArrayList<Vehicle> getVehiclesSoldRecently() {
recentlySold = new ArrayList<Vehicle>();
//Each vehicle has a sale date
//Determine the difference between sale date & todays date
//If the difference is greater than 14 days (2 weeks) it won't be added to the array
//If the difference is less than or equal to 14 days (2 weeks), then they will be added to the array
if (theVehicles.isEmpty()) {
System.out.println("\n*** The Showroom is Empty!***");
} else
{
for (Vehicle v : theVehicles) {
Date now = new Date();
dateSold = v.getDateSold2();
long diff = now.getTime() - v.getDateSold2().getTime();
long age = (diff / (1000L * 60 * 60 * 24 * 7));
if (age <= 2) {
recentlySold.add(v);
System.out.println("\nVEHICLES RECENTLY SOLD: " + v.toString());
}
}
return recentlySold;
}
}
& 客户类别
public class Customer {
private String custName = null;
private String custPhone = null;
private String custEmail = null;
public Customer() {
}
public Customer(String name) {
custName = name;
custPhone = "n/a";
custEmail = "n/a";
}
public Customer(String name, String phone) {
custName = name;
custPhone = phone;
custEmail = "n/a";
}
//***CAN'T HAVE A CONSTRUCTOR DETAILING NAME & EMAIL ONLY
//AS A (String, String) CONSTRUCTOR ALREADY DEFINED
public Customer(String name, String phone, String email) {
custName = name;
custPhone = phone;
custEmail = email;
}
//***AUTO GENERATED GETTERS & SETTERS - this.*
public String getCustName() {
return custName;
}
public void setCustName(String custName) {
this.custName = custName;
}
public String getCustPhone() {
return custPhone;
}
public void setCustPhone(String custPhone) {
this.custPhone = custPhone;
}
public String getCustEmail() {
return custEmail;
}
public void setCustEmail(String custEmail) {
this.custEmail = custEmail;
}
//***AUTO GENERATED TOSTRING METHOD (THOUGH EDITED FOR FORMAT)
@Override
public String toString() {
String cust = "\n*** CUSTOMER ***"
+ "\nName: " + getCustName()
+ "\nPhone: " + getCustPhone()
+ "\nEmail: " + getCustEmail();
//System.out.println(cust);
return cust;
}
}
ShowroomDriver 是
public class ShowroomDriver {
public static void main(String args[]) {
Showroom showDrive = new Showroom("ShowroomDriver Showroom");
System.out.println("\n*** OUTPUT SHOWROOM DETAILS ***");
showDrive.outputShowroomDetails();
System.out.println("\n*** CREATE / ADD 4 VEHICLES ***");
Vehicle sdv1 = new Vehicle("Audi", "R8 Spider", "FAVE 101", "MAR-04-2011", 'E', 45000);
//Vehicle sdv1 = new Vehicle("Audi", "R8 Spider", "FAVE 101", 03 / 04 / 2011, 'E', 45000);
System.out.println("\nTesting toString: " + sdv1.toString());
Vehicle sdv2 = new Vehicle("Tesla", "Model S", "ELEC TRIC", "JAN-01-2013", 'A', 55000);
//Vehicle sdv2 = new Vehicle("Tesla", "Model S", "ELEC TRIC", 01 / 01 / 2013, 'A', 55000);
System.out.println("\nTesting to String: " + sdv2.toString());
Vehicle sdv3 = new Vehicle("Ford", "Cortina", "1212 NUM", "JUN-06-2006", 'D', 55000);
//Vehicle sdv3 = new Vehicle("Ford", "Cortina", "1212 NUM", 06 / 06 / 2006, 'D', 55000);
System.out.println("\nTesting to String: " + sdv3.toString());
Vehicle sdv4 = new Vehicle("VW", "Golf MK 1", "DUB DUB", "NOV-11-1971", 'E', 25000);
//Vehicle sdv4 = new Vehicle("VW", "Golf MK 1", "DUB DUB", 11/11/1971, 'E', 25000);
System.out.println("\nTesting to String: " + sdv4.toString());
showDrive.addVehicle(sdv1);
showDrive.addVehicle(sdv2);
showDrive.addVehicle(sdv3);
showDrive.addVehicle(sdv4);
System.out.println("\n*** OUTPUT SHOWROOM DETAILS ***");
showDrive.outputShowroomDetails();
System.out.println("\n*** BUY 2 VEHICLES ***");
Customer cust1 = new Customer("Andrew Antivan", "01785 111 111");
Customer cust2 = new Customer("Belinda Belle", "01782 222 222", "belbel@gmail.com");
sdv1.buyVehicle("JUL-07-2013", cust1);
sdv2.buyVehicle("MAR-01-2013", cust2);
showDrive.outputShowroomDetails();
System.out.println("\n***CREATE 4 VEHICLES ***");
Vehicle purV1 = new Vehicle("Ford", "Fiesta", "NAT NAT", "MAR-09-2006", 'E', 5000);
Vehicle purV2 = new Vehicle("Vauxhall", "Corsa", "LEE 123", "JUL-07-2011", 'D', 5500);
Vehicle purV3 = new Vehicle("Toyota", "Aygo", "JOHN 32A", "FEB-02-2010", 'E', 2000);
Vehicle purV4 = new Vehicle("Marvel", "Bat Mobile", "KA BOOM", "MAR-11-2008", 'C', 3000);
System.out.println("\n*** SELL 2 of 4 VEHICLES ***");
System.out.println("\n*** ADD THE 4 NEW VEHICLES TO SHOWROOM ***");
showDrive.addVehicle(purV1);
showDrive.addVehicle(purV2);
showDrive.addVehicle(purV3);
showDrive.addVehicle(purV4);
purV1.buyVehicle("DEC-12-2012", cust1);
purV2.buyVehicle("DEC-12-2012", cust1);
purV3.buyVehicle("OCT-18-2013", cust2);
purV4.buyVehicle("OCT-19-2013", cust2);
showDrive.outputShowroomDetails();
showDrive.getVehiclesSoldRecently();
}
}
程序是逐步修改的。最初,制造和销售日期作为字符串对象传入(硬编码),但现在必须作为日期对象添加。
仅当我尝试调用 getVehiclesSoldRecently() 方法时,才会出现我遇到的问题。我收到空指针异常警告,这表明问题是由于该方法中的“diff...”行而发生的。
收到的错误:
线程“main”中的异常 java.lang.NullPointerException
在 Showroom.getVehiclesSoldRecently(Showroom.java:197)
diff=now.getTime()-v.getDateSold2().getTime();
在 ShowroomDriver.main(ShowroomDriver.java:93)
showDrive.getVehiclesSoldRecently();
任何帮助指出我出错的地方将不胜感激。
谢谢。
* 编辑*
除 getDates 之外的所有方法的方法签名必须保持相同,这意味着必须传入 String 参数,然后将其转换为 Date 对象。
最佳答案
这意味着:
v.getDateSold2()
返回null
:
diff=now.getTime()-v.getDateSold2().getTime();
因此,您实际上所做的是在 null 对象上调用 getTime()
。在尝试访问之前,您需要检查 v.getDateSold2()
是否为 null。
如果为空,则可以忽略它或打印其他内容。
关于java - 日期对象比较 - Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19512114/
我想使用 NetworkX Graph 对象作为 Python dict 中的键。但是,我不希望默认的比较行为(即通过对象的地址)。相反,我希望同构图是 dict 中相同元素的键。 此行为是否已在某处
这个问题已经有答案了: What is the most effective way for float and double comparison? (33 个回答) 已关闭 7 年前。 在您认为我
我正在学习 C 编程,为了练习,我找到了一个需要解决的任务。这有点像一个游戏,有人选择一个单词,其他人猜测字母。我必须检查有多少给定的单词可能是所选单词的正确答案。 输入: 3 3//数字 n 和 m
我两天前开始学习C,在做作业时遇到了问题。我们的目的是从字符数组中获取字符列表,并通过计算连续字符并将其替换为数字来缩短它。对“a4b5c5”说“aaaabbbbbccccc”。这是我到目前为止的代码
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
为什么我在 if 中的比较不起作用?答案应该是 8 但它返回 0。 function findMissing(missingArray){ var getArray = missing
我想知道为什么以下 JavaScript 比较会给出不同的结果。 (1==true==1) true (2==true==2) false (0==false==0) false (0==false)
我想知道是否有人可以帮助我完成这个程序。编写一个接受两个字符串的函数。该函数应该将这两个字符串与字典顺序上排在第一位的字符串组合起来。两个字符串之间应该有一个空格。在一行上打印结果字符串。在一行上打印
有谁知道一个免费的开源库(实用程序类),它允许您比较一个 Java bean 的两个实例并返回一个属性列表/数组,这两个实例的值不同?请发布一个小样本。 干杯 托马斯 最佳答案 BeanCompara
我是java新手。任何人都可以给出以下类声明的含义 public class ListNode, V> { K key; V value; ListNode next;
我需要用 C 语言计算和比较 3 种不同大小(100 * 100、1000 * 1000 和 10000 * 10000)的 2 个矩阵相乘的执行时间。我编写了以下简单代码来为 1000 * 1000
当我在 ACCESS 2007 中运行以下 SQL 时 Select Location, COUNT(ApartmentBuildings) AS TotalIBuildingsManaged Fro
根据我对互斥锁的了解——它们通常提供对共享资源的锁定功能。因此,如果一个新线程想要访问这个锁定的共享资源——它要么退出,要么必须不断轮询锁(并在等待锁时浪费处理器周期)。 但是,监视器具有条件变量,它
通常在编程中,不应该比较浮点数据类型是否相等,因为存储的值通常是近似值。 由于两个非整数 Oracle NUMBER 值的存储方式不同(以 10 为基数),是否可以可靠地比较它们是否相等? 最佳答案
使用 PowerShell 时,我们偶尔会比较不同类型的对象。一个常见的场景是 $int -eq $bool (即其中 0 -eq $false 、 0 -ne $true 和任何非零值仅等于真,但不
#include #define MAX 1000 void any(char s1[], char s2[], char s3[]); int main() { char string1[
我想比较两个日期。 从这两个日期中,我只使用 ToShortDateString() 获取日期组件, 如下所示。现在的问题是当我比较两个日期时。它的 throw 错误—— "Operator >= c
用户输入一个数字( float 或整数),并且它必须大于下限。 这是从 UITextField 获取数字的代码: NSNumberFormatter * f = [[NSNumberFormatter
我已经摆弄这段代码大约一个小时了,它让我难以置信。我认为解决方案相当简单,但我似乎无法弄清楚。无论如何,这里去。我制作了一个 javascript 函数来检查用户输入的字符,以便它只能接受 7 个字符
我不太明白为什么当我们在不覆盖 equals 的情况下比较具有相同类属性的两个实例时方法,它将给出 false .但它会给出 true当我们比较一个案例类的两个实例时。例如 class A(val
我是一名优秀的程序员,十分优秀!