0

in a Java project that uses servlets & .jsp, I've created an object of Car class. The field purchaseDate is set by user input in format String purchaseDate = "03 19 2019";. I would like to check if the difference between the currentDate & the purchaseDate is over say, 60 days. If over 60 days, trigger a method that would allow buyer to bid on the car. If less than 60 days, no bid possible. This would render in the car inventory HTML page that the buyer sees on the specific car. After looking through many articles, I'm not seeing anything specific to my problem. I would like to use java.util.Date as I've figured out how to calculate the days between dates. Not sure how I should wire this up. Should I put the method on the Car class that keeps up with the date or use some jstl & EL in my inventory.jsp page? Perhaps a while loop that compares the difference between purchaseDate & currentDate. Below, here's my Car class with me starting to use Date but unsure how:

Also below, here's my inventory.jsp where I render each carCard which is read from a .txt file on my computer.

package com.user;

import java.io.BufferedWriter;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Scanner;
import java.util.Date;

public class Car {
    private String make;
    private String model;
    private String year;
    private String condition; //used or new
    private String purchaseDate; 
    private String purchasePrice;
    private String sellPrice;
    private String description;
    private int ID;
    private final static String path="/Users/chrisyoung/Documents/carText/";

    private String today;




    public String getToday() {

        String pattern = "MM dd yyyy";

        SimpleDateFormat simpleDateFormat  = new SimpleDateFormat(pattern);

        String today = simpleDateFormat.format(new Date());

        return today;
    }


    public void setToday(String today) {
        this.today = today;
    }


    public String getPurchasePrice() {
        return purchasePrice;
    }


    public void setPurchasePrice(String purchasePrice) {
        this.purchasePrice = purchasePrice;
    }


    public String getSellPrice() {
        return sellPrice;
    }


    public void setSellPrice(String sellPrice) {
        this.sellPrice = sellPrice;
    }




public Car() {

    setID();

}


public String getPurchaseDate() {
    return purchaseDate;
}


public void setPurchaseDate(String purchaseDate) {
    this.purchaseDate = purchaseDate;
}


public String getMake() {
    return make;
}


public void setMake(String make) {
    this.make = make;
}


public String getModel() {
    return model;
}


public void setModel(String model) {
    this.model = model;
}


public String getYear() {
    return year;
}


public void setYear(String year) {
    this.year = year;
}


public String getCondition() {
    return condition;
}


public void setCondition(String condition) {
    this.condition = condition;
}


public String getDescription() {
    return description;
}


public void setDescription(String description) {
    this.description = description;
}






public int getID() {


    return ID;
}


public void setID() {

    ID = (int) (Math.random() * Math.pow(10,  4));

}


//building .txt file and including unique ID 
public static Car readFromFile(int ID) {
    Car car = new Car();

            String shark = "shark";

            String fileName = path + shark + ID + ".txt";

        try {

            Scanner scanner = new Scanner(new File(fileName));
            while(scanner.hasNextLine()) {
                //grab user input 
                String line = scanner.nextLine();               
                //each comma will represent a position in the array
                //Create a String array called parseLine. Set it to line which is the user input.  
                //Split the array with a comma 
                String[] parsedLine = line.split(",");



                //set the new car object 
                car.setMake(parsedLine[0]);
                car.setModel(parsedLine[1]);
                car.setYear(parsedLine[2]);
                car.setCondition(parsedLine[3]);
                car.setPurchaseDate(parsedLine[4]);
                car.setPurchasePrice(parsedLine[5]);
                car.setSellPrice(parsedLine[6]);
                car.setDescription(parsedLine[7]);
                car.setID();





            }
        }catch(FileNotFoundException e) {
            System.out.println("Invalid file name");
        }



    return car;
}

//save file 
    //Think about how you want to parse this data  
    //Be sure to put this where you are creating the object so that you can call method and saveToFile
    public void saveToFile() {

        String shark = "shark";

        String fileName = path + shark + ID + ".txt";

        try {
        BufferedWriter bw = 
                new BufferedWriter(new FileWriter(fileName));
        bw.write(formatData());
        //close stream or it will not write 
        bw.close();
        } catch (IOException e) {
            System.out.println("Save to file failed");
        }
    }

//no one else needs to accesss this method but this class so make it private 
private String formatData(){
    //firstName = 0 first that got stored 
    return this.make + "," + this.model + "," + this.year + "," + this.condition + "," 

            + this.purchaseDate + "," + this.purchasePrice + "," + this.sellPrice + "," + this.description + "," + this.ID;
}

public String toString() {
return this.make + "," + this.model + "," + this.year + "," + this.condition + "," 

            + this.purchaseDate + "," + this.purchasePrice + "," + this.sellPrice + "," +  this.description + " ] " + "," + this.ID;
}






}


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

     <!-- import jstl if we need to use jstl on the same page  -->
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <!-- import object  -->
<%@ page import="com.user.Car" %>

<!-- import ArrayList  -->
<%@page import="java.util.ArrayList" %>

<% ArrayList<Car> carCards = (ArrayList<Car>) session.getAttribute("carCards"); %>


<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
     <!--Import Google Icon Font-->
      <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">

    <!-- Compiled and minified CSS -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">

    <!-- Compiled and minified JavaScript -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>

<title>Insert title here</title>
</head>
<body style="background-color:powderblue;">

    <form class="container" action="ShowInventoryServlet" method="post">
            <button type="submit">Show Inventory</button>
    </form>

    <div class="center">
        <a class="big" href="index.jsp">HOME</a>
    </div>

<!-- <h1>Current Inventory</h1> -->
<c:forEach var="car" items="${carCards}">
    <!--  m seems to be width  -->
    <div class="row">
        <div class="col s12 m3">
          <div class="card blue-grey darken-1">
            <div class="card-content white-text">
              <span class="card-title">Card Title</span>
              <ul>
              <!--
                private String make;
                private String model;
                private String year;
                private String condition; //used or new
                private String purchaseDate; 
                private String purchasePrice;
                private String sellPrice;
                private String description;
                private int ID; -->

                  <li>MAKE :&nbsp;<c:out value="${car.make}"/></li>
                  <li>MODEL :&nbsp;<c:out value="${car.model}"/></li>
                  <li>YEAR :&nbsp;<c:out value="${car.year}"/></li>
                  <li>CONDITION :&nbsp;<c:out value="${car.condition}"/></li>
                  <li>PURCHASED :&nbsp;<c:out value="${car.purchaseDate}"/></li>
                  <li>COST :&nbsp;$<c:out value="${car.purchasePrice}"/></li>
                  <li>SELL :&nbsp;$<c:out value="${car.sellPrice}"/></li>
                  <li>ID :&nbsp;<c:out value="${car.ID}"/></li>
                </ul>
                <h7>Description</h7>
              <p><c:out value="${car.description}"/></p>
            </div>
            <div class="card-action">
              <a href="#">This is a link</a>
              <a href="#">This is a link</a>
            </div>
          </div>
        </div>
    </div>
</c:forEach>



</body>
</html>

When a specific Car object is 60 days or older, render an option to bid on the car card in inventory.jsp

wallwalker
  • 591
  • 7
  • 14
  • 27

0 Answers0