Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the ordering machine with java

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you how to achieve the java ordering machine, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

The details are as follows

It takes two days to complete with the swing toolset, and the basics are all used without difficulties.

After entering the table number, you can order if there is no one at the table.

The following is a summary of the problems encountered in the completion process.

Null pointer exception

At the beginning of entering the table number, click to confirm, there has been a null pointer exception message, initially thought to be the reason for no initialization, the reason for the exception also points to this, but after a long time found that it has not been good, it is very strange, and finally found that the original error in the use of if and else if.

The initial code is so many if plus else, because else matches the nearest if, so when there are multiple if, the first if executes else, which leads to new OrderDishes (mealMenus,menusName,file) .setVisible (true) in else when I press the confirm button, but the mealMenus is not initialized at this time, so a null pointer exception occurs.

If (e.getSource () = = querenButton) {if (idText.getText (). Length ()! = 0) {/ / Code ellipsis} if (e.getSource () = = showButton) {/ / Code ellipsis} if (e.getSource () = = stopOrderingButton) {} else {String menusName=null If (e.getSource () = = meatButton) {/ / Code ellipsis} if (e.getSource () = = vegetarianButton) {/ / Code ellipsis} if (e.getSource () = = stapleFoodButton) {/ / Code ellipsis} if (e.getSource () = = soupAndPorridgeButton) {/ / Code ellipsis} new OrderDishes (mealMenus,menusName,file) .setVisible (true); / / new is executed after the confirm button is pressed, but mealMenus has not yet initialized}

Changing multiple if to if,else if can solve the problem. When the if is executed, the else if is not executing. (I didn't expect that the java who has been studying for a year will still make this kind of mistake. Failure.)

If (e.getSource () = = querenButton) {if (idText.getText (). Length ()! = 0) {/ / Code ellipsis} else if (e.getSource () = = showButton) {/ / Code ellipsis} else if (e.getSource () = = stopOrderingButton) {} else {String menusName=null If (e.getSource () = = meatButton) {/ / Code ellipsis} else if (e.getSource () = = vegetarianButton) {/ / Code ellipsis} else if (e.getSource () = = stapleFoodButton) {/ / Code ellipsis} else if (e.getSource () = = soupAndPorridgeButton) {/ / Code ellipsis} new OrderDishes (mealMenus,menusName,file) .setVisible (true);}

Randomaccessfile

This program uses randomaccessfile to write and read files to the menu, which makes me more aware of the role of randomaccessfile. Randomaccessfile is the most versatile file access class in randomaccessfile O, which can access any file through the seek () method, and provides many ways to access the file contents. This program uses seek () to put the file pointer at the end every time to record the menu. But when using readDouble to write the price into the file, the price will be garbled.

/ / read out the file try {RandomAccessFile in=new RandomAccessFile (file, "r"); String mealName=null; while ((mealName=in.readUTF ())! = null) {showArea.append ("\ n" + mealName); String mealPrice=in.readUTF (); showArea.append ("" + mealPrice); totalPrice=totalPrice+Double.parseDouble (mealPrice);} / / write to the file try {RandomAccessFile out=new RandomAccessFile (file, "rw"); if (file.exists ()) {long length=file.length (); out.seek (length);} for (int item0witi)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report