2016-08-09 83 views
-5

我想从扫描仪输入创建arraylist。这是摩托车租赁系统。请帮助:)我是绝对的初学者:d从扫描仪到阵列列表

我试图创建表是这样的: (日期,日期,姓名,licenceID)

  1. 2016年8月9日,2016-08 -20,史密斯先生,1234567
  2. 2016年8月5日,2016年8月6日,杰克逊先生,6435434

这是我的代码:

public class Launcher{ 

    public static void main(String[] args) throws IOException { 

     ArrayList<Launcher> reservationlist = new ArrayList<>(); 
     reservationlist.add(2016-08-09, 2016-08-20, Mr Smith, 1234567; // there is an error 

     Helper.welcomeMessage(); 
     char choice, ignore; 

     for (;;) { 
      do { 
       Helper.showMenu(); 

       choice = (char) System.in.read(); 
       do { 
        ignore = (char) System.in.read(); 
       } while (ignore != '\n'); 
      } while (choice < '1' | choice > '5' & choice != 'q'); 
      if (choice == 'q') { 
       break; 
      } 

      Orders order = new Orders(); 

      switch (choice) { 
       case '1': // Get information about company 
        Helper.aboutUs(); 
        break; 
       case '2': // List of our motorcycles (enum) 
        System.out.println("Price per 1 day: PREMIUM 500, GOLD 500, BASIC 500"); 
        for (Prices pricelist : Prices.values()) 
         System.out.printf("%s\t%s\n", pricelist.getTitle(), pricelist); 
        break; 
       case '3': // Make a reservation 
        Scanner hp = new Scanner(System.in); 
        Orders reservation = new Orders(); 

        System.out.println("Type initial date:"); 
        reservation.initialDate = hp.next(); 

        System.out.println("Type final date:"); 
        reservation.finalDate = hp.next(); 

        System.out.println("Type your first name and last name:"); 
        reservation.name = hp.next(); 

        System.out.print("Type your driving licence ID:"); 
        reservation.drivingLicenceID = hp.next(); 

        System.out.println("\n\n Reservation completed " + reservation.name + "\n Thanks for your reservation\n"); 
        break; 

        reservationlist.add(reservation.initialDate, reservation.finalDate, reservation.name, reservation.drivingLicenceID)); // there is an error 

       case '4': // Check availability of motorcycles 
        System.out.println(""); 
        break; 
       case '5': // Contact with us 
        Helper.contact(); 
        break; 
      } 


public class Orders { 

     String initialDate; 
     String finalDate; 
     String name; 
     String drivingLicenceID; 

    public Orders(String initialDate, String finalDate, String name, String drivingLicenceID) { 
     this.initialDate = initialDate; 
     this.finalDate = finalDate; 
     this.name = name; 
     this.drivingLicenceID = drivingLicenceID; 
    } 

    public Orders() {} 

    @Override 
    public String toString() { 
     return "Orders{" + 
       "initialDate='" + initialDate + '\'' + 
       ", finalDate='" + finalDate + '\'' + 
       ", name='" + name + '\'' + 
       ", drivingLicenceID=" + drivingLicenceID + 
       '}'; 
    } 

    public void setInitialDate(String initialDate) { 
     this.initialDate = initialDate; 
    } 
    public void setFinalDate(String finalDate) { 
     this.finalDate = finalDate; 
    } 
    public void setName(String name) { 
     this.name = name; 
    } 
    public void setDrivingLicenceID(String drivingLicenceID) { 
     this.drivingLicenceID = drivingLicenceID; 
    } 
} 
+6

有你的代码,但你忘了问题。 – Kayaman

+0

'reservationlist.add(2016-08-09,2016-08-20,史密斯先生,1234567;'这条线的预期结果是什么?首先,它缺少必要的')'。其次,'add'方法期望你提供一个'Launcher'。 – bradimus

+0

当您将数据加载到预定列表中时,有几个主要错误。首先,你为什么要宣布它是发射器?其次,你将不同的类型插入同一个数组列表中(在Java中,数组列表只能存储一种类型的对象)。第三,你试图插入的变量都没有被正确写入(即字符串周围没有双引号,日期写成表达式)。我强烈建议你从一个较小的程序开始,然后继续前进。 – alexgbelov

回答

2

我想这是你努力实现的,而这样的:

List<Orders> reservationlist = new ArrayList<>(); 
reservationlist.add(new Orders("2016-08-09", "2016-08-20", "Mr Smith", "1234567")); 

而不是

ArrayList<Launcher> reservationlist = new ArrayList<>(); 
reservationlist.add(2016-08-09, 2016-08-20, Mr Smith, 1234567; // there is an error 

而且这样的:

reservationlist.add(reservation); 

reservationlist.add(
    reservation.initialDate, reservation.finalDate, reservation.name, 
    reservation.drivingLicenceID) 
); // there is an error