Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I'm looking for a way in java (not sql), to store a few inputs. And be able to refer each entry.
So for example the Id of person entering info, date entered, a score of some kind (int).
All I can find is a multi-dimensional array.
A potential solution is to create a class for the data you want, for example:
public class Data {
private int score;
private String info, date;
public Data(String info, String date, int score) {
this.score = score;
this.info = info;
this.data = data;
}
}
Now you can create these objects with the data that the user inputs, and add them to a collection (i.e an ArrayList) or simply to an array. Here's an example with an array:
Data[] myData = new Data[4]; // 4 is an arbitrary maximum number of entries
// Get the data into variables
// Now create a new Data object with the information and add it to the array
myData[0] = new Data(info, date, score);
// Repeat this process for all the input
What you are looking for can be done in many ways, the most accessible and understandable one probably being a List or ArrayList.
You seem to want to keep a list of Players, each having a few attributes. You could start by creating a Player object, like so
Class Player {
Public Player(int score, String name) {
this.score = score;
this.name = name;
}
private int score;
private String name;
public void setScore(int score){
this.score = score;
}
public int getScore(){
return this.score;
}
// Repeat for all class variables
Then, you would declare an ArrayList in one of your other classes. Note that it has the parameter type Player.
ArrayList<Player> playerList = new ArrayList<Player>();
You can then add Player objects to the playerList like so:
Player p = new Player(345, "randomName");
playerList.add(p);
If you then want to access one of your Players, and one of their scores specifically, you would do this:
int currentPlayerScore = playerList.get(0).getScore();
Naturally, 0 here stands for the number of the ArrayList entry.
Good luck.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 months ago.
Improve this question
Its a cinema booking system. My system has 2 different types of Room (common room and 3D room) and a class of reservations (with name, age and date).
public class Room {
int number;
int capacity;
List<Reservations> reservation = new ArrayList();
// constructor
public Room(int number, int capacity, List<Reservations> reservation) {
this.number = number;
this.capacity = capacity;
this.reservation = reservation;
}
}
public class Reservations {
String name;
int age;
Date date;
public Reservations(String name, int age, Date date) {
this.name = name;
this.age = age;
this.date = date;
}
}
public class CommonRoom extends Room{
public CommonRoom(int number, int capacity, List<Reservations> reservation) {
super(number, capacity, reservation);
}
public class 3DRoom extends Room{
public 3DRoom(int number, int capacity, List<Reservations> reservation) {
super(number, capacity, reservation);
}
How can I make a reservation for Common Rooms and 3D Rooms? Because the list only store type Reservations, and I need to store CommonRooms and 3DRooms.
I suspect your question is too broad for Stack Overflow. But your code currently looks confused in terms of design so I'll offer some thoughts.
Firstly, why would the Room (the theatre where the films are screened) be responsible for handling its own reservations? Wouldn't it be better to have a BoxOffice class which handles the reservations for the whole cinema?
Secondly, what does a Room (theatre) actually need to do? If we move the handling of tickets to a BoxOffice, then keeping track of reservations would also be handled there. Each Room would just need to be able to return the name of the room, the capacity of the room, the seat map for the room (if you want to allow customers to pre-book a specific seat) and any special features (such as support for 3D movies, fancy surround sound, etc).
Thirdly, is there any real difference between a standard theatre and a 3D theatre in terms of your booking software? Probably not, and in fact a BoxOffice would need to be able to keep track of reservations, remaining capacity, and specific seat numbers still available, regardless of which type of theatre is being booked. (Don't forget that each theatre will show several different movie showings each day, so you'd need a MovieScreening type too which the BoxOffice links to reservations.) So there's probably no good reason to use inheritance to define Room and then CommonRoom and 3DRoom and it would be cleaner to simply have Room and use a name field and a supportedFeatures field to differentiate the different theatres in your cinema, allowing all of them to fit within the same List<Room> or Map<MovieScreening, Room> without worrying about the mess caused by subclasses.
If this is in fact just a homework exercise where your tutor is trying to get you to show off an example of using inheritance, then ignore all of this advice. But be aware that inheritance is more often a curse than a benefit, and there's usually a better way of structuring your type hierarchy.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Hello so I'm having a bit of difficulties with a setter method on objects.
public class Company {
private String companyName;
public static int numberOfEmployees;
public Employees employees[];
public void setEmployees( String name, String heritage, String [] programmingLanguages, Salary d) {
Employees employee1 = new Programmer(name, heritage,programmingLanguages, d,d.getBasicBrutoSalary());
employees[numberOfEmployees] = employee1;
numberOfEmployees++;
}
So basicly this is a method defined in the 'company class' while making an Employees object who's using the parameters for making a 'Programmer'.
But that's not the deal, what I want tot do is by calling this setter method, automaticly create an object. So each time it's used, kind of increment the name of the object it's going to make.
So for example the first time I use it it makes an object called Employee1 and stores it in Employee[0].. second time I want it to store Employee2 into Employee[1].
Maybe I'm making this way too difficult but I'm just trying things out, and can't seem to find a way to make this work.
I suppose that Programmer object is subclass of Employees, or else it will not work. More or less it should look like the following:
public class Company {
private String companyName;
public static int numberOfEmployees;
public static Employees employees[];
public void setEmployees( String name, String heritage, String [] programmingLanguages, Salary d) {
numberOfEmployees++;
employees[numberOfEmployees] = new Programmer(name, heritage,programmingLanguages, d,d.getBasicBrutoSalary());;
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have a model: fase.java with Integers and Strings + getters and setters:
public class Fase implements Serializable {
private Integer age;
private String name;
}
I want to store both the Integer and String in a Array or ArrayList. I now use this:
public String[] getAllValues(){
String[] values = {age.toString(), name};
return values;
Then in dataServiceImpl.java I retrieve the data with:
user.getFase().getAllValues()[0];
and retrieve the age.
This works, but I have a lot more than age and name, and was thinking if I could put everything in Fase.java in one Array/ArrayList, because they are Integer and String, and then retrieve it in dataServiceImpl.java?
Something like this in Fase.java: ArrayList <Objects> f3Values = new ArrayList <Objects>();
or Fase [] f3Array = new Fase[34];
and then retrieve that in dataServiceImpl.java with: ArrayList<Fase3.Fase3Array> f3List = new ArrayList<Fase3.Fase3Array>();
and use something like: user.f3List[0]; ?
First, you should learn how Java works.
Is Java "pass-by-reference" or "pass-by-value"?
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
Then, you should learn how to properly create an encapsulated class, by defining both constructor(s) and getters, methods, setters (if needed; note that setters in general break encapsulation) etc.
Then, you should understand that to aggregate data you:
create a class, i.e. definition object that holds all the necessary fields,
create a storage aggregate (array, ArrayList, Map, whatever),
3a. create an object of a given class, setting the values of the fields,
3b. add the object to the aggregate,
3c. goto 3a until the aggregate is filled with the data needed.
Explaining that on the code provided, you should first have
public class Fase implements Serializable {
private int age;
private String name;
public Fase( int age, String name ) {
this.age = age;
this.name = name;
}
public int getAge() { return age; }
public String getName() { return name; }
}
then you can create the aggregate, e.g.
int FASE_MAX = 34;
Fase[] fArray = new Fase[FASE_MAX];
ArrayList<Fase> fArrayList = new ArrayList<Fase>(FASE_MAX);
then you create the objects and add them to the aggregate, e.g.
for( int i = 0; i < FASE_MAX; i++ ) {
Fase newFase = new Fase( i, "John Doe" );
fArrayList.add( newFase );
fArray[i] = newFase;
}
then, and only then, you can access the aggregate:
Fase someFase = fArrayList.get( n );
Fase someOtherFase = fArray[n];
Your Fase class can have whatever members and however many members you like and you can access them all. If you want an array of Fase then create one and each element of the array will contain all the Fase members.
Fase[] myArray = new Fase[34];
You have an array of 34 "Fase's" just add whatever members you want to your Fase class.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I have a problem with two solutions, which one to pick. I wonder what a right way of implementing attributes in a RPG are. Let's for the sake of it say we have three attributes. Offensive, Defensive and Speed. Every character will have those. Somewhere I need to store the name, the description and the values of the attributes. I figure it should be done in Attributes.java. Giving me the following Attribute.java
public class Attribute
{
private String name;
private String desc;
private int value;
public Attribute (String name, String desc, int value) {
this.name = name;
this.desc = desc;
this.value = value;
}
public String getName () { // for desc and value as well
return name;
}
}
Now onto the problem, were do I create and store these attributes?
Option #1, I create them here, filling the ArrayList with them
Character.java
import java.util.ArrayList;
import java.util.List;
public class Character
{
private List<Attribute> attributes;
public Character() {
attributes = new ArrayList<Attribute>();
}
public List<Attribute> getAttributes () {
return attributes;
}
}
Option #2, I create an AttributeSystem
AttributeSystem.java
public class AttributeSystem
{
private List<Attribute> attributes;
public AttributeSystem () {
attributes = new ArrayList<Attribute>();
attributes.add (new Attribute ("Offensive", "Your damage.", 5);
attributes.add (new Attribute ("Defensive", "Your defense.", 5);
attributes.add (new Attribute ("Speed", "Your speed.", 5);
}
public Attribute getAttribute(int i) {
return attributes[i];
}
}
Character.java
public class Character
{
private AttributeSystem attributes;
public Character() {
attributes = new AttributeSystem();
}
public AttributeSystem getAttributes () {
return attributes;
}
}
Conclusion
Option #2 makes logically more sense to me.
In both cases I can use a HashMap instead of an ArrayList.
Which one and why would I use?
Final Note
This does not yet effects on your character. Nor does it have the ability to add attribute points. Ignore those two factors for now.
Both options seems legit to me.
But I would declare the list of Attribute as static, as the list of available attribute for player will not change no matter what.
Then for each player, you would map each available attribute to a given value.
I agree with Pablo, but I'd like to expand it a bit. So I think this design is a bit too meta for your requirements. In that Attribute is so generic it's not that helpful, and its flexibility is getting in your way.
In design #2 a Character could never vary their attributes. They would all be 5. Maybe that's just an example, but it becomes more cumbersome to to work with when you go beyond the hard coded values.
If every character has to have those three values then why not a more straightforward design? This is also much faster and less code to access than the attribute scheme.
class Character {
int defensive, offensive, speed;
}
Now you can encapsulate modifiers like weapons, armor, etc by using getter methods like so:
class Character {
int defensive, offensive, speed;
Item weapon;
Item armor;
Item shoes;
public int getTotalDefensive() {
return armor.getDefensive() + defensive;
}
public int getTotalOffensive() {
return weapon.getOffensive() + offensive;
}
public int getTotalSpeed() {
return shoes.getSpeed() + speed;
}
public List<Attribute> getAttributes() {
// if you really need to work with a character like this then you can do that too.
List<Attribute> attributes = new ArrayList<Attribute>();
attributes.add( new Attribute( "offensive", "How much damage you can do", offensive );
attributes.add( new Attribute( "defensive", "How much damage you can sustain", defensive );
attributes.add( new Attribute( "speed", "How fast you can move", offensive );
return attributes;
}
}
Maybe you are over engineering: Assuming this is the classic RPG the chosen name for those values (attributes) should give you a hint: If you have a Character object, and it will have attributes... then I'd add those attributes to the character itself:
class Character {
int defensive,offensive,speed;
... //other attributes here, like name, race or whatever you need
}
The equipment, skills and other things that can be more variable (you can have an item or 20, you can learn one skill or 20... and their nature can be totally different between them) have more sense to be in a list or in another related object.
Besides, don't mix model and view: the description of each attribute should not be part of the Character, that kind of information is not needed by that object and it would be duplicated each time a new character is created.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have created an ArrayList which contains some infos .
Every line has this pattern : name age sex job .
I'm searching for a way to take for example the every value separately and to assign them in different variables. I've search the web , but I came out to nothing ! So if there is someone that could help me I would appreciate it !
I have no idea how to do it so I can't provide code ! Soory I'm newbie in java .
Create a class Person that will hold your data:
class Person {
private String name;
private int age;
private String sex;
private String job;
//class constructor...
//getters and setters...
}
Then, read the file. For every line in the file, create an instance of Person class and store it in a List. I'll do this in pseudocode, it's up to you the concrete implementation (otherwise it would be me doing your homework =\):
List<Person> people <- new ArrayList<Person>()
open_file(theFile)
while not_end_of_file
String name <- read_text
String age <- read_int
String sex <- read_text
String job <- read_text
Person person <- new Person()
person->setName(name)
//similar for other fields...
people->add(person)
end while
Sounds like you have a List of Strings. If so, you could call the String split(String regex) method to get a String[] back.
I think what you need to do is create a Person object which would contain a name, age, sex and job.
Then you could create an Arraylist of type Person.
List<Person> people = new ArrayList<Person>();
The person class would look something like this:
public class Person {
String name, sex, jobTitle;
int age;
public Person(String name){
this.name = name;
}
}