GRY-Online.pl --> Archiwum Forum

Poprawienie programu Java

10.03.2009
14:47
[1]

..::capsel22::.. [ Konsul ]

Poprawienie programu Java

Witam,

Napisalem program w Java, ale jako ze dopiero poznaje tajemniki to cos narypalem i nie bardzo wiem jak to naprawic zeby dzialalo jak powinno.

Cel programu.

Stworzenie programu ktory zapisuje podana liczbe ksiazek i pozwala na wprowadzonych danych przeprowadzac operacje, typu dodawanie nowych ksiazek, wyszukiwanie, skopiowanie bazy do pliku txt.

Co jest nie tak.

Okno pytajace ile ksiazek wprowadzic powinno wyskakiwac po wcisnieciu 0, stworzony arrey powinnien sie zapisaywac w programie co umozliwy dodawanie, wyszukiwanie i kopiowanie bazy.

Program

import javax.swing.*;

import java.util.*;

import java.io.*;



public class MyBookDatabase ‹


public static void main(String[] args) ‹

final int ARRAY_SIZE =Integer.parseInt(JOptionPane.showInputDialog(" How many books do you want to enter?"));

final int BookEntries = 1;


Book[] record = new Book[BookEntries];

Book[] number = new Book[ARRAY_SIZE];


final int SENTINEL = -1;



String Title;

String menu;

int MenuChoice;

int i;

int p;


menu = JOptionPane.showInputDialog("Enter New Entry = 0, Print Database = 1, Quit = 2, Search = 3, Output Database = 4");

MenuChoice = Integer.parseInt(menu);

while (MenuChoice != SENTINEL) ‹

if (MenuChoice == 0 ) ‹

for ( i = 0; i<BookEntries; i++) ‹

record = new Book();




for ( i = 0; i<BookEntries; i++) ‹

record.printBook();



menu = JOptionPane.showInputDialog("Enter New Entry = 0, Print Database = 1, Quit = 2, Search = 3, Output Database = 4");


MenuChoice = Integer.parseInt(menu);



else if (MenuChoice == 1) ‹

for ( p = 0; p<ARRAY_SIZE; p++) ‹


number[p].printnumber();





menu = JOptionPane.showInputDialog("Enter New Entry = 0, Print Database = 1, Quit = 2, Search = 3, Output Database = 4");


MenuChoice = Integer.parseInt(menu);




else if (MenuChoice == 2) ‹

System.exit(0);



else if (MenuChoice == 3) ‹

Title = JOptionPane.showInputDialog("Enter title's name To Search For :");



System.out.println("Searching For Album ... : " + Title);


for ( p = 0; p<ARRAY_SIZE; p++) ‹

if (number[p].Title.equals(Title))

number[p].printnumber();



menu = JOptionPane.showInputDialog("Enter New Entry = 0, Print Database = 1, Quit = 2, Search = 3, Output Database = 4");


MenuChoice = Integer.parseInt(menu);



else if (MenuChoice == 4) ‹

try ‹

fileoutput(number);


› catch (IOException e) ‹

System.out.println("FileReader I/O failure" + e);



menu = JOptionPane.showInputDialog("Enter New Entry = 0, Print Database = 1, Quit = 2, Search = 3, Output Database = 4");


MenuChoice = Integer.parseInt(menu);







public static void fileoutput(Book[] number) throws IOException ‹

final FileWriter outputFile = new FileWriter("Book_Database.txt");


final BufferedWriter OutputBuffer = new BufferedWriter(outputFile);

final PrintWriter printstream = new PrintWriter(OutputBuffer);



for(int p = 0; p < number.length; p++) ‹


printstream.println(number[p]);

printstream.close();








class Book ‹

public String Title;

public String Author;

public int Year;


public Book() ‹

Title = JOptionPane.showInputDialog("Enter Title's name : ");

Author = JOptionPane.showInputDialog("Enter Author's Name :");

Year = Integer.parseInt(JOptionPane.showInputDialog("Enter Year of publication :"));



public Book(String t, String a, int y) ‹


Title = t;

Author = a;

Year = y;



public void printBook() ‹


String print = "Title: " + Title + " By: " + Author + " in " + Year;

System.out.println(print);





public void printnumber() ‹

String number = "Title: " + Title + " By: " + Author + " in " + Year;

System.out.println(number);






Dziekuje

© 2000-2025 GRY-OnLine S.A.