package com.biz;
/*
* 请输入商品编号:1001
请输入商品名称:ipad3
请输入商品价格:3300
请输入商品数量:10
*/
public class Goods implements java.io.Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
int id;
String name;
int price;
int number;
public Goods() {
super();
// TODO Auto-generated constructor stub
}
public Goods(int id, String name, int price, int number) {
super();
this.id = id;
this.name = name;
this.price = price;
this.number = number;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
}
package com.io;
import java.io.*;
import java.util.Iterator;
import java.util.List;
import com.biz.Goods;
public class InputFile {
FileInputStream in=null;
ObjectInputStream inn=null;
public void read(){
try {
in = new FileInputStream("D:\\data.txt");
inn = new ObjectInputStream(in);
List<?> list = (List<?>)inn.readObject();
Goods go;
Iterator<?> it = list.iterator();
while(it.hasNext())
{
go = (Goods) it.next();
System.out.println("编号 名称 价格 数量");
System.out.println(go.getId()+" "+go.getName()+" "+go.getPrice()+" "+go.getNumber());
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
inn.close();
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
package com.io;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.List;
public class OutputFile {
FileOutputStream in =null;
ObjectOutputStream out = null;
public void write(List<?> list){
try {
in = new FileOutputStream("D:\\data.txt");
out = new ObjectOutputStream(in);
out.writeObject(list);
out.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
out.close();
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
package com.view;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import com.biz.Goods;
import com.io.*;
public class TextView {
String s;
List<Goods> list = new ArrayList<Goods>();
public void login(){
while(true){
System.out.println("|----------------------------|");
System.out.println("|-------欢迎进入商品信息管理系统------|");
System.out.println("|----------------------------|");
System.out.println("|---1.添加商品 2.查询商品 3.退出--|");
System.out.println("|----------------------------|");
System.out.println("请选择:");
Scanner sc = new Scanner(System.in);
s=sc.next();
System.out.println("|-----------------------------|");
System.out.println("您选择的是:"+s);
System.out.println("|-----------------------------|");
int i;
i=Integer.parseInt(s);
if(i==1){
update();
}else if(i==2){
check();
}else if(i==3){
break;
}
}
System.out.println("退出成功!!!");
exit();
}
public void update(){
Goods go = new Goods();
Scanner sc = new Scanner(System.in);
System.out.println("请输入商品的编号:");
int i;
i=Integer.parseInt(sc.next());
go.setId(i);
System.out.println("请输入商品的名称:");
go.setName(sc.next());
System.out.println("请输入商品的价格:");
i=Integer.parseInt(sc.next());
go.setPrice(i);
System.out.println("请输入商品的数量:");
i=Integer.parseInt(sc.next());
go.setNumber(i);
System.out.println("是否保存(y/n)?");
String s;
s=sc.next();
if(s.equals("y")||s.equals("Y"))
{
list.add(go);
new OutputFile().write(list);
System.out.println("保存成功!!!");
}else{
System.out.println("商品未保存!!!");
}
}
public void check(){
new InputFile().read();
}
public void exit(){
System.exit(0);
}
}
package com.view;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new TextView().login();
}
}
商品管理系统
最新推荐文章于 2023-05-14 21:08:45 发布