I'm new in using tables. I have a series of getter methods and I wants to display them in a table. I've been trying to use the external file that my program saves but I'm having a hard time how to display it in the table. Then my classmate told me that it's easier to just use methods in our CarRecordManage r class. The external file contains several data types... (int, String, String, double, int) to be specific... In that order as well How can I do this? Can somebody please help me?
Here is the RecordManager class:
I wanted to display the data with a click of a button but I don't know how to. Can somebody please help me.
I tried to use the getters from the CarRecordManage r but I wasn't successful.
Here's the part that I tried to code for the displaying of data:
And this is also that part on how I add items to my external files.
I have attached a screenshot of my program on how it looks like.
Here is the RecordManager class:
Code:
package util;
import java.util.*;
import java.io.*;
public class CarRecordManager{
private RandomAccessFile raf;
private int NUMBER_OF_SPACES = 0;
private StringBuilder SPACE;
private Product[] productList;
public CarRecordManager(String fileName)throws IOException{
raf = new RandomAccessFile(fileName, "rw");
countSpace();
}
/*
add operation
*/
public void addNewCar(Product p)throws IOException{
//error traps
if(!inRange(p.getProductID())) return;
if(productExists(p.getProductName())){
System.out.println(p.getProductName()+" already exists. Operation cancelled.");
return;
}
//main operation
int index;
if(hasSpace()){
index = getIndexOfSpace();
p.appendID(index);
}else{
index = p.getProductID();
}
raf.seek(index*Product.RECORD_SIZE);
p.writeData(raf);
refresh();
raf.close();
}
/*
delete operation
*/
public void deleteRecordAt(int index)throws IOException{
//error traps
if(!inRange(index)) return;
if(productList[index].getProductName().equals("?")) return;
//main operation
raf.seek(index * Product.RECORD_SIZE);
Product tempProduct = new Product(index, "?", 0.0, 0);
tempProduct.writeData(raf);
refresh();
}
/*
append operators
*/
public void appendQuantityAt(int index, int quantity)throws IOException{
//error traps
if(!inRange(index)) return;
if(productList[index].getProductName().equals("?")) return;
//main operation
raf.seek(index * Product.RECORD_SIZE);
Product tempProduct = productList[index];
tempProduct.appendQuantity(quantity);
tempProduct.writeData(raf);
refresh();
}
public void appendPriceAt(int index, double price)throws IOException{
//error traps
if(!inRange(index)) {
return;
}
if(productList[index].getProductName().equals("?")) {
return;
}
//main operation
raf.seek(index * Product.RECORD_SIZE);
Product tempProduct = productList[index];
tempProduct.appendPrice(price);
tempProduct.writeData(raf);
refresh();
}
public void appendNameAt(int index,String name)throws IOException{
//error traps
if(!inRange(index)) return;
if(productList[index].getProductName().equals("?")) return;
//main operation
raf.seek(index * Product.RECORD_SIZE);
Product tempProduct = productList[index];
tempProduct.appendName(name);
tempProduct.writeData(raf);
refresh();
}
public void appendProductAt(int index, Product product)throws IOException{
//error traps
if(!inRange(index)) return;
if(productList[index].getProductName().equals("?")) return;
//main operation
raf.seek(index * Product.RECORD_SIZE);
product.appendID(index);
product.writeData(raf);
refresh();
}
/*
record accessor
*/
public Product getRecordAt(int index){
return productList[index];
}
/*
property accessors
*/
public int getProductIDAt(int index){
return productList[index].getProductID();
}
public String getProductNameAt(int index){
return productList[index].getProductName();
}
public double getProductPriceAt(int index){
return productList[index].getProductPrice();
}
public int getProductQuantityAt(int index){
return productList[index].getProductQuantity();
}
/*
index accessors
*/
public int indexOf(String name){
int index = -1;
for (Product e : productList){
if(e.getProductName().equalsIgnoreCase(name)){
index = e.getProductID();
}
}
return index;
}
public int indexOf(Product p){
int index = -1;
for (Product e : productList){
if(e.getProductName().equalsIgnoreCase(p.getProductName())){
index = e.getProductID();
}
}
return index;
}
public int getLastIndex()throws IOException{
return (int)(raf.length() / Product.RECORD_SIZE);
}
/*
boolean checkers
*/
public boolean inRange(int index)throws IOException{
if(index>getLastIndex()) return false;
else return true;
}
public boolean hasSpace(){
if(NUMBER_OF_SPACES>0) return true;
else return false;
}
public boolean dataExistsAt(int index){
if(index >= productList.length){
System.out.println("Index out of bound!");
return false;
}
if(productList[index].getProductName().equals("?")){
System.out.println("Index exists but is considered a Space! ");
return false;
}else return true;
}
public boolean productExists(String name){
boolean found=false;
for (Product e : productList){
if(e.getProductName().equalsIgnoreCase(name)){
found=true;
}
}
return found;
}
/*
console helps
*/
public void print(){
for (Product e : productList){
if(!e.getProductName().equals("?")){
System.out.println(e);
}
}
}
public void printRecordAt(int index){
System.out.println(productList[index]);
}
public void printByProductID(){
sortByProductID();
print();
}
public void printByProductName(){
sortByProductName();
print();
}
public void printByProductPrice(){
sortByProductPrice();
print();
}
public void printByProductQuantity(){
sortByProductQuantity();
print();
}
/*
sorting operations
*/
public void sortByProductID(){
for(int i=0;i<productList.length;i++){
for(int j=i+1;j<productList.length;j++){
if(productList[i].getProductID() > productList[j].getProductID()){
Product temp = productList[i];
productList[i] = productList[j];
productList[j] = temp;
}
}
}
}
public void sortByProductName(){
Arrays.sort(productList);
//operation erased
}
public void sortByProductPrice(){
for(int i=0;i<productList.length;i++){
for(int j=i+1;j<productList.length;j++){
if(productList[i].getProductPrice() > productList[j].getProductPrice()){
Product temp = productList[i];
productList[i] = productList[j];
productList[j] = temp;
}
}
}
}
public void sortByProductQuantity(){
for(int i=0;i<productList.length;i++){
for(int j=i+1;j<productList.length;j++){
if(productList[i].getProductQuantity() > productList[j].getProductQuantity()){
Product temp = productList[i];
productList[i] = productList[j];
productList[j] = temp;
}
}
}
}
/*
private methods
*/
private void countSpace()throws IOException{
productList = new Product[getLastIndex()];
for (int i = getLastIndex() - 1; i >= 0; i--){
productList[i] = new Product();
raf.seek(i * Product.RECORD_SIZE);
productList[i].readData(raf);
}
SPACE = new StringBuilder();
for (Product e : productList){
if(e.getProductName().equals("?")){
NUMBER_OF_SPACES++;
SPACE.append(e.getProductID()+",");
}
}
}
private int getIndexOfSpace()throws IOException{
int index = 0, ctrl = 0;
String temp = "";
String space = SPACE.toString();
while(space.charAt(ctrl) != ','){
temp += Character.toString(space.charAt(ctrl));
ctrl++;
}
index = Integer.parseInt(temp);
while((ctrl)>=0){
SPACE.deleteCharAt(ctrl);
ctrl--;
}
refresh();
return index;
}
private void reset()throws IOException{
sortByProductID();
refresh();
}
private void refresh()throws IOException{
countSpace();
}
}
I tried to use the getters from the CarRecordManage r but I wasn't successful.
Here's the part that I tried to code for the displaying of data:
Code:
public void readFile() throws Exception{
for(int i = 0; i<inventoryTable.getRowCount(); i++){
inventoryTable.(cars.getProductIDAt(i), new Object[]{cars.getProductIDAt(i), cars.getProductNameAt(i), cars.getProductPriceAt(i),
cars.getProductQuantityAt(i)});}}
Code:
public void add() throws Exception{
String brand = brandTxt.getText();
String model = modelTxt.getText();
String displacement = engineTxt.getText();
double price = Double.parseDouble(priceTxt.getText());
int quantity = Integer.parseInt(quantityTxt.getText());
switch(brand){
case "Honda":
cars = new CarRecordManager("hondaInventory.txt");
cars.addNewCar(new Product(cars.getLastIndex(), model, displacement, price, quantity));
JOptionPane.showMessageDialog(null, "You have successfully added a car under " + brand+".");
break;
case "Ford":
cars = new CarRecordManager("fordInventory.txt");
cars.addNewCar(new Product(cars.getLastIndex(), model, displacement, price, quantity));
JOptionPane.showMessageDialog(null, "You have successfully added a car under " + brand+".");
break;
case "Toyota":
cars = new CarRecordManager("toyotaInventory.txt");
cars.addNewCar(new Product(cars.getLastIndex(), model, displacement, price, quantity));
JOptionPane.showMessageDialog(null, "You have successfully added a car under " + brand+".");
break;
}
}
Comment