J third blog of object-oriented programming

The third blog of object-oriented programming

1: Foreword

(1) : knowledge points

Installation and basic use of JavaFX
The role and use of abstract classes
Addition, deletion, modification and query of container ArrayList
Finding iterators

(1) : JavaFX installation and basic use

1: Overview

Stage: scenes can be set in the window, but only one scene can be displayed at a time.

Scene: the scene can add UI Component diagram Tree structure of.

Root node (parent): set the root node as a layout and place different nodes (components). Can be at the root node nesting Place the root node.

2: Basic heading

On Drag Detected: when you drag from a Node, the drag operation will be detected and the EventHandler will be executed.

On Drag Done: when you Drag and let go, execute Drag to complete the operation.

On Drag Dropped: when you drag to the target and release the mouse, execute this DragDropped event.

On Drag Entered: when you drag to the target control, this event callback will be executed.

On Drag Exited: execute this operation when you drag to move out the target control.

On Drag Over: this event is triggered when a dragged object is dragged within the scope of another object container, and it will be executed continuously.

On Mouse Drag Entered: defines the event to be triggered when the gesture is fully pressed and released to enter the Node node.

On Mouse Drag Exited: defines the event to be triggered when the gesture is fully pressed and released to leave the Node node.

On Mouse Drag Over: defines the event to be triggered when the gesture is fully pressed and released in the Node.

On Mouse Drag Released: defines the event to be triggered when the gesture (by releasing the mouse button) of fully pressing and releasing in the Node is over.

On Key Pressed: an event is triggered when the keyboard key is pressed.

On Key Released: an event is triggered when the keyboard key is released after being pressed.

On Key Typed: it will only respond to text input keys, such as letters, numbers, punctuation marks, etc. it will not respond to function keys such as CTRL/ENTER/F1.

reference:

(1 message) JavaFX Scene Builder detailed usage instructions setting chapter (4) - Code code_ Ermu Chenglin's blog CSDN blog

(2) The role and use of abstract classes

1: Role of abstract classes

1. In object-oriented methods, abstract classes are mainly used for type hiding. Construct a fixed abstract description of a group of behaviors, but this group of behaviors can have any possible concrete implementation. This abstract description is an abstract class, and this group

Any possible concrete implementation is represented by all possible derived classes. A module can operate on an abstract body. Since a module depends on a fixed abstract body, it can not be modified.

2. By deriving from this abstract body, the behavior function of this module can also be extended. In order to realize OCP (open closed principle), one of the core principles of object-oriented design, abstract classes are the key.

3. Abstract classes are often used to represent the abstract concepts obtained from the analysis and design of the problem domain. They are abstractions of a series of concrete concepts that look different but are essentially the same.

Abstract classes are often used to represent the abstract concepts obtained from the analysis and design of the problem domain. They are abstractions of a series of concrete concepts that look different but are essentially the same.

The classes that are usually decorated with abstract in programming statements are abstract classes. In C++, classes containing pure virtual functions are called abstract classes, which cannot generate objects; In java, classes with abstract methods are called abstract classes, and objects cannot be generated.

Abstract classes are incomplete and can only be used as base classes. In object-oriented methods, abstract classes are mainly used to hide types and act as global variables.

Abstract classes are not much different from general classes.

How to describe things is how to describe things, but there are some things that you can't understand.

These uncertain parts, also the function of the thing, need to appear clearly. However, the principal cannot be defined.

adopt Abstract method To express.

Abstract analogy general classes have more than one abstract function. That is, abstract methods can be defined in classes.

Abstract classes cannot be instantiated.

Special: abstract methods can not be defined in an abstract class. This is just to prevent the class from establishing objects

2: Use of abstract classes

1. Format of abstract classes and methods

The abstract keyword is abstract

Abstract class: decorated with abstract keyword

Abstract method: add the abstract keyword and remove the braces

2. Use of abstract classes and methods

To use abstract classes and methods, you must have inheritance relationships

3. Precautions for abstract classes

