2017-03-07 7 views
-1

私は私のArrayListが保たれている以下のクラスがあります。アレイリストのデータ値を更新するにはどうすればよいですか?

import java.util.ArrayList; 
import java.util.Random; 

public class Theatre { 

private ArrayList<Room> theatres; 

public Theatre(){ 
    theatres = new ArrayList<Room>(); 
    theatres.add(new Room("Theatre 1", 120)); 
    theatres.add(new Room("Theatre 2", 180)); 
    theatres.add(new Room("Theatre 3", 100)); 
    theatres.add(new Room("Theatre 4", 120)); 
    theatres.add(new Room("Theatre 5", 200)); 
    theatres.add(new Room("Theatre 6", 180)); 
    theatres.add(new Room("Theatre 7", 80)); 
    theatres.add(new Room("Theatre 8", 50)); 
    theatres.add(new Room("Theatre 9", 120)); 
    theatres.add(new Room("Theatre 10", 150)); 

} 

public void addTheatre(String theatreName, int seatsAvailable){ 
    Room t = new Room(theatreName, seatsAvailable); 
} 

public void printTheatres(){ 
    for (int index=0; index<theatres.size(); index++) 
    { 
    System.out.println(theatres.get(index)); 
} 
    } 

public void updateSeats(){ 
    int numSelected = Basket.seatsBooked; 
    int userFilmSelection = MainActivity.filmSelectionNumber; 

    theatres.get(userFilmSelection).setSeatsAvailable(theatres.get(userFilmSelection).getSeatsAvailable() - numSelected); 
} 


public int getSeats(){ 
    int theatre = 0; 
    int userFilmSelection = MainActivity.filmSelectionNumber; 

    theatre = theatres.get(userFilmSelection).getSeatsAvailable(); 
    Basket.theatre+=theatres.get(userFilmSelection).getTheatreName(); 

    return theatre; 
} 

} 

を彼らは私のGUIクラスからバスケットに追加されたとき、私は、このArrayListの中に席を更新する方法をしたいと思います。これどうやってするの。私のGUIでは次のようなことがありますが、MainActivityの注文画面に戻るたびに座席数が減るようです。これは、毎回新しいTheaterオブジェクトを作成するためだが、これを回避する方法はわからないからだと思う。

私はGUIに出力できるように、更新された後に利用可能な現在の座席数を返すために以下のメソッドが必要です。

public int getSeats(){ 
    Theatre t = new Theatre(); 
    t.updateSeats(); 
    return t.getSeats(); 
} 

私はイムは、新しいオブジェクトを毎回作成するので、私はここで、オブジェクトが、doesntのを見ることができますし、最も可能性の高い青色のJと呼ばれるJavaのIDEでそれを試してみましたので、コードは動作します。

更新 これは更新されたシート数を返すはずですが、そうではありません。 getSeats()もし

public int getSeats(){ 
     Theatre.updateSeats(); 
     return Theatre.getSeats(); 
    } 
+2

は '「私はイムは、新しい劇場のオブジェクトごとに作成しているため、これがあると思いますが、これを回避する方法を知ってはいけない。」' - おそらく非静的プライベートシアターフィールドを作成し、それだけを扱います。 –

答えて

0

public int getSeats(){ 
    Theatre t = new Theatre(); 
    t.updateSeats(); 
    return t.getSeats(); 
} 

は、すべての呼び出し新しいインスタンスを作成している、とあなたが新しいデータを毎回使用している、その後、Theatre方法です。 updateSeats()後に呼び出された場合
あなたが最初

public int getSeats(){ 
    int theatre = 0; 
    int userFilmSelection = MainActivity.filmSelectionNumber; 

    theatre = theatres.get(userFilmSelection).getSeatsAvailable(); 
    Basket.theatre+=theatres.get(userFilmSelection).getTheatreName(); 

    return theatre; 
} 

を投稿する方法は罰金です。
あなたの質問を完全に理解しているかどうかはわかりません。あなたが常に利用可能であるためにあなたのRoom秒を必要とする場合、私はあなたが何をしたか作り直しList

private static final List<Room> theatres; 

static { 
    theatres = new ArrayList<>(10); 
    theatres.add(new Room("Theatre 1", 120)); 
    theatres.add(new Room("Theatre 2", 180)); 
    theatres.add(new Room("Theatre 3", 100)); 
    theatres.add(new Room("Theatre 4", 120)); 
    theatres.add(new Room("Theatre 5", 200)); 
    theatres.add(new Room("Theatre 6", 180)); 
    theatres.add(new Room("Theatre 7", 80)); 
    theatres.add(new Room("Theatre 8", 50)); 
    theatres.add(new Room("Theatre 9", 120)); 
    theatres.add(new Room("Theatre 10", 150)); 
} 

public Theatre(){ 
    // Nothing here 
} 

staticとして

[OK]を、あなたは理解してかどうかを確認、宣言したいことがあります。

public class Room 
{ 
    private String _name; 
    private int _seats; 

    public Room(final String name, final int seats) { 
     _name = name; 
     _seats = seats; 
    } 

    public String getName() { 
     return _name; 
    } 

    public int getAvailableSeats() { 
     return _seats; 
    } 

    public void setName(final String name) { 
     _name = name; 
    } 

    public void setAvailableSeats(final int seats) { 
     _seats = seats; 
    } 
} 

public class Theatre 
{ 
    private static final List<Room> ROOMS; 

    static { 
     ROOMS = new ArrayList<Room>(10); 
     ROOMS.add(new Room("Room 1", 120)); 
     ROOMS.add(new Room("Room 2", 180)); 
     ROOMS.add(new Room("Room 3", 100)); 
     ROOMS.add(new Room("Room 4", 120)); 
     ROOMS.add(new Room("Room 5", 200)); 
     ROOMS.add(new Room("Room 6", 180)); 
     ROOMS.add(new Room("Room 7", 80)); 
     ROOMS.add(new Room("Room 8", 50)); 
     ROOMS.add(new Room("Room 9", 120)); 
     ROOMS.add(new Room("Room 10", 150)); 
    } 

    public static void addRoom(final String roomName, final int seatsAvailable) { 
     ROOMS.add(new Room(roomName, seatsAvailable)); 
    } 

    public static void printRooms() { 
     for (final Room room : ROOMS) { 
     System.out.println(room.getName()); 
     } 
    } 

    public static void updateSeats() { 
     final Room room = ROOMS.get(MainActivity.filmSelectionNumber); 
     room.setAvailableSeats(room.getAvailableSeats() - Basket.seatsBooked); 
    } 

    public static int getSeats() { 
     final Room room = ROOMS.get(MainActivity.filmSelectionNumber); 
     Basket.theatre += room.getName(); 

     return room.getAvailableSeats(); 
    } 
} 
+0

'getSeats()'の最初のメソッドは私のGUIにあり、座席数を返して、私が持っているコンポーネントの1つを介して出力することができます。データを破棄するたびに新しいオブジェクトを作成しないようにする方法が必要です。 – user982467

+0

しかし、私はどのようにユーザーの入力に応じてシートを更新しますか。 – user982467

+0

更新された答え、別の時間を参照してください。hahaha:D – LppEdd

関連する問題