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();
}
}