【无标题】一家杂货店的店主,需要查询、输入、修改任何一个商品的品名、价格、库存量信息。请用随机存取文件满足其要求,可以跟新,查询信息。每件商品的标志位其记录号。

文章描述了一个名为Thing的类,用于表示商品,包括名称、编号、价格和库存量。商品名称限制为8个字符。使用RandomAccessFile进行磁盘操作,支持写入、读取、修改商品信息。程序提供了输入、查询和更改商品记录的功能。
摘要由CSDN通过智能技术生成

//商品 Thing 类 public 

 c class Thing [ char 

 Name []= new 

 int 

 Number ;//4个字节 double Price ;//8个字节 int 

 count ;//4个字节所以保存一个商品要32个字节

 public Thing ( String name , intnumber , doubleprice int count ){ if ( name . toCharArray O . length >8)

 System . arraycopy ( name . toCharArrax 0.0, this . e ◇

 

 char [8];//16个字节

 

长度大于8.只取前8个

 else 

 System . arraycopy ( name . toCharArray 0.0, this . Name ,0, name toCharArray ,

0.length◇;//如果字符长度小于8.有几个填几个

 this . Number = number ; this . Price = price ;

 this . Count - count :

 

 public String getNameO {

 return new ! String ( this . Name );

 

 public 

 voidsetName ( String name ){

 if ( name .toCharArray0. length >8)

 System . arraycopy ( name . toCharArrax 0.0, this . Name ,0.8); else 

 System . arraycepy ( name . toCharArtay ,0.0, this . Name , O , nametoCharArrax 

0.length);

 

 public 

 doublegetPriceO {

 Price ;

 return 

 

 public void setPrice ( doubleprice ){

 this . Price = price :

 

 public 

 int 

getNumber0{ Number ;

 return 

 

 public voidsetNumber ( int number ){ this . Number = number ;

 

 public 

 int 

 return 

 getCountO {

 Count :

 public 

 void 

 this . Count = count ;

 

 setCount ( int count ){

 

//操作 Work 类(包含输入、查询、更改操作) import javaio *

 public 

 class Work { String 

 FileName ;

 public Work ( String FileName ){ this . FileName = FileName ;

//写第 n 条商品记录 public 

 void 

 RandomAccessFile rafw = new 

 rafw . seek ( n *32);//将位置指示器移到指定位置上,每个商品占32个字节 for ( inti =0; i <8; i ++)

 rafw . wxiteChar ( th Name [ i ]]://写入商品名

 rafw . wxitelnt ( th . Number );//写入商品号码

 rafw . writeDouble ( thPrice );//写入价格

 rafw . writelnt ( thCount );//写入商品库存量

 WriteThing ( Thing th int n ) throwsException {

 RandomAccessFile ( FileName . tw "):

 

 rafw . close ]:

 

//输入商品信息 public 

 void readThing ( int n ) throws Exception { charthingname []= new 

 char [8];

 Random AccessFile rafr = newRandomAccessFile ( FileName , r ");

 rafr seek ( n *32);//将位置指示器移到指定位置上,每个商品占32个字节 for ( int i =0; i <8; i ++)

 thingname [ i ]- rafr - readCharO ://读到 char 商品名 System . out . print ("商品名:");

 System . out . println ( thingname ):

 System . out . printin ("编号:"+ rafr readint0); System . out . printin ("价格:"+ rafr . read Double (); System . out . printin ("商品库存量:"+ rafr readintO ); rafr . close :

 System . out printinO :

 

 wv 

 

//修改存入 n 号商品名字 public 

 cvoid 

 Random AccessFile rafw = new ] rafw . seek ( n *32);

 char [] borrow = new 

 char [8];

 if ( name .toCharArray0.length>8)

 System . arraycopy ( name . toCharArrayO .0.borrow.0.8): else 

 System . arraycopy ( name . toCharArraxO .0, borrow ,0, na me toCharArcaxxO .

 

 setThingName ( int n . String name ) throws Exception {

 RandomAccessFile ( FileName ," rw ");

 

 length );

 for ( inti =0; i <8:i++)

 taw - wziteChar ( borrow [ i ]): rafw . closeO :

//修改存入 n 号商品价格 publicvoid 

 RandomAccessFile rafy - newBandom ä ccsssEile ( EileName ." tx "):

 rafw . seek ( n *32+8+4);//将位置指示器移到指定位置上,每个商品占24个字节,名字占8字节

 rafwwriteDouble ( price ); rafw . closeO :

 

 setThingPrice ( int n , double price ) throws Exception {

 

//修改存入 n 号商品库存量 publicvoid 

 dsetThingCount ( int n int count ) throws Exception {

 RandomAccessFilerahw - newBandomAcceasFile ( EileNams ." tx "):

 rafw . seek ( n *32+8+4+8];//将位置指示器移到指定位置上,每个商品占24个字节,名字占8字节

 tafwwriteFloat ( count ): rafw .close0:

//test6_10

 import javautil Scanner :

publicclasstest6_10{

 public static voidmain ( String [] args ) throws Exception { Scanner scanner = newScanner ( System in ):

 int number ;

 String name :

 Work work = new Work (" d :/ naruto . tst "); Thing [] thing ={

 new 

 Thing ("立白洗衣粉",1,3.5,55), Thing ("编辑者林楚金",2,0,0), Thing ("舒肤佳",4,8.9,89),

 Thing ("杜蕾丝超薄系列",5,1.2,444444)小;

 

 new 

 new 

 new 

 for ( inti =0; icthing length ; i ++)

 workwriteThing ( thing [ i ]. i ];

 

 for ( int i =0; icthing length ; i ++) workreadThing ( i );

 System . out printin ("请输入要查询商品编号"); number - scanner . nextintO :

 workreadThing ( number -1);

 System . out . println ("请输入要修改某一商品名字:编号和名字"); number = scanner . nextintO :

 name = scanner . next ();

 worksetT hingName ( number -1, name ); workreadThing ( number -1);

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值