我一直在研究这个问题,似乎无法正常运行。它应该创建一个arraylist然后添加,删除,标记为已租借,标记为已返回,并在您退出时写入文件。我找不到我搞砸的东西。
package cs1181proj1;
import static cs1181proj1.Media.media;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;
/**
*
* @author Veteran
*/
public class CS1181Proj1 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// Create ArrayList and mediaFile.
ArrayList<Media> Item = new ArrayList<>();
Scanner keyboard = new Scanner(System.in);
File mediaFile = new File("mediaFile.dat");
String Title;
String Format;
String loanedTo = null;
String dateLoaned = null;
int number = 0;
if (mediaFile.exists()) {
try {
try (ObjectInputStream dos = new ObjectInputStream(
new FileInputStream(mediaFile))) {
Item = (ArrayList<Media>) dos.readObject();
}
} catch (FileNotFoundException a) {
System.out.println("Thou hast erred! The file doth not exist!");
} catch (IOException b) {
System.out.println("GIGO My Lord or Lady!");
} catch (ClassNotFoundException c) {
System.out.println("Avast! I am in a quandry");
}
}
//Print out menu of choices
while (number != 6) {
System.out.println("");
System.out.println("1. Add new media item");
System.out.println("2. Mark an item out on loan");
System.out.println("3. Mark an item as Returned");
System.out.println("4. List items in collection");
System.out.println("5. Remove a media item");
System.out.println("6. Quit");
System.out.println("");
//accept users menu choice
try {
number = keyboard.nextInt();
} catch (InputMismatchException e) {
System.out.println("That's not a number between 1 and ` `6. IS IT!");
}
//Switch to tell program what method to use depending on user ` `//option
switch (number) {
case 1:
insert();
break;
case 2:
onLoan();
break;
case 3:
returned();
break;
case 4:
list();
break;
case 5:
removeItem();
break;
case 6:
System.out.println("Goodbye");
Quit(mediaFile);
break;
}
}
}
//Method for inserting items in arraylist
public static void insert() {
ArrayList<Media> media = new ArrayList<>();
System.out.println("New titles name");
Scanner newEntry = new Scanner(System.in);
String title = newEntry.nextLine();
System.out.println("");
//title of media
while (titleAlreadyExists(title, media)) {
System.out.println("Title already exists");
title = newEntry.nextLine();
System.out.println("");
}
//format of media
System.out.println("What format");
String format = newEntry.nextLine();
media.add(new Media(title, format));
System.out.println("Your entry has been created.");
}
//In case entry already exists
public static boolean titleAlreadyExists(
String title, ArrayList<Media> media) {
for (Media entry : media) {
if (entry.getTitle().equals(title)) {
return true;
}
}
return false;
}
//Method for putting item on loan
public static void onLoan() {
System.out.println("Enter name of Title on loan");
Scanner newLoan = new Scanner(System.in);
ArrayList<Media> media = new ArrayList<>();
String loanTitle = newLoan.nextLine();
System.out.println("Who did you loan this to");
Scanner To = new Scanner(System.in);
String loanedTo = To.nextLine();
System.out.println("");
System.out.println("When was it lent out?");
Scanner date = new Scanner(System.in);
String dateLoaned = date.nextLine();
System.out.println("");
for (Media title : media) {
if (title.getTitle().equals(loanTitle)) {
title.setDateLoaned(dateLoaned);
title.setloanedTo(loanedTo);
System.out.println(title + " loaned to " + loanedTo + ` `"on date" + dateLoaned + "is listed as loaned");
}
}
}
//Method for returning item in collection
public static void returned() {
System.out.println("Enter name of Title returned");
Scanner returned = new Scanner(System.in);
ArrayList<Media> media = new ArrayList<>();
String returnedTitle = returned.nextLine();
String loanedTo = null;
String dateLoaned = null;
System.out.println("");
for (Media title : media) {
if (title.getTitle().equals(returnedTitle)) {
title.setDateLoaned(dateLoaned);
title.setloanedTo(loanedTo);
System.out.println(title + "is listed as returned");
}
}
}
//Method for listing items in collection
public static void list() {
System.out.println("Your collection contains:");
for (int i = 0; i < media.size(); i++) {
System.out.print((media.get(i)).toString());
System.out.println("");
}
}
//method for removing an item from the collection
private static void removeItem() {
System.out.println("What item do you want to remove?");
Scanner remover = new Scanner(System.in);
ArrayList<Media> media = new ArrayList<>();
String removeTitle = remover.nextLine();
System.out.println("");
media.stream().filter((title) -> ` ` (title.getTitle().equals(removeTitle))).forEach((title) -> {
media.remove(removeTitle);
System.out.println(title + "has been removed");
});
}
//method for saving to file when the user quits the program.
//To ensure there is no data loss.
public static void Quit(File mediaFile) {
try {
ArrayList<Media> media = new ArrayList<>();
try (ObjectOutputStream oos = new ObjectOutputStream(new ` ` FileOutputStream(mediaFile))) {
oos.writeObject(media);
System.out.println("Your file has been saved.");
}
} catch (IOException e) {
System.out.println("Unable to write to file. Data not ` `saved");
}
}
}
以下是其配套媒体文件:
package cs1181proj1;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Comparator;
/**
*
* @author Veteran
*/
public class Media implements Serializable {
static ArrayList<String> media = new ArrayList<>();
private String Title;
private String Format;
private String loanedTo = null;
private String dateLoaned = null;
Media() {
this.Title = ("Incorrect");
this.Format = ("Incorrect");
this.loanedTo = ("Available");
this.dateLoaned = ("Available");
}
Media(String Title, String Format) {
this.Title = Title;
this.Format = Format;
this.loanedTo = ("Available");
this.dateLoaned = ("Available");
}
//Get and Set Title
public String getTitle() {
return this.Title;
}
public void setTitle(String Title) {
this.Title = Title;
}
//Get and Set Format
public String getFormat() {
return this.Format;
}
public void setFormat(String Format) {
this.Format = Format;
}
//Get and Set loanedTo
public String getLoanedTo() {
return this.loanedTo;
}
public void setloanedTo (String loanedTo){
this.loanedTo = loanedTo;
}
//Get and set dateLoaned
public String getDateLoaned() {
return this.dateLoaned;
}
public void setDateLoaned(String dateLoaned){
this.dateLoaned = dateLoaned;
}
@Override
public String toString(){
return this.Title + ", " + this.Format +", " + this.loanedTo + ", "
+ this.dateLoaned;
}
public static Comparator <Media> TitleComparator = new Comparator <Media>(){
@Override
public int compare(Media t, Media t1) {
String title = t.getTitle().toUpperCase();
String title1 = t1.getTitle().toUpperCase();
return title.compareTo(title1);
}
};
}