Im using Java and mySql I have already done my code to prompt user to enter a date in the format DDMMYY and then store the info into the database, however, when i enter a valid date such as 121221, it will say "invalid date, please re-enter" Im not sure whats the problem..
this is my validation class
public class Validation {
public static boolean isValidDate(String date) {
String day = date.substring(0,2);
String month = date.substring(2,4);
String year = date.substring(4,6);
int i = Integer.parseInt(day);
int j = Integer.parseInt(year);
int k = Integer.parseInt(month);
if (k%2 !=0 || k == 8 || k == 10 || k == 12) {
if(i<=0 || i>31) {
return false;
}
}else if (k == 2) {
if(i<=0 || i>28) {
return false;
}
}else if (k%2 == 0 || k == 9 || k == 11) {
if (i<=0 || i>30) {
return false;
}
}
if(j<22 || j>100) {
return false;
}
return true;
}
public boolean isValidTransactionId(int transactionId) {
return true;
}
}
my UI class
package view;
import java.util.*;
import java.text.*;
import controller.DBController;
import controller.Validation;
import model.Transaction;
import model.Member;
public class UserInterface {
DBController db;
Validation vd;
Scanner scan;
Random rand;
String date;
boolean invalid;
int menuChoice, memberChoice;
Transaction transaction;
public UserInterface()throws Exception{
db= new DBController();
scan = new Scanner(System.in);
rand = new Random();
transaction = new Transaction();
}
public void MainMenu()throws Exception {
System.out.println("Welcome to SuperFun Theme Park!");
do {
System.out.print("Main Menu\n");
System.out.println("1. New Ticket Transaction");
System.out.println("2. Modify a Ticket Transaction");
System.out.println("3. Delete a Ticket Transaction");
System.out.println("4. View Ticket Transactions");
System.out.println("5. Exit");
System.out.print("Enter a number (1-5): ");
menuChoice = scan.nextInt();
scan.nextLine();
switch(menuChoice) {
case 1: newTicket(); break;
case 2: modifyTicket(); break;
case 3: deleteTicket(); break;
case 4: viewAllTransactions(); break;
case 5: System.out.println("Thank you. Have a nice day!"); break;
default: System.out.println("Invalid input. Please re-enter.\n");break;
}
}while(menuChoice!=5);
}
public void dateInput()throws Exception{
do {
if(menuChoice ==1 ) {
System.out.print("Enter ticket date (DDMMYY): ");
date = scan.nextLine();
transaction.setDate(date);
}else if (menuChoice == 4) {
System.out.print("Enter start date (DDMMYY): ");
date = scan.nextLine();
transaction.setDate(date);
}
invalid = false;
if(!vd.isValidDate(date)) {
invalid = true;
}else
invalid = false;
}while(invalid);
}
public void displayMemberInfo()throws Exception{
String result[] = null;
System.out.printf("\n%s %17s %20s\n","Index","Name", "Phone Number");
result = db.retrieveMemberInfo();
for(String temp: result)
System.out.println(temp);
}
public void newTicket() throws Exception{
int member=0, numOfMemberTickets=0, transactionId =0;
int memberTicket=80, nonMemberTicket=100, total=0, num=0;
boolean full = false, repeat= false;
//auto generate transaction id //100-200
int upperbound = 200;
int lowerbound = 100;
do {
transactionId = rand.nextInt(upperbound - lowerbound)+ lowerbound;
transaction.setId(transactionId);
}while(db.idExists(transaction.getId()));
//display transaction id
System.out.println("\nTransaction Id: " + transaction.getId());
//prompt user to enter ticket date
//validation for date
do {
dateInput();
db.maximumTicket(transaction.getDate());
if(db.sum>=100) {
System.out.println("Tickets are fully booked for the day. Please re-enter date.\n");
full = true;
}else {
System.out.printf("Number of tickets left for %s are %d\n" , date, 100-db.sum);
full = false;
}
}while(full);
//ask for total number of tickets then ask for members tickets
do {
System.out.print("Enter number of ticket(s) wanted: ");
num = scan.nextInt();
scan.nextLine();
transaction.setTicketNum(num);
if(transaction.getTicketNum() > 100-db.sum)
System.out.println("Number of ticket(s) wanted exceeded availability. Please re-enter. ");
}while(transaction.getTicketNum() > 100-db.sum);
//if member, must choose which member
//loop through member info for validation
do {
repeat=false;
do {
System.out.print("Are there ticket(s) for member(s)? (1-yes 2-no): ");
member = scan.nextInt();
scan.nextLine();
if(member !=1 && member !=2)
System.out.println("Invalid input. Please re-enter.");
}while(member !=1 && member !=2);
if(member==1) {
do {
System.out.print("Enter the number of member tickets: ");
numOfMemberTickets = scan.nextInt();
scan.nextLine();
if(numOfMemberTickets<=0)
System.out.println("Invalid number of tickets. Please re-enter.");
}while(numOfMemberTickets<=0);
//display member info table
displayMemberInfo();
for(int cnt=0; cnt<numOfMemberTickets; cnt++) {
do {
System.out.print("\nPlease enter index number of member: ");
memberChoice = scan.nextInt();
// if(db.memberExists(transaction.getId())) {
// System.out.println("Member are only allowed to purchase one ticket in a day. Please re-enter.");
// repeat = true;
// }else
db.insertNumber(memberChoice, transaction.getId());
}while(memberChoice<=0);
}
}
}while(repeat);
//calculate and display total price
total = (numOfMemberTickets * memberTicket) + (Math.abs((transaction.getTicketNum() - numOfMemberTickets)) * nonMemberTicket);
transaction.setTotal(total);
System.out.println("Total price: RM" + transaction.getTotal());
//save data into database
boolean success = db.insertInfo(transaction.getDate(), transaction.getId(), transaction.getTicketNum(), transaction.getTotal());
if(success)
System.out.println("Transaction record inserted successfully.\n");
else
System.out.println("Error in inserting record.\n");
}
public void modifyTicket() throws Exception{
int transactionId=0, newTicketNum=0, newNumOfMemberTicket=0, newTotal=0, change=0;
String newDate="";
//enter transaction id
do {
System.out.print("Enter transaction id: ");
transactionId = scan.nextInt();
scan.nextLine();
transaction.setId(transactionId);
if(!db.idExists(transaction.getId()))
System.out.println("Transaction id does not exists. Please re-enter.");
}while(!db.idExists(transaction.getId()));
//display details
System.out.printf("\n%-4s %20s %25s %20s\n","Ticket Date", "Transaction Id", "Number of Tickets", "Total Price");
db.displayRecord(transaction.getId());
//user can edit date, number and members' phone number
System.out.print("\nEnter new ticket date: ");
newDate = scan.nextLine();
transaction.setDate(newDate);
System.out.print("Enter new number of tickets(including members' tickets): ");
newTicketNum = scan.nextInt();
scan.nextLine();
transaction.setTicketNum(newTicketNum);
System.out.print("Enter new number of member ticket(s): ");
newNumOfMemberTicket = scan.nextInt();
scan.nextLine();
System.out.print("Is there a change in member(s)? (1-yes 2-no): ");
change = scan.nextInt();
scan.nextLine();
if(change==1) {
displayMemberInfo();
for(int cnt=0; cnt<newNumOfMemberTicket; cnt++) {
do {
System.out.print("\nPlease enter index number of member: ");
memberChoice = scan.nextInt();
db.updateNumber(memberChoice, transactionId);
}while(memberChoice<=0);
}
}
//recalculate total price
newTotal = (newNumOfMemberTicket * 80) + (Math.abs(newTicketNum - newNumOfMemberTicket)* 100);
transaction.setTotal(newTotal);
//update database
boolean success = db.updateInfo(transaction.getDate(), transaction.getTicketNum(), transaction.getTotal(), transaction.getId());
if(success)
System.out.println("Information updated successfully.\n");
else
System.out.println("Error in updating information.\n");
}
public void deleteTicket()throws Exception{
int transactionId=0, choice =0;
//enter transaction id
do {
System.out.print("Enter transaction id: ");
transactionId = scan.nextInt();
scan.nextLine();
if(!db.idExists(transactionId))
System.out.println("Transaction id does not exists. Please re-enter.");
}while(!db.idExists(transactionId));
transaction.setId(transactionId);
//display all information of that transaction
System.out.printf("\n%-4s %20s %25s %20s\n","Ticket Date", "Transaction Id", "Number of Tickets", "Total Price");
db.displayRecord(transactionId);
//re-confirm whether to delete
do {
System.out.print("\nConfirm delete? (1-yes 2-no): ");
choice = scan.nextInt();
}while(choice !=1 && choice !=2);
//delete from database
if(choice == 1) {
boolean success = db.deleteInfo(transaction.getId());
if (success)
System.out.println("Transaction Info deleted successfully.\n");
else
System.out.println("Error in deleting transaction info.\n");
}else
System.out.println("Returning to main menu...\n");
}
public void viewAllTransactions() throws Exception{
String result[] = null;
String date2 ="", letter="";
//enter start date
dateInput();
switch(date.substring(2,4)) {
case "01": letter = "Jan"; break;
case "02": letter = "Feb"; break;
case "03": letter = "Mar"; break;
case "04": letter = "Apr"; break;
case "05": letter = "May"; break;
case "06": letter = "June"; break;
case "07": letter = "July"; break;
case "08": letter = "Aug"; break;
case "09": letter = "Sep"; break;
case "10": letter = "Oct"; break;
case "11": letter = "Nov"; break;
case "12": letter = "Dec"; break;
}
date2 = date.substring(0,2) + letter + "20" + date.substring(4,6);
//show all records from start date
//display error if no matching one
boolean success = db.matchingDate(transaction.getDate());
if(success) {
System.out.println("\nTransaction Report from " + date2);
System.out.printf("%-4s %20s %25s %20s\n","Ticket Date", "Transaction Id", "Number of Tickets", "Total Price");
result = db.retrieveAllInfo(date);
for(String temp: result)
System.out.println(temp);
}else
System.out.println("Error in finding date entered.");
System.out.println("\nReturning to main menu...\n");
}
}
transaction class
public void setDate(String date) {
if(Validation.isValidDate(date))
this.date = date;
else
System.out.println("Date is invalid. Please re-enter.");
}
Honestly, those if checks are kind of hard to look at. Try this:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
public class dateTime {
public static void main(String[] args) throws ParseException {
SimpleDateFormat datefr = new SimpleDateFormat("dd-MM-yy");
datefr.setTimeZone(TimeZone.getTimeZone("UTC"));
datefr.setLenient(false);
try {
Date date = datefr.parse("12-10-21");
System.out.println(true);
} catch (ParseException e) {
System.out.println(false);
System.out.println(e.getMessage());
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.