- 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/
我正在编写一个具有以下签名的 Java 方法。 void Logger(Method method, Object[] args); 如果一个方法(例如 ABC() )调用此方法 Logger,它应该
我是 Java 新手。 我的问题是我的 Java 程序找不到我试图用作的图像文件一个 JButton。 (目前这段代码什么也没做,因为我只是得到了想要的外观第一的)。这是我的主课 代码: packag
好的,今天我在接受采访,我已经编写 Java 代码多年了。采访中说“Java 垃圾收集是一个棘手的问题,我有几个 friend 一直在努力弄清楚。你在这方面做得怎么样?”。她是想骗我吗?还是我的一生都
我的 friend 给了我一个谜语让我解开。它是这样的: There are 100 people. Each one of them, in his turn, does the following
如果我将使用 Java 5 代码的应用程序编译成字节码,生成的 .class 文件是否能够在 Java 1.4 下运行? 如果后者可以工作并且我正在尝试在我的 Java 1.4 应用程序中使用 Jav
有关于why Java doesn't support unsigned types的问题以及一些关于处理无符号类型的问题。我做了一些搜索,似乎 Scala 也不支持无符号数据类型。限制是Java和S
我只是想知道在一个 java 版本中生成的字节码是否可以在其他 java 版本上运行 最佳答案 通常,字节码无需修改即可在 较新 版本的 Java 上运行。它不会在旧版本上运行,除非您使用特殊参数 (
我有一个关于在命令提示符下执行 java 程序的基本问题。 在某些机器上我们需要指定 -cp 。 (类路径)同时执行java程序 (test为java文件名与.class文件存在于同一目录下) jav
我已经阅读 StackOverflow 有一段时间了,现在我才鼓起勇气提出问题。我今年 20 岁,目前在我的家乡(罗马尼亚克卢日-纳波卡)就读 IT 大学。足以介绍:D。 基本上,我有一家提供簿记应用
我有 public JSONObject parseXML(String xml) { JSONObject jsonObject = XML.toJSONObject(xml); r
我已经在 Java 中实现了带有动态类型的简单解释语言。不幸的是我遇到了以下问题。测试时如下代码: def main() { def ks = Map[[1, 2]].keySet()
一直提示输入 1 到 10 的数字 - 结果应将 st、rd、th 和 nd 添加到数字中。编写一个程序,提示用户输入 1 到 10 之间的任意整数,然后以序数形式显示该整数并附加后缀。 public
我有这个 DownloadFile.java 并按预期下载该文件: import java.io.*; import java.net.URL; public class DownloadFile {
我想在 GUI 上添加延迟。我放置了 2 个 for 循环,然后重新绘制了一个标签,但这 2 个 for 循环一个接一个地执行,并且标签被重新绘制到最后一个。 我能做什么? for(int i=0;
我正在对对象 Student 的列表项进行一些测试,但是我更喜欢在 java 类对象中创建硬编码列表,然后从那里提取数据,而不是连接到数据库并在结果集中选择记录。然而,自从我这样做以来已经很长时间了,
我知道对象创建分为三个部分: 声明 实例化 初始化 classA{} classB extends classA{} classA obj = new classB(1,1); 实例化 它必须使用
我有兴趣使用 GPRS 构建车辆跟踪系统。但是,我有一些问题要问以前做过此操作的人: GPRS 是最好的技术吗?人们意识到任何问题吗? 我计划使用 Java/Java EE - 有更好的技术吗? 如果
我可以通过递归方法反转数组,例如:数组={1,2,3,4,5} 数组结果={5,4,3,2,1}但我的结果是相同的数组,我不知道为什么,请帮助我。 public class Recursion { p
有这样的标准方式吗? 包括 Java源代码-测试代码- Ant 或 Maven联合单元持续集成(可能是巡航控制)ClearCase 版本控制工具部署到应用服务器 最后我希望有一个自动构建和集成环境。
我什至不知道这是否可能,我非常怀疑它是否可能,但如果可以,您能告诉我怎么做吗?我只是想知道如何从打印机打印一些文本。 有什么想法吗? 最佳答案 这里有更简单的事情。 import javax.swin
我是一名优秀的程序员,十分优秀!