从另一个方法中提取数组值

时间:2020-11-30 09:50:57
public void DisplaySB() throws IOException{
System.out.println("\n|| ID || FirstName || LastName ||TelNum || Adults || Kids || Total Price || Total\n");

ArrayList<String> fb = new ArrayList<String>();
                           //Creats an array called fb(First booking) and gives it 4 identifiers. 
            fb.add("1"); //ID
            fb.add("James"); //First name
            fb.add("Pearce"); //Last name
            fb.add("07703768910"); //Phone number

    ArrayList<Integer> fbV = new ArrayList<Integer>();//First booking array storing 4 array items
             fbV.add(3); //first being the adult total
             fbV.add(4); //second kid total    
             fbV.add(fbV.get(0) * AdultPrice + fbV.get(1) * KidPrice); //Calculated the above 2 arrays by set variables 6 and 3
             fbV.add(fbV.get(0) + fbV.get(1)); //Adds the array value [0] 3 and [1] 4

    ArrayList<String> sb = new ArrayList<String>();
    //Creats an array called SecondBooking(sb) and gives it 4 identifiers. 
             sb.add("2"); // ID
             sb.add("Ryan"); //First name
             sb.add("Fellah"); //Last name
             sb.add("07707627112"); //tel num

    ArrayList<Integer> sbV = new ArrayList<Integer>(); // New array storing values like previously above for the second booking array
             sbV.add(2); //Adult array integer
             sbV.add(3); //Kid array integer
             sbV.add(sbV.get(0) * AdultPrice + sbV.get(1) * KidPrice); //Multiplication integer by adults * kids
             sbV.add(sbV.get(0) + sbV.get(1)); //Adding the total from array [0]2 and [1]3


    ArrayList<String> tb = new ArrayList<String>();
    //Creats an array called SetBooking and gives it 4 identifiers. 
             tb.add("3"); //Id
             tb.add("Josh"); // first name
             tb.add("Ford"); // second name
             tb.add("07725687654"); //phone number

    ArrayList<Integer> tbV = new ArrayList<Integer>(); //Set a third value(tb) arrayList
            tbV.add(6); //Storing adult value into the array
            tbV.add(6); //Storing kid value into the array
            tbV.add(tbV.get(0) * AdultPrice + tbV.get(1) * KidPrice); //multipling The above values by the correct variable
            tbV.add(tbV.get(0) + tbV.get(1)); //adding the array values [0]6 and [1]6

    ArrayList<Integer> totalMoney = new ArrayList<Integer>();
             totalMoney.add(sbV.get(2)+ fbV.get(2) + tbV.get(2)); //adding up everymultiplication from the above arrays, first booking, second and third

             ArrayList<Integer> total = new ArrayList<Integer>();
             total.add(sbV.get(3)+ fbV.get(3) + tbV.get(3));// Adding every total from the above arrays, first booking, second and third




    System.out.print(fb);
    System.out.println(fbV);
    System.out.print(sb);
    System.out.println(sbV);    //Printing arrays
    System.out.print(tb);
    System.out.print(tbV);

    //Creating an integer, using the integer to store the array value, displaying the array value.
            for (int money: totalMoney){
                System.out.println("\n\nCoach Revenue: £"+(money)); //displaying the money total from the array
                }

    //Creating an integer, using the integer to store the array value, displaying the array value.
            for (int endTotal: total){
                 System.out.println("Seats remaining:"+ (maxSeats - endTotal)); //display the total ammount of bookings from all relatble arrays.
            }

            System.out.println("\nWould you like to enter a new value?");
            System.out.println("[1]Yes [2]Menu");
            int choice = 0; //Creating a local variable named choice and setting it to 0
            Scanner scan = new Scanner(System.in); //Creating a scan variable named scan and using the system.in functon to store information within the variable
            choice = scan.nextInt(); //Using the choice variable to store an integer

            if(choice > 2){
            System.out.print("Incorrect value, try again!....");
            System.in.read();
            Display();
            }  

   }




