Code for implementing table-driven address change access-scenario from code Daquan chapter 12th: complex data types

Source: Internet
Author: User

The following is the implementation of access without address change

// Inventory class <br/> public class stock {<br/> int stockid; // No., which must be 0-9999 <br/> string category; // Category <br/> string goodname; // product name <br/> int amount; // quantity <br/> Private Static stock [] stocks = new stock [10000]; // if the address change access method is not used, only one primary query table is stocks <br/> static {<br/> for (INT I = 0; I <stocks. length; I ++) {<br/> stocks [I] = new stock (); <br/>}< br/> public stock () {// empty constructor </P> <p >}< br/> public stock (INT stockid, S Tring category, string goodname, int amount) {<br/> // For the sake of simplicity, check whether the Number, category, product name, and quantity are legal. <br/>, assuming that the stockid of each newly added stock is not the same as the previous one <br/> This. stockid = stockid; <br/> This. category = category; <br/> This. goodname = goodname; <br/> This. amount = amount; <br/> stocks [stockid] = This; // you can directly access the corresponding inventory to stocks through stockid, in this way, you can directly query the specific inventory information through stockid <br/>}< br/> Public int getstockid () {<br/> return stockid; <br/>}< br/> Public void setstockid (INT stockid) {<br/> This. stockid = stockid; <br/>}< br/> Public String getcategory () {<br/> return category; <br/>}< br/> Public void setcategory (string category) {<br/> This. category = category; <br/>}< br/> Public String getgoodname () {<br/> return goodname; <br/>}< br/> Public void setgoodname (string goodname) {<br/> This. goodname = goodname; <br/>}< br/> Public int getamount () {<Br/> return amount; <br/>}< br/> Public void setamount (INT amount) {<br/> This. amount = amount; <br/>}< br/> Public static stock [] getstocks () {<br/> return stocks; <br/>}< br/> Public static void main (string [] ARGs) {<br/> stock [] stocks = stock. getstocks (); // get all inventories, no inventory currently <br/> system. out. println (stocks [0]); // The printed result is null <br/> // Add inventory to stocks <br/> stock stock1 = new stock (0, "Fruit", "app Le ", 100); // fruit classification, 100 cases for Apple <br/> stock stock2 = new stock (235," vegetable "," carrot ", 200 ); // vegetable classification, 200 cases of carrots <br/> stocks = stock. getstocks (); // obtain all existing inventory information </P> <p> system. out. println (); <br/> // obtain the inventory information from the primary query table based on the stockid, and print <br/> stock stock3 = stocks [1]; <br/> stock stock4 = stocks [235]; </P> <p> system. out. println ("stock3 inventory information:"); <br/> If (stock3 = NULL) {<br/> system. out. println ("no inventory corresponding to stock3! "); <Br/>}< br/> else {<br/> system. out. println ("inventory type:" + stock3.getcategory (); <br/> system. out. println ("inventory name:" + stock3.getgoodname (); <br/> system. out. println ("inventory quantity:" + stock3.getamount (); <br/>}</P> <p> system. out. println (); </P> <p> system. out. println ("stock4 inventory information:"); <br/> If (stock4 = NULL) {<br/> system. out. println ("no inventory corresponding to stock4! "); <Br/>}< br/> else {<br/> system. out. println ("inventory type:" + stock4.getcategory (); <br/> system. out. println ("inventory name:" + stock4.getgoodname (); <br/> system. out. println ("inventory quantity:" + stock4.getamount (); <br/>}< br/> 

 

Step-by-Step access:

 

/* <Br/> * ≥ 90% a <br/> * <90.0% B <br/> * <77.5% C: C between 65 and 77.5 <br/> * <65.0% D: D between 50 and 65 <br/> * <50.0% E: E under 50 <br/> */<br/> public class studentscore {<br /> Private Static final int maxgradelevel = 4; </P> <p> Private Static double [] rangelimits = new double [5]; // score limit <br/> Private Static char [] grades = new char [5]; // grade, including a, B, c, d, e, F <br/> static {<br/> rangelimits [0] = 50.0; <br/> grades [0] = 'E '; <br/> rangelimits [1] = 65.0; <br/> grades [1] = 'D'; <br/> rangelimits [2] = 77.5; <br/> grades [2] = 'C'; <br/> rangelimits [3] = 90.0; <br/> grades [3] = 'B '; <br/> rangelimits [4] = 100.0; <br/> grades [4] = 'a '; <br/>}</P> <p> // obtain the description character of the corresponding level based on the Student Score: [abcdef] <br/> Public static char getstudentgrade (Double score) {<br/> int level = 0; <br/> char grade = 'a'; <br/> while (grade = 'A' & level <maxgradelevel) {<br/> If (score <rangelimits [level]) {<br/> grade = grades [level]; <br/> system. out. println ("current level:" + grade + "/T level:" + level); <br/> break; <br/>}< br/> else {<br/> level ++; <br/>}</P> <p> return grade; <br/>}</P> <p> Public static void main (string [] ARGs) {<br/> system. out. println ("78 points student level:" + studentscore. getstudentgrade (78); <br/>}< br/>


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.