Home > database >  Java cooking chicken, too ~
Java cooking chicken, too ~

Time:09-20

B1 - stock class (packaging)
(time limit: 1000 ms memory limit: 65536 KB)


Description

Design a class called Stock, this class include (not limited to the following data and methods) :
A string data domain named symbol) stock code;
A group called the name of the string data domain name said stocks;
A named previousClosingPrice type double data fields to store the day before the stock value;
A type double data field named currentPrice, store the current stock value;
To create a shares of a particular code and name of the constructor;
A called getChange () method returns from previousClosingPrice to currentPrice value changes (currentPrice - previousClosingPrice return 1 is greater than zero, return 1 is less than zero, zero return 0),
According to the input data (comma-separated) to create a Stock object, output the change of the basic information and Stock value,
!!!!!!!!! Note: the input and output part directly copy the reference code as far as possible after modification, in order to avoid errors caused by the inconsistent symbols,
Enter
Enter a stock information to English comma to separate (a symbol, name, currentPrice, previousClosingPrice)
The input statement example:
Scanner s=new Scanner(System.in);
String infomation=s.n extLine ();
String [] info=infomation. The split (", ");
Stock Stock=new Stock (info [0], the info [1]).
Float current=(float. ParseFloat (info [2]));
Float previous=(float. ParseFloat (info [3]));
O
Output according to the stock price, stock information, specific as follows,
Output:
System. The out. Print (" stock code: "+ stock. GetSymbol ());
System. The out. Print ("; Stock name: "+ stock getName ());
System. The out. Print ("; The current price is: "+ stock getCurrentPrice ());
If (stock. GetChange ()==1)
System. The out. Print ("; Up/down: the up ");
Else if (stock. GetChange ()==1)
System. The out. Print ("; Up/down: ");
The else
System. The out. Print ("; Up/down: flat ");
Difficulty
General
The input sample
In 2019001, Java, 3.5, 3.45
Sample output
Stock code: 2019001. Stock name: Java; The current price: 3.5; Up/down: up
Complete code:

CodePudding user response:

import java.util.Scanner;
Public class stock class {

Public static void main (String [] args) {
//TODO Auto - generated method stub

Scanner s=new Scanner(System.in);
String infomation=s.n extLine ();
String [] info=infomation. The split (", ");
Float current=(float. ParseFloat (info [2]));
Float previous=(float. ParseFloat (info [3]));
Stock Stock=new Stock (info [0], the info [1], the current and previous);

System. The out. Print (" stock code: "+ stock. GetSymbol ());
System. The out. Print ("; Stock name: "+ stock getName ());
System. The out. Print ("; The current price is: "+ stock getCurrentPrice ());
If (stock. GetChange ()==1)
System. The out. Print ("; Up/down: the up ");
Else if (stock. GetChange ()==1)
System. The out. Print ("; Up/down: ");
The else
System. The out. Print ("; Up/down: flat ");
}

}

The class Stock {
String symbol, name;
Double previousClosingPrice, currentPrice;
Public String getSymbol () {
Return the symbol;
}
Public String getName () {
return name;
}
Public double getCurrentPrice () {
Return currentPrice;
}
Public double getpreviousClosingPrice () {
Return previousClosingPrice;
}


Public Stock (String symbol, String name, double currentPrice, double previousClosingPrice) {

This. The symbol=symbol;
this.name=name;
Enclosing previousClosingPrice=previousClosingPrice;
Enclosing currentPrice=currentPrice;
}

//method
Public int getChange () {
If (this. CurrentPrice - this. PreviousClosingPrice> 0)
return 1;
Else if (this. CurrentPrice - this. PreviousClosingPrice==0)
return 0;
The else
return -1;
}

}



I what the output is correct, but OJ by not ah
  • Related