ログインしてさらにmixiを楽しもう

コメントを投稿して情報交換!
更新通知を受け取って、最新情報をゲット!

Javaの課題丸投げコミュのLibraryを作る

  • mixiチェック
  • このエントリーをはてなブックマークに追加
はじめまして、なぜかオーストラリアでJAVAの勉強しているノヴです。
課題はLinkedlistとGUIを使ったLibrary作りです。
リストは借り手(member)と本(book)の2つ作ります。
借り手の情報はid, first name, last name, 借りた本の数、
本の情報はid, title, 残りの冊数です。
借り手は最大5冊まで借りられ、本はそれぞれ2個のコピー、つまり
同じ本が2冊あります。
GUIでできる操作は
?新しく一人のmemberを追加する
?新しく本を追加する(自動的に2冊)
?一人のmemberを消す
?本(一種類)を消す
?本を借りる(この操作ではまずmemberを選んでから)
?すべての本の情報を見る
?すべてのmemberの情報を見る
?1人のmemberの情報を見る
?ある(1つ)本の情報を見る
です。
さらに例外も決められていて
?特定のidのmemberが存在しないとき
?特定のidの本が存在しないとき
?本の冊数が0のとき(2冊とも借りられているとき)
?同じidが存在するとき
?まだ借りている本があるmemberを消そうとしたとき
?memberか本のidがintegerじゃないとき(数字じゃないとき)
です。
最後に、GUIを閉じるときには入力したデータが保存されて
次起動したときに同じところから始められるようにしろ、
と言われています。
でも、よくわからないので、saveボタンで記録して、次の起動でopenかなと
勝手に思っています。

一応、途中まではできているのでそれを載せてみます。
とりあえずGUIにつなげる前にコンソールでやってみようと思っているので
driver.javaも一応載せました。
今、memberを選んで本を借りる所でつまづいています。

//membernode(memberの情報)
public class MemberNode
{
MemberNode next;

int id;
String fName;
String lName;
int[] books = new int[5];
int bookCount = 0;


public MemberNode( int id, String fName, String lName)
{
this.id = id;
this.fName = fName;
this.lName = lName;
this.books[0] = 0;
this.books[1] = 0;
this.books[2] = 0;
this.books[3] = 0;
this.books[4] = 0;

next = null;
}
}

//member list
import java.util.*;

public class MemberList
{
MemberNode head;


public MemberList()
{
head = null;
}

public void addNode( int id, String fName, String lName )
{
MemberNode m = new MemberNode( id, fName, lName );

MemberNode current = head;

if ( head == null )
head = m;
else
{
while ( current.next != null )
{
current = current.next;
}

current.next = m;
}

}

public void createNodeFromFile( String str )
{
StringTokenizer t = new StringTokenizer( str, "|");
String id = t.nextToken();
String fName = t.nextToken();
String lName = t.nextToken();
int bookId;

MemberNode m = new MemberNode( Integer.parseInt( id ), fName, lName );

for (int i = 0; i < 5; i++ )
{
m.books[i] = Integer.parseInt( t.nextToken() );
}

MemberNode current = head;

if ( head == null )
head = m;
else
{
while ( current.next != null )
{
current = current.next;
}

current.next = m;
}
}

public boolean removeMember(int key)
{
MemberNode current = head;
MemberNode previous = null;

while (current != null && current.id != key)
{
previous = current;
current = current.next;
}

if (current == null)
{
System.out.println("There's no such member");
return false;
}
else if (previous == null)
head = head.next;
else
previous.next = current.next;

return true;
}

public boolean viewAMember(int key)
{
MemberNode current = head;

while (current != null)
{
if (current.id == key)
{
System.out.println("-----------------------------------------------------------");
System.out.println("ID | First Name | Last Name | Bollowed Books");
System.out.println("-----------------------------------------------------------");
System.out.println(current.id + "\t" + current.fName + "\t" + current.lName + "\t" + current.books);
System.out.println("-----------------------------------------------------------");
return true;
}
current = current.next;
}
System.out.println("Member's ID cannot be found");
return false;
}

public void viewAllMembers()
{
MemberNode current = head;

System.out.println("-----------------------------------------------------------");
System.out.println("ID | First Name | Last Name | Bollowed Books");
System.out.println("-----------------------------------------------------------");
while ( current != null )
{
System.out.println(current.id + "\t" + current.fName + "\t" + current.lName + "\t" + current.books);
current = current.next;
}
System.out.println("-----------------------------------------------------------");
}

}

//book node
public class BookNode
{
BookNode next;

int id;
String title;
int num;


public BookNode( int id, String title )
{
this.id = id;
this.title = title;
this.num = 2;

next = null;
}
}

