使用io流在控制太进行输出永久保存在文件夹中 Car

domain层

 int cid;
    String cname;
    String color;
    int price;

    public Car() {
    }

    public Car(int cid, String cname, String color, int price) {
        this.cid = cid;
        this.cname = cname;
        this.color = color;
        this.price = price;
    }


    public int getCid() {
        return cid;
    }

    public void setCid(int cid) {
        this.cid = cid;
    }

    public String getCname() {
        return cname;
    }

    public void setCname(String cname) {
        this.cname = cname;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return
                cid +","+ cname +","+ color +","+ price;

    }
}

io层

package com.test4.io;

import com.test4.domain.Car;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class Io {
    public void add(Car car) throws IOException, ClassNotFoundException {
        FileInputStream fis=new FileInputStream("E:/z/car.txt");
        List<Car> cars;
        if (fis.available() == 0) {
            cars=new ArrayList<>();
        }else {
            ObjectInputStream ois=new ObjectInputStream(fis);
            cars= (List<Car>) ois.readObject();
        }
        cars.add(car);
        FileOutputStream fos = new FileOutputStream("E:/z/car.txt");
        ObjectOutputStream oos=new ObjectOutputStream(fos);
        oos.writeObject(cars);
        oos.flush();
    }

    public void show() throws IOException, ClassNotFoundException {
        FileInputStream fis=new FileInputStream("E:/z/car.txt");
        if (fis.available() == 0) {
            System.out.println("没有任何记录");
        }else {
            ObjectInputStream ois=new ObjectInputStream(fis);
            List<Car> cars = (List<Car>) ois.readObject();
            for (Car car:cars){
                System.out.println(car);
            }
        }
    }
}

service层

package com.test4.service;

import com.test4.domain.Car;
import com.test4.io.Io;

import java.io.IOException;


public class Service1 {
    public void addCar(Car cars) throws IOException, ClassNotFoundException {
        Io io = new Io();
        io.add(cars);
    }

    public void showCar() throws IOException, ClassNotFoundException {
        Io io = new Io();
        io.show();
    }
}

view层

1.

package com.test4.view;

import java.io.IOException;
import java.util.Scanner;

public class View1 implements View {
    @Override
    public void show() throws IOException, ClassNotFoundException {
        Scanner input = new Scanner(System.in);
        System.out.println("----汽车信息----");
        System.out.println("1.添加,2.展示");
        int a = input.nextInt();
        switch (a) {
            case 1:
                new View2().show();
                break;
            case 2:
                new View3().show();
                break;
        }
    }
}

2.

package com.test4.view;

import com.test4.domain.Car;
import com.test4.service.Service1;

import java.io.IOException;
import java.util.Scanner;

public class View2 implements View {
    @Override
    public void show() throws IOException, ClassNotFoundException {
        Scanner scanner = new Scanner(System.in);
        System.out.println("-----新增汽车页面----");
        System.out.println("编号:");
        String s1 = scanner.next();
        System.out.println("名字:");
        String s2 = scanner.next();
        System.out.println("颜色:");
        String s3 = scanner.next();
        System.out.println("价格:");
        String s4 = scanner.next();
        Service1 service = new Service1();
        Car cars = new Car(Integer.parseInt(s1),s2,s3,Integer.parseInt(s4));
        service.addCar(cars);
        System.out.println("新增成功!按任意键继续");
        String b = scanner.next();
        System.out.println(b);
        System.out.println("显示管理员主页");
        new View1().show();
    }
}

3.

package com.test4.view;

import com.test4.service.Service1;

import java.io.IOException;

public class View3 implements View {
    @Override
    public void show() throws IOException, ClassNotFoundException {
        System.out.println("-----汽车页面展示----");
        Service1 service = new Service1();
        service.showCar();
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值