我正在尝试编写一个存储数组列表并具有多种方法的类

时间:2018-11-19 19:47:05

标签: java arrays class

我必须编写一个与用户互动的程序,并添加和查看机票预订。我在这里将此类称为ReservationSystem,需要创建以下方法: addReservation()将名字,姓氏,价格,座位号和座位号作为输入,并使用此信息创建一个“预定”,然后我需要将预定添加到预定列表中。接下来,我需要创建另一个addReservation()方法,该方法提示用户输入进行新预订所需的信息,并将此预订添加到预订列表中。接下来,我需要创建一种方法viewReservations(),该方法使用System.out打印所有保留,并且我必须使用toString()类提供的Reservation方法。由于某种原因,它没有将我的String firstName变量视为 我的viewReservations()方法中的字符串。还有如何将所有输入组合到一个变量中,在这段代码中,我刚刚使用+来组合输入,不确定是否正确。

ReservationSystem class:

package finalproject;
import java.util.*;

public class ReservationSystem {

ArrayList<String> reservations = new ArrayList<>();


public String addReservation(String firstName, String lastName, String 
price, String seatNumber, String seatLetter){

    String reservation = firstName + lastName + price + seatNumber + 
seatLetter;
    reservations.add(reservation);

    return reservation;
}

public void addReservation(){

    Scanner s = new Scanner(System.in);
    System.out.print("Enter new reservation: ");
    String reservation = s.nextLine();

    reservations.add(reservation);
}

public void viewReservation(){

    Reservation res = new Reservation(String firstName, String lastName, 
String price, String seatNumber, String seatLetter);

    res.toString(firstName, lastName, String, seatNumber, seatNumber);
}




}

预订等级:

package finalproject;

public class Reservation {

private String firstName;
private String lastName;
private String price;
private String seatNumber;
private String seatLetter;

public Reservation(String firstName, String lastName){

this.firstName = firstName;
this.lastName = lastName;
}   

/**
 *
 * @param price
 */
public Reservation(String price){

this.price = price;
}

public Reservation(String seatNumber){

this.seatNumber = seatNumber;
}

public Reservation(String seatLetter){

this.seatLetter = seatLetter;
}

/**
 *
 * @param firstName
 * @param lastName
 * @param price
 * @param seatNumber
 * @param seatLetter
 * @return
 */
public String toString(String firstName, String lastName, String price, 
String seatNumber, String seatLetter){

String info = firstName + lastName + price + seatNumber + seatLetter;
return info;

} 

/**
 *
 * @param price
 * @return
 */
public String getPrice(String price){

return price;
}

}

1 个答案:

答案 0 :(得分:1)

预订班

public class Reservation {

    private String firstName;
    private String lastName;
    private float price;
    private int seatNumber;
    private char seatLetter;

    // Constructor. Aligns values to the fields.
    public Reservation(String firstName, String lastName,
            float price, int seatNumber, char seatLetter){
        this.firstName = firstName;
        this.lastName = lastName;
        this.price = price;
        this.seatNumber = seatNumber;
        this.seatLetter = seatLetter;
    }

    // Returns a string with the values of the fields.
    public String toString(){
        return firstName + " " + lastName
            + "\nPrice: " + price
            + "\nSeat: " + seatLetter + seatNumber;
    }

    // Returns the value of seatLetter
    public char getSeatLetter() {
        return seatLetter;
    }
    // Returns the value of seatNumber
    public int getSeatNumber() {
        return seatNumber;
    }
}

ReservationSystem类

import java.util.ArrayList;
import java.util.List;

public class ReservationSystem {

    List<Reservation> reservations = new ArrayList<>();

    //Adds a Reservation object to the list
    public void addReservation(Reservation reservation){
        reservations.add(reservation);
    }
    // Prints a Reservation from the list based on seat number and seat letter
    public void viewReservation(int seatNumber, char seatLetter){
        for(Reservation res : reservations){
            // if seat number and seat letter dont match continue to the next reservation
            if(res.getSeatNumber() != seatNumber && res.getSeatLetter() != seatLetter){ continue; }
                System.out.println(res.toString());
            return;
        }
}
    // Prints all the Reservation objects of the list
    public void viewAllReservations(){
        for(Reservation res : reservations){
            System.out.println(res.toString() + "\n");
        }
    }
}

在您的主要班级

public static void main(String[] args) {

    ReservationSystem resSystem = new ReservationSystem();
    //Getting the values for a new reservation from user
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter First Name: ");
    String firstName = sc.nextLine();
    System.out.println("Enter Last Name: ");
    String lastName = sc.nextLine();
    System.out.println("Enter Price: ");
    float price = sc.nextFloat();
    System.out.println("Enter Seat Number: ");
    int seatNumber = sc.nextInt();
    System.out.println("Enter Seat Letter: ");
    char seatLetter = sc.next().charAt(0);
    sc.close();
    // Adding new reservation object to the list
    resSystem.addReservation(new Reservation(firstName, lastName, price, seatNumber, seatLetter));

    // Printing All the reservations from the list
    resSystem.viewAllReservations();
}