1. Abstract classes can have all the things that ordinary classes have

2. The abstract method has no braces

3. Abstract classes cannot create objects

4. Abstract classes do not necessarily have abstract methods, but classes with abstract methods must be abstract classes

5. Subclasses of abstract classes must override all abstract methods of the parent class (except that subclasses are abstract classes)

6. The construction method of abstract classes is provided for subclasses to initialize parent class members

reference

(1 message) introduction to the use of java abstract classes_ Second brother wants to eat meat blog -CSDN blog_ Use of java abstract classes

 

3: Addition, deletion, modification and query of container ArrayLIst

ArrayList is not Multithreading Version of the container, so there is no need to synchronize, which ensures the efficiency of the operation. If we need to multithread a single ArrayList container, we can synchronize it externally. And the List family

Another ancient container class Vector is a multi-threaded version, which is not used much now.

1. Increase

① the add() method adds an element after the last element actually stored in the array

② add(int, E) put an element at the specified position.

③ addall (collection < >) adds all elements in a given container to the end of this container

2. Delete

① remove(int) deletes the element at the specified position.

② remove(Object) deletes a specific element in the container array, which equals the parameters passed in, and only deletes the element at the first occurrence position.

③ removeAll (collection < >) deletes the same elements as all elements in a given container in batches from this container.

④ removeif (Predicate < >, int, int) given a detector Predicate, search from the i position of the container array to the end. If the conditions are met, delete the elements at that position.

3. Change

① the set(int, E) method modifies the element value at the given index position to other element values, and finally returns the old value of the index position.

4. Check

① get(int) get the element of the given index position.

② the indexOf(Object) method obtains the index position of the first element equal to the given element in the container array.

4: Use of iterators

The two basic operations of iterator it are next, hasNext, and remove.

Call it Next () returns the next element of the iterator and updates the state of the iterator.

Call it Hasnext() is used to detect whether there are elements in the set.

Call it Remove() deletes the element returned by the iterator.

Delete element

To delete the elements in the collection, you can use the remove() method.

reference

(1 message) JDK source code reading (6): container - ArrayList_ Blog of CSDN -csdn blog

2: Design and analysis

1: Telecom Billing

(1) : landline users call

Get the user's call phone number, the caller's area, the caller's area, and the call duration. Carry out different billing types for different areas, and finally output the call fee and the remaining call fee.

(2) : mobile phones and landlines are charged at the same time

To get whether it's a mobile phone or a landline, if it's a mobile phone, you have to get the area of the mobile phone separately. The mobile phone adds a billing type, that is, when the mobile phone is outside the province, you need to consider roaming billing;

(3) : SMS billing

For short messages, there is no need to consider the location of the message, only the length of the message, and the whole code idea is clear;

(4) : Design

At first, I thought the number of digits of the entered phone number was fixed. I directly used CharAt to obtain the following string, but finally I realized that it was wrong. Then I used spit segmentation to process the first digit of the array and store it in the phone number

In, if it is a landline, the second digit is to answer the phone. If not, judge the length of the array. If it is six digits, it is all landlines. If it is seven digits, it is one landline and one mobile phone. If it is eight digits, it is both mobile phones, so data processing

It's still very good.

Whether the format of time acquisition and input time period is correct, use simpledataformat, which can judge whether the time format is correct, and also obtain the length of time

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
import java.util.regex.Pattern;

public class Main {
	public static void main(String[] args) 
	{
//		Scanner input =  new Scanner(System.in);
//		String str = input.nextLine();
		String[] ss = new String[20];
		getinput in = new getinput();

		ss = in.getit();
		people[] p = new people[20];
		people[] pc = new people[20];
		people[] pa = new people[20];
		people pp = new people(" ");
		pp.getpeople(p, ss);
		pc = pp.getcallpeople(ss);
		pa = pp.getanswerpeople(ss);
		pp.dealmeassage(pc);
		pp.dealmeassage(pa);
		pp.getcallmoney(pc);
		pp.getanswermoney(pa);
		p = pp.getrealpeople(p, pc, pa);
		in.output(p);
		
//		input.close();
		
	}
	

}

