商品管理系统

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

}

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值