//book list
import java.util.*;

public class BookList
{
BookNode head;


public BookList()
{
head = null;
}

public void addNode( int id, String title )
{
BookNode b = new BookNode( id, title );

BookNode current = head;

if ( head == null )
head = b;
else
{
while ( current.next != null )
{
current = current.next;
}

current.next = b;
}

}

public void createNodeFromFile( String str )
{
StringTokenizer t = new StringTokenizer( str, "|");
String id = t.nextToken();
String title = t.nextToken();
String num = t.nextToken();

BookNode b = new BookNode( Integer.parseInt( id ), title );

BookNode current = head;

if ( head == null )
head = b;
else
{
while ( current.next != null )
{
current = current.next;
}

current.next = b;
}
}

public boolean removeBook(int key)
{
BookNode current = head;
BookNode previous = null;

while (current != null && current.id != key)
{
previous = current;
current = current.next;
}

if (current == null)
{
System.out.println("There's no such book");
return false;
}
else if (previous == null)
head = head.next;
else
previous.next = current.next;

return true;
}

public boolean viewABook(int key)
{
BookNode current = head;

while (current != null)
{
if (current.id == key)
{
System.out.println("-----------------------------------------------------------");
System.out.println("ID | Title | Number of Available Copies");
System.out.println("-----------------------------------------------------------");
System.out.println(current.id + "\t" + current.title + "\t" + current.num);
System.out.println("-----------------------------------------------------------");
return true;
}
current = current.next;
}
System.out.println("Book's ID cannot be found");
return false;
}

public void viewAllBooks()
{
BookNode current = head;

System.out.println("-----------------------------------------------------------");
System.out.println("ID | Title | Number of Available Copies");
System.out.println("-----------------------------------------------------------");
while ( current != null )
{
System.out.println(current.id + "\t" + current.title + "\t" + current.num);
current = current.next;
}
System.out.println("-----------------------------------------------------------");
}
}

//main class(consoleでまずやってみようと思いまして)

import java.io.*;
import java.util.*;
import java.lang.Integer;

public class Driver
{
public static void main(String args[]) throws IOException
{
int aKey;
MemberList memberList = new MemberList();
BookList bookList = new BookList();
boolean start = true;
while(start)
{
System.out.println("Select From the Follwing List");
System.out.println("1 - Add a new member");
System.out.println("2 - Add a new book");
System.out.println("3 - Remove a member");
System.out.println("4 - Remove a book");
System.out.println("5 - borrow a book");
System.out.println("6 - View information about all books");
System.out.println("7 - View information about all members");
System.out.println("8 - View information about a member");
System.out.println("9 - View information about a book");

char choice = getChar();

switch(choice)
{
case '1':
System.out.print("Input member ID : ");
int id = getInt();
System.out.print("Input First Name : ");
String fName = getString();
System.out.print("Input Last Name : ");
String lName = getString();
memberList.addNode(new MemberNode(id,fName,lName));
break;

case '2':
System.out.print("Input book ID : ");
int id = getInt();
System.out.print("Input book Title : ");
String title = getString();
bookList.addNode(new BookNode(id,title));
break;

case '3':
System.out.println("Enter a Member ID to delete: ");
aKey = getInt();
itemList.removeMember(aKey);
break;

case '4':
System.out.println("Enter a Book ID to delete: ");
aKey = getInt();
itemList.removeBook(aKey);
break;

case '5':
System.out.println("Enter a Member ID to delete: ");
aKey = getInt();
itemList.removeItem(aKey);
break;

case 'p':
itemList.printDatabase();
break;

case 't':
itemList.printTotal();
break;

case 'g':
System.out.println("Enter key value to find: ");
aKey = getInt();
itemList.printItem(aKey);
break;

case 'e':
start = false;
break;

default:
System.out.println("Invalid entry\n");
}
}
}

public static String getString() throws IOException
{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String s = br.readLine();
return s;
}

public static char getChar() throws IOException
{
String s = getString();
return s.charAt(0);
}

public static int getInt() throws IOException
{
String s = getString();
return Integer.parseInt(s);
}

public static double getDouble() throws IOException
{
String s = getString();
return Double.parseDouble(s);
}
}

