Java 使用多个对象类型从arraylist打印表
我试图将字符串转换为日期,然后用用户输入填充多个数组列表。我还尝试以平板电脑格式打印阵列列表。不幸的是,我在格式化程序和使用for循环传递每个数组列表值的打印过程中不断遇到错误。我被要求使用MVC。控制器类:Java 使用多个对象类型从arraylist打印表,java,arraylist,Java,Arraylist,我试图将字符串转换为日期,然后用用户输入填充多个数组列表。我还尝试以平板电脑格式打印阵列列表。不幸的是,我在格式化程序和使用for循环传递每个数组列表值的打印过程中不断遇到错误。我被要求使用MVC。控制器类: import java.text.DateFormat; import java.util.Date; import java.text.SimpleDateFormat; import java.time.LocalDate; import jav
import java.text.DateFormat;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class Controller {
View view = new View();
Model model = new Model();
public void Run(){
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
Date today = new Date();
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
String firstDate = view.invoiceDate();
String secondDate = dateFormat.format(today);
LocalDate date1 = LocalDate.parse(firstDate,formatter);
LocalDate date2 = LocalDate.parse(secondDate,formatter);
model.setDaysLeft(date1, date2);
long daysBetween = model.getDaysLeft();
model.buildL1(view.Department());
model.buildL2(view.Amount());
if(daysBetween > 0)
{
view.showInvDate(firstDate);
view.showTodayDate(secondDate);
view.showDaysLeft(daysBetween);
model.buildL3(date1);
model.buildL4(daysBetween);
runAgain();
}
else
{
view.showInvDate(firstDate);
view.showTodayDate(secondDate);
view.showDaysLeft(daysBetween*-1);
model.buildL3(date1);
model.buildL4(daysBetween);
runAgain();
}
}
public void runAgain(){
if (!view.exit()) {
Run();
}
else {
for (int i =0; i < model.depList.size(); i++){
view.printInvoiceHeader();
view.printInvoice(model.getdepList(i), model.getAmtList(i), model.getDueDList(i), model.getDaysList(i));
}
}
}
}
Model class:
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
public class Model {
String depName;
double amount;
LocalDate dueDate;
long daysBetween;
ArrayList<String> depList = new ArrayList<String>();
ArrayList<Double> amtList = new ArrayList<Double>();
ArrayList<LocalDate> dueDList = new ArrayList<LocalDate>();
ArrayList<Long> daysLList = new ArrayList<Long>();
public String getDepName() {
return depName;
}
public void setDepName(String depName) {
this.depName = depName;
}
public double getAmount() {
return amount;
}
public void setAmount(double amount) {
this.amount = amount;
}
public LocalDate getDueDate() {
return dueDate;
}
public void setDueDate(LocalDate dueDate) {
this.dueDate = dueDate;
}
public void setDaysLeft(LocalDate date1,LocalDate date2){
long daysBetween = ChronoUnit.DAYS.between(date1,date2);
}
public long getDaysLeft(){
return daysBetween;
}
public ArrayList<String> buildL1(String depName){
depList.add(depName);
return depList;
}
public String getdepList(int i){
return depList.get(i);
}
public ArrayList<Double> buildL2(Double amount){
amtList.add(amount);
return amtList;
}
public Double getAmtList(int i){
return amtList.get(i);
}
public ArrayList<LocalDate> buildL3(LocalDate dueDate){
dueDList.add(dueDate);
return dueDList;
}
public LocalDate getDueDList(int i){
return dueDList.get(i);
}
public ArrayList<Long> buildL4(Long daysBetween){
daysLList.add(daysBetween);
return daysLList;
}
public Long getDaysList(int i){
return daysLList.get(i);
}
}
导入java.text.DateFormat;
导入java.util.Date;
导入java.text.simpleDataFormat;
导入java.time.LocalDate;
导入java.time.format.DateTimeFormatter;
公共类控制器{
视图=新视图();
模型=新模型();
公开募捐{
DateTimeFormatter formatter=模式的DateTimeFormatter.of(“dd/MM/yyyy”);
今天日期=新日期();
DateFormat DateFormat=新的简化格式(“dd/MM/yyyy”);
字符串firstDate=view.invoiceDate();
String secondDate=dateFormat.format(今天);
LocalDate date1=LocalDate.parse(firstDate,格式化程序);
LocalDate date2=LocalDate.parse(secondDate,格式化程序);
model.setDaysLeft(日期1,日期2);
long daysBetween=model.getDaysLeft();
model.buildL1(view.Department());
model.buildL2(view.Amount());
如果(介于>0之间的天数)
{
view.showInvDate(firstDate);
查看。showTodayDate(secondDate);
view.showDaysLeft(中间日期);
模型3(日期1);
模型建筑L4(两天之间);
再次运行();
}
其他的
{
view.showInvDate(firstDate);
查看。showTodayDate(secondDate);
视图。showDaysLeft(介于*-1之间的日期);
模型3(日期1);
模型建筑L4(两天之间);
再次运行();
}
}
公开作废再次运行(){
如果(!view.exit()){
Run();
}
否则{
对于(int i=0;i
似乎您正在打印一个与float不匹配的变量。我不能说太多,因为您的打印方法不存在。您会得到什么错误?我得到几个:线程“main”java.util.IllegalFormatConversionException中的异常:f!=java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:4302)在java.util.Formatter$FormatSpecifier.printFloat(Formatter.java:2806)在java.util.Formatter$FormatSpecifier.print(Formatter.java:2753)在java.util.Formatter.Formatter.format(Formatter.java:2520)在java.util.Formatter.Formatter.Formatter.2455位于java.lang.String.format(