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: - 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 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: - 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)});}}
And this is also that part on how I add items to my external files. - 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;
-
}
-
}
I have attached a screenshot of my program on how it looks like.
2 2416
Hi,
See, if you want to load the data into JTable, you have to use DefaultTableMod el. I have given below sample how to load the data into the JTable. You can place the code where you want -
// Initialize JTable and DefaultTableModel
-
JTable jtbTable =new javax.swing.JTable();
-
DefaultTableModel dtmRecordDetails = null;
-
dtmRecordDetails = new DefaultTableModel(
-
new Object[][]{},
-
new String[]{
-
"PRODUCT",
-
"PRICE",
-
"QUANTITY",
-
}) {
-
-
boolean[] canEdit = new boolean[]{
-
false,false,false
-
};
-
// Set the Model in JTable
-
jtbTable.setModel(dtmRecordDetails);
-
-
.
-
.
-
.
-
.
-
-
// Add the Records in each row
-
Vector vctData=new Vector(3,2);
-
v.addElement(strProduct);
-
v.addElement(dblPrice);
-
v.addElement(intQty);
-
dtmRecordDetails.addRow(v);
-
Thanks
Rajkumar
Thanks Rajkumar... I'll try to do this... I hope it will work...
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Igor Shulgin |
last post by:
Hi!
What is standard and direct way (within Oracle 9.2 stored procedures)
for
writing binary data from Oracle to external file on disk?
We have tried to use UTL_FILE package, but it operates on files
consisting out of "line" of size no more than 32767 bytes and adds new_line
symbol upon closing files.
Also we checked out interMedia types...
|
by: Kevin Myers |
last post by:
Hello,
I'm an experienced application developer in some languages (including
various SQL dialects), but have very little experience with MS Access or
VBA, and am having trouble figuring out how to even get started with the
following seemingly simple task...
I have a table with a composite primary key made up of three columns. The
lowest...
|
by: Xero |
last post by:
How do you display an external file to the user in vb.net?
Thanks.
--
Xero
http://www.chezjeff.net
My personal web portal
|
by: Geoff Cox |
last post by:
Hello,
Just starting C# with MS Visual Studio Pro 2005 ...
How do I access array data which is in an external file? I would
prefer to have it there to make the code less cluttered..
Thanks
Geoff
|
by: teddarr |
last post by:
I'm having trouble reading the first 2 lines of data in an external file.
I am supposed to use a while loop to read the first 2 lines of an external file that contains several random integers. I think I need 2 loops. I also think it is best not to reinitialize the variables 'sum' and 'num' before the second loop executes.
The overall goal...
| |
by: teddarr |
last post by:
I'm having trouble reading the first 2 lines of data in an external file.
I am supposed to use a while loop to read the first 2 lines of an external file that contains several random integers. I think I need 2 loops. I also think it is best not to reinitialize the variables 'sum' and 'num' before the second loop executes.
The overall goal is...
|
by: McGowan |
last post by:
Hi, I'm trying to display data from a mysql database in a HTML table
but for some reason my code isn't working. At the moment I have got it
to read and display the headers and the first row of the table and it
actually creates the remaining rows in the html table but it doesn't
put any data in them. This is my code so far:
<?php
$con =...
|
by: seth_hickel |
last post by:
With other solutions I would get a recordset, read each record and
display data by formating my html as I wanted to display values of
each record. I am trying to display data in a three column three row
table with paging. How does this translate to ASP.NET - or - what
ASP.NET tools do I use to accomplish this and how.
Thank you.
|
by: shelley_2000 |
last post by:
What is the best approach to collect and load Employee Resume Data
from External Employees who may not have Microsoft access? If is
likely they will have Microsoft Word, but not Microsoft Access.
Is there any type of form/template that I could send to employees to
fill out and send back for me to import into the Access Database?
Or should...
|
by: amel86 |
last post by:
Hello everyone,
My problem is i want to display data in txtfile to the xml file.
For example i write www.yahoo.com in the test.txt and i want to display the link (www.yahoo.com) in the xml file.
Please anyone help me.
<albuminfo>
<artLocation>slideshow/slide1.jpg</artLocation>
<artist>The Doors</artist>
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
| |
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |