having problem with array of arrays
hi! am quite confused with a program that I need to do. I have to make a program for a collection of basketball players. Each player comes with information like age, name, height, and others. I have to make use of arrays only to store those information. The thing is, I also need to have features like adding records, deleting, searching,displaying. Should I make use of a switch to add these features? I am also having trouble with the adding record feature. Where should I start? Anybody who knows?.. I badly need help....
re: having problem with array of arrays
hi! thanks for the reply... i am just wondering, should i use this kind of array declaration?
char[][] team = new char[][];
am familiar with array but am still quite confused with a 2D array
anyone who wants to check this program?.. having a hard time..
hi! thanks for the reply.. i tried the program but it didn't work.. can u kndly check on this one.... thanks again.....
public class coursework{
private String plate;
private String brand;
private int price;
private int speed;
private String color;
private String model;
public String getPlate()
{
return plate;
}
public String getBrand()
{
return brand;
}
public int getPrice()
{
return price;
}
public int getSpeed()
{
return speed;
}
public String getColor()
{
return color;
}
public String getModel()
{
return model;
}
public String compareTo(Object anotherPerson) throws ClassCastException {
if (!(anothercoursework instanceof coursework))
throw new ClassCastException("A Person object expected.");
String anothercourseworkBrand = ((coursework) anothercourseworkBrand).getBrand();
return this.brand - anothercourseworkBrand;
}
public static void main (String args[])throws Exception{
coursework cs = new coursework();
char choice;
System.out.println("CAR COLLECTION MAIN MENU");
System.out.println("[A] Display all cars information");
System.out.println("[B] Add another record to the collection");
System.out.println("[C] Search for a record");
System.out.println("[D] Display records based on brand");
System.out.println("[E] Exit");
System.out.println("Please Enter your choice [A-E]:");
choice=(char)System.in.read();
System.in.read();System.in.read();
choice=choice.toUpperCase();
switch(choice)
{
case 'A':
System.out.println("CARS INFORMATION");
cs.displayCar();
break;
case 'B':
System.out.println("ADD RECORD:");
cs.addCar();
break;
case 'C':
System.out.println("ENTER PLATE NO. OF CAR TO SEARCH");
cs.searchCar();
case 'D':
System.out.println("LIST OF CARS BASED ON BRAND NAME");
cs.displayBrand();
case 'E':
System.out.println("EXITING...");
break;
default:
System.out.println("Invalid Choice![A-E only].");
}
class carcollect{
coursework[] info = new coursework[100];
int x=0;
void addEntry(String plate, String brand, String price, String speed, String color, String model);
{
info[x] = new coursework();
info[x].plate = plate;
info[x].brand = brand;
info[x].price = price;
info[x].speed = speed;
info[x].color = color;
info[x].model = model;
x++;
}
}
public void addCar(){
String carPrice;
String carSpeed;
try{
BufferedReader input=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter plate no. of car:");
info[x].plate=input.readLine();
System.out.println("Enter brand of car:");
info[x].brand=input.readLine();
System.out.println("Enter price of car:");
carPrice=input.readLine;
info[x].price=Integer.parseInt(carPrice);
System.out.println("Enter speed of car:");
carSpeed=input.readLine();
info[x].speed=Integer.parseInt(carSpeed);
System.out.println("Enter color of car:");
info[x].color=input.readLine();
System.out.println("Enter car model:");
info[x].model=input.readLine();
}catch(IOexception e) {}
}
public void displayCar(){
System.out.println("ALL CARS INFORMATION");
for(i=0; i<100;i++){
coursework work = new coursework[i];
String plate = coursework.getPlate();
String brand = coursework.getbrand();
String price = coursework.getprice();
String speed = coursework.getspeed();
String color = coursework.getcolor();
String model = coursework.getmodel();
System.out.println("Plate No. of Car:" + plate);
System.out.println("Brand Name:" + brand);
System.out.println("Price:" + price );
System.out.println("Speed:" + speed);
System.out.println("Color:" + color);
System.out.println("Model:" + model);
}
}
public void searchCar(){
for(int i=0; i<100; i++){
if(plate.equals(info[i].name))
return info[i].brand;
return info[i].price;
return info[i].speed;
return info[i].color;
return info[i].model;
}
return null;
}
public void displayBrand(){
Arrays.sort(persons);
System.out.println("SORTED BY BRAND:");
for (int i=0; i<4; i++) {
coursework cw = coursework[i];
String plate = coursework.getPlate();
String brand = coursework.getbrand();
String price = coursework.getprice();
String speed = coursework.getspeed();
String color = coursework.getcolor();
String model = coursework.getmodel();
System.out.println("Plate No. of Car:" + plate);
System.out.println("Brand Name:" + brand);
System.out.println("Price:" + price );
System.out.println("Speed:" + speed);
System.out.println("Color:" + color);
System.out.println("Model:" + model);
}
}