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 use Java to send red packets to Wechat

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article is about how to use Java to send red envelopes to Wechat. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Demand analysis

Analyze and use the given class, write a program, and set the red packet type.

Tips Red packet Type:

1. Ordinary red packet: the amount is divided equally. If it is not divisible, the balance will be added to the last red packet. two。 Lucky red packet: the amount is random. The amount of each red packet is equal to the total amount. Red packet scenario: this case simulates a scenario in which a group owner sends a red packet to a group member and opens the last red packet by himself.

Case realization

Environment building:

1. Create a project: the name is customized, and RedPacketDemo is recommended. two。 Import pictures: import the pic directory into the project at the same level as the src directory. 3. Import known classes: create a package under src with a custom name and store the edited class.

1. RedPacketFrame: (interface)

An abstract class, which contains some properties, is the page of the red packet case.

Public abstract class RedPacketFrame extends JFrame {/ * ownerName: group name * / public String ownerName = "who is who"; / * openMode: the type of red packet [ordinary red packet / lucky red packet] * / public OpenMode openMode = null; / * * Construction method: generate red packet interface. The title of the * @ param title page. * / public RedPacketFrame (String title) {super (title); init (); / / Page-related initialization operations} / * set method * / public void setOwnerName (String ownerName) {this.ownerName = ownerName;} public void setOpenMode (OpenMode openMode) {this.openMode = openMode;}}

2. OpenMode: (type)

An interface that contains an allocation method that specifies the type of red packet.

The total amount of public interface OpenMode {/ * * @ param totalMoney in points. The total amount has been converted to an integer in minutes for convenient calculation. * @ param count number of red packets * @ return ArrayList element is the gold value of each red packet, and the sum of all elements is equal to the total amount. * * divide the totalMoney into count, save it in ArrayList, and return it. * / public abstract ArrayList pide (int totalMoney, int count);}

3. Define RedPacket:

The RedPacket class, which inherits RedPacketFrame:

Public class RedPacket extends RedPacketFrame {public RedPacket (String title) {super (title);}}

4. Define the test class and create the RedPacket:

Public class RedPacketTest {public static void main (String [] args) {RedPacket rp = new RedPacket ("big red packet");}}

Run the code to open a page that sends red packets. You can enter the total amount, the number of red packets, message information.

Click the button to put money into the red packet and jump to the next page.

5. RedPacket object

RedPacket object, which sets the group master name. SetOwnerName (String ownerName), is a string as a parameter. We just need to pass a string.

Public class RedPacketTest {public static void main (String [] args) {/ / create a red packet object RedPacket rp = new RedPacket ("big red packet"); / / set the group principal name rp.setOwnerName ("I am a big group");}}

6. RedPacket object

Sets the red packet type. SetOpenMode (OpenMode openMode), which is the interface as a parameter. We must define the implementation class of the interface, override the method in the interface, and pass the implementation class object to the setOpenMode method. Then look at the interface:

The total amount of public interface OpenMode {/ * * @ param totalMoney in points. The total amount has been converted to an integer in minutes for convenient calculation. * @ param count number of red packets * @ return ArrayList element is the gold value of each red packet, and the sum of all elements is equal to the total amount. * * divide the totalMoney into count, save it in ArrayList, and return it. * / public abstract ArrayList pide (int totalMoney, int count);}

Ordinary red packet

Open mode Common.

Public class Common implements OpenMode {@ Override public ArrayList pide (int totalMoney, int count) {/ / create a collection that saves the amount of each red packet ArrayList list = new ArrayList (); / / calculate the average amount int avgMoney = totalMoney / count; / / set the average amount of each person in front of for (int I = 0; I < count-1; ipackets +) {list.add (avgMoney) / / subtract the allocated amount from the total amount, which is the remaining amount of the last person totalMoney-= avgMoney;} / / set the remaining amount to the last person list.add (totalMoney); return list;}}

Send ordinary red packets

Public class RedPacketTest {public static void main (String [] args) {/ / create a red packet object RedPacket rp = new RedPacket ("big red packet"); / / set the group principal name rp.setOwnerName ("I am a big group"); / / set the red packet type rp.setOpenMode (new Common ()); / / ordinary red packet}}

Fight for the red packet.

In essence, the lucky red packet is to randomly divide the total amount of totalMoney into designated count shares, so the value range of each amount must be specified. If the range is too small, it may lead to a large amount of red packets after distribution. On the contrary, the scope is too large, which may lead to a red packet amount of 0, which is not enough. It can be seen that the definition rule of the value range is the key to the lucky red packet. We stipulate that each random amount range (except the last one), the minimum value is 1, the maximum value is 2 times the current remaining average amount, the unit is "points". Calculation formula:

Current average remaining amount = remaining total amount / number of remaining red packets

For example: the total amount is 50 yuan and 5 red packets are given out.

Tip: to facilitate the operation in the table, here, the unit is "yuan". In the program, it is suggested that the conversion should be converted to "points" for operation.

Lucky:

The lucky red packet is opened by Lucky. The code is as follows:

Public class Lucky implements OpenMode {@ Override public ArrayList pide (int totalMoney, int count) {/ / create a set to save each red packet amount ArrayList list = new ArrayList (); / / define the number of cycles, the total number of cycles-1 int time = count-1; / / create a random number object Random random = new Random (); / / circularly allocate for (int I = 0; I < time) (totalMoney +) {/ * * each recalculation, generate random amount * random range: totalMoney / count * 2 count totalMoney is constantly decreasing, * Tata is also decreasing, so this is a variable range. * / int money = random.nextInt (totalMoney / count * 2) + 1; / / the amount is added to the collection list.add (money); / / the total amount deducts the allocated amount totalMoney-= money; / / number of red packets-1 count--;} / / the remaining amount is the last red packet list.add (totalMoney); return list;}}

Send red packets

Public class RedPacketTest {public static void main (String [] args) {/ / create red packet object RedPacket rp = new RedPacket ("big red packet"); / / set the group principal name rp.setOwnerName ("I am a big group"); / / set the red packet type, choose one of the two / / rp.setOpenMode (new Common ()); / / ordinary red packet rp.setOpenMode (new Lucky ()); / / lucky red packet}}

Thank you for reading! This is the end of the article on "how to use Java to send red envelopes on Wechat". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!

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