Java简单实例--小超市

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

class Product{
	private int id;
	private String name;
	private double price;
	private int store;

	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 double getPrice() {
		return price;
	}
	public void setPrice(double price) {
		this.price = price;
	}
	public int getStore() {
		return store;
	}
	public void setStore(int store) {
		this.store = store;
	}

	public Product(int id, String name, double price, int store) {
		super();
		this.id = id;
		this.name = name;
		this.price = price;
		this.store = store;
	}
	public Product() {
		super();
	}
	@Override
	public String toString() {
		return "product [id=" + id + ", name=" + name + ", price=" + price + ", store=" + store + "]";
	}
}

class SuperMarket{
	private List<Product> products = new ArrayList<Product>();

	public SuperMarket(){

	}

	public List<Product> getProducts() {
		return products;
	}

	public void setProducts(List<Product> products) {
		this.products = products;
	}
}

class Manager{
	private SuperMarket market;
	private File file;

	private void init(String[] strs){
		int id = 0;
		String name = null;
		double price = 0;
		int store = 0;
		List<Product> products = new ArrayList<Product>();
		for(int i = 0; i < strs.length; i++){
			if((i+1)%4==1){
				id = Integer.parseInt(strs[i]);
			}
			if((i+1)%4==2){
				name = strs[i];
			}
			if((i+1)%4==3){
				price = Double.valueOf(strs[i]);
			}
			if((i+1)%4==0){
				store = Integer.parseInt(strs[i]);
				Product product = new Product(id, name, price, store);
				products.add(product);
			}
		}
		market.setProducts(products);
	}

