saveFile

replace
mail@pastecode.io avatar
unknown
java
3 years ago
2.1 kB
3
Indexable
Never
public void saveFile(String fileName) {

        try {
            FileWriter fw = new FileWriter(new File(fileName));
            Node temp = head;
            while (temp != null) {
                StringBuilder text = new StringBuilder("");
                if (temp.data instanceof Consumable) {
                    text.append("Consumable, ");
                }
                else if(temp.data instanceof Material){
                    text.append("Material, ");
                }
                else{
                    text.append("Equipment, ");
                }
                text.append(temp.data.name + ", ");
                text.append(temp.data.Value + ", ");
                text.append(temp.data.Weight + ", ");
                text.append(temp.data.Durability + ", ");
                text.append(temp.data.ID + ", ");

                if (temp.data instanceof Consumable) {
                    Consumable con = (Consumable) temp.data;
                    text.append(con.c1 + ", ");
                    text.append(con.c2 + ", ");
                    text.append(con.c3 + "\r\n");
                }
                if (temp.data.getClass().getName().equalsIgnoreCase("Equipment")) {
                    Equipment eqip = (Equipment) temp.data;
                    text.append(eqip.e1 + ", ");
                    text.append(eqip.e2 + ", ");
                    text.append(eqip.e3 + "\r\n");
                }
                if (temp.data.getClass().getName().equalsIgnoreCase("Material")) {
                    Material mate = (Material) temp.data;
                    text.append(mate.m1 + ", ");
                    text.append(mate.m2 + ", ");
                    text.append(mate.m3 + "\r\n");
                }

                fw.write(text.toString());

                temp = temp.next;
            }
            fw.close();
        } catch (FileNotFoundException e) {

            e.printStackTrace();
        } catch (IOException e) {

            e.printStackTrace();
        }

    }