Reputation: 13
I am working on a project to help get me back into Java coding and it is a text based game (I know, not much, but I have to start somewhere). Anyway, I have come across a problem. I need to be able to put names of parts (it is a hardware tycoon game) and prices along with them into an array. For example, a desktop computer has parts such as a CPU, and the game would list your CPU choices. I need a way of storing this data, and it's pretty complicated to me because I need to not only store all of the names of CPUs for the player's benefit, but also store the prices alongside the names. On top of that, I have multiple product types such as desktops, laptops, and consoles, which each pretty much have different part names and prices. I thought of a 3 dimensional array to store the product types such as desktop (in columns), the part names (in rows), and the prices (behind the rows, if that makes sense in a 3 dimensional way. But I do not know how to initialize such an array and how to set the values on initialization.
Also, I thought of creating classes for each product type and putting arrays in each class to define parts and prices (2d arrays), but it is still complex and I would like to know how to sort this data and potentially make a system where certain parts are unlocked as game time progresses. Thank you in advance.
Upvotes: 1
Views: 755
Reputation: 463
More than multidimensional arrays, you could tag this with Object Oriented Programming (OOP). From your description it looks like you would need a class hierarchy, something like:
abstract class Product
class Desktop extends Product
class Laptop extends Product
class Console extends Product
Put all common fields/methods that can be used by Desktop, Laptop, Console etc into your Product class.
//Just an example
abstract class Product {
String name;
ArrayList<Component> components;
public String getName(){
return name;
}
}
Since each product has several components the product needs to have a list of components as shown above.
abstract class Component{
Double price;
public Double getPrice(){
return price;
}
}
Now you can have components like CPU, Power supply etc, they have some common behavior and fields like price that is put into Component class. Any specialized behavior / fields for each component can be put into the corresponding class like clock frequency shown below:
class CPU extends Component {
Double clockFreq;
}
So if your list of parts is 3 items long it could be written to a text file like so:
name,type,price
intelCPU6600,CPU,200
amdCPU7789,CPU,150
PS1Power,PSU,120
newPart,unknown,500
This list could be 100's of items without any problem. Read it into your program using Scanner & for each line do something like:
String line = scanner.nextLine();
String[] fields = line.split(",");
if("CPU".equals(fields[1]){
CPU cpu = new CPU(fields[0],fields[1],fields[2]);
//Product is an object of the class Product that you should have created earlier
product.components.add(cpu);
} else if("PSU".equals(fields[1]) {
PSU psu = new PSU(fields[0],fields[1],fields[2]);
product.components.add(psu);
} //..so on
if there is a generic product that you don't have a class for that's where the abstract class can be used:
if("unknown".equals(fields[1]){
Component comp = new Component(fields[0],fields[1],fields[2]);
product.components.add(comp);
}
Upvotes: 0
Reputation: 2251
How about using a Map
instead. A Map
let's you store information in Key
/Value
pairs. You can make a Map
that has a key
type of String
(that represents the product type) and a value
type of Map
(that represents individual products) that has a String
key
(product name) and a value that stores the price (perhaps a double
) or the info (as a String
or something). That way you can do something like outerMap.get("laptops").get("laptop1")
and that would return the information of laptop1. The first get
, gets the Map that contains all of the laptop products (or whatever product type ypu would want). The second returns the information for the specific product in that category.
You can implement this like this.
Map<String, Map<String, String>> productMap = new HashMap<>();
Using this you would place a product type like this:
productMap.put("laptops", new HashMap<String,String>());
And then add a product like this:
productMap.get("laptops").put("laptop1","This is information about laptop 1");
PS: In case you aren't aware, the reason I used = new HashMap
instead of = new Map
is because Map
is an interface not a class. A HashMap
is a class that implements
the Map
interface.
Upvotes: 0
Reputation: 734
I agree with @Regis that you should be using a custom object to describe your products, and then storing them in a some kind of data structure.
However, you can solve the problem you described by declaring a multi-dimensional array of the type "Object," which will allow you to put basically anything into it, even primitives (autoboxing and autounboxing was added in Java 5 and will seamlessly translate between primitive types like int
and the Java wrapper class for that type, like java.lang.Integer
).
Of course such an array would be very weakly typed and there would be nothing stopping you from adding doubles to the product name column, or vice versa. You'd also have to do a lot of casting, which is a code smell.
Upvotes: 0
Reputation: 176
I might take you in a different direction. Why don't you create classes and objects? Then create instances of those objects? Java is an object oriented language. Creating objects would allow you to hold all of the values you need. Quick example,
Public abstract class CPU {
// Declare fields
private float price;
private String name;
// Declare constructors
}
Public class intelCPU extends CPU {
// GetPrice
public int getPrice() {
return price;
}
// Set Name
public void setName(n) {
name = n;
}
}
Upvotes: 1