	private void save(){
		int id;
		String name;
		double price;
		int store;
		String str = new String("");
		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream(file);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		for(int i = 0 ;i < market.getProducts().size();i++){
			id = market.getProducts().get(i).getId();
			name = market.getProducts().get(i).getName();
			price = market.getProducts().get(i).getPrice();
			store = market.getProducts().get(i).getStore();
			str += String.valueOf(id) + "#" + name + "#" + String.valueOf(price) + "#" + String.valueOf(store) + "#";
		}
		byte[] b = new byte[str.length()+1];
		b = str.getBytes();
		try {
			fos.write(b);
			fos.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public Manager(){
		market = new SuperMarket();
		file = new File("market.txt");
	}

	public SuperMarket getMarket() {
		return market;
	}

	public void setMarket(SuperMarket market) {
		this.market = market;
	}

	public void query(){
		System.out.println(" id		 name		 price		 store");
		FileInputStream fis = null;
		String str = null;
		try {
			fis = new FileInputStream(file);
			int length = fis.available();
			byte[] b = new byte[length];
			fis.read(b);
			str = new String(b,"GB2312");
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				fis.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		String[] strs = str.split("#");
		for(int i = 0 ; i < strs.length ; i++){
			System.out.print(" "+strs[i]);
			if((i+1)%4!=0){
				System.out.print("		");
			}else{
				System.out.println();
			}
		}
		init(strs);
	}

	public void update(){
		query();
		System.out.println("输入要修改的产品id");
		int n;
		Scanner sc = new Scanner(System.in);
		n = sc.nextInt();
		String name;
		double price;
		int store;
		System.out.println("input the info:");
		System.out.print("name:");
		name = sc.next();
		System.out.print("price:");
		price = sc.nextDouble();
		System.out.print("store:");
		store = sc.nextInt();
		market.getProducts().get(n-1).setName(name);
		market.getProducts().get(n-1).setPrice(price);
		market.getProducts().get(n-1).setStore(store);
		save();
	}

	public void add(){
		System.out.println("现在有如下产品:");
		query();
		System.out.println("输入你想添加的产品:");
		Scanner sc = new Scanner(System.in);
		String name;
		double price;
		int store;
		System.out.println("input the info:");
		System.out.print("name:");
		name = sc.next();
		System.out.print("price:");
		price = sc.nextDouble();
		System.out.print("store:");
		store = sc.nextInt();
		int id = market.getProducts().size() + 1;
		Product product = new Product(id, name, price, store);
		market.getProducts().add(product);
		save();
	}
}

public class Main {
	public static void main(String[] args){
		Manager manager = new Manager();
		int n = 0;
		Scanner sc = new Scanner(System.in);
		while(n!=4){
			System.out.println("输入你的操作:");
			System.out.println("1.query			2.update		3.add		4.exit");
			n = sc.nextInt();
			switch(n){
			case 1 : manager.query();break;
			case 2 : manager.update();break;
			case 3 : manager.add();break;
			case 4 : break;
			}
			System.out.println("\n");
		}
		sc.close();
	}
}

时间: 2024-08-12 11:33:21

Java简单实例--小超市的相关文章

java GUI(实例小项目--列出磁盘目录)

//java实例小项目:列出磁盘目录. import java.awt.*; import java.awt.event.*; import java.io.File; class MyWindDemo{ //定义一个对话框 private Dialog d; //定义一个标签 private Label lab; //定义一个对话框上的按钮 private Button but_dlg; //定义一个窗体 private Frame f; //定义一个文本框 private TextField

Selenium WebDriver java 简单实例

开发环境 JDK 下载地址: http://www.oracle.com/technetwork/java/javase/downloads/index.html Eclipse: 下载地址:http://www.eclipse.org/downloads/ Selenium jar包 (这里用的是:selenium-Java-2.45.0.zip ,selenium-server-standalone-2.45.0.jar) 下载地址:http://code.google.com/p/sele

Face++ Java 简单实例

Face++ 官网 http://www.faceplusplus.com.cn/ 11.jpg import java.nio.charset.Charset; import java.util.ArrayList; import org.json.JSONException; import org.json.JSONObject; import com.facepp.error.FaceppParseException; import com.facepp.http.HttpRequests

Eclipse运行Java简单实例

运行eclipse前首先配置好JDK环境变量等  双击这句话可跳转配置环境变量详细步骤 运行eclipse软件 1.File菜单-New - project 2.Java Project - Next 3.Project name 项目名:Test  首字母大写 =>Finish 4.右击src 新建一个包Package (包名尽量小写) package => yin=>Finish 5.右击新建好的yin包   New-Class 创建一个Class类 Name为第一个程序  firs

JAVA RMI远程方法调用简单实例[转]

RMI的概念 RMI(Remote Method Invocation)远程方法调用是一种计算机之间利用远程对象互相调用实现双方通讯的一种通讯机制.使用这种机制,某一台计算机上的对象可以调用另外 一台计算机上的对象来获取远程数据.RMI是Enterprise JavaBeans的支柱,是建立分布式Java应用程序的方便途径.在过去,TCP/IP套接字通讯是远程通讯的主要手段,但此开发方式没有使用面向对 象的方式实现开发,在开发一个如此的通讯机制时往往令程序员感觉到乏味,对此RPC(Remote

java创建一个简单的小框架frame

import java.awt.*; import javax.swing.*; public class SimpleFrameTest { public static void main(String[] args) { EventQueue.invokeLater(new Runnable(){ // 开一个线程 public void run() { SimpleFrame frame = new SimpleFrame(); frame.setTitle("记事本"); //

小贝_php+redis简单实例

php+redis简单实例 一.说明 因为redis是c/s架构.从这个角度上.不论什么符合redis的client要求的.都能够与redis进行通讯.官方提供了非常多的client. php在web方面的发展.大家够有目共睹.因此这里主要是解说php与redis的使用实例 二.实例 这里仅使用了redis的字符串类型.用到了get和set命令 <? php /** * @explain php操作redis * 1.设置key为name,其值为脚本小子 * 2.获取key为name的值 * @

主题:Java WebService 简单实例

链接地址:主题:Java WebService 简单实例    http://www.iteye.com/topic/1135747 前言:朋友们开始以下教程前,请先看第五大点的注意事项,以避免不必要的重复操作. 一.准备工作(以下为本实例使用工具) 1.MyEclipse10.7.1 2.JDK 1.6.0_22 二.创建服务端 1.创建[Web Service Project],命名为[TheService].   2.创建[Class]类,命名为[ServiceHello],位于[com.

java网页爬虫简单实例详解——获取天气预报。

[本文介绍] 爬取别人网页上的内容,听上似乎很有趣的样子,只要几步,就可以获取到力所不能及的东西,例如呢?例如天气预报,总不能自己拿着仪器去测吧!当然,要获取天气预报还是用webService好.这里只是举个例子.话不多说了,上看看效果吧. [效果] 我们随便找个天气预报的网站来试试:http://www.weather.com.cn/html/weather/101280101.shtml 从图中可用看出,今天(6日)的天气.我们就以这个为例,获取今天的天气吧! 最终后台打印出: 今天:6日