0

I need to save the data collected from the scanner input to use again throughout my code. For example, if someone enters the value one it gets saved into an array, and then afterward they enter the value 2 both should be saved into the array.

This is my code up until now:


public static void main(String[] args){
        
    Scanner myObj = new Scanner(System.in);

    System.out.println("Enter the customer name, the day of the booking, the month of the booking, and the number of people:");

    String name = myObj.nextLine();
    int day = myObj.nextInt();
    int month = myObj.nextInt();
    int number = myObj.nextInt();
    
  int y = day;
    int x = month;
    if (y > 21 && x > 03) { 
    System.out.println("Upcoming bookings: ");
    System.out.println("Customer name: " + name); 
    System.out.println("Date of booking: " + day + "/" + month);
    System.out.println("Number of People: " + number); 
      }
    else {
    System.out.println("There are no upcoming bookings");
   }

}

The idea is that the user can enter booking details but they only get displayed if the date has not passed, for that however, I need to be able to save all the inputs collected by the scanner.

dariosicily
  • 4,239
  • 2
  • 11
  • 17

3 Answers3

2

I will not write the whole code but will give you some idea so you can work on it. You need to create a class you can name it as Booking

 class Booking {
          public String name;
          public int day;
          public int month;
          public int number;
    
          public Booking(Scanner sc) {
           System.out.println("Enter the customer name, the day of the booking, the month of the booking, and the number of people:");
              this.name = sc.nextLine();
              .....
          }
    
          public boolean validDate(int month, int year){
             if(this.month < month && this.year < year)
                return false;
           return true;
          }     
    }
    
    class Main(){ 
    
        List<Booking> objLst = new ArrayList<Booking>();


        public static void main (String[] args){
        Scanner sc = new Scanner(System.in);
    
       
        Booking booking1 = new Booking(sc);
        objLst.add(booking1);
        Booking booking2 = new Booking(sc);
        objLst.add(booking2);
        for (int i = 0; i < objLst.size(); i++) {
                if(objLst.get(i).validDate(22, 2021){
                    //Create print function in Booking class and call it here.
                 }
            }
        //and booking.validDate will return true or false to check if date is not passed
    }

}

Syed Mohib Uddin
  • 718
  • 7
  • 16
  • Hello good sir may I ask if there is a difference between `List objLst = new ArrayList();` and `ArrayList objLst = new ArrayList();`? Is this the same but just shortcut? hahaha. Thanks – Gerome Tahud Mar 21 '21 at 08:25
  • Hello Thanks for your answer but I'm a little stuck on the objList part as the java program keeps marking it as an error. I would really appreciate it if you could further elaborate on that. Thanks in advance – anonymous45678 Mar 21 '21 at 08:59
  • 1
    @Gerome both are the same the difference is only List is interface and Arraylist is class – Syed Mohib Uddin Mar 21 '21 at 11:30
  • @anonymous45678 can you share the error. I am creating a list that will store booking data in it on .add(booking). – Syed Mohib Uddin Mar 21 '21 at 11:32
0

For these data you can declare class and can use array of object to store all the inputs. But for if condition you need to iterate for all the data stored inside array

  • could you please elaborate on what you mean by iterate and how exactly to use array of object, I'm pretty new to java so apologies for asking rather dumb questions – anonymous45678 Mar 21 '21 at 07:27
0

If by save you mean "you can retrieve the data again after terminating the program" then you might want to check out Java File Handling or Java Database Connectivity. If you dont want the database approach I suggest you try File Handling also check out my question a year ago. I created a functioning save file but you need to store your data in the arraylist of object(see @Syed Mohib Uddin answer).

Gerome Tahud
  • 103
  • 8