package com.liu.Order;
import java.io.Serializable;
public class Order implements Serializable {
private int id;
private String name;
private double price;
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 Order(int id, String name, double price) {
super();
this.id = id;
this.name = name;
this.price = price;
}
public Order() {
super();
}
@Override
public String toString() {
return "Order [id=" + id + ", name=" + name + ", price=" + price + "]";
}
}
package com.liu.Order;
import java.awt.geom.FlatteningPathIterator;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.Map;
public class OrderManager {
public static Map<Integer, Order> map=new HashMap<>();
static {
ObjectInputStream ois;
try {
ois=new ObjectInputStream(new FileInputStream("book4.txt"));
if(ois.read()==-1) {
Write(map);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void Read() {
ObjectInputStream ois;
try {
ois = new ObjectInputStream(new FileInputStream("book4.txt"));
int len=0;
while((len=ois.read())!=-1) {
System.out.println(len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void Write(Map<Integer, Order> map) {
ObjectOutputStream oos;
try {
oos = new ObjectOutputStream(new FileOutputStream("book4.txt"));
oos.writeObject(map);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static boolean addManager(Order order) {
Read();
if(map.containsKey(order.getId())) {
return false;
}
map.put(order.getId(),order);
Write(map);
return true;
}
public static boolean DelManager(int id) {
Read();
if(map.containsKey(id)) {
map.remove(id);
return true;
}
return false;
}
public static boolean updateManager(Order order) {
Read();
if(!map.containsKey(order.getId())) {
return false;
}
map.put(order.getId(), order);
Write(map);
return true;
}
public static Map<Integer, Order> showManager() {
Read();
return map;
}
public static boolean exit(int id) {
Read();
if(map.containsKey(id)) {
return false;
}
return true;
}
}
package com.liu.Order;
import java.io.IOException;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;
public class OrderTest {
public static Scanner sc=new Scanner(System.in);
public static void main(String[] args) {
OrderManager om=new OrderManager();
menu();
}
public static void menu() {
System.out.println("欢送使用订单系统");
while(true) {
System.out.println("1增加"+"\t"+"2删除"+"\t"+"3修改"+"\t"+"4查找"+"\t"+"5退出系统");
System.out.println("请输入你要选择的功能");
int i=sc.nextInt();
switch (i) {
case 1:
addTest();
break;
case 2:
delOrder();
break;
case 3:
update();
break;
case 4:
showTest();
break;
case 5:
return;
default:
System.out.println("输入有误,请重新输入!");
}
}
}
public static void addTest() {
System.out.println("请输入ID");
int id=sc.nextInt();
boolean is =OrderManager.exit(id);
if(!is) {
System.out.println("你输入的ID已经存在");
menu();
}
System.out.println("请输入姓名");
String name=sc.next();
System.out.println("请输入价格");
double price=sc.nextDouble();
Order order=new Order(id,name,price);
boolean isok=OrderManager.addManager(order);
if(isok) {
System.out.println("新增胜利");
}
}
public static void delOrder() {
System.out.println("请输入你要删除的ID号");
int id=sc.nextInt();
boolean isok=OrderManager.DelManager(id);
if(isok) {
System.out.println("删除胜利");
}else {
System.out.println("没有找到ID");
}
}
public static void update() {
System.out.println("请输入你要修改的ID");
int id=sc.nextInt();
boolean is =OrderManager.exit(id);
if(is) {
System.out.println("你输入的ID不存在");
menu();
}
System.out.println("请输入你要修改的姓名");
String name=sc.next();
System.out.println("请输入你要修改的价格");
double price=sc.nextDouble();
Order order=new Order(id,name,price);
boolean isok= OrderManager.updateManager(order);
if(isok) {
System.out.println("修改胜利");
}
}
public static void showTest() {
Map<Integer, Order> map=OrderManager.showManager();
Set<Entry<Integer, Order>> set=map.entrySet();
System.out.println();
System.out.println("序号\t订单名\t订单价格");
for (Entry<Integer, Order> entry : set) {
Order order=entry.getValue();
System.out.println(order.getId()+"\t"+order.getName()+"\t"+order.getPrice());
}
System.out.println();
}
} |