MyDatabse Class using Arrays
كود:
import java.util.InputMismatchException;
import java.util.Scanner;
public class MyDatabase
{
private static Scanner input = new Scanner(System.in);
private int CDs;
private int DVDs;
private CD[] CD_Collection;
private DVD[] DVD_Collection;
private char choice;
public MyDatabase()
{
System.out.println("The rental system program has begun.");
System.out.println();
System.out.println("~=(Getting information from the shop’s employer)=~");
System.out.println();
askUser();
do
{
System.out.println();
System.out.println("~=(Main Menu)=~");
printMenu();
choice = input.nextLine().toUpperCase().charAt(0);
System.out.println();
switch (choice)
{
case 'A':
System.out.print("Insert the title name you are looking for: ");
String CD_title = input.nextLine();
int CD_title_index = -1;
for(int i = 0; i < CDs; i++)
{
if(CD_Collection[i].getTitle().equals(CD_title))
{
CD_title_index = i;
break;
}
}
if(CD_title_index == -1) System.out.println("no matched title.");
else
{
System.out.println("The title name matches the following CD's title name:");
System.out.println(CD_Collection[CD_title_index].toString());
}
break;
case 'B':
System.out.print("Insert the artist name you are looking for: ");
String artist = input.nextLine();
int artist_index = -1;
for(int i = 0; i < CDs; i++)
{
if(CD_Collection[i].getArtist().equals(artist))
{
artist_index = i;
break;
}
}
if(artist_index == -1) System.out.println("no matched artist.");
else
{
System.out.println("The artist name matches the following CD's artist name:");
System.out.println(CD_Collection[artist_index].toString());
}
break;
case 'C':
System.out.print("Insert the title name you are looking for: ");
String DVD_title = input.nextLine();
int DVD_title_index = -1;
for(int i = 0; i < DVDs; i++)
{
if(DVD_Collection[i].getTitle().equals(DVD_title))
{
DVD_title_index = i;
break;
}
}
if(DVD_title_index == -1) System.out.println("no matched title.");
else
{
System.out.println("The title name matches the following DVD's title name:");
System.out.println(DVD_Collection[DVD_title_index].toString());
}
break;
case 'D':
System.out.print("Insert the director name you are looking for: ");
String director = input.nextLine();
int director_index = -1;
for(int i = 0; i < DVDs; i++)
{
if(DVD_Collection[i].getDirector().equals(director))
{
director_index = i;
break;
}
}
if(director_index == -1) System.out.println("no matched director.");
else
{
System.out.println("The director name matches the following DVD's director name:");
System.out.println(DVD_Collection[director_index].toString());
}
break;
case 'P': System.out.println("The shop has " +
(CDs == 0? "no CDs":CDs == 1? "only one CD":CDs + " CDs") +
" and " +
(DVDs == 0? "no DVDs":DVDs == 1? "only one DVD":DVDs + " DVDs")
+ ".");
System.out.println();
System.out.println(".:CDs Details:.");
for(int i = 1; i <= CDs; i++)
{
System.out.println("CD#" + i + ":");
System.out.println(CD_Collection[i-1].toString());
}
System.out.println();
System.out.println(".:DVDs Details:.");
for(int j = 1; j <= DVDs; j++)
{
System.out.println("DVD#" + j + ":");
System.out.println(DVD_Collection[j-1].toString());
}
break;
case 'R':
int rented_CDs = 0;
int rented_DVDs = 0;
for(int i = 1; i <= CDs; i++)
{
if(CD_Collection[i-1].getRented()) rented_CDs++;
}
for(int j = 1; j <= DVDs; j++)
{
if(CD_Collection[j-1].getRented()) rented_DVDs++;
}
System.out.println("The shop has " +
(rented_CDs == 0? "no rented CDs":rented_CDs == 1? "only one rented CD":rented_CDs + " rented CDs") +
" and " +
(rented_DVDs == 0? "no rented DVDs":rented_DVDs == 1? "only one rented DVD":rented_DVDs + " rented DVDs")
+ ".");
System.out.println();
System.out.println(".:Rented CDs Details:.");
int rented_CD_index = 1;
for(CD myCD : CD_Collection)
{
if(myCD.getRented())
{
System.out.println("CD#" + rented_CD_index + ":");
System.out.println(CD_Collection[rented_CD_index-1].toString());
rented_CD_index++;
}
}
System.out.println();
System.out.println(".:Rented DVDs Details:.");
int rented_DVD_index = 1;
for(DVD myDVD : DVD_Collection)
{
if(myDVD.getRented())
{
System.out.println("DVD#" + rented_DVD_index + ":");
System.out.println(DVD_Collection[rented_DVD_index-1].toString());
rented_DVD_index++;
}
}
break;
case 'Q': System.out.println("The program has been closed.");
break;
default: System.out.println(choice + " is invalid choice.");
}
}
while (choice != 'Q');
}
private void printMenu()
{
System.out.println();
System.out.println("The following choices are available: ");
System.out.println(" A Search for a specific CD by a title name.");
System.out.println(" B Search for a specific CD by an artist name.");
System.out.println(" C Search for a specific DVD by a title name.");
System.out.println(" D Search for a specific DVD by a director name.");
System.out.println(" P Print a report about all CDs and DVDs available.");
System.out.println(" R Print a report about all rented CDs and DVDs available.");
System.out.println(" Q Exit the program.");
System.out.print("Insert your choice: ");
}
private void askUser()
{
CDs = getUserInteger("How many CDs the shop has? ");
DVDs = getUserInteger("How many DVDs the shop has? ");
CD_Collection = new CD[CDs];
DVD_Collection = new DVD[DVDs];
System.out.println();
System.out.println("The shop has " +
(CDs == 0? "no CDs":CDs == 1? "only one CD":CDs + " CDs") +
" and " +
(DVDs == 0? "no DVDs":DVDs == 1? "only one DVD":DVDs + " DVDs")
+ ".");
if(CDs > 0)
{
System.out.println();
System.out.print("Insert CD data:" + "\n");
}
for(int i = 1; i <= CDs; i++)
{
System.out.println("CD#" + i);
System.out.print("Title: ");
String title = input.nextLine();
System.out.print("Artist: ");
String artist = input.nextLine();
boolean rented = getUserYesNo("Is it rented?(Yes/No) ");
System.out.print("Comment: ");
String comment = input.nextLine();
CD_Collection[i-1] = new CD(title, artist, rented, comment);
}
if(DVDs > 0)
{
System.out.println();
System.out.println("Insert DVD data:");
}
for(int j = 1; j <= DVDs; j++)
{
System.out.println("DVD#" + j);
System.out.print("Title: ");
String title = input.nextLine();
System.out.print("Director: ");
String director = input.nextLine();
boolean rented = getUserYesNo("Is it rented?(Yes/No) ");
System.out.print("Comment: ");
String comment = input.nextLine();
DVD_Collection[j-1] = new DVD(title, director, rented, comment);
}
}
private boolean getUserYesNo(String question)
{
String result;
while(true)
{
System.out.print(question);
result = input.nextLine();
if(result.equalsIgnoreCase("Yes") || result.equalsIgnoreCase("No"))
{
break;
}
System.out.println("Wrong input! You can only insert \"Yes\" or \"No\"");
}
return result.equalsIgnoreCase("Yes");
}
private int getUserInteger(String question)
{
int result;
while(true)
{
System.out.print(question);
try
{
result = input.nextInt();
if(result < 0)
{
System.out.println("Wrong input! You can only insert positive integer numbers");
input.nextLine(); // discard \n
continue;
}
input.nextLine(); // discard \n
break;
}
catch(InputMismatchException ex)
{
System.out.println("Wrong input! You can only insert positive integer numbers");
input.next(); // discard non-integer input
}
}
return result;
}
}