27-05-2010, 02:58 PM
|
#78
|
تاريخ التسجيل: Jul 2008
كلية: كلية الحاسبات وتقنية المعلومات
التخصص: IT
نوع الدراسة: عضو هيئة تدريس
المستوى: متخرج
البلد: جــــدة
الجنس: ذكر
المشاركات: 2,477
|
رد: [cpcs 203 - برمجة 2] لديك سؤال , أفكار , شيء غير مفهوم ,,, تفضل هنا , موضوع مفيد
يعني لوكان عندي مكتبة فيها book و Text Book و وطالب في السؤال اني اعمل كل وحده لحاله class لحاله غير main وطالب في main اعمل array لكل نوع مثل [Book []book=new Book[10 وإذا زاد على 10 يقوله لايوجد مكان// وبد ذلك عمل كود يعمل اظهار للقائمة ويكون فيها(add or remove or display) وبعدين يختار وفي اي خيار يختاره تظهر قائمة ثانية فيها book و Text Book وفي اي خيار يختاره تكون عملية غير يعني في book لوعمل add يدخل (Title,ISBN and Aythor) أما بالنسبة Text Book يدخل (Title,ISBN ,Aythor and ask if there exercise Answers ) ولو اختار display تطلع القائمة مرة ثانية ويختار ماذا يريد أن يعرض من الانواع ونفس الشي بالنسبة remove
وفي كل وحده من هذه العمليات يسأله هل يريد ان يعمل add اوremove او display من نفس النوع و إذا قال لا يطلع ويقوله هل تريد ان يطبق العملية التي اختارها على نوع ثاني
إن شاء الله يكون مفهوم
|
الحل :
الكود
كود PHP:
/*
Programming by Mr.Ahmad
*/
import java.util.Scanner;
class Book {
private String bookTitle;
private String bookAuthor;
private String bookISBN;
private static Scanner sc = new Scanner(System.in);
public void setBookTitle(String x) {
this.bookTitle = x;
}
public void setBookAuthor(String x) {
this.bookAuthor = x;
}
public void setBookISBN(String x) {
this.bookISBN = x;
}
public void Display() {
System.out.printf("Title: %s\n", this.bookTitle);
System.out.printf("ISBN: %s\n", this.bookAuthor);
System.out.printf("Author: %s\n", this.bookISBN);
}
public static void Remove(Book[] list, String ISBN) {
int index = list.length;
for (int i=0; i<list.length; i++)
{
if ((list[i] == null)) break;
if (list[i].bookISBN.equals(ISBN)) {
System.out.println("-> " + list[i].bookTitle + " book Has been successfully deleted!");
index = i;
list[index] = list[list.length - 1];
list[list.length - 1] = null;
break;
}
}
if (index == list.length)
{
System.out.println("-> Book, which has the ISBN [" + ISBN + "] does not exist!");
}
}
public static void Add(Book[] list) {
Boolean isAdd = false;
for (int i = 0; i < list.length; i++) {
if (list[i] == null) {
System.out.println("\n--- Enter New Book ---");
list[i] = new Book();
System.out.print("Enter Book Title: ");
list[i].setBookTitle(sc.nextLine());
System.out.print("Enter Book Author: ");
list[i].setBookAuthor(sc.nextLine());
System.out.print("Enter Book ISBN: ");
list[i].setBookISBN(sc.nextLine());
isAdd = true;
System.out.println("----------------------");
break;
}
}
if (isAdd == false) {
System.out.println("Can not add the new Book!");
}
}
public static void Display(Book[] list) {
boolean isDisp = false;
for (int i = 0; i < list.length; i++) {
if (list[i] != null) {
System.out.printf("---[%d]---\n", i+1);
list[i].Display();
isDisp = true;
}
}
if (isDisp == false) {
System.out.println("There are no books to display!");
}
}
}
class TextBook {
private String textBookTitle;
private String textBookAuthor;
private String textBookISBN;
private boolean exerciseAnswers;
private static Scanner sc = new Scanner(System.in);
public void setTextBookTitle(String x) {
this.textBookTitle = x;
}
public void setTextBookAuthor(String x) {
this.textBookAuthor = x;
}
public void setTextBookISBN(String x) {
this.textBookISBN = x;
}
public void isExerciseAnswers(boolean x) {
this.exerciseAnswers = x;
}
public void Display() {
System.out.printf("Title: %s\n", this.textBookTitle);
System.out.printf("ISBN: %s\n", this.textBookAuthor);
System.out.printf("Author: %s\n", this.textBookISBN);
System.out.printf("Answers provided: %s\n", this.exerciseAnswers);
}
public static void Remove(TextBook[] list, String ISBN) {
int index = list.length;
for (int i = 0; i < list.length; i++) {
if ((list[i] == null)) break;
if (list[i].textBookISBN.equals(ISBN)) {
System.out.println("-> " + list[i].textBookTitle + " textbook Has been successfully deleted!");
index = i;
list[index] = list[list.length - 1];
list[list.length - 1] = null;
break;
}
}
if (index == list.length) {
System.out.println("-> TextBook, which has the ISBN [" + ISBN + "] does not exist!");
}
}
public static void Add(TextBook[] list) {
Boolean isAdd = false;
for (int i = 0; i < list.length; i++) {
if (list[i] == null) {
sc = new Scanner(System.in); // to clear the Buffer
System.out.println("\n--- Enter New TextBook ---");
list[i] = new TextBook();
System.out.print("Enter Book Title: ");
list[i].setTextBookTitle(sc.nextLine());
System.out.print("Enter Book Author: ");
list[i].setTextBookAuthor(sc.nextLine());
System.out.print("Enter Book ISBN: ");
list[i].setTextBookISBN(sc.nextLine());
System.out.print("Is there TextBook exercise Answers? [true-false]: ");
try {
list[i].isExerciseAnswers(sc.nextBoolean());
} catch (Exception e) {
System.out.println("Error!");
isAdd = false;
}
isAdd = true;
System.out.println("----------------------");
break;
}
}
if (isAdd == false) {
System.out.println("Can not add the new TextBook!");
}
}
public static void Display(TextBook[] list) {
boolean isDisp = false;
for (int i = 0; i < list.length; i++) {
if (list[i] != null) {
System.out.printf("---[%d]---\n", i+1);
list[i].Display();
isDisp = true;
}
}
if (isDisp == false) {
System.out.println("There are no books to display!");
}
}
}
public class libraryDemo {
public static void main(String[] args) {
(new libraryDemo()).Run();
}
private void Run() {
Book listBook[] = new Book[10];
TextBook[] listTextBox = new TextBook[10];
Scanner sc = new Scanner(System.in);
boolean isGo = true;
int select, select2;
char select_char;
while (isGo) {
System.out.println("\n STUDENT LIBRARY ");
System.out.println("1)ADD \t 2)Display \t 3)Remove");
System.out.print(":: ");
select = sc.nextInt();
switch (select) {
case 1:
add_Select(listBook, listTextBox);
break;
case 2:
display_Select(listBook, listTextBox);
break;
case 3:
remove_Select(listBook, listTextBox);
break;
}
System.out.print("Another operation ? [y-n] : ");
select_char = sc.next().charAt(0);
if (select_char == 'y') {
System.out.println("--------------------------");
continue;
} else {
break;
}
}
System.out.println("Thank you.");
}
private static void add_Select(Book[] listBook, TextBook[] listTextBox) {
Scanner sc = new Scanner(System.in);
int select;
char select_char;
//->
while (true) {
System.out.println("\n-> ADD: 1)Book \t 2)TextBook");
System.out.print(":: ");
select = sc.nextInt();
//-->
switch (select) {
case 1:
while (true) {
Book.Add(listBook);
System.out.print("\nAdd another Book ? [y-n]: ");
select_char = sc.next().charAt(0);
if (select_char == 'n') {
break;
}
}
break;
case 2:
while (true) {
TextBook.Add(listTextBox);
System.out.print("\nAdd another TextBook ? [y-n]: ");
select_char = sc.next().charAt(0);
if (select_char == 'n') {
break;
}
}
break;
}
//--
System.out.print("Add another type? [y-n]: ");
select_char = sc.next().charAt(0);
if (select_char == 'n') {
break;
}
}
//-
}
private static void remove_Select(Book[] listBook, TextBook[] listTextBox) {
Scanner sc = new Scanner(System.in);
int select;
char select_char;
//->
while (true) {
System.out.println("\n-> Remove: 1)Book \t 2)TextBook");
System.out.print(":: ");
select = sc.nextInt();
//-->
switch (select) {
case 1:
while (true) {
sc = new Scanner(System.in); // to Clear the buffer
System.out.print("* Enter the Book ISBN You want to delete: ");
Book.Remove(listBook, sc.nextLine());
System.out.println("----------------------");
System.out.print("\n---> Delete another book? [y-n]: ");
select_char = sc.next().charAt(0);
if (select_char == 'n') {
break;
}
}
break;
case 2:
while (true) {
sc = new Scanner(System.in); // to Clear the buffer
System.out.print("* Enter the TextBox ISBN You want to delete: ");
TextBook.Remove(listTextBox, sc.nextLine());
System.out.println("----------------------");
System.out.print("\n--> Delete another Textbook? [y-n]: ");
select_char = sc.next().charAt(0);
if (select_char == 'n') {
break;
}
}
break;
}
//--
System.out.print("Delete another type? [y-n]: ");
select_char = sc.next().charAt(0);
if (select_char == 'n') {
break;
}
}
//-
}
private static void display_Select(Book[] listBook, TextBook[] listTextBox) {
Scanner sc = new Scanner(System.in);
int select;
char select_char;
//->
while (true) {
System.out.println("\n-> Display: 1)Book \t 2)TextBook");
System.out.print(":: ");
select = sc.nextInt();
//-->
switch (select) {
case 1:
Book.Display(listBook);
break;
case 2:
TextBook.Display(listTextBox);
break;
}
//--
System.out.println("----------------------");
System.out.print("display another type? [y-n]: ");
select_char = sc.next().charAt(0);
if (select_char == 'n') {
break;
}
}
//-
}
}
|
|
سبحان الله وبحمد ,,, سبحان الله العظيم الحمد لله كما ينبغي لجلال وجهه وعظيم سلطانه . اللهم صل على محمد وعلى آل محمد كما صليت على إبراهيم وعلى آل إبراهيم إنك حميد مجيد . اللهم بارك على محمد وعلى آل محمد كما باركت على إبراهيم وعلى آل إبراهيم إنك حميد مجيد.
|
التعديل الأخير تم بواسطة Mr.Ahmad ; 09-06-2010 الساعة 09:51 AM.
|
|
|