- 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/
我的一位教授给了我们一些考试练习题,其中一个问题类似于下面(伪代码): a.setColor(blue); b.setColor(red); a = b; b.setColor(purple); b
我似乎经常使用这个测试 if( object && object !== "null" && object !== "undefined" ){ doSomething(); } 在对象上,我
C# Object/object 是值类型还是引用类型? 我检查过它们可以保留引用,但是这个引用不能用于更改对象。 using System; class MyClass { public s
我在通过 AJAX 发送 json 时遇到问题。 var data = [{"name": "Will", "surname": "Smith", "age": "40"},{"name": "Wil
当我尝试访问我的 View 中的对象 {{result}} 时(我从 Express js 服务器发送该对象),它只显示 [object][object]有谁知道如何获取 JSON 格式的值吗? 这是
我有不同类型的数据(可能是字符串、整数......)。这是一个简单的例子: public static void main(String[] args) { before("one"); }
嗨,我是 json 和 javascript 的新手。 我在这个网站找到了使用json数据作为表格的方法。 我很好奇为什么当我尝试使用 json 数据作为表时,我得到 [Object,Object]
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我听别人说 null == object 比 object == null check 例如: void m1(Object obj ) { if(null == obj) // Is thi
Match 对象 提供了对正则表达式匹配的只读属性的访问。 说明 Match 对象只能通过 RegExp 对象的 Execute 方法来创建,该方法实际上返回了 Match 对象的集合。所有的
Class 对象 使用 Class 语句创建的对象。提供了对类的各种事件的访问。 说明 不允许显式地将一个变量声明为 Class 类型。在 VBScript 的上下文中,“类对象”一词指的是用
Folder 对象 提供对文件夹所有属性的访问。 说明 以下代码举例说明如何获得 Folder 对象并查看它的属性: Function ShowDateCreated(f
File 对象 提供对文件的所有属性的访问。 说明 以下代码举例说明如何获得一个 File 对象并查看它的属性: Function ShowDateCreated(fil
Drive 对象 提供对磁盘驱动器或网络共享的属性的访问。 说明 以下代码举例说明如何使用 Drive 对象访问驱动器的属性: Function ShowFreeSpac
FileSystemObject 对象 提供对计算机文件系统的访问。 说明 以下代码举例说明如何使用 FileSystemObject 对象返回一个 TextStream 对象,此对象可以被读
我是 javascript OOP 的新手,我认为这是一个相对基本的问题,但我无法通过搜索网络找到任何帮助。我是否遗漏了什么,或者我只是以错误的方式解决了这个问题? 这是我的示例代码: functio
我可以很容易地创造出很多不同的对象。例如像这样: var myObject = { myFunction: function () { return ""; } };
function Person(fname, lname) { this.fname = fname, this.lname = lname, this.getName = function()
任何人都可以向我解释为什么下面的代码给出 (object, Object) 吗? (console.log(dope) 给出了它应该的内容,但在 JSON.stringify 和 JSON.parse
我正在尝试完成散点图 exercise来自免费代码营。然而,我现在只自己学习了 d3 几个小时,在遵循 lynda.com 的教程后,我一直在尝试确定如何在工具提示中显示特定数据。 This code
我是一名优秀的程序员,十分优秀!