1.掌握Java中文件的读写操作。
2.学会使用Java提供的实用类(Vector, ArrayList)来完成特定的功能。
3.掌握字符串类(String, StringBuffer)的使用。
4.掌握用面向对象的方法分析和解决复杂问题
Item number | 字符串型 | 货物编号 |
---|---|---|
Quantity | 整型 | 货物数量 |
Supplier | 字符串型 | 供应商编号 |
Description | 字符串型 | 货物描述 |
输入
try (FileReader reader = new FileReader("Inventory.txt")) {
@SuppressWarnings("resource")
Scanner scanner = new Scanner(reader);
while (scanner.hasNextLine()) {
temInventory.add(new Inventory(scanner.next(), scanner.nextInt(), scanner.next(), scanner.nextLine().trim()));
}
}
这里涉及到scanner的知识 (详情请见我的JAVA学习博客)
java.util.Scanner
帮助我们接受键盘数据
1.Scanner s = new Scanner(System.in)
2.接受用户的输入,可以直接返回相应的数据类型
String nextLine ();换行符分割 int next():空格分割
3.判断是否还有有效的输入 boolean hasNextXxx();
输出
FileWriter Shipping = new FileWriter("Shipping.txt");
for(int i =0;i<shippingList.size();++i) {
O tem_O = shippingList.get(i);
Shipping.write(tem_O.Customer_number+" "+tem_O.Item_number+" "+tem_O.Quantity+'\n');
}
Shipping.close();
writer写文件,没有文件则创建
靠+‘\n’ 换行
package research;//包
public class Inventory implements Comparable<Inventory>{
public String Item_number;
public Integer Quantity;
public String Supplier_number;
public String description;
Inventory(String Item_number,Integer Quantity,String Supplier_number,String description){//构造函数
this.Item_number=Item_number;
this.Quantity=Quantity;
this.Supplier_number=Supplier_number;
this.description=description;
}
void out() {//检测输出
System.out.println(Item_number+" "+Quantity+" "+Supplier_number+" "+description);
}
@Override//检查
public int compareTo(Inventory o) {//重写compareTo用于后来的排序
// TODO Auto-generated method stub
if(this.Item_number.equals(o.Item_number)) {
return this.Quantity-o.Quantity;
}else {
return Integer.parseInt(this.Item_number)-Integer.parseInt(o.Item_number);
}
}
}
package research;//包
import java.util.Scanner;
public interface Transactions {//接口
public static void read(Scanner scanner) {}
//对于四种不同的事物创建不同的class再分别进行实现接口
}
class O implements Transactions,Comparable<O> {//针对0创建的class
public String Item_number;
public Integer Quantity;
public String Customer_number;
public void read(Scanner scanner) {//实现接口
// TODO Auto-generated method stub
this.Item_number=scanner.next();
this.Quantity=scanner.nextInt();
this.Customer_number= scanner.nextLine().trim();
}
public O(String Customer_number,String Item_number,Integer Quantity) {//构造函数
// TODO Auto-generated constructor stub
this.Customer_number=Customer_number;
this.Item_number=Item_number;
this.Quantity=Quantity;
}
public O() {//无参构造函数
}
@Override
public int compareTo(O o) {//与上文同样,重写compareTo排序 因为货量小的优先发货
//什么鬼规定,我要是顾客得气死
// TODO Auto-generated method stub
if(this.Item_number.equals(o.Item_number)) {
return this.Quantity-o.Quantity;
}else {
return Integer.parseInt(this.Item_number)-Integer.parseInt(o.Item_number);
}
}
}
class R implements Transactions {//R
public String Item_number;
public Integer Quantity;
public void read(Scanner scanner){
this.Item_number=scanner.next();
this.Quantity= Integer.parseInt(scanner.nextLine().trim());
}
}
class A implements Transactions {//A
public String Item_number;
public String Supplier;
public String description;
public void read(Scanner scanner){
this.Item_number=scanner.next();
this.Supplier=scanner.next();
this.description= scanner.nextLine().trim();
}
}
class D implements Transactions {//D
public String Item_number;
public void read(Scanner scanner){
this.Item_number=scanner.nextLine().trim();
}
}
package research;//包而已
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;//一大堆import
public class Main {
static List <Inventory> temInventory = new ArrayList<Inventory>();
static List <A> temA = new ArrayList<A>();
static List <R> temR = new ArrayList<R>();
static List <O> temO = new ArrayList<O>();
static List <D> temD = new ArrayList<D>();
static List <O> shippingList = new ArrayList<O>();
static List <O> errorsList = new ArrayList<O>();
static int n=0;//个人喜好,用set或者vector差不多,还是喜欢list
public static void main(String[] args) throws IOException, IOException {
try (FileReader reader = new FileReader("Inventory.txt")) {//读入inventory
@SuppressWarnings("resource")
Scanner scanner = new Scanner(reader);
while (scanner.hasNextLine()) {
temInventory.add(new Inventory(scanner.next(), scanner.nextInt(), scanner.next(), scanner.nextLine().trim()));
}
}
try (FileReader reader = new FileReader("Transactions.txt")) {//读入transaction
Scanner scanner = new Scanner(reader);
while (scanner.hasNextLine()) {
String tem=scanner.next();//开始分别实现read()接口然后压入list
if(tem.equals("0")) {
O tem_O = new O();
tem_O.read(scanner);
temO.add(tem_O);
}else {
if(tem.equals("R")) {
R tem_R = new R();
tem_R.read(scanner);
temR.add(tem_R);
}else {
if(tem.equals("D")){
D tem_D = new D();
tem_D.read(scanner);
temD.add(tem_D);
}else {
A tem_A = new A();
tem_A.read(scanner);
temA.add(tem_A);
}
}
}
}
}
//事务处理顺序A R O D不懂回去看题目加粗字体吧~
// for(int i =0;i<temO.size();++i) {
// System.out.println(temO.get(i).Item_number);
// }//留给你尝试玩玩?
for(int i=0;i<temA.size();++i) {//处理A
temInventory.add(new Inventory(temA.get(i).Item_number, 0,temA.get(i).Supplier, temA.get(i).description));
}
for (int i = 0; i < temR.size(); i++) {//R
for(int j =0;j< temInventory.size();++j) {
if(temInventory.get(j).Item_number.equals(temR.get(i).Item_number)) {
temInventory.get(j).Quantity+=temR.get(i).Quantity;
break;
}
}
}
Collections.sort(temInventory);
Collections.sort(temO);//升序排序
for(int i=0;i<temO.size();++i) {//O
O tem_O = temO.get(i);
for(int j = 0;j<temInventory.size();++j) {
if(temInventory.get(j).Item_number.equals(tem_O.Item_number)) {
if(temInventory.get(j).Quantity>=tem_O.Quantity) {
temInventory.get(j).Quantity-=tem_O.Quantity;
shippingList.add(new O(tem_O.Customer_number, tem_O.Item_number, tem_O.Quantity));
}else {//Error 处理
errorsList.add(new O(tem_O.Customer_number, tem_O.Item_number, tem_O.Quantity));
}
break;
}
}
}
for (int i = 0; i < temD.size(); i++) {//D
D tem_D= temD.get(i);
for(int j = 0;j<temInventory.size();++j) {
if(temInventory.get(j).Item_number.equals(tem_D.Item_number)) {
if(temInventory.get(j).Quantity==0) {
temInventory.remove(j);
}else {//Error 处理
errorsList.add(new O("0", tem_D.Item_number, temInventory.get(j).Quantity));
}
break;
}
}
}
for(int i =0;i<shippingList.size();++i) {//shipping重复相加
O tem_O = shippingList.get(i);
for(int j=i+1;j<shippingList.size();++j) {
if(tem_O.Customer_number.equals(shippingList.get(j).Customer_number)&&tem_O.Item_number.equals(shippingList.get(j).Item_number)) {
tem_O.Quantity+=shippingList.get(j).Quantity;
shippingList.remove(j);
--i;break;
}
}
}//开始索然无味的输出
FileWriter Shipping = new FileWriter("Shipping.txt");
for(int i =0;i<shippingList.size();++i) {
O tem_O = shippingList.get(i);
Shipping.write(tem_O.Customer_number+" "+tem_O.Item_number+" "+tem_O.Quantity+'\n');
}
Shipping.close();
FileWriter Errors = new FileWriter("Errors.txt");
for(int i =0;i<errorsList.size();++i) {
O tem_O = errorsList.get(i);
Errors.write(tem_O.Customer_number+" "+tem_O.Item_number+" "+tem_O.Quantity+'\n');
//System.out.println(tem_O.Customer_number+" "+tem_O.Item_number+" "+tem_O.Quantity);
}
Errors.close();
FileWriter NewInventory = new FileWriter("NewInventory.txt");
for(int i =0;i<temInventory.size();++i) {
Inventory tem_Inventory = temInventory.get(i);
NewInventory.write(tem_Inventory.Item_number+" "+tem_Inventory.Quantity+" "+tem_Inventory.Supplier_number+" "+tem_Inventory.description+'\n');
}
NewInventory.close();
//over
}
}
inventory
transaction
errors
shipping
NewInventory
以后有需求做一个有界面的算了