前几天刚写完这个小代码,想要记录下,代码功能有部分是根据csdn上文章参考写入,如有不足不要骂我/(ㄒoㄒ)/~~
首先:该代码需要利用两个栈创建一个商品货架,实现栈顶商品的生产日期最早,栈底商品的生产日期最近
接下来就是我的代码部分
1.先对商品信息里面的内容进行命名(初始化)
public class Commodit {
private String year;
private String month;
private String day;
private String num;
public String getYear() {
return year;
}
public void setYear(String year) {
this.year = year;
}
public String getMonth() {
return month;
}
public void setMonth(String month) {
this.month = month;
}
public String getDay() {
return day;
}
public void setDay(String day) {
this.day = day;
}
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
}
2.显示商品信息界面-ShowCom.java
Vector column =new Vector(); //将标题写入JTable中
column.add("该商品的生产日期");
column.add("该批商品的数量");
Vector data = createData(); //创建并调用对应的creatData函数(实现将数据写入表格中)
model = new DefaultTableModel(data,column);
table = new JTable(model);
table.setEnabled(false);
RowSorter sorter = new TableRowSorter(model);
JScrollPane scrollPane = new JScrollPane(table);
如果DefaultTableModel报错的话记得在开头的地方加上:DefaultTableModel model = null;
private Vector createData() {
// TODO Auto-generated method stub
// 先对txt文件中的数据进行读取
Vector data = new Vector();
String line = null;
try {
FileReader f1 = new FileReader("test.txt");
BufferedReader br = new BufferedReader(f1);
while((line = br.readLine())!=null) {
// 根据文件中的空格进行分割
String[] s =line.split(" ");
Vector rowData = new Vector();
rowData.add(s[0]+"年"+s[1]+"月"+s[2]+"日");//将文件中一行的内容先读取到s数组中再分配相应放入容器
rowData.add(s[3]);
data.add(rowData);// 最后放入容器data
}
f1.close();
br.close();
}catch(IOException e) {
e.printStackTrace();
}
return data;// 将数据返回
}
2.1 就是添加按钮的页面跳转
public void actionPerformed(ActionEvent e) {
AddFrm adfrm= new AddFrm();
adfrm.setVisible(true);
dispose(); //当打开AddFrm时ShowCom关闭
}
3.添加商品信息页面
这一部分可以用windowbuilder直接实现拖拉拽就好了,然后给对应按钮加上监听器功能直接右键add event handle->action就可以生成对应的代码
3.1 这一部分就是重置按钮的实现:
public void actionPerformed(ActionEvent e) {
Yeartxt.setText(""); // setText设置对应内容为空
Monthtxt.setText("");
Daytxt.setText(" ");
Numtxt.setText(" ");
}
3.2 这一部分就是跳转到显示 (前面已说到)
public void actionPerformed(ActionEvent e) {
StoreCom sto = new StoreCom();
sto.setVisible(true);
dispose();
}
3.3 这一部分就是实现将其商品进行添加,然后进行弹栈以及压栈
public void actionPerformed(ActionEvent e) {
addFrm(); // 我将函数写在了外面,直接调用addFrm
}
addFrm.java
private void addFrm() {
String year = Yeartxt.getText();
String month = Monthtxt.getText();
String day = Daytxt.getText();
String num = Numtxt.getText();
// 先将其转为int类型
int intyear = Integer.parseInt(year);
int intmonth = Integer.parseInt(month);
int intday = Integer.parseInt(day);
int intnum = Integer.parseInt(num);
// java获取当前时间戳:
Calendar cal = Calendar.getInstance();
int a = cal.get(Calendar.YEAR);
int b = cal.get(Calendar.MONTH);
int c = cal.get(Calendar.DATE);
String acc = Firread();// 调用这个函数(写在下面了)用于获取txt文件中时间最晚的商品日期(处于栈的最底部)
int Time = Integer.parseInt(acc);// 获取到最晚的日期后将其转换为int型,并将且拆成year、month、day三个部分
int lastyear = Time/100;
int lastmonth = (Time-(lastyear*100))/10;
System.out.println(lastmonth);
int lastday = (Time-(lastyear*100)-(lastmonth*10));
// intyear是后面输入的(数据) 根据自己的要求写比对信息(这里我忘记进行空写条件判断)
if(intyear<0||intyear>a||intmonth<0||intmonth>12||intday<0||intday>31)// 与当前时间进行比对(我的要求是生产日期不能超过今天这个时刻)
{
JOptionPane.showMessageDialog(null,"请正确填写日期"); // 出现弹窗
Yeartxt.setText(""); // 进行重置
Monthtxt.setText("");
Daytxt.setText(" ");
Numtxt.setText(" ");
}
else if(intnum>10000) // 存入的商品数量不能超过10000
{
JOptionPane.showMessageDialog(null,"无法存入10000+的货物请重新填写!");
Yeartxt.setText("");
Monthtxt.setText("");
Daytxt.setText(" ");
Numtxt.setText(" ");
}
else if(intyear<lastyear)
{
JOptionPane.showMessageDialog(null,"请填写比"+lastyear+"年"+lastmonth+"月"+lastday+"日晚的日期!");
Yeartxt.setText("");
Monthtxt.setText("");
Daytxt.setText(" ");
Numtxt.setText(" ");
}
else if(intyear==lastyear&&intmonth<lastmonth)
{
JOptionPane.showMessageDialog(null,"请填写比"+lastyear+"年"+lastmonth+"月"+lastday+"日晚的日期!");
Yeartxt.setText("");
Monthtxt.setText("");
Daytxt.setText(" ");
Numtxt.setText(" ");
}
else if(intyear==lastyear&&intmonth==lastmonth&&intday<lastday)
{
JOptionPane.showMessageDialog(null,"请填写比"+lastyear+"年"+lastmonth+"月"+lastday+"日晚的日期!");
Yeartxt.setText("");
Monthtxt.setText("");
Daytxt.setText(" ");
Numtxt.setText(" ");
}
else //(如果以上条件都满足就进行写入)
{
Pupo pu = new Pupo(); // 我将写入信息这个功能写在Pupo这个类里面,现先创建Pupo的对象
pu.reader(); // 调用Pupo类里面的函数(进行数据读取)
int A = intyear*10000+intmonth*100+intday;
pu.push(A,intnum); // 将新获得的日期数据以及数量传入push函数中
// JOptionPane.showMessageDialog(null,"添加成功");
}
}
public void reader()
public void reader()
{
try {
FileReader f1= new FileReader("test.txt");
BufferedReader br = new BufferedReader(f1);
while((line = br.readLine())!=null) {
String[] s = line.split(" "); // 根据空格分隔读取
int a = Integer.parseInt(s[0]); // 年
int b = Integer.parseInt(s[1]);
int c = Integer.parseInt(s[2]);
int d = Integer.parseInt(s[3]);
int E = a*10000+b*100+c; //将年月日合成一个完整的数字
stack2.push(E); // 存入stack2栈中
stack3.push(d); // 将数量存入stack3这个栈中
}
f1.close();
br.close();
}catch(IOException e) {
e.printStackTrace();
}
cleanfile(); // 调用这个函数实现txt文件清空,将该商品信息重新排序
}
public void cleanfile清空txt文件信息
public void cleanfile() {
// TODO Auto-generated method stub
File file = new File("test.txt");
try {
FileWriter fileWriter = new FileWriter(file);
fileWriter.write("");
fileWriter.flush();
fileWriter.close();
}catch(IOException e) {
e.printStackTrace();
}
}
public void push写入商品信息数据
public void push(int node,int num) {
stack1.push(node); // 先将日期信息放入空栈stack1中
stack4.push(num); // 先将数量信息放入空栈stack4中
while(!stack2.isEmpty()) {
stack1.push(stack2.pop()); // 将stack2中的信息循环弹出
stack4.push(stack3.pop());
}
ArrayList <Commodit> commodits = new ArrayList<Commodit>();
while(!stack1.isEmpty()) {
int A = stack1.pop();
int B = stack4.pop();
// 数量
String B1 = String.valueOf(B);
System.out.println(B1);
将黏在一起的日期进行拆分
// 年
int Fir = A/10000;
String Fir1 = String.valueOf(Fir);
System.out.println(Fir1);
// 月
int Sec = (A-(Fir*10000))/100;
String Sec1 = String.valueOf(Sec);
System.out.println(Sec1);
// 日
int Thir = (A-(Fir*10000)-(Sec*100));
String Thir1 = String.valueOf(Thir);
System.out.println(Thir1);
Commodit c = new Commodit();
c.setYear(Fir1);
c.setMonth(Sec1);
c.setDay(Thir1);
c.setNum(B1);
commodits.add(c);
}
// 将信息写入
try {
File destPath = new File("test.txt");
BufferedWriter bw = new BufferedWriter(new FileWriter(destPath));
for(Commodit c : commodits)
{
String Year = c.getYear();
String Month = c.getMonth();
String Day = c.getDay();
String Num = c.getNum();
bw.write(Year +" "+Month+" "+ Day +" " + Num +" ");
bw.newLine();
bw.flush();
}
bw.close();
}catch(IOException e) {
e.printStackTrace();
}
JOptionPane.showMessageDialog(null,"添加成功");
}
Pupo.java全部代码
public class Pupo {
Stack<Integer> stack1 = new Stack<Integer>();
Stack<Integer> stack2 = new Stack<Integer>();
Stack<Integer> stack3 = new Stack<Integer>();
Stack<Integer> stack4 = new Stack<Integer>();
String line = null;
public void reader()
{
try {
FileReader f1= new FileReader("test.txt");
BufferedReader br = new BufferedReader(f1);
while((line = br.readLine())!=null) {
String[] s = line.split(" ");
int a = Integer.parseInt(s[0]);
int b = Integer.parseInt(s[1]);
int c = Integer.parseInt(s[2]);
int d = Integer.parseInt(s[3]);
int E = a*10000+b*100+c;
stack2.push(E);
stack3.push(d);
}
f1.close();
br.close();
}catch(IOException e) {
e.printStackTrace();
}
cleanfile();
}
public void cleanfile() {
// TODO Auto-generated method stub
File file = new File("test.txt");
try {
FileWriter fileWriter = new FileWriter(file);
fileWriter.write("");
fileWriter.flush();
fileWriter.close();
}catch(IOException e) {
e.printStackTrace();
}
}
Vector writer = new Vector();
public void push(int node,int num) {
stack1.push(node);
stack4.push(num);
while(!stack2.isEmpty()) {
stack1.push(stack2.pop());
stack4.push(stack3.pop());
}
ArrayList <Commodit> commodits = new ArrayList<Commodit>();
while(!stack1.isEmpty()) {
int A = stack1.pop();
int B = stack4.pop();
// 数量
String B1 = String.valueOf(B);
System.out.println(B1);
// 年
int Fir = A/10000;
String Fir1 = String.valueOf(Fir);
System.out.println(Fir1);
// 月
int Sec = (A-(Fir*10000))/100;
String Sec1 = String.valueOf(Sec);
System.out.println(Sec1);
// 日
int Thir = (A-(Fir*10000)-(Sec*100));
String Thir1 = String.valueOf(Thir);
System.out.println(Thir1);
Commodit c = new Commodit();
c.setYear(Fir1);
c.setMonth(Sec1);
c.setDay(Thir1);
c.setNum(B1);
commodits.add(c);
}
// 将信息写入
try {
File destPath = new File("test.txt");
BufferedWriter bw = new BufferedWriter(new FileWriter(destPath));
for(Commodit c : commodits)
{
String Year = c.getYear();
String Month = c.getMonth();
String Day = c.getDay();
String Num = c.getNum();
bw.write(Year +" "+Month+" "+ Day +" " + Num +" ");
bw.newLine();
bw.flush();
}
bw.close();
}catch(IOException e) {
e.printStackTrace();
}
JOptionPane.showMessageDialog(null,"添加成功");
}
}
码龄有点短,如果有说的不对的地方请各位指出!