Java Can';似乎无法让它先写入arrayList,然后再写入文件

Java Can';似乎无法让它先写入arrayList,然后再写入文件,java,Java,我一直在做这个,似乎无法让它正常运行。它应该创建一个arraylist,然后添加、删除、标记为借出、标记为返回,并在退出时写入文件。我找不到我搞砸的东西 package cs1181proj1; import static cs1181proj1.Media.media; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoun

我一直在做这个,似乎无法让它正常运行。它应该创建一个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");
        }
    }
}
包cs1181proj1;
导入静态cs1181proj1.Media.Media;
导入java.io.DataOutputStream;
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.FileNotFoundException;
导入java.io.FileOutputStream;
导入java.io.IOException;
导入java.io.ObjectInputStream;
导入java.io.ObjectOutputStream;
导入java.io.PrintWriter;
导入java.util.ArrayList;
导入java.util.InputMismatchException;
导入java.util.Scanner;
/**
*
*@作家老兵
*/
公共类CS1181Proj1{
/**
*@param指定命令行参数
*/
公共静态void main(字符串[]args){
//创建ArrayList和mediaFile。
ArrayList项=新建ArrayList();
扫描仪键盘=新扫描仪(System.in);
文件mediaFile=新文件(“mediaFile.dat”);
字符串标题;
字符串格式;
字符串loanedTo=null;
字符串dateLoaned=null;
整数=0;
if(mediaFile.exists()){
试一试{
try(ObjectInputStream dos=newobjectinputstream(
新文件输入流(mediaFile))){
Item=(ArrayList)dos.readObject();
}
}捕获(FileNotFoundException a){
System.out.println(“你犯了错误!文件不存在!”);
}捕获(IOB异常){
System.out.println(“吉戈,我的主人或夫人!”);
}捕获(ClassNotFoundException c){
System.out.println(“Avast!我在quandry中”);
}
}
//打印出选项菜单
while(数字!=6){
System.out.println(“”);
System.out.println(“1.添加新媒体项”);
System.out.println(“2.标记借出的项目”);
System.out.println(“3.将项目标记为已退回”);
System.out.println(“4.列出集合中的项目”);
System.out.println(“5.删除媒体项”);
System.out.println(“6.Quit”);
System.out.println(“”);
//接受用户的菜单选择
试一试{
number=键盘.nextInt();
}捕获(输入不匹配异常e){
System.out.println(“这不是一个介于1和``6.IS`之间的数字!”);
}
//根据用户``//选项,切换以告知程序要使用的方法
开关(编号){
案例1:
插入();
打破
案例2:
onLoan();
打破
案例3:
返回();
打破
案例4:
list();
打破
案例5:
removeItem();
打破
案例6:
System.out.println(“再见”);
退出(媒体文件);
打破
}
}
}
//在arraylist中插入项的方法
公共静态void insert(){
ArrayList媒体=新的ArrayList();
System.out.println(“新标题名称”);
Scanner newEntry=新扫描仪(System.in);
字符串title=newEntry.nextLine();
System.out.println(“”);
//媒体名称
while(标题已存在(标题、媒体)){
System.out.println(“标题已存在”);
title=newEntry.nextLine();
System.out.println(“”);
}
//媒体格式
System.out.println(“什么格式”);
字符串格式=newEntry.nextLine();
添加(新媒体(标题、格式));
System.out.println(“您的条目已创建”);
}
//以防条目已存在
公共静态布尔标题ReadyExists(
字符串标题,ArrayList媒体){
用于(媒体条目:媒体){
if(entry.getTitle().equals(title)){
返回true;
}
}
返回false;
}
//借出物品的方法
公共静态void onLoan(){
System.out.println(“输入贷款所有权名称”);
扫描仪newLoan=新扫描仪(System.in);
ArrayList媒体=新的ArrayList();
String loanTitle=newLoan.nextLine();
System.out.println(“你把这个借给谁了”);
扫描器到=新扫描器(System.in);
字符串loanedTo=To.nextLine();
System.out.println(“”);
System.out.println(“它是什么时候借出的?”);
扫描仪日期=新扫描仪(System.in);
字符串dateLoaned=date.nextLine();
System.out.println(“”);
用于(媒体标题:媒体){
if(title.getTitle().equals(loanTitle)){
标题:setDateLoaned(dateLoaned);
标题:setloanedTo(loanedTo);
System.out.println(title+“借出给”+借出给”+“`”于日期“+借出日期+”列为借出”);
}
}
}
//返回集合中的项的方法
返回的公共静态void(){
System.out.println(“输入返回的标题名称”);
返回的扫描仪=新扫描仪(System.in);
ArrayList媒体=新的ArrayList();
字符串returnedTitle=returned.nextLine();
字符串loanedTo=null;
字符串dateLoaned=null;
System.out.println(“”);
用于(媒体标题:媒体){
if(title.getTitle().equals(returnedTitle)){
标题:setDateLoaned(dateLoaned);
标题:setloanedTo(loanedTo);
System.out.println(title+“列为已返回”);
}
}
}
//方法列出collectio中的项
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);
        }
    };

}