public void UserBooking(){ 
  ArrayList<String> ub = new ArrayList<String>(); //Creating a new array for user bookings(ub)

         String choice1;
         String choice2; // Creating 3 variables later to be used later with the scanner
         String choice3;
         int adultInput;
         int kidInput;

         System.out.println("Please enter your first name:");
         Scanner scan2 = new Scanner(System.in); //Using scanner to store input into a variable
         choice1 = scan2.nextLine(); //Storing the input into variable choice1

         System.out.println("Please enter your last name:");
         choice2 = scan2.nextLine();//Storing the input into variable choice2

         System.out.println("Please enter your phone number:");
         choice3 = scan2.nextLine();//Storing the input into variable choice3

        ArrayList<Integer> ubV = new ArrayList<Integer>(); //Array being used to store integer values for the children and adults

        System.out.println("Please enter the number of adults:");
        adultInput = scan2.nextInt(); //Storing the input into variable adultInput

        System.out.println("Please enter the number of children:");
        kidInput = scan2.nextInt(); //Storing the input into variable kidInput

            ub.add("4");
            ub.add(choice1);
            ub.add(choice2); 
            ub.add(choice3);    //Pring the array values
            ubV.add(adultInput);
            ubV.add(kidInput); 
            ubV.add(adultInput * AdultPrice + kidInput * KidPrice ); //Calculating the above inputs by the set variable price 6 for adults and 3 for kids
            ubV.add(ubV.get(0) + ubV.get(1)); //Adding the array value [0] and [1], this would be taken from the variables

        ArrayList<Integer> totalMoneyUser = new ArrayList<Integer>(); //Another array that is gathering the new money total with the new user input array value
                 totalMoneyUser.add(sbV.get(2)+ fbV.get(2) + tbV.get(2) + ubV.get(2));

        ArrayList<Integer> totalUser = new ArrayList<Integer>();
                 totalUser.add(sbV.get(3)+ fbV.get(3) + tbV.get(3) + ubV.get(3));//Another array that is gathering the new booking total with the new user input array value

 }
}

Hoping the current project is readable, tried my best to edit it. Anyhow. How can I pull the values from DisplaySB arrays and place them into the UserBooking arrays? I need to do this to total the amount of seats left for the coach. The errors are within the last snippet, specifically on the last two arrays.

希望当前项目可读,尽我所能编辑它。无论如何。如何从DisplaySB数组中提取值并将它们放入UserBooking数组中?我需要这样做才能累计为教练留下的座位数。错误在最后一个片段内,特别是在最后两个数组上。

2 个解决方案

#1


0  

Declare the DisplaySb-arrays as class variables with getters and setters and fetch them from UserBooking like

使用getter和setter将DisplaySb数组声明为类变量,并从UserBooking中获取它们

ArrayList<String> fb = new MyClassNameWhereDisplaySBLives().getFBArray();

#2


0  

public class MyClass{

//class variables - outside method body, so every method "knows" them
private ArrayList<String> fb = new ArrayList<>();
    public void displaySB() throws IOException{
      //fill fb with values
      fb.add("1"); //ID
      //...
    }

    public void userBooking(){
      //retrieve data from fb
     fb.get(0);
   }
}

#1


0  

Declare the DisplaySb-arrays as class variables with getters and setters and fetch them from UserBooking like

使用getter和setter将DisplaySb数组声明为类变量,并从UserBooking中获取它们

ArrayList<String> fb = new MyClassNameWhereDisplaySBLives().getFBArray();

#2


0  

public class MyClass{

//class variables - outside method body, so every method "knows" them
private ArrayList<String> fb = new ArrayList<>();
    public void displaySB() throws IOException{
      //fill fb with values
      fb.add("1"); //ID
      //...
    }

    public void userBooking(){
      //retrieve data from fb
     fb.get(0);
   }
}