gpt4 book ai didi

java - Java 中的 BigDecimal、String 和 LocalDate 如何处理这些问题?

转载 作者:行者123 更新时间:2023-12-01 20:10:02 26 4
gpt4 key购买 nike

我目前遇到以下异常。问题出在 Price 类的构造函数部分。我该如何处理这个问题。我应该能够将价格 2.29 和日期 10202017 作为字符串传递

Price price = new Price("2.29", "10/20/2017");

但是 Price 类应该有

private BigDecimal price;
private LocalDate effectiveDate;

我对转换不太满意。谁能告诉我如何实现这一目标并指导我。

期望的输出:

商品编号:1 商品:火鸡三明治类别:杂货 UPC:1001 价格:2.29

错误

Exception in thread "main" java.time.format.DateTimeParseException: Text '10/20/2017' could not be parsed at index 0
at java.time.format.DateTimeFormatter.parseResolved0(Unknown Source)
at java.time.format.DateTimeFormatter.parse(Unknown Source)
at java.time.LocalDate.parse(Unknown Source)
at java.time.LocalDate.parse(Unknown Source)
at posPD.Price.<init>(Price.java:35)
at posTest.MainTest.main(MainTest.java:27)

主类

package posTest;

import java.math.BigDecimal;

import posPD.*;

public class MainTest {


public static void main(String[] args) {
// TODO Auto-generated method stub
Store myStore = new Store("1", "My Store 1");

TaxCategory taxCategory1 = new TaxCategory("Beverages");
TaxCategory taxCategory2 = new TaxCategory("Grocery");

Register register1 = new Register("Register 1");
Register register2 = new Register("Register 2");

Person person1 = new Person("David", "OK" ,"405000000", "800-800-1000");
Person person2 = new Person("Sally", "Ktm", "123456789", "000-000-0000");


Item item = new Item("1", "Turkey Sandwich");


Price price = new Price("2.029", "10/20/2017");

UPC upc = new UPC("1001");
//Price price = new Price("1.49", "2005");

//Session session = new Session();
try {


//CashDrawer cashDraw1 = new CashDrawer(1, BigDecimal.valueOf(500));

//System.out.println(cashDraw1);
//System.out.println(register.toString());

Cashier cashier1 = new Cashier("1", person1 , "Password1");
//person1.setCashier(cashier1);
//myStore.addCashier(cashier);
Cashier cashier2 = new Cashier("1", person2 , "Password1");
person1.setCashier(cashier1);
person2.setCashier(cashier2);

myStore.addCashier(cashier1);

myStore.addCashier(cashier2);




//CashDrawer cashDrawer1 = new CashDrawer("Drawer 1.");
CashDrawer cashDrawer1 = new CashDrawer(1, BigDecimal.valueOf(500));
CashDrawer cashDrawer2 = new CashDrawer(2, BigDecimal.valueOf(500));

myStore.addRegister(register1);
myStore.addRegister(register2);
register1.setCashDrawer(cashDrawer1);
register2.setCashDrawer(cashDrawer2);

//myStore.addTaxCategory(taxCategory1);
//myStore.addTaxCategory(taxCategory2);

Session session1 = new Session(cashier1, register1);
Session session2 = new Session(cashier2, register2);

myStore.addSession(session1);
myStore.addSession(session2);

myStore.addItem(item);
//myStore.addUPC(upc);
item.addUPC(upc);

//item.addPrice(price);
item.setTaxCategory(taxCategory2);




//myStore.addCashier(cashier2);

SaleLineItem sli = new SaleLineItem();

System.out.println("=========");
System.out.println( " "+myStore);
System.out.println("=========");



} catch(Exception e) {System.out.println("Exception exists");}

}

}

价格等级

package posPD;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.LocalDateTime;

/**
* Price details
*/
public class Price {

private BigDecimal price;
private LocalDate effectiveDate;
private Item item;

public Price() {
// TODO - implement Price.Price
//throw new UnsupportedOperationException();

}

/**
*
* @param price
* @param effectiveDate
*/
public Price(String price, String effectiveDate) {
// TODO - implement Price.Price
//throw new UnsupportedOperationException();
this();
BigDecimal bdprice = new BigDecimal (price);
this.price = bdprice;

LocalDate dt = LocalDate.parse(effectiveDate);
this.setEffectiveDate(dt);

}

/**
*
* @param date
*/
public Boolean isEffective(LocalDate date) {
// TODO - implement Price.isEffective


throw new UnsupportedOperationException();
/*
if (LocalDate.now().isAfter(date)) {
return false;
}
return true;
*/

}

/**
*
* @param quantity
*/
public BigDecimal calcAmountForQty(int quantity) {
// TODO - implement Price.calcAmountForQty
//throw new UnsupportedOperationException();

return price;
}

/**
*
* @param date
*/
/*
public Boolean isPriceEffectiveForDate(LocalDate date) {
// TODO - implement Price.isPriceEffectiveForDate
throw new UnsupportedOperationException();
}
*/

/**
*
* @param price
*/
public void compareTo(Price price) {
// TODO - implement Price.compareTo
throw new UnsupportedOperationException();

}

public String toString() {
// TODO - implement Price.toString
throw new UnsupportedOperationException();
//return
}

public Item getItem() {
return item;
}

public void setItem(Item item) {
this.item = item;
}

public LocalDate getEffectiveDate() {
return effectiveDate;
}

public void setEffectiveDate(LocalDate effectiveDate) {
this.effectiveDate = effectiveDate;
}

}

最佳答案

您使用的 LocalDate.parse 的单参数版本需要这种格式(“yyyy-MM-dd”)的 CharSequence。您可以这样格式化您的日期,例如

Price price = new Price("2.29", "2017-10-20");

最好通过使用 DateTimeFormatter 来支持您使用的字符串格式(“10/20/2017”),如下所示:

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
LocalDate dt = LocalDate.parse(effectiveDate, formatter);

[编辑以根据您的评论添加测试示例]:

    String input1 = "2017-10-20";
LocalDate date1 = LocalDate.parse(input1);
System.out.println("Using no formatter input1["+input1+"] date1 ["+date1+"]");

String input2 = "10/20/2017";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
LocalDate date2 = LocalDate.parse(input2, formatter);
System.out.println("Formatter MM/dd/yyyy input2["+input2+"] date2 ["+date2+"]");

对我来说,这会输出以下内容,没有错误:

Using no formatter    input1[2017-10-20]  date1 [2017-10-20]
Formatter MM/dd/yyyy input2[10/20/2017] date2 [2017-10-20]

关于java - Java 中的 BigDecimal、String 和 LocalDate 如何处理这些问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46802332/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com