현재 다음과 같은 예외가 발생합니다. 문제는 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]
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다