//先生がくれたframeのヒント
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class Assignment extends JFrame
implements ActionListener
{
// GUI ............................

MemberList memberList = new MemberList();
//BookList bookList = new BookList();

NewMemberDialog newMemberDialog;


public Assignment()
{

//memberAddItem.addActionListener( this );
//readMembers.addActionListener( this );



}

public static void main(String[] args)
{
JFrame f = new Assignment();
f.setSize(600,600);
f.setLocation(200,100);
f.setVisible( true );
}

public void actionPerformed(ActionEvent e)
{
String s = e.getActionCommand();

if ( s.equals("Add a new member"))
{
if (newMemberDialog == null) // first time
newMemberDialog = new NewMemberDialog(this);

if (newMemberDialog.showDialog(this))
{
validate();
}
}
else if ( s.equals("Read members" ))
{
readMemberFile();
}
}


public void createNewMember(int id, String fName, String lName )
{
memberList.addNode(id, fName, lName );
}



public void readMemberFile()
{
try
{
BufferedReader in = new BufferedReader(
new FileReader("Members.txt"));

memberList.head = null;

String str = in.readLine();

while ( str != null )
{
memberList.createNodeFromFile( str );
str = in.readLine();

};

}
catch ( IOException exc )
{
System.out.println("Cannot read from the file");
System.out.println("Error " + exc );
}
}
}

//先生がくれたdialogのヒント
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class NewMemberDialog extends JDialog
implements ActionListener
{
JLabel lblId = new JLabel("Member id");
JLabel lblFName = new JLabel("First name");
JLabel lblLName = new JLabel("Last Name");

JTextField tfId = new JTextField(15);
JTextField tfFName = new JTextField(15);
JTextField tfLName = new JTextField(15);

JPanel editPanel = new JPanel();

JButton btnOK = new JButton("OK");
JButton btnCancel = new JButton("Cancel");

boolean ok;

JPanel btnPanel = new JPanel();

public NewMemberDialog(Assignment pfr)
{
super(pfr, "New member", true );
Container cp = getContentPane();

GridBagLayout gridbag = new GridBagLayout();
editPanel.setLayout(gridbag);

GridBagConstraints c = new GridBagConstraints();

//setting a default constraint value
c.fill = GridBagConstraints.HORIZONTAL;

c.gridx = 0; //x grid position
c.gridy = 0; //y grid position
gridbag.setConstraints(lblId, c); //associate the label with a constraint object
editPanel.add(lblId); //add it to content pane

c.gridx = 0; //x grid position
c.gridy = 1; //y grid position
gridbag.setConstraints(lblFName, c); //associate the label with a constraint object
editPanel.add(lblFName); //add it to content pane

c.gridx = 0; //x grid position
c.gridy = 2; //y grid position
gridbag.setConstraints(lblLName, c); //associate the label with a constraint object
editPanel.add(lblLName); //add it to content pane


c.gridx = 1; //x grid position
c.gridy = 0; //y grid position
c.gridwidth = 2;
gridbag.setConstraints(tfId, c); //associate the label with a constraint object
editPanel.add(tfId); //add it to content pane

c.gridx = 1; //x grid position
c.gridy = 1; //y grid position
c.gridwidth = 2;
gridbag.setConstraints(tfFName, c); //associate the label with a constraint object
editPanel.add(tfFName); //add it to content pane

c.gridx = 1; //x grid position
c.gridy = 2; //y grid position
c.gridwidth = 2;
gridbag.setConstraints(tfLName, c); //associate the label with a constraint object
editPanel.add(tfLName); //add it to content pane

btnPanel.add( btnOK );
btnPanel.add( btnCancel );

cp.add( editPanel, BorderLayout.CENTER );
cp.add( btnPanel, BorderLayout.SOUTH );

setSize(300,200);

btnOK.addActionListener( this );
btnCancel.addActionListener( this );
}

public boolean showDialog(Assignment m)
{
ok = false;
show();
if (ok)
{
System.out.println("Dialog OK clicked out");

m.createNewMember( tfId.getText(), tfFName.getText(),
tfLName.getText() );
}

tfId.setText("");
tfFName.setText("");
tfLName.setText("");
return ok;
}

public void actionPerformed(ActionEvent e )
{
String s = e.getActionCommand();

if ( s.equals("OK") )
{
ok = true;
setVisible(false);
}
else if ( s.equals("Cancel"))
{
setVisible(false);
}
}

}

コメント(3)

長くなりすぎて、読みづらくてすみません。
> 課題はLinkedlistとGUIを使ったLibrary作りです。

Linkedlistを使うべきところが見付からないのですが…。
無理矢理使えってこと?
はい、課題はMemberListとBookListを使ったDatabaseです。ウインク

ログインすると、みんなのコメントがもっと見れるよ

mixiユーザー
ログインしてコメントしよう!

Javaの課題丸投げ 更新情報

Javaの課題丸投げのメンバーはこんなコミュニティにも参加しています

星印の数は、共通して参加しているメンバーが多いほど増えます。

人気コミュニティランキング