//Get input and process
class getinput
{
	public String[] getit()
	{
		Scanner input =  new Scanner(System.in);
		String[] ss = new String[20];
		sum a = new sum();
		int i;
		i = 0;
		boolean flag;
		while(true)
		{
			String line = input.nextLine();
			
			ss[i] = line;
			if(ss[i].equals("end"))
				break;
			flag = a.isright(line);
			if(flag)
			{
				ss[i] = line;
				i++;
			}
			
			
		}
		a.cutit(ss);
		return ss;
		
	}
	
	//output
	public void output(people[] pp)
	{
		int i,j,m,n,f;
		people p = new people();
//		for(i=0;i<pp.length&&!(pp[i]==null);i++)
//		{
//			for(j = i;j<pp.length&&!(pp[j]==null);i++) {
//				if()
//			}
//		}
		for(i = 0;i<pp.length&&!(pp[i]==null);i++)
		{
			
				System.out.printf(pp[i].call+" %.1f %.1f",pp[i].cost,pp[i].balance-pp[i].cost);
				System.out.println("");
			
		}
	}

}

class sum
{
	
	//Get phone days
	public int day(String str)
	
	{
		
		int i,j,k,g,f;
		g=j =f= 0;
		j=1;
		for(i=0;i<str.length();i+=j)
		{
			if(str.charAt(i)=='.')
				g++;
			if(g==2)
			{
				for(j = j-1;!(str.charAt(j+i)==' ');j++)
				{
					f =f*10+(int)(str.charAt(j+i)-48);
				}
			}
		}
		return f;

	}
	
	
	//Calculate the number of callers
	public int getnumberlog(String str)
	{
		int i,j;
		j = 0;
		for(i = 2;!(str.charAt(i)==' ');i++)
		{
			j++;
		}
		return j;
	}
	
	
	//Calculate landline call costs
	public double free1(int area,int time)
	{
		double money;
		money = 0;
		if(area==1)
			money = 0.1*time;
		if(area==2)
			money = 0.3*time;
		if(area==3)
			money = 0.6*time;
		
		return money;
			
	}
	
	//Preliminarily determine whether the format is correct
	public boolean isright(String str)
	{
		sum s =new sum();
		String pattern,time1,time2;
		String[] ss;
		int i;
		boolean f1,f2;
		ss = str.split(" ");
		for(i = 0;i<ss.length;i++) {
			
		}
		
		pattern = "^[ut]-[0-9]{11,12}.+";
		
		if(!str.matches(pattern))
			return false;
			
		if(str.length()>20)
		{
			time1 = ss[i-4]+" "+ss[i-3];
			time2 = ss[i-2]+" "+ss[i-1];
			f1 = s.validDateTimeSimple(time1);
			f2 = s.validDateTimeSimple(time2);
			if(!(f1&&f2))
				return false;
			boolean flag1,flag2;
			flag1 = true;
			flag2 = true;
			pattern = "^t-[0-9]{10,12}\\s([0-9]{3,4}\\s)?[0-9]{10,12}\\s([0-9]{3,4}\\s)?[0-9]{4}\\.[0-9]{1,2}\\.[0-9]{1,2}\\s[0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2}\\s[0-9]{4}\\.[0-9]{1,2}\\.[0-9]{1,2}\\s[0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2}";
            if(!str.matches(pattern))
                return false;
			
		}
		return true;
			
		
	}
	
	public  boolean validDateTimeSimple(String dateTime) {
        if(dateTime == null ) {
            return false;
        }
        DateFormat df = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
        df.setLenient(false);//Indicates strict verification
        
        try {
            df.parse(dateTime);
        } catch (ParseException e) {
            return false;
        }
        return true;
    }
	
	//Duplicate input
	public void cutit(String[] s)
	{
		int i,j,k;
		for(i = 0;!(s[i]==null);i++)
		{
			for(j = i+1;!(s[j]==null);j++)
			{
				if(s[i].equals(s[j]))
				{
					for(k = j;!(s[k]==null);k++)
						s[k] = s[k+1];
				}
			}
		}
		
	}
	
	
}
 
class people
{
	String s;//Code entered
	String call,callphone,answerphone,calldata,answerdata;//enter phone number
	int numbersum;//Enter the length of the phone number
	int carea,area,time,type1,type2;//Call telephone area access telephone area, call duration, billing type
	double cost,free,where,balance;//Amount spent, remaining amount
	public people()
	{
		
	}
	public people(String str)
	{
		this.balance =  100;
		this.call = str;
		int i;
		i = (int)(str.charAt(0)-48);
		if(i==0)
			this.balance-= 20;
		else 
			this.balance -= 15;
	}
	
	
	
	
	//Get the caller's phone number enter the phone number
	public String getcallphone(String str)
	{
//		String num;
//		String[] ss;
//		ss = str.split(" ");
//		num = ss[0].substring(2);
		this.type1 = (int)(str.charAt(0)-48);
		return str;
	}
	
	//Get the phone number and phone type of the receiver, and enter it as a phone number
	public String getanswerphone(String str)
	{
		this.type2 = (int)(str.charAt(0)-48);
		this.answerphone = str;
		return str;
	}
	
	
	
	//Get the receiver area and input it as the phone number
	public void getanswerarea(String str)
	{
		this.area = this.thaarea(str);
	}
	
	//Get caller area input as phone number
	public void getcallarea(String str)
	{
		this.carea = this.thaarea(str);
	}
	
	
		
		

	
	//Get input call time
	public void getcalldata(String str1,String str2)
	{
		this.calldata = str1+" "+str2;
	}
	
	//Get the call time
	public void getanswerdata(String str1,String str2)
	{
		this.answerdata = str1+" "+str2;
	}
	
	//Calculate landline telephone charges
	public void getmoney11()
	{
		if(this.area==1)
			this.cost += this.time*0.1;
		else if(this.area==2)
			this.cost += this.time*0.3;
		else this.cost += this.time*0.6;
	}
	
	//Calculate the charging of mobile phone calls
	public void getmoney12()
	{
		if(this.carea==1) {
			if(this.area==1)
				this.cost+=this.time*0.1;
			else if(this.area==2)
				this.cost+=this.time*0.2;
			else this.cost+=this.time*0.3;
		}
		else if(this.carea==2)
			this.cost+=this.time*0.3;
		else this.cost+=this.time*0.6;
	}
	
	//Charging for mobile phone answering calls
	public void getmoney22() 
	{
		if(this.area==3)
			this.cost+=this.time*0.3;
		
	}
	
	//Get call duration
	public void gettime()
	{
		SimpleDateFormat df = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); 
		this.time = 0;
        try  
        {  
          Date d1 = df.parse(this.calldata);  
          Date d2 = df.parse(this.answerdata);  
          long diff = d2.getTime() - d1.getTime();//The difference thus obtained is on the microsecond level  
          long days = diff / (1000 * 60 * 60 * 24);  
       
          long hours = (diff-days*(1000 * 60 * 60 * 24))/(1000* 60 * 60);  
          long minutes = (diff-days*(1000 * 60 * 60 * 24)-hours*(1000* 60 * 60))/(1000* 60);
          int sec = (int)((diff-days*(1000 * 60 * 60 * 24)-hours*(1000* 60 * 60)-minutes*(1000*60))/(1000));
          this.time =(int) (days*24*60+hours*60+minutes);
          if(sec>0)
        	  this.time+=1;
        }catch (Exception e)  
        {  
        }  
	}

	//Get the caller's phone area 1: within the city 2: within the province 3: outside the province
	public int thaarea(String str)
	{
		int i,area,n;
		area = 0;
		for(i = 0;i<4&&i<str.length();i++)
		{
			n = (int)((str.charAt(i)-48));
			area = area*10+n;
		}
		//In Nanchang
		if(area==791)
			return 1;
		//In Jiangxi Province
		if(area<=799&&area>=790||area==701)
			return 2;
		return 3;
	}
		
	//Account opening
	public void getpeople(people[] p,String[] s)
	{
		int i,j;
		j = 0;
		String[] ss;
		for(i = 0;!(s[i+1]==null);i++)
		{
			if(s[i].length()<20) {
				//Open an account and create a new object
				ss = s[i].split(" ");
				people pp = new people(ss[0].substring(2));
				pp.type1 =(int)(ss[0].substring(2).charAt(0)-48);
				p[j] = pp;
				j++;
			}
		}	
	}
	
	
	//Get the call list
	public people[] getcallpeople(String[] s)
	{
		int i,j=0;
		String num;
		people[] ps = new people[20];
		people p = new people();
		for(i = 0;!(s[i]==null);i++) {
			if(s[i].length()>20) {
				num = p.getcallphone(s[i]);
				people pp = new people(num);
				pp.s = s[i];
				ps[j] = pp;j++;
				
			}
				
			}
		return ps;
	}
	
	//Get the call list
	public people[] getanswerpeople(String[] s)
	{
		people[] ps = new people[20];
		String num;
		people p = new people();
		int i,j=0;
		for(i = 0;!(s[i]==null);i++) {
			if(s[i].length()>20) {
				num = p.getanswerphone(s[i]);
				people pp = new people(num);
				pp.s = s[i];
				ps[j] = pp;j++;
				
			}
		}
		return ps;
		
	}
	
	//process information
	public void dealmeassage(people[] pp)
	{
		String[] ss;
		int i,j=1;
		for(i = 0;!(pp[i]==null);i++) {
			j=1;
			ss = pp[i].s.split(" ");
			pp[i].callphone = pp[i].getcallphone(ss[0].substring(2));
			if(ss.length==6) {
				pp[i].answerphone=pp[i].getanswerphone(ss[j]);j++;
				pp[i].carea = pp[i].thaarea(ss[0].substring(2));
				pp[i].area = pp[i].thaarea(ss[1]);
			}
			else if(ss.length==7) {
				if(pp[i].type1==0) {
					pp[i].answerphone=pp[i].getanswerphone(ss[j]);j+=2;
					pp[i].carea = pp[i].thaarea(ss[0].substring(2));
					pp[i].area = pp[i].thaarea(ss[2]);
				}
				else {
					pp[i].answerphone=pp[i].getanswerphone(ss[j+1]);j+=2;
					pp[i].carea = pp[i].thaarea(ss[1]);
					pp[i].area = pp[i].thaarea(ss[2]);
				}
			}
			else {
				pp[i].answerphone=pp[i].getanswerphone(ss[j+1]);j+=3;
				pp[i].carea = pp[i].thaarea(ss[1]);
				pp[i].area = pp[i].thaarea(ss[3]);
			}
			pp[i].getcalldata(ss[j], ss[j+1]);j+=2;
			pp[i].getanswerdata(ss[j],ss[j+1]);
			pp[i].gettime();
		}
	}
	
	//Calculate the call fee
	public void getcallmoney(people[] pp) 
	{
		int i;
		for(i = 0;!(pp[i]==null);i++) {
			if(pp[i].type1==0)
				pp[i].getmoney11();
			else
				pp[i].getmoney12();
			
		}
	}
	
	//Calculate the cost of answering the phone
	public void getanswermoney(people[] pp)
	{
		int i;
		for(i = 0;!(pp[i]==null);i++) {
			if(pp[i].type2==1)
				pp[i].getmoney22();
		}
	}

	//Two phone processing
	public people[] getrealpeople(people[] pp,people[] p1,people[] p2)
	{
		int i,j,k;
		people p =new people(" ");
		for(i = 0;!(pp[i]==null);i++) {
			for(j = 0;!(p1[j]==null);j++) {
				if(pp[i].call.equals(p1[j].callphone)) {
					pp[i].cost+=p1[j].cost;
				}
			}
			for(j = 0;!(p2[j]==null);j++) {
				if(pp[i].call.equals(p2[j].answerphone)) {
					pp[i].cost+=p2[j].cost;
				}
			}
			
		}
		
		p.getsetposition(pp);
		p.orderset(pp);
		p.orderphone(pp);
		
		return pp;
	}
	//Move all the landlines to the front
	public void getsetposition(people[] pp)
	{
		int i,j;
		people p = new people(" ");
		for(i = 0;!(pp[i]==null);i++) {
			for(j = i+1;!(pp[j]==null);j++) {
				if(pp[i].type1==1&&pp[j].type1==0) {
					p = pp[i];pp[i]=pp[j];pp[j]=p;
					j--;
				}
			}
		}
		
	}
	
	//Sort the phones in the landline only with the last number
	public void orderset(people[] pp)
	{
		int i,j,k,n;
		people p = new people(" ");
		int f1=0,f2=0;
		
		for(i = 0;!(pp[i]==null);i++) {
			if(pp[i].type1==0) {
			for(j = i+1;!(pp[j]==null);j++) {
				for(k = 9;k<pp[i].call.length()-1;k++) {
					
				}
				f1 = (int)(pp[i].call.charAt(k)-48);
				for(n = 9;n<pp[j].call.length()-1;n++) {
					
				}
				f2 =(int)(pp[j].call.charAt(n)-48);
				if(f1>f2) {
					p = pp[i];pp[i]=pp[j];pp[j]=p;j--;
					}
				}
			}
		}
	}
	
	//Sort mobile phones only by the last number
	public void orderphone(people[] pp)
	{
		int i,j,k,n;
		people p = new people(" ");
		int f1=0,f2=0;
		
		for(i = 0;!(pp[i]==null);i++) {
			if(pp[i].type1==1) {
			for(j = i+1;!(pp[j]==null);j++) {
				for(k = 9;k<pp[i].call.length()-1;k++) {
					
				}
				f1 = (int)(pp[i].call.charAt(k)-48);
				for(n = 9;n<pp[j].call.length()-1;n++) {
					
				}
				f2 =(int)(pp[j].call.charAt(n)-48);
				if(f1>f2) {
					p = pp[i];pp[i]=pp[j];pp[j]=p;j--;
					}
				}
			}
		}
	}
	
}

  

 

 

3: Experience of stepping on the pit

1: At first, a fixed acquisition method was adopted for topic information acquisition. The reusability of the code was not strong, and the code was all the same in a large area, and the division of responsibilities was not obvious. The incoming and outgoing data were very chaotic. Two similar codes, one incoming was a whole line of code, and the other one

First, only part of the code is passed in, so you have to see what you want to pass in every time, which makes your brain more confused.

2. In addition to writing notes carefully, you should also carefully clarify the meaning of the topic before writing once, and realize the general process, so as to facilitate the writing of the code later

3. Although it is said that homework should be completed independently, because you don't know much about some functions in Java, sometimes you can learn from your classmates' code. Knowing some useful functions will be much more efficient than writing aimlessly. Of course, it's not copying your classmates' thoughts

Lu and code ha, make good use of online resources, go to see more topics of the same type, or search for the functions you want, which may already be available in the software

4: Suggestions for improvement

1. You can add more notes so that you can understand them later

2. You can go to see more excellent code, so that you can know some useful methods, so as not to know nothing when using it

3. Maybe the whole code is still process oriented, and there are some changes later, but I think I may not fully understand the essence of object-oriented, and I need to strengthen it

5: Summary

1. These times are much better than before, which is also the reason why the topic is relatively simple. Then I know to ask my classmates and teachers. The teacher pointed out that my process orientation is still too serious, and then I made simple improvements. Although it is not so good, it is almost the same

The students got some methods they didn't know before, which made their code more efficient and more accurate than tapping the code themselves

2. It's still those mistakes, but they have been improved in the process again and again

 

 

Posted by nic9 on Sun, 07 Aug 2022 02:31